Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 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_RS_OBJECT_BASE_H |
| 18 | #define ANDROID_RS_OBJECT_BASE_H |
| 19 | |
| 20 | #include "rsUtils.h" |
| 21 | |
| 22 | |
| 23 | namespace android { |
| 24 | namespace renderscript { |
| 25 | |
| 26 | // An element is a group of Components that occupies one cell in a structure. |
| 27 | class ObjectBase |
| 28 | { |
| 29 | public: |
| 30 | ObjectBase(); |
| 31 | virtual ~ObjectBase(); |
| 32 | |
| 33 | void incRef() const; |
| 34 | void decRef() const; |
| 35 | |
Jason Sams | 3eaa338 | 2009-06-10 15:04:38 -0700 | [diff] [blame^] | 36 | const char * getName() const { |
| 37 | return mName; |
| 38 | } |
| 39 | void setName(const char *); |
| 40 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 41 | private: |
Jason Sams | 3eaa338 | 2009-06-10 15:04:38 -0700 | [diff] [blame^] | 42 | char * mName; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 43 | mutable int32_t mRefCount; |
| 44 | |
| 45 | |
| 46 | }; |
| 47 | |
| 48 | template<class T> |
| 49 | class ObjectBaseRef |
| 50 | { |
| 51 | public: |
| 52 | ObjectBaseRef() { |
| 53 | mRef = NULL; |
| 54 | } |
| 55 | |
Jason Sams | da423d8 | 2009-06-09 12:15:30 -0700 | [diff] [blame] | 56 | ObjectBaseRef(const ObjectBaseRef &ref) { |
| 57 | mRef = ref.get(); |
Jason Sams | 3eaa338 | 2009-06-10 15:04:38 -0700 | [diff] [blame^] | 58 | if (mRef) { |
| 59 | mRef->incRef(); |
| 60 | } |
Jason Sams | da423d8 | 2009-06-09 12:15:30 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | ObjectBaseRef(T *ref) { |
| 64 | mRef = ref; |
Jason Sams | 3eaa338 | 2009-06-10 15:04:38 -0700 | [diff] [blame^] | 65 | if (mRef) { |
| 66 | ref->incRef(); |
| 67 | } |
Jason Sams | da423d8 | 2009-06-09 12:15:30 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 70 | ~ObjectBaseRef() { |
| 71 | clear(); |
| 72 | } |
| 73 | |
| 74 | void set(T *ref) { |
| 75 | if (mRef != ref) { |
| 76 | clear(); |
| 77 | mRef = ref; |
Jason Sams | 3eaa338 | 2009-06-10 15:04:38 -0700 | [diff] [blame^] | 78 | if (mRef) { |
| 79 | ref->incRef(); |
| 80 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | |
Jason Sams | 3eaa338 | 2009-06-10 15:04:38 -0700 | [diff] [blame^] | 84 | void set(const ObjectBaseRef &ref) { |
| 85 | set(ref.mRef); |
| 86 | } |
| 87 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 88 | void clear() { |
| 89 | if (mRef) { |
| 90 | mRef->decRef(); |
| 91 | } |
| 92 | mRef = NULL; |
| 93 | } |
| 94 | |
| 95 | inline T * get() const { |
| 96 | return mRef; |
| 97 | } |
| 98 | |
| 99 | inline T * operator-> () const { |
| 100 | return mRef; |
| 101 | } |
| 102 | |
| 103 | protected: |
| 104 | T * mRef; |
| 105 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 106 | }; |
| 107 | |
| 108 | |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | #endif //ANDROID_RS_OBJECT_BASE_H |
| 113 | |