rsObjectBase.h revision 1030893d9b99b72468034da13df025bda479bb97
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
36private:
37    mutable int32_t mRefCount;
38
39
40};
41
42template<class T>
43class ObjectBaseRef
44{
45public:
46    ObjectBaseRef() {
47        mRef = NULL;
48    }
49
50    ObjectBaseRef(const ObjectBaseRef &ref) {
51        mRef = ref.get();
52        mRef->incRef();
53    }
54
55    ObjectBaseRef(T *ref) {
56        mRef = ref;
57        ref->incRef();
58    }
59
60    ~ObjectBaseRef() {
61        clear();
62    }
63
64    void set(T *ref) {
65        if (mRef != ref) {
66            clear();
67            mRef = ref;
68            ref->incRef();
69        }
70    }
71
72    void clear() {
73        if (mRef) {
74            mRef->decRef();
75        }
76        mRef = NULL;
77    }
78
79    inline T * get() const {
80        return mRef;
81    }
82
83    inline T * operator-> () const {
84        return mRef;
85    }
86
87protected:
88    T * mRef;
89
90};
91
92
93}
94}
95
96#endif //ANDROID_RS_OBJECT_BASE_H
97
98