blob: e9d5d8d40baaa66d2a1632e525db93ed621c677c [file] [log] [blame]
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001/*
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>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070021
Mathias Agopian6a3c05b2017-04-27 20:06:55 -070022#include <nativebase/nativebase.h>
Mathias Agopian5f2165f2012-02-24 18:25:41 -080023
Mathias Agopian076b1cc2009-04-10 14:24:30 -070024
25/*****************************************************************************/
26
27#ifdef __cplusplus
Mathias Agopian076b1cc2009-04-10 14:24:30 -070028
29#include <utils/RefBase.h>
30
31namespace android {
32
33/*
Mathias Agopian5f2165f2012-02-24 18:25:41 -080034 * This helper class turns a ANativeXXX object type into a C++
Mathias Agopian076b1cc2009-04-10 14:24:30 -070035 * reference-counted object; with proper type conversions.
36 */
Mathias Agopian6a3c05b2017-04-27 20:06:55 -070037template <typename NATIVE_TYPE, typename TYPE, typename REF,
38 typename NATIVE_BASE = android_native_base_t>
Mathias Agopian5f2165f2012-02-24 18:25:41 -080039class ANativeObjectBase : public NATIVE_TYPE, public REF
Mathias Agopian076b1cc2009-04-10 14:24:30 -070040{
Jamie Gennis5e67f872010-05-10 17:33:32 -070041public:
42 // Disambiguate between the incStrong in REF and NATIVE_TYPE
43 void incStrong(const void* id) const {
44 REF::incStrong(id);
45 }
46 void decStrong(const void* id) const {
47 REF::decStrong(id);
48 }
49
Mathias Agopian076b1cc2009-04-10 14:24:30 -070050protected:
Mathias Agopian6a3c05b2017-04-27 20:06:55 -070051 typedef ANativeObjectBase<NATIVE_TYPE, TYPE, REF, NATIVE_BASE> BASE;
Mathias Agopian5f2165f2012-02-24 18:25:41 -080052 ANativeObjectBase() : NATIVE_TYPE(), REF() {
Mathias Agopian076b1cc2009-04-10 14:24:30 -070053 NATIVE_TYPE::common.incRef = incRef;
54 NATIVE_TYPE::common.decRef = decRef;
55 }
56 static inline TYPE* getSelf(NATIVE_TYPE* self) {
57 return static_cast<TYPE*>(self);
58 }
59 static inline TYPE const* getSelf(NATIVE_TYPE const* self) {
60 return static_cast<TYPE const *>(self);
61 }
Mathias Agopian6a3c05b2017-04-27 20:06:55 -070062 static inline TYPE* getSelf(NATIVE_BASE* base) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -070063 return getSelf(reinterpret_cast<NATIVE_TYPE*>(base));
64 }
Mathias Agopian6a3c05b2017-04-27 20:06:55 -070065 static inline TYPE const * getSelf(NATIVE_BASE const* base) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -070066 return getSelf(reinterpret_cast<NATIVE_TYPE const*>(base));
67 }
Mathias Agopian6a3c05b2017-04-27 20:06:55 -070068 static void incRef(NATIVE_BASE* base) {
Mathias Agopian5f2165f2012-02-24 18:25:41 -080069 ANativeObjectBase* self = getSelf(base);
Mathias Agopian076b1cc2009-04-10 14:24:30 -070070 self->incStrong(self);
71 }
Mathias Agopian6a3c05b2017-04-27 20:06:55 -070072 static void decRef(NATIVE_BASE* base) {
Mathias Agopian5f2165f2012-02-24 18:25:41 -080073 ANativeObjectBase* self = getSelf(base);
Mathias Agopian076b1cc2009-04-10 14:24:30 -070074 self->decStrong(self);
75 }
76};
77
78} // namespace android
79#endif // __cplusplus
80
81/*****************************************************************************/
82
83#endif /* ANDROID_ANDROID_NATIVES_H */