OLD | NEW |
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/SkTLogic.h" | 11 #include "../private/SkTLogic.h" |
12 #include "SkTypes.h" | 12 #include "SkTypes.h" |
13 #include <atomic> | 13 #include <atomic> |
14 #include <functional> | 14 #include <functional> |
15 #include <memory> | 15 #include <memory> |
| 16 #include <type_traits> |
16 #include <utility> | 17 #include <utility> |
17 | 18 |
18 #define SK_SUPPORT_TRANSITION_TO_SP_INTERFACES | 19 #define SK_SUPPORT_TRANSITION_TO_SP_INTERFACES |
19 | 20 |
20 /** \class SkRefCntBase | 21 /** \class SkRefCntBase |
21 | 22 |
22 SkRefCntBase is the base class for objects that may be shared by multiple | 23 SkRefCntBase is the base class for objects that may be shared by multiple |
23 objects. When an existing owner wants to share a reference, it calls ref(). | 24 objects. When an existing owner wants to share a reference, it calls ref(). |
24 When an owner wants to release its reference, it calls unref(). When the | 25 When an owner wants to release its reference, it calls unref(). When the |
25 shared object's reference count goes to zero as the result of an unref() | 26 shared object's reference count goes to zero as the result of an unref() |
(...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
264 using element_type = T; | 265 using element_type = T; |
265 | 266 |
266 sk_sp() : fPtr(nullptr) {} | 267 sk_sp() : fPtr(nullptr) {} |
267 sk_sp(std::nullptr_t) : fPtr(nullptr) {} | 268 sk_sp(std::nullptr_t) : fPtr(nullptr) {} |
268 | 269 |
269 /** | 270 /** |
270 * Shares the underlying object by calling ref(), so that both the argument
and the newly | 271 * Shares the underlying object by calling ref(), so that both the argument
and the newly |
271 * created sk_sp both have a reference to it. | 272 * created sk_sp both have a reference to it. |
272 */ | 273 */ |
273 sk_sp(const sk_sp<T>& that) : fPtr(SkSafeRef(that.get())) {} | 274 sk_sp(const sk_sp<T>& that) : fPtr(SkSafeRef(that.get())) {} |
274 template <typename U, typename = skstd::enable_if_t<skstd::is_convertible<U*
, T*>::value>> | 275 template <typename U, typename = skstd::enable_if_t<std::is_convertible<U*,
T*>::value>> |
275 sk_sp(const sk_sp<U>& that) : fPtr(SkSafeRef(that.get())) {} | 276 sk_sp(const sk_sp<U>& that) : fPtr(SkSafeRef(that.get())) {} |
276 | 277 |
277 /** | 278 /** |
278 * Move the underlying object from the argument to the newly created sk_sp.
Afterwards only | 279 * Move the underlying object from the argument to the newly created sk_sp.
Afterwards only |
279 * the new sk_sp will have a reference to the object, and the argument will
point to null. | 280 * the new sk_sp will have a reference to the object, and the argument will
point to null. |
280 * No call to ref() or unref() will be made. | 281 * No call to ref() or unref() will be made. |
281 */ | 282 */ |
282 sk_sp(sk_sp<T>&& that) : fPtr(that.release()) {} | 283 sk_sp(sk_sp<T>&& that) : fPtr(that.release()) {} |
283 template <typename U, typename = skstd::enable_if_t<skstd::is_convertible<U*
, T*>::value>> | 284 template <typename U, typename = skstd::enable_if_t<std::is_convertible<U*,
T*>::value>> |
284 sk_sp(sk_sp<U>&& that) : fPtr(that.release()) {} | 285 sk_sp(sk_sp<U>&& that) : fPtr(that.release()) {} |
285 | 286 |
286 /** | 287 /** |
287 * Adopt the bare pointer into the newly created sk_sp. | 288 * Adopt the bare pointer into the newly created sk_sp. |
288 * No call to ref() or unref() will be made. | 289 * No call to ref() or unref() will be made. |
289 */ | 290 */ |
290 explicit sk_sp(T* obj) : fPtr(obj) {} | 291 explicit sk_sp(T* obj) : fPtr(obj) {} |
291 | 292 |
292 /** | 293 /** |
293 * Calls unref() on the underlying object pointer. | 294 * Calls unref() on the underlying object pointer. |
294 */ | 295 */ |
295 ~sk_sp() { | 296 ~sk_sp() { |
296 SkSafeUnref(fPtr); | 297 SkSafeUnref(fPtr); |
297 SkDEBUGCODE(fPtr = nullptr); | 298 SkDEBUGCODE(fPtr = nullptr); |
298 } | 299 } |
299 | 300 |
300 sk_sp<T>& operator=(std::nullptr_t) { this->reset(); return *this; } | 301 sk_sp<T>& operator=(std::nullptr_t) { this->reset(); return *this; } |
301 | 302 |
302 /** | 303 /** |
303 * Shares the underlying object referenced by the argument by calling ref()
on it. If this | 304 * Shares the underlying object referenced by the argument by calling ref()
on it. If this |
304 * sk_sp previously had a reference to an object (i.e. not null) it will ca
ll unref() on that | 305 * sk_sp previously had a reference to an object (i.e. not null) it will ca
ll unref() on that |
305 * object. | 306 * object. |
306 */ | 307 */ |
307 sk_sp<T>& operator=(const sk_sp<T>& that) { | 308 sk_sp<T>& operator=(const sk_sp<T>& that) { |
308 this->reset(SkSafeRef(that.get())); | 309 this->reset(SkSafeRef(that.get())); |
309 return *this; | 310 return *this; |
310 } | 311 } |
311 template <typename U, typename = skstd::enable_if_t<skstd::is_convertible<U*
, T*>::value>> | 312 template <typename U, typename = skstd::enable_if_t<std::is_convertible<U*,
T*>::value>> |
312 sk_sp<T>& operator=(const sk_sp<U>& that) { | 313 sk_sp<T>& operator=(const sk_sp<U>& that) { |
313 this->reset(SkSafeRef(that.get())); | 314 this->reset(SkSafeRef(that.get())); |
314 return *this; | 315 return *this; |
315 } | 316 } |
316 | 317 |
317 /** | 318 /** |
318 * Move the underlying object from the argument to the sk_sp. If the sk_sp
previously held | 319 * Move the underlying object from the argument to the sk_sp. If the sk_sp
previously held |
319 * a reference to another object, unref() will be called on that object. No
call to ref() | 320 * a reference to another object, unref() will be called on that object. No
call to ref() |
320 * will be made. | 321 * will be made. |
321 */ | 322 */ |
322 sk_sp<T>& operator=(sk_sp<T>&& that) { | 323 sk_sp<T>& operator=(sk_sp<T>&& that) { |
323 this->reset(that.release()); | 324 this->reset(that.release()); |
324 return *this; | 325 return *this; |
325 } | 326 } |
326 template <typename U, typename = skstd::enable_if_t<skstd::is_convertible<U*
, T*>::value>> | 327 template <typename U, typename = skstd::enable_if_t<std::is_convertible<U*,
T*>::value>> |
327 sk_sp<T>& operator=(sk_sp<U>&& that) { | 328 sk_sp<T>& operator=(sk_sp<U>&& that) { |
328 this->reset(that.release()); | 329 this->reset(that.release()); |
329 return *this; | 330 return *this; |
330 } | 331 } |
331 | 332 |
332 T& operator*() const { | 333 T& operator*() const { |
333 SkASSERT(this->get() != nullptr); | 334 SkASSERT(this->get() != nullptr); |
334 return *this->get(); | 335 return *this->get(); |
335 } | 336 } |
336 | 337 |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
459 * | 460 * |
460 * This function may be helpful while we convert callers from ptr-based to sk_s
p-based parameters. | 461 * This function may be helpful while we convert callers from ptr-based to sk_s
p-based parameters. |
461 */ | 462 */ |
462 template <typename T> sk_sp<T> sk_ref_sp(T* obj) { | 463 template <typename T> sk_sp<T> sk_ref_sp(T* obj) { |
463 return sk_sp<T>(SkSafeRef(obj)); | 464 return sk_sp<T>(SkSafeRef(obj)); |
464 } | 465 } |
465 | 466 |
466 #endif | 467 #endif |
467 | 468 |
468 #endif | 469 #endif |
OLD | NEW |