rsObjectBase.h revision e12c1c591b4219e80f29c6c0e0c62c9578b75450
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
26class Context;
27
28// An element is a group of Components that occupies one cell in a structure.
29class ObjectBase
30{
31public:
32    ObjectBase(Context *rsc);
33    virtual ~ObjectBase();
34
35    void incSysRef() const;
36    bool decSysRef() const;
37
38    void incUserRef() const;
39    bool decUserRef() const;
40    bool zeroUserRef() const;
41
42    const char * getName() const {
43        return mName;
44    }
45    void setName(const char *);
46    void setName(const char *, uint32_t len);
47
48    Context * getContext() const {return mRSC;}
49    void setContext(Context *);
50
51    static void zeroAllUserRef(Context *rsc);
52
53    virtual void dumpLOGV(const char *prefix) const;
54
55protected:
56    const char *mAllocFile;
57    uint32_t mAllocLine;
58
59private:
60    void add() const;
61    void remove() const;
62
63    bool checkDelete() const;
64
65    char * mName;
66    Context *mRSC;
67    mutable int32_t mSysRefCount;
68    mutable int32_t mUserRefCount;
69
70    mutable const ObjectBase * mPrev;
71    mutable const ObjectBase * mNext;
72};
73
74template<class T>
75class ObjectBaseRef
76{
77public:
78    ObjectBaseRef() {
79        mRef = NULL;
80    }
81
82    ObjectBaseRef(const ObjectBaseRef &ref) {
83        mRef = ref.get();
84        if (mRef) {
85            mRef->incSysRef();
86        }
87    }
88
89    ObjectBaseRef(T *ref) {
90        mRef = ref;
91        if (mRef) {
92            ref->incSysRef();
93        }
94    }
95
96    ~ObjectBaseRef() {
97        clear();
98    }
99
100    void set(T *ref) {
101        if (mRef != ref) {
102            clear();
103            mRef = ref;
104            if (mRef) {
105                ref->incSysRef();
106            }
107        }
108    }
109
110    void set(const ObjectBaseRef &ref) {
111        set(ref.mRef);
112    }
113
114    void clear() {
115        if (mRef) {
116            mRef->decSysRef();
117        }
118        mRef = NULL;
119    }
120
121    inline T * get() const {
122        return mRef;
123    }
124
125    inline T * operator-> () const {
126        return mRef;
127    }
128
129protected:
130    T * mRef;
131
132};
133
134
135}
136}
137
138#endif //ANDROID_RS_OBJECT_BASE_H
139
140