SkRefCnt.h revision 149e2f6159a797989f6f0fa93ecfaa66cdd55c40
1/*
2 * Copyright (C) 2006 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 SkRefCnt_DEFINED
18#define SkRefCnt_DEFINED
19
20#include "SkThread.h"
21
22/** \class SkRefCnt
23
24    SkRefCnt is the base class for objects that may be shared by multiple
25    objects. When a new owner wants a reference, it calls ref(). When an owner
26    wants to release its reference, it calls unref(). When the shared object's
27    reference count goes to zero as the result of an unref() call, its (virtual)
28    destructor is called. It is an error for the destructor to be called
29    explicitly (or via the object going out of scope on the stack or calling
30    delete) if getRefCnt() > 1.
31*/
32class SkRefCnt : SkNoncopyable {
33public:
34    /** Default construct, initializing the reference count to 1.
35    */
36    SkRefCnt() : fRefCnt(1) {}
37
38    /**  Destruct, asserting that the reference count is 1.
39    */
40    virtual ~SkRefCnt() { SkASSERT(fRefCnt == 1); }
41
42    /** Return the reference count.
43    */
44    int32_t getRefCnt() const { return fRefCnt; }
45
46    /** Increment the reference count. Must be balanced by a call to unref().
47    */
48    void ref() const {
49        SkASSERT(fRefCnt > 0);
50        sk_atomic_inc(&fRefCnt);
51    }
52
53    /** Decrement the reference count. If the reference count is 1 before the
54        decrement, then call delete on the object. Note that if this is the
55        case, then the object needs to have been allocated via new, and not on
56        the stack.
57    */
58    void unref() const {
59        SkASSERT(fRefCnt > 0);
60        if (sk_atomic_dec(&fRefCnt) == 1) {
61            fRefCnt = 1;    // so our destructor won't complain
62            SkDELETE(this);
63        }
64    }
65
66    /** Helper version of ref(), that first checks to see if this is not null.
67        If this is null, then do nothing.
68    */
69    void safeRef() const {
70        if (this) {
71            this->ref();
72        }
73    }
74
75    /** Helper version of unref(), that first checks to see if this is not null.
76        If this is null, then do nothing.
77    */
78    void safeUnref() const {
79        if (this) {
80            this->unref();
81        }
82    }
83
84private:
85    mutable int32_t fRefCnt;
86};
87
88/** \class SkAutoUnref
89
90    SkAutoUnref is a stack-helper class that will automatically call unref() on
91    the object it points to when the SkAutoUnref object goes out of scope.
92    If obj is null, do nothing.
93*/
94class SkAutoUnref : SkNoncopyable {
95public:
96    SkAutoUnref(SkRefCnt* obj) : fObj(obj) {}
97    ~SkAutoUnref();
98
99    SkRefCnt*   get() const { return fObj; }
100
101    /** If the hosted object is null, do nothing and return false, else call
102        ref() on it and return true
103    */
104    bool        ref();
105
106    /** If the hosted object is null, do nothing and return false, else call
107        unref() on it, set its reference to null, and return true
108    */
109    bool        unref();
110
111    /** If the hosted object is null, do nothing and return NULL, else call
112        unref() on it, set its reference to null, and return the object
113    */
114    SkRefCnt*   detach();
115
116private:
117    SkRefCnt*   fObj;
118};
119
120///////////////////////////////////////////////////////////////////////////////
121
122/** Helper macro to safely assign one SkRefCnt[TS]* to another, checking for
123    null in on each side of the assignment, and ensuring that ref() is called
124    before unref(), in case the two pointers point to the same object.
125*/
126#define SkRefCnt_SafeAssign(dst, src)   \
127    do {                                \
128        if (src) src->ref();            \
129        if (dst) dst->unref();          \
130        dst = src;                      \
131    } while (0)
132
133
134/** Check if the argument is non-null, and if so, call obj->ref()
135 */
136template <typename T> static inline void SkSafeRef(T* obj) {
137    if (obj) {
138        obj->ref();
139    }
140}
141
142/** Check if the argument is non-null, and if so, call obj->unref()
143 */
144template <typename T> static inline void SkSafeUnref(T* obj) {
145    if (obj) {
146        obj->unref();
147    }
148}
149
150#endif
151
152