blob: 4c52e9ccc4fe7a7244d10b181541653052f0e584 [file] [log] [blame]
Jason Sams326e0dd2009-05-22 14:03:28 -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_RS_OBJECT_BASE_H
18#define ANDROID_RS_OBJECT_BASE_H
19
20#include "rsUtils.h"
21
22
23namespace android {
24namespace renderscript {
25
26// An element is a group of Components that occupies one cell in a structure.
27class ObjectBase
28{
29public:
30 ObjectBase();
31 virtual ~ObjectBase();
32
33 void incRef() const;
34 void decRef() const;
35
Jason Samsa0a1b6f2009-06-10 15:04:38 -070036 const char * getName() const {
37 return mName;
38 }
39 void setName(const char *);
40
Jason Sams326e0dd2009-05-22 14:03:28 -070041private:
Jason Samsa0a1b6f2009-06-10 15:04:38 -070042 char * mName;
Jason Sams326e0dd2009-05-22 14:03:28 -070043 mutable int32_t mRefCount;
44
45
46};
47
48template<class T>
49class ObjectBaseRef
50{
51public:
52 ObjectBaseRef() {
53 mRef = NULL;
54 }
55
Jason Sams10308932009-06-09 12:15:30 -070056 ObjectBaseRef(const ObjectBaseRef &ref) {
57 mRef = ref.get();
Jason Samsa0a1b6f2009-06-10 15:04:38 -070058 if (mRef) {
59 mRef->incRef();
60 }
Jason Sams10308932009-06-09 12:15:30 -070061 }
62
63 ObjectBaseRef(T *ref) {
64 mRef = ref;
Jason Samsa0a1b6f2009-06-10 15:04:38 -070065 if (mRef) {
66 ref->incRef();
67 }
Jason Sams10308932009-06-09 12:15:30 -070068 }
69
Jason Sams326e0dd2009-05-22 14:03:28 -070070 ~ObjectBaseRef() {
71 clear();
72 }
73
74 void set(T *ref) {
75 if (mRef != ref) {
76 clear();
77 mRef = ref;
Jason Samsa0a1b6f2009-06-10 15:04:38 -070078 if (mRef) {
79 ref->incRef();
80 }
Jason Sams326e0dd2009-05-22 14:03:28 -070081 }
82 }
83
Jason Samsa0a1b6f2009-06-10 15:04:38 -070084 void set(const ObjectBaseRef &ref) {
85 set(ref.mRef);
86 }
87
Jason Sams326e0dd2009-05-22 14:03:28 -070088 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
103protected:
104 T * mRef;
105
Jason Sams326e0dd2009-05-22 14:03:28 -0700106};
107
108
109}
110}
111
112#endif //ANDROID_RS_OBJECT_BASE_H
113