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