blob: d1e6baa0badb4a70458ca3b90f58c45f12c66ddc [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
Jason Sams9397e302009-08-27 20:23:34 -070033 void incSysRef() const;
34 void decSysRef() const;
35
36 void incUserRef() const;
37 void decUserRef() const;
Jason Sams326e0dd2009-05-22 14:03:28 -070038
Jason Samsa0a1b6f2009-06-10 15:04:38 -070039 const char * getName() const {
40 return mName;
41 }
42 void setName(const char *);
Jason Samsa4a54e42009-06-10 18:39:40 -070043 void setName(const char *, uint32_t len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -070044
Jason Sams326e0dd2009-05-22 14:03:28 -070045private:
Jason Samsa0a1b6f2009-06-10 15:04:38 -070046 char * mName;
Jason Sams9397e302009-08-27 20:23:34 -070047 mutable int32_t mSysRefCount;
48 mutable int32_t mUserRefCount;
Jason Sams326e0dd2009-05-22 14:03:28 -070049
50
51};
52
Jason Sams9397e302009-08-27 20:23:34 -070053template<class T>
54class ObjectBaseRef
Jason Sams326e0dd2009-05-22 14:03:28 -070055{
56public:
57 ObjectBaseRef() {
58 mRef = NULL;
59 }
60
Jason Sams10308932009-06-09 12:15:30 -070061 ObjectBaseRef(const ObjectBaseRef &ref) {
62 mRef = ref.get();
Jason Samsa0a1b6f2009-06-10 15:04:38 -070063 if (mRef) {
Jason Sams9397e302009-08-27 20:23:34 -070064 mRef->incSysRef();
Jason Samsa0a1b6f2009-06-10 15:04:38 -070065 }
Jason Sams10308932009-06-09 12:15:30 -070066 }
67
68 ObjectBaseRef(T *ref) {
69 mRef = ref;
Jason Samsa0a1b6f2009-06-10 15:04:38 -070070 if (mRef) {
Jason Sams9397e302009-08-27 20:23:34 -070071 ref->incSysRef();
Jason Samsa0a1b6f2009-06-10 15:04:38 -070072 }
Jason Sams10308932009-06-09 12:15:30 -070073 }
74
Jason Sams326e0dd2009-05-22 14:03:28 -070075 ~ObjectBaseRef() {
76 clear();
77 }
78
79 void set(T *ref) {
80 if (mRef != ref) {
81 clear();
82 mRef = ref;
Jason Samsa0a1b6f2009-06-10 15:04:38 -070083 if (mRef) {
Jason Sams9397e302009-08-27 20:23:34 -070084 ref->incSysRef();
Jason Samsa0a1b6f2009-06-10 15:04:38 -070085 }
Jason Sams326e0dd2009-05-22 14:03:28 -070086 }
87 }
88
Jason Samsa0a1b6f2009-06-10 15:04:38 -070089 void set(const ObjectBaseRef &ref) {
90 set(ref.mRef);
91 }
92
Jason Sams326e0dd2009-05-22 14:03:28 -070093 void clear() {
94 if (mRef) {
Jason Sams9397e302009-08-27 20:23:34 -070095 mRef->decSysRef();
Jason Sams326e0dd2009-05-22 14:03:28 -070096 }
97 mRef = NULL;
98 }
99
100 inline T * get() const {
101 return mRef;
102 }
103
Jason Sams9397e302009-08-27 20:23:34 -0700104 inline T * operator-> () const {
105 return mRef;
Jason Sams326e0dd2009-05-22 14:03:28 -0700106 }
107
108protected:
109 T * mRef;
110
Jason Sams326e0dd2009-05-22 14:03:28 -0700111};
112
113
114}
115}
116
117#endif //ANDROID_RS_OBJECT_BASE_H
118