rsObjectBase.h revision 9397e30ce5fe3f6af9212a93b490836b04fdfffa
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 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 incSysRef() const; 34 void decSysRef() const; 35 36 void incUserRef() const; 37 void decUserRef() const; 38 39 const char * getName() const { 40 return mName; 41 } 42 void setName(const char *); 43 void setName(const char *, uint32_t len); 44 45private: 46 char * mName; 47 mutable int32_t mSysRefCount; 48 mutable int32_t mUserRefCount; 49 50 51}; 52 53template<class T> 54class ObjectBaseRef 55{ 56public: 57 ObjectBaseRef() { 58 mRef = NULL; 59 } 60 61 ObjectBaseRef(const ObjectBaseRef &ref) { 62 mRef = ref.get(); 63 if (mRef) { 64 mRef->incSysRef(); 65 } 66 } 67 68 ObjectBaseRef(T *ref) { 69 mRef = ref; 70 if (mRef) { 71 ref->incSysRef(); 72 } 73 } 74 75 ~ObjectBaseRef() { 76 clear(); 77 } 78 79 void set(T *ref) { 80 if (mRef != ref) { 81 clear(); 82 mRef = ref; 83 if (mRef) { 84 ref->incSysRef(); 85 } 86 } 87 } 88 89 void set(const ObjectBaseRef &ref) { 90 set(ref.mRef); 91 } 92 93 void clear() { 94 if (mRef) { 95 mRef->decSysRef(); 96 } 97 mRef = NULL; 98 } 99 100 inline T * get() const { 101 return mRef; 102 } 103 104 inline T * operator-> () const { 105 return mRef; 106 } 107 108protected: 109 T * mRef; 110 111}; 112 113 114} 115} 116 117#endif //ANDROID_RS_OBJECT_BASE_H 118 119