Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(822)

Side by Side Diff: include/core/SkRefCnt.h

Issue 1370803002: Base SkAutoTUnref on skstd::unique_ptr. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Rebase Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | src/core/SkPath.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2006 The Android Open Source Project 2 * Copyright 2006 The Android Open Source Project
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #ifndef SkRefCnt_DEFINED 8 #ifndef SkRefCnt_DEFINED
9 #define SkRefCnt_DEFINED 9 #define SkRefCnt_DEFINED
10 10
11 #include "../private/SkAtomics.h" 11 #include "../private/SkAtomics.h"
12 #include "../private/SkUniquePtr.h"
12 #include "SkTypes.h" 13 #include "SkTypes.h"
13 14
14 /** \class SkRefCntBase 15 /** \class SkRefCntBase
15 16
16 SkRefCntBase is the base class for objects that may be shared by multiple 17 SkRefCntBase is the base class for objects that may be shared by multiple
17 objects. When an existing owner wants to share a reference, it calls ref(). 18 objects. When an existing owner wants to share a reference, it calls ref().
18 When an owner wants to release its reference, it calls unref(). When the 19 When an owner wants to release its reference, it calls unref(). When the
19 shared object's reference count goes to zero as the result of an unref() 20 shared object's reference count goes to zero as the result of an unref()
20 call, its (virtual) destructor is called. It is an error for the 21 call, its (virtual) destructor is called. It is an error for the
21 destructor to be called explicitly (or via the object going out of scope on 22 destructor to be called explicitly (or via the object going out of scope on
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
136 before unref(), in case the two pointers point to the same object. 137 before unref(), in case the two pointers point to the same object.
137 */ 138 */
138 #define SkRefCnt_SafeAssign(dst, src) \ 139 #define SkRefCnt_SafeAssign(dst, src) \
139 do { \ 140 do { \
140 if (src) src->ref(); \ 141 if (src) src->ref(); \
141 if (dst) dst->unref(); \ 142 if (dst) dst->unref(); \
142 dst = src; \ 143 dst = src; \
143 } while (0) 144 } while (0)
144 145
145 146
146 /** Call obj->ref() and return obj. The obj must not be NULL. 147 /** Call obj->ref() and return obj. The obj must not be nullptr.
147 */ 148 */
148 template <typename T> static inline T* SkRef(T* obj) { 149 template <typename T> static inline T* SkRef(T* obj) {
149 SkASSERT(obj); 150 SkASSERT(obj);
150 obj->ref(); 151 obj->ref();
151 return obj; 152 return obj;
152 } 153 }
153 154
154 /** Check if the argument is non-null, and if so, call obj->ref() and return obj . 155 /** Check if the argument is non-null, and if so, call obj->ref() and return obj .
155 */ 156 */
156 template <typename T> static inline T* SkSafeRef(T* obj) { 157 template <typename T> static inline T* SkSafeRef(T* obj) {
157 if (obj) { 158 if (obj) {
158 obj->ref(); 159 obj->ref();
159 } 160 }
160 return obj; 161 return obj;
161 } 162 }
162 163
163 /** Check if the argument is non-null, and if so, call obj->unref() 164 /** Check if the argument is non-null, and if so, call obj->unref()
164 */ 165 */
165 template <typename T> static inline void SkSafeUnref(T* obj) { 166 template <typename T> static inline void SkSafeUnref(T* obj) {
166 if (obj) { 167 if (obj) {
167 obj->unref(); 168 obj->unref();
168 } 169 }
169 } 170 }
170 171
171 template<typename T> static inline void SkSafeSetNull(T*& obj) { 172 template<typename T> static inline void SkSafeSetNull(T*& obj) {
172 if (obj) { 173 if (obj) {
173 obj->unref(); 174 obj->unref();
174 obj = NULL; 175 obj = nullptr;
175 } 176 }
176 } 177 }
177 178
178 /////////////////////////////////////////////////////////////////////////////// 179 ///////////////////////////////////////////////////////////////////////////////
179 180
181 template <typename T> struct SkTUnref {
182 void operator()(T* t) { t->unref(); }
183 };
184
180 /** 185 /**
181 * Utility class that simply unref's its argument in the destructor. 186 * Utility class that simply unref's its argument in the destructor.
182 */ 187 */
183 template <typename T> class SkAutoTUnref : SkNoncopyable { 188 template <typename T> class SkAutoTUnref : public skstd::unique_ptr<T, SkTUnref< T>> {
184 public: 189 public:
185 explicit SkAutoTUnref(T* obj = NULL) : fObj(obj) {} 190 explicit SkAutoTUnref(T* obj = nullptr) : skstd::unique_ptr<T, SkTUnref<T>>( obj) {}
186 ~SkAutoTUnref() { SkSafeUnref(fObj); }
187 191
188 T* get() const { return fObj; } 192 T* detach() { return this->release(); }
189 193 operator T*() const { return this->get(); }
190 T* reset(T* obj) {
191 SkSafeUnref(fObj);
192 fObj = obj;
193 return obj;
194 }
195
196 void swap(SkAutoTUnref* other) {
197 T* tmp = fObj;
198 fObj = other->fObj;
199 other->fObj = tmp;
200 }
201
202 /**
203 * Return the hosted object (which may be null), transferring ownership.
204 * The reference count is not modified, and the internal ptr is set to NULL
205 * so unref() will not be called in our destructor. A subsequent call to
206 * detach() will do nothing and return null.
207 */
208 T* detach() {
209 T* obj = fObj;
210 fObj = NULL;
211 return obj;
212 }
213
214 T* operator->() const { return fObj; }
215 operator T*() const { return fObj; }
216
217 private:
218 T* fObj;
219 }; 194 };
220 // Can't use the #define trick below to guard a bare SkAutoTUnref(...) because i t's templated. :( 195 // Can't use the #define trick below to guard a bare SkAutoTUnref(...) because i t's templated. :(
221 196
222 class SkAutoUnref : public SkAutoTUnref<SkRefCnt> { 197 class SkAutoUnref : public SkAutoTUnref<SkRefCnt> {
223 public: 198 public:
224 SkAutoUnref(SkRefCnt* obj) : SkAutoTUnref<SkRefCnt>(obj) {} 199 SkAutoUnref(SkRefCnt* obj) : SkAutoTUnref<SkRefCnt>(obj) {}
225 }; 200 };
226 #define SkAutoUnref(...) SK_REQUIRE_LOCAL_VAR(SkAutoUnref) 201 #define SkAutoUnref(...) SK_REQUIRE_LOCAL_VAR(SkAutoUnref)
227 202
228 // This is a variant of SkRefCnt that's Not Virtual, so weighs 4 bytes instead o f 8 or 16. 203 // This is a variant of SkRefCnt that's Not Virtual, so weighs 4 bytes instead o f 8 or 16.
(...skipping 17 matching lines...) Expand all
246 delete (const Derived*)this; 221 delete (const Derived*)this;
247 } 222 }
248 } 223 }
249 void deref() const { this->unref(); } 224 void deref() const { this->unref(); }
250 225
251 private: 226 private:
252 mutable int32_t fRefCnt; 227 mutable int32_t fRefCnt;
253 }; 228 };
254 229
255 #endif 230 #endif
OLDNEW
« no previous file with comments | « no previous file | src/core/SkPath.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698