blob: b2c33386e18efe058ac8030ea08a0fc0b1880fac [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 *);
Jason Samsa4a54e42009-06-10 18:39:40 -070040 void setName(const char *, uint32_t len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -070041
Jason Sams326e0dd2009-05-22 14:03:28 -070042private:
Jason Samsa0a1b6f2009-06-10 15:04:38 -070043 char * mName;
Jason Sams326e0dd2009-05-22 14:03:28 -070044 mutable int32_t mRefCount;
45
46
47};
48
49template<class T>
50class ObjectBaseRef
51{
52public:
53 ObjectBaseRef() {
54 mRef = NULL;
55 }
56
Jason Sams10308932009-06-09 12:15:30 -070057 ObjectBaseRef(const ObjectBaseRef &ref) {
58 mRef = ref.get();
Jason Samsa0a1b6f2009-06-10 15:04:38 -070059 if (mRef) {
60 mRef->incRef();
61 }
Jason Sams10308932009-06-09 12:15:30 -070062 }
63
64 ObjectBaseRef(T *ref) {
65 mRef = ref;
Jason Samsa0a1b6f2009-06-10 15:04:38 -070066 if (mRef) {
67 ref->incRef();
68 }
Jason Sams10308932009-06-09 12:15:30 -070069 }
70
Jason Sams326e0dd2009-05-22 14:03:28 -070071 ~ObjectBaseRef() {
72 clear();
73 }
74
75 void set(T *ref) {
76 if (mRef != ref) {
77 clear();
78 mRef = ref;
Jason Samsa0a1b6f2009-06-10 15:04:38 -070079 if (mRef) {
80 ref->incRef();
81 }
Jason Sams326e0dd2009-05-22 14:03:28 -070082 }
83 }
84
Jason Samsa0a1b6f2009-06-10 15:04:38 -070085 void set(const ObjectBaseRef &ref) {
86 set(ref.mRef);
87 }
88
Jason Sams326e0dd2009-05-22 14:03:28 -070089 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
104protected:
105 T * mRef;
106
Jason Sams326e0dd2009-05-22 14:03:28 -0700107};
108
109
110}
111}
112
113#endif //ANDROID_RS_OBJECT_BASE_H
114