reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 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 | */ |
| 32 | class SkRefCnt : SkNoncopyable { |
| 33 | public: |
| 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 | |
| 84 | private: |
| 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 | */ |
| 94 | class SkAutoUnref : SkNoncopyable { |
| 95 | public: |
| 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 | |
| 116 | private: |
| 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 | |
reed@android.com | 149e2f6 | 2009-05-22 14:39:03 +0000 | [diff] [blame] | 133 | |
| 134 | /** Check if the argument is non-null, and if so, call obj->ref() |
| 135 | */ |
| 136 | template <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 | */ |
| 144 | template <typename T> static inline void SkSafeUnref(T* obj) { |
| 145 | if (obj) { |
| 146 | obj->unref(); |
| 147 | } |
| 148 | } |
| 149 | |
reed@android.com | b00cd72 | 2010-04-16 20:35:47 +0000 | [diff] [blame^] | 150 | template <typename T> |
| 151 | class SkRefPtr { |
| 152 | public: |
| 153 | SkRefPtr() : fObj(NULL) {} |
| 154 | SkRefPtr(T* obj) : fObj(obj) { SkSafeRef(fObj); } |
| 155 | SkRefPtr(const SkRefPtr& o) : fObj(o.fObj) { SkSafeRef(fObj); } |
| 156 | ~SkRefPtr() { SkSafeUnref(fObj); } |
| 157 | |
| 158 | SkRefPtr& operator=(const SkRefPtr& rp) { |
| 159 | SkRefCnt_SafeAssign(fObj, rp.fObj); |
| 160 | return *this; |
| 161 | } |
| 162 | SkRefPtr& operator=(T* obj) { |
| 163 | SkRefCnt_SafeAssign(fObj, obj); |
| 164 | return *this; |
| 165 | } |
| 166 | |
| 167 | bool operator==(const SkRefPtr& rp) const { return fObj == rp.fObj; } |
| 168 | bool operator==(const T* obj) const { return fObj == obj; } |
| 169 | bool operator!=(const SkRefPtr& rp) const { return fObj != rp.fObj; } |
| 170 | bool operator!=(const T* obj) const { return fObj != obj; } |
| 171 | |
| 172 | T* get() const { return fObj; } |
| 173 | T& operator*() const { return *fObj; } |
| 174 | T* operator->() const { return fObj; } |
| 175 | bool operator!() const { return !fObj; } |
| 176 | |
| 177 | typedef T* SkRefPtr::*unspecified_bool_type; |
| 178 | operator unspecified_bool_type() const { return fObj ? &SkRefPtr::fObj : NULL; } |
| 179 | |
| 180 | private: |
| 181 | T* fObj; |
| 182 | }; |
| 183 | |
| 184 | template <typename T> |
| 185 | inline bool operator==(T* obj, const SkRefPtr<T>& rp) { |
| 186 | return obj == rp.get(); |
| 187 | } |
| 188 | |
| 189 | template <typename T> |
| 190 | inline bool operator!=(T* obj, const SkRefPtr<T>& rp) { |
| 191 | return obj != rp.get(); |
| 192 | } |
| 193 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 194 | #endif |
| 195 | |