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 *); |
Jason Sams | d5680f9 | 2009-06-10 18:39:40 -0700 | [diff] [blame] | 40 | void setName(const char *, uint32_t len); |
Jason Sams | 3eaa338 | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 41 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 42 | private: |
Jason Sams | 3eaa338 | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 43 | char * mName; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 44 | mutable int32_t mRefCount; |
| 45 | |
| 46 | |
| 47 | }; |
| 48 | |
| 49 | template<class T> |
| 50 | class ObjectBaseRef |
| 51 | { |
| 52 | public: |
| 53 | ObjectBaseRef() { |
| 54 | mRef = NULL; |
| 55 | } |
| 56 | |
Jason Sams | da423d8 | 2009-06-09 12:15:30 -0700 | [diff] [blame] | 57 | ObjectBaseRef(const ObjectBaseRef &ref) { |
| 58 | mRef = ref.get(); |
Jason Sams | 3eaa338 | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 59 | if (mRef) { |
| 60 | mRef->incRef(); |
| 61 | } |
Jason Sams | da423d8 | 2009-06-09 12:15:30 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | ObjectBaseRef(T *ref) { |
| 65 | mRef = ref; |
Jason Sams | 3eaa338 | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 66 | if (mRef) { |
| 67 | ref->incRef(); |
| 68 | } |
Jason Sams | da423d8 | 2009-06-09 12:15:30 -0700 | [diff] [blame] | 69 | } |
| 70 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 71 | ~ObjectBaseRef() { |
| 72 | clear(); |
| 73 | } |
| 74 | |
| 75 | void set(T *ref) { |
| 76 | if (mRef != ref) { |
| 77 | clear(); |
| 78 | mRef = ref; |
Jason Sams | 3eaa338 | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 79 | if (mRef) { |
| 80 | ref->incRef(); |
| 81 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 82 | } |
| 83 | } |
| 84 | |
Jason Sams | 3eaa338 | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 85 | void set(const ObjectBaseRef &ref) { |
| 86 | set(ref.mRef); |
| 87 | } |
| 88 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 89 | void clear() { |
| 90 | if (mRef) { |
| 91 | mRef->decRef(); |
| 92 | } |
| 93 | mRef = NULL; |
| 94 | } |
| 95 | |
| 96 | inline T * get() const { |
| 97 | return mRef; |
| 98 | } |
| 99 | |
| 100 | inline T * operator-> () const { |
| 101 | return mRef; |
| 102 | } |
| 103 | |
| 104 | protected: |
| 105 | T * mRef; |
| 106 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 107 | }; |
| 108 | |
| 109 | |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | #endif //ANDROID_RS_OBJECT_BASE_H |
| 114 | |