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