rsObjectBase.h revision 326e0ddf89e8df2837752fbfd7a014814b32082c
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() {
51        clear();
52    }
53
54    void set(T *ref) {
55        if (mRef != ref) {
56            clear();
57            mRef = ref;
58            ref->incRef();
59        }
60    }
61
62    void clear() {
63        if (mRef) {
64            mRef->decRef();
65        }
66        mRef = NULL;
67    }
68
69    inline T * get() const {
70        return mRef;
71    }
72
73    inline T * operator-> () const {
74        return mRef;
75    }
76
77protected:
78    T * mRef;
79
80private:
81    ObjectBaseRef(const ObjectBaseRef &) {};
82
83};
84
85
86}
87}
88
89#endif //ANDROID_RS_OBJECT_BASE_H
90
91