SkRefCnt.h revision ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976e
1
2/*
3 * Copyright 2006 The Android Open Source Project
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9
10#ifndef SkRefCnt_DEFINED
11#define SkRefCnt_DEFINED
12
13#include "SkThread.h"
14
15/** \class SkRefCnt
16
17    SkRefCnt is the base class for objects that may be shared by multiple
18    objects. When a new owner wants a reference, it calls ref(). When an owner
19    wants to release its reference, it calls unref(). When the shared object's
20    reference count goes to zero as the result of an unref() call, its (virtual)
21    destructor is called. It is an error for the destructor to be called
22    explicitly (or via the object going out of scope on the stack or calling
23    delete) if getRefCnt() > 1.
24*/
25class SK_API SkRefCnt : SkNoncopyable {
26public:
27    /** Default construct, initializing the reference count to 1.
28    */
29    SkRefCnt() : fRefCnt(1) {}
30
31    /**  Destruct, asserting that the reference count is 1.
32    */
33    virtual ~SkRefCnt() { SkASSERT(fRefCnt == 1); }
34
35    /** Return the reference count.
36    */
37    int32_t getRefCnt() const { return fRefCnt; }
38
39    /** Increment the reference count. Must be balanced by a call to unref().
40    */
41    void ref() const {
42        SkASSERT(fRefCnt > 0);
43        sk_atomic_inc(&fRefCnt);
44    }
45
46    /** Decrement the reference count. If the reference count is 1 before the
47        decrement, then call delete on the object. Note that if this is the
48        case, then the object needs to have been allocated via new, and not on
49        the stack.
50    */
51    void unref() const {
52        SkASSERT(fRefCnt > 0);
53        if (sk_atomic_dec(&fRefCnt) == 1) {
54            fRefCnt = 1;    // so our destructor won't complain
55            SkDELETE(this);
56        }
57    }
58
59    void validate() const {
60        SkASSERT(fRefCnt > 0);
61    }
62
63private:
64    mutable int32_t fRefCnt;
65};
66
67///////////////////////////////////////////////////////////////////////////////
68
69/** Helper macro to safely assign one SkRefCnt[TS]* to another, checking for
70    null in on each side of the assignment, and ensuring that ref() is called
71    before unref(), in case the two pointers point to the same object.
72 */
73#define SkRefCnt_SafeAssign(dst, src)   \
74    do {                                \
75        if (src) src->ref();            \
76        if (dst) dst->unref();          \
77        dst = src;                      \
78    } while (0)
79
80
81/** Check if the argument is non-null, and if so, call obj->ref()
82 */
83template <typename T> static inline void SkSafeRef(T* obj) {
84    if (obj) {
85        obj->ref();
86    }
87}
88
89/** Check if the argument is non-null, and if so, call obj->unref()
90 */
91template <typename T> static inline void SkSafeUnref(T* obj) {
92    if (obj) {
93        obj->unref();
94    }
95}
96
97///////////////////////////////////////////////////////////////////////////////
98
99/**
100 *  Utility class that simply unref's its argument in the destructor.
101 */
102template <typename T> class SkAutoTUnref : SkNoncopyable {
103public:
104    SkAutoTUnref(T* obj) : fObj(obj) {}
105    ~SkAutoTUnref() { SkSafeUnref(fObj); }
106
107    T* get() const { return fObj; }
108
109    /**
110     *  Return the hosted object (which may be null), transferring ownership.
111     *  The reference count is not modified, and the internal ptr is set to NULL
112     *  so unref() will not be called in our destructor. A subsequent call to
113     *  detach() will do nothing and return null.
114     */
115    T* detach() {
116        T* obj = fObj;
117        fObj = NULL;
118        return obj;
119    }
120
121private:
122    T*  fObj;
123};
124
125class SkAutoUnref : public SkAutoTUnref<SkRefCnt> {
126public:
127    SkAutoUnref(SkRefCnt* obj) : SkAutoTUnref<SkRefCnt>(obj) {}
128};
129
130class SkAutoRef : SkNoncopyable {
131public:
132    SkAutoRef(SkRefCnt* obj) : fObj(obj) { SkSafeRef(obj); }
133    ~SkAutoRef() { SkSafeUnref(fObj); }
134private:
135    SkRefCnt* fObj;
136};
137
138/** Wrapper class for SkRefCnt pointers. This manages ref/unref of a pointer to
139    a SkRefCnt (or subclass) object.
140 */
141template <typename T> class SkRefPtr {
142public:
143    SkRefPtr() : fObj(NULL) {}
144    SkRefPtr(T* obj) : fObj(obj) { SkSafeRef(fObj); }
145    SkRefPtr(const SkRefPtr& o) : fObj(o.fObj) { SkSafeRef(fObj); }
146    ~SkRefPtr() { SkSafeUnref(fObj); }
147
148    SkRefPtr& operator=(const SkRefPtr& rp) {
149        SkRefCnt_SafeAssign(fObj, rp.fObj);
150        return *this;
151    }
152    SkRefPtr& operator=(T* obj) {
153        SkRefCnt_SafeAssign(fObj, obj);
154        return *this;
155    }
156
157    T* get() const { return fObj; }
158    T& operator*() const { return *fObj; }
159    T* operator->() const { return fObj; }
160
161    typedef T* SkRefPtr::*unspecified_bool_type;
162    operator unspecified_bool_type() const {
163        return fObj ? &SkRefPtr::fObj : NULL;
164    }
165
166private:
167    T* fObj;
168};
169
170#endif
171
172