rsObjectBase.h revision a0a1b6fbece2eb8d72d788422ab3e5f58d5a9216
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 incRef() const;
34    void decRef() const;
35
36    const char * getName() const {
37        return mName;
38    }
39    void setName(const char *);
40
41private:
42    char * mName;
43    mutable int32_t mRefCount;
44
45
46};
47
48template<class T>
49class ObjectBaseRef
50{
51public:
52    ObjectBaseRef() {
53        mRef = NULL;
54    }
55
56    ObjectBaseRef(const ObjectBaseRef &ref) {
57        mRef = ref.get();
58        if (mRef) {
59            mRef->incRef();
60        }
61    }
62
63    ObjectBaseRef(T *ref) {
64        mRef = ref;
65        if (mRef) {
66            ref->incRef();
67        }
68    }
69
70    ~ObjectBaseRef() {
71        clear();
72    }
73
74    void set(T *ref) {
75        if (mRef != ref) {
76            clear();
77            mRef = ref;
78            if (mRef) {
79                ref->incRef();
80            }
81        }
82    }
83
84    void set(const ObjectBaseRef &ref) {
85        set(ref.mRef);
86    }
87
88    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
106};
107
108
109}
110}
111
112#endif //ANDROID_RS_OBJECT_BASE_H
113
114