1 /* 2 * Copyright (C) 2009 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef ANDROID_ANDROID_NATIVES_H 18 #define ANDROID_ANDROID_NATIVES_H 19 20 #include <sys/types.h> 21 22 #include <nativebase/nativebase.h> 23 24 25 /*****************************************************************************/ 26 27 #ifdef __cplusplus 28 29 #include <utils/RefBase.h> 30 31 namespace android { 32 33 /* 34 * This helper class turns a ANativeXXX object type into a C++ 35 * reference-counted object; with proper type conversions. 36 */ 37 template <typename NATIVE_TYPE, typename TYPE, typename REF, 38 typename NATIVE_BASE = android_native_base_t> 39 class ANativeObjectBase : public NATIVE_TYPE, public REF 40 { 41 public: 42 // Disambiguate between the incStrong in REF and NATIVE_TYPE incStrong(const void * id)43 void incStrong(const void* id) const { 44 REF::incStrong(id); 45 } decStrong(const void * id)46 void decStrong(const void* id) const { 47 REF::decStrong(id); 48 } 49 50 protected: 51 typedef ANativeObjectBase<NATIVE_TYPE, TYPE, REF, NATIVE_BASE> BASE; ANativeObjectBase()52 ANativeObjectBase() : NATIVE_TYPE(), REF() { 53 NATIVE_TYPE::common.incRef = incRef; 54 NATIVE_TYPE::common.decRef = decRef; 55 } getSelf(NATIVE_TYPE * self)56 static inline TYPE* getSelf(NATIVE_TYPE* self) { 57 return static_cast<TYPE*>(self); 58 } getSelf(NATIVE_TYPE const * self)59 static inline TYPE const* getSelf(NATIVE_TYPE const* self) { 60 return static_cast<TYPE const *>(self); 61 } getSelf(NATIVE_BASE * base)62 static inline TYPE* getSelf(NATIVE_BASE* base) { 63 return getSelf(reinterpret_cast<NATIVE_TYPE*>(base)); 64 } getSelf(NATIVE_BASE const * base)65 static inline TYPE const * getSelf(NATIVE_BASE const* base) { 66 return getSelf(reinterpret_cast<NATIVE_TYPE const*>(base)); 67 } incRef(NATIVE_BASE * base)68 static void incRef(NATIVE_BASE* base) { 69 ANativeObjectBase* self = getSelf(base); 70 self->incStrong(self); 71 } decRef(NATIVE_BASE * base)72 static void decRef(NATIVE_BASE* base) { 73 ANativeObjectBase* self = getSelf(base); 74 self->decStrong(self); 75 } 76 }; 77 78 } // namespace android 79 #endif // __cplusplus 80 81 /*****************************************************************************/ 82 83 #endif /* ANDROID_ANDROID_NATIVES_H */ 84