blob: 04670e8e12161fdf153e94278a197e7f291ac89e [file] [log] [blame]
reed@android.com8a1c16f2008-12-17 15:59:43 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2006 The Android Open Source Project
reed@android.com8a1c16f2008-12-17 15:59:43 +00003 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00004 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
reed@android.com8a1c16f2008-12-17 15:59:43 +00006 */
7
8#ifndef SkRefCnt_DEFINED
9#define SkRefCnt_DEFINED
10
mtklein5f939ab2016-03-16 10:28:35 -070011#include "../private/SkTLogic.h"
bungemanf3c15b72015-08-19 11:56:48 -070012#include "SkTypes.h"
bungeman2c4bd072016-04-08 06:58:51 -070013#include <atomic>
bungemanc901c112016-03-08 08:35:23 -080014#include <functional>
mtklein5f939ab2016-03-16 10:28:35 -070015#include <memory>
bungeman801b44c2016-04-28 11:18:07 -070016#include <type_traits>
halcanary217c0b32016-03-02 08:06:20 -080017#include <utility>
reed@android.com8a1c16f2008-12-17 15:59:43 +000018
commit-bot@chromium.org6d2533e2013-10-16 15:15:58 +000019/** \class SkRefCntBase
20
bungeman@google.com10ba0062013-10-25 18:40:24 +000021 SkRefCntBase is the base class for objects that may be shared by multiple
bungeman@google.coma02bc152012-05-16 18:21:56 +000022 objects. When an existing owner wants to share a reference, it calls ref().
23 When an owner wants to release its reference, it calls unref(). When the
24 shared object's reference count goes to zero as the result of an unref()
25 call, its (virtual) destructor is called. It is an error for the
26 destructor to be called explicitly (or via the object going out of scope on
27 the stack or calling delete) if getRefCnt() > 1.
reed@android.com8a1c16f2008-12-17 15:59:43 +000028*/
commit-bot@chromium.orge3beb6b2014-04-07 19:34:38 +000029class SK_API SkRefCntBase : SkNoncopyable {
reed@android.com8a1c16f2008-12-17 15:59:43 +000030public:
31 /** Default construct, initializing the reference count to 1.
32 */
bungeman@google.com10ba0062013-10-25 18:40:24 +000033 SkRefCntBase() : fRefCnt(1) {}
reed@android.com8a1c16f2008-12-17 15:59:43 +000034
bungeman@google.coma02bc152012-05-16 18:21:56 +000035 /** Destruct, asserting that the reference count is 1.
reed@android.com8a1c16f2008-12-17 15:59:43 +000036 */
bungeman@google.com10ba0062013-10-25 18:40:24 +000037 virtual ~SkRefCntBase() {
reed@google.com4c888aa2011-09-12 19:54:12 +000038#ifdef SK_DEBUG
bungeman2c4bd072016-04-08 06:58:51 -070039 SkASSERTF(getRefCnt() == 1, "fRefCnt was %d", getRefCnt());
40 // illegal value, to catch us if we reuse after delete
41 fRefCnt.store(0, std::memory_order_relaxed);
reed@google.com4c888aa2011-09-12 19:54:12 +000042#endif
43 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000044
mtklein21180e22014-11-25 07:30:19 -080045#ifdef SK_DEBUG
bungeman@google.comf64c6842013-07-19 23:18:52 +000046 /** Return the reference count. Use only for debugging. */
bungeman2c4bd072016-04-08 06:58:51 -070047 int32_t getRefCnt() const {
48 return fRefCnt.load(std::memory_order_relaxed);
49 }
50
51 void validate() const {
52 SkASSERT(getRefCnt() > 0);
53 }
mtklein21180e22014-11-25 07:30:19 -080054#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +000055
commit-bot@chromium.orgea6e14a2014-02-04 18:00:23 +000056 /** May return true if the caller is the only owner.
bungeman@google.comf64c6842013-07-19 23:18:52 +000057 * Ensures that all previous owner's actions are complete.
58 */
59 bool unique() const {
bungeman2c4bd072016-04-08 06:58:51 -070060 if (1 == fRefCnt.load(std::memory_order_acquire)) {
mtklein7b274c72015-02-03 13:38:58 -080061 // The acquire barrier is only really needed if we return true. It
62 // prevents code conditioned on the result of unique() from running
63 // until previous owners are all totally done calling unref().
64 return true;
bungeman@google.comf64c6842013-07-19 23:18:52 +000065 }
mtklein7b274c72015-02-03 13:38:58 -080066 return false;
bungeman@google.comf64c6842013-07-19 23:18:52 +000067 }
68
reed@android.com8a1c16f2008-12-17 15:59:43 +000069 /** Increment the reference count. Must be balanced by a call to unref().
70 */
71 void ref() const {
bungeman2c4bd072016-04-08 06:58:51 -070072 SkASSERT(getRefCnt() > 0);
bungeman2c4bd072016-04-08 06:58:51 -070073 // No barrier required.
74 (void)fRefCnt.fetch_add(+1, std::memory_order_relaxed);
reed@android.com8a1c16f2008-12-17 15:59:43 +000075 }
76
77 /** Decrement the reference count. If the reference count is 1 before the
bungeman@google.coma02bc152012-05-16 18:21:56 +000078 decrement, then delete the object. Note that if this is the case, then
79 the object needs to have been allocated via new, and not on the stack.
reed@android.com8a1c16f2008-12-17 15:59:43 +000080 */
81 void unref() const {
bungeman2c4bd072016-04-08 06:58:51 -070082 SkASSERT(getRefCnt() > 0);
mtklein7b274c72015-02-03 13:38:58 -080083 // A release here acts in place of all releases we "should" have been doing in ref().
bungeman2c4bd072016-04-08 06:58:51 -070084 if (1 == fRefCnt.fetch_add(-1, std::memory_order_acq_rel)) {
mtklein7b274c72015-02-03 13:38:58 -080085 // Like unique(), the acquire is only needed on success, to make sure
86 // code in internal_dispose() doesn't happen before the decrement.
87 this->internal_dispose();
reed@android.com8a1c16f2008-12-17 15:59:43 +000088 }
89 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000090
reed@google.comf7943032012-07-23 14:50:38 +000091protected:
92 /**
93 * Allow subclasses to call this if they've overridden internal_dispose
mtklein9c5052f2016-08-06 12:51:51 -070094 * so they can reset fRefCnt before the destructor is called or if they
95 * choose not to call the destructor (e.g. using a free list).
reed@google.comf7943032012-07-23 14:50:38 +000096 */
97 void internal_dispose_restore_refcnt_to_1() const {
bungeman2c4bd072016-04-08 06:58:51 -070098 SkASSERT(0 == getRefCnt());
99 fRefCnt.store(1, std::memory_order_relaxed);
reed@google.comf7943032012-07-23 14:50:38 +0000100 }
101
102private:
103 /**
104 * Called when the ref count goes to 0.
105 */
106 virtual void internal_dispose() const {
107 this->internal_dispose_restore_refcnt_to_1();
halcanary385fe4d2015-08-26 13:07:48 -0700108 delete this;
bungeman@google.coma02bc152012-05-16 18:21:56 +0000109 }
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000110
bungeman@google.comf64c6842013-07-19 23:18:52 +0000111 // The following friends are those which override internal_dispose()
112 // and conditionally call SkRefCnt::internal_dispose().
bungeman@google.coma02bc152012-05-16 18:21:56 +0000113 friend class SkWeakRefCnt;
114
bungeman2c4bd072016-04-08 06:58:51 -0700115 mutable std::atomic<int32_t> fRefCnt;
robertphillips@google.com4d73ac22012-06-13 18:54:08 +0000116
bungeman@google.com10ba0062013-10-25 18:40:24 +0000117 typedef SkNoncopyable INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000118};
119
bungeman@google.com10ba0062013-10-25 18:40:24 +0000120#ifdef SK_REF_CNT_MIXIN_INCLUDE
121// It is the responsibility of the following include to define the type SkRefCnt.
122// This SkRefCnt should normally derive from SkRefCntBase.
123#include SK_REF_CNT_MIXIN_INCLUDE
124#else
benjaminwagnerf9634b92016-04-13 17:51:57 -0700125class SK_API SkRefCnt : public SkRefCntBase {
126 // "#include SK_REF_CNT_MIXIN_INCLUDE" doesn't work with this build system.
127 #if defined(GOOGLE3)
128 public:
129 void deref() const { this->unref(); }
130 #endif
131};
bungeman@google.com10ba0062013-10-25 18:40:24 +0000132#endif
133
reed@google.com7f6d6d42011-07-15 15:25:22 +0000134///////////////////////////////////////////////////////////////////////////////
135
136/** Helper macro to safely assign one SkRefCnt[TS]* to another, checking for
137 null in on each side of the assignment, and ensuring that ref() is called
138 before unref(), in case the two pointers point to the same object.
139 */
Hans Boehm0e908122016-09-19 22:31:29 -0700140
141#if defined(SK_BUILD_FOR_ANDROID_FRAMEWORK) || defined(SK_DEBUG)
142// This version heuristically detects data races, since those otherwise result
143// in redundant reference count decrements, which are exceedingly
144// difficult to debug.
145
146#define SkRefCnt_SafeAssign(dst, src) \
147 do { \
Hans Boehme9f90a22016-09-27 14:25:27 -0700148 typedef typename std::remove_reference<decltype(dst)>::type \
149 SkRefCntPtrT; \
150 SkRefCntPtrT old_dst = *const_cast<SkRefCntPtrT volatile *>(&dst); \
Hans Boehm0e908122016-09-19 22:31:29 -0700151 if (src) src->ref(); \
152 if (old_dst) old_dst->unref(); \
Hans Boehme9f90a22016-09-27 14:25:27 -0700153 if (old_dst != *const_cast<SkRefCntPtrT volatile *>(&dst)) { \
154 SkDebugf("Detected racing Skia calls at %s:%d\n", \
155 __FILE__, __LINE__); \
Hans Boehm0e908122016-09-19 22:31:29 -0700156 } \
157 dst = src; \
158 } while (0)
159
160#else /* !(SK_BUILD_FOR_ANDROID_FRAMEWORK || SK_DEBUG) */
161
reed@google.com7f6d6d42011-07-15 15:25:22 +0000162#define SkRefCnt_SafeAssign(dst, src) \
163 do { \
164 if (src) src->ref(); \
165 if (dst) dst->unref(); \
166 dst = src; \
167 } while (0)
168
Hans Boehm0e908122016-09-19 22:31:29 -0700169#endif
170
reed@google.com7f6d6d42011-07-15 15:25:22 +0000171
bungeman77a53de2015-10-01 12:28:49 -0700172/** Call obj->ref() and return obj. The obj must not be nullptr.
reed@google.com7f6d6d42011-07-15 15:25:22 +0000173 */
bungeman@google.com1fd201b2012-08-22 18:56:56 +0000174template <typename T> static inline T* SkRef(T* obj) {
175 SkASSERT(obj);
176 obj->ref();
177 return obj;
178}
179
180/** Check if the argument is non-null, and if so, call obj->ref() and return obj.
181 */
182template <typename T> static inline T* SkSafeRef(T* obj) {
reed@google.com7f6d6d42011-07-15 15:25:22 +0000183 if (obj) {
184 obj->ref();
185 }
bungeman@google.com1fd201b2012-08-22 18:56:56 +0000186 return obj;
reed@google.com7f6d6d42011-07-15 15:25:22 +0000187}
188
189/** Check if the argument is non-null, and if so, call obj->unref()
190 */
191template <typename T> static inline void SkSafeUnref(T* obj) {
192 if (obj) {
193 obj->unref();
194 }
195}
196
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000197template<typename T> static inline void SkSafeSetNull(T*& obj) {
bsalomon49f085d2014-09-05 13:34:00 -0700198 if (obj) {
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000199 obj->unref();
bungeman77a53de2015-10-01 12:28:49 -0700200 obj = nullptr;
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000201 }
202}
203
reed@google.com7f6d6d42011-07-15 15:25:22 +0000204///////////////////////////////////////////////////////////////////////////////
205
mtklein08d1fcc2014-11-20 09:18:31 -0800206// This is a variant of SkRefCnt that's Not Virtual, so weighs 4 bytes instead of 8 or 16.
207// There's only benefit to using this if the deriving class does not otherwise need a vtable.
208template <typename Derived>
209class SkNVRefCnt : SkNoncopyable {
210public:
211 SkNVRefCnt() : fRefCnt(1) {}
bungeman2c4bd072016-04-08 06:58:51 -0700212 ~SkNVRefCnt() { SkASSERTF(1 == getRefCnt(), "NVRefCnt was %d", getRefCnt()); }
mtklein08d1fcc2014-11-20 09:18:31 -0800213
214 // Implementation is pretty much the same as SkRefCntBase. All required barriers are the same:
215 // - unique() needs acquire when it returns true, and no barrier if it returns false;
216 // - ref() doesn't need any barrier;
217 // - unref() needs a release barrier, and an acquire if it's going to call delete.
218
bungeman2c4bd072016-04-08 06:58:51 -0700219 bool unique() const { return 1 == fRefCnt.load(std::memory_order_acquire); }
220 void ref() const { (void)fRefCnt.fetch_add(+1, std::memory_order_relaxed); }
reed90d0ff02014-11-24 12:02:31 -0800221 void unref() const {
bungeman2c4bd072016-04-08 06:58:51 -0700222 if (1 == fRefCnt.fetch_add(-1, std::memory_order_acq_rel)) {
223 // restore the 1 for our destructor's assert
224 SkDEBUGCODE(fRefCnt.store(1, std::memory_order_relaxed));
mtklein97466ab2015-12-07 13:37:00 -0800225 delete (const Derived*)this;
reed90d0ff02014-11-24 12:02:31 -0800226 }
227 }
mtklein7b274c72015-02-03 13:38:58 -0800228 void deref() const { this->unref(); }
Florin Malita844aa332014-11-20 16:56:22 -0500229
mtklein08d1fcc2014-11-20 09:18:31 -0800230private:
bungeman2c4bd072016-04-08 06:58:51 -0700231 mutable std::atomic<int32_t> fRefCnt;
232 int32_t getRefCnt() const {
233 return fRefCnt.load(std::memory_order_relaxed);
234 }
mtklein08d1fcc2014-11-20 09:18:31 -0800235};
236
reedbb7b0432016-03-01 07:28:51 -0800237///////////////////////////////////////////////////////////////////////////////////////////////////
238
239/**
240 * Shared pointer class to wrap classes that support a ref()/unref() interface.
241 *
242 * This can be used for classes inheriting from SkRefCnt, but it also works for other
243 * classes that match the interface, but have different internal choices: e.g. the hosted class
244 * may have its ref/unref be thread-safe, but that is not assumed/imposed by sk_sp.
245 */
246template <typename T> class sk_sp {
bungemanbeab9e72016-03-03 07:50:49 -0800247 /** Supports safe bool idiom. Obsolete with explicit operator bool. */
248 using unspecified_bool_type = T* sk_sp::*;
reedbb7b0432016-03-01 07:28:51 -0800249public:
bungemanc901c112016-03-08 08:35:23 -0800250 using element_type = T;
251
bungeman06ca8ec2016-06-09 08:01:03 -0700252 constexpr sk_sp() : fPtr(nullptr) {}
253 constexpr sk_sp(std::nullptr_t) : fPtr(nullptr) {}
reedbb7b0432016-03-01 07:28:51 -0800254
255 /**
256 * Shares the underlying object by calling ref(), so that both the argument and the newly
257 * created sk_sp both have a reference to it.
258 */
259 sk_sp(const sk_sp<T>& that) : fPtr(SkSafeRef(that.get())) {}
bungeman801b44c2016-04-28 11:18:07 -0700260 template <typename U, typename = skstd::enable_if_t<std::is_convertible<U*, T*>::value>>
halcanarycb6cb382016-03-02 08:11:26 -0800261 sk_sp(const sk_sp<U>& that) : fPtr(SkSafeRef(that.get())) {}
reedbb7b0432016-03-01 07:28:51 -0800262
263 /**
264 * Move the underlying object from the argument to the newly created sk_sp. Afterwards only
265 * the new sk_sp will have a reference to the object, and the argument will point to null.
266 * No call to ref() or unref() will be made.
267 */
268 sk_sp(sk_sp<T>&& that) : fPtr(that.release()) {}
bungeman801b44c2016-04-28 11:18:07 -0700269 template <typename U, typename = skstd::enable_if_t<std::is_convertible<U*, T*>::value>>
halcanarycb6cb382016-03-02 08:11:26 -0800270 sk_sp(sk_sp<U>&& that) : fPtr(that.release()) {}
reedbb7b0432016-03-01 07:28:51 -0800271
272 /**
273 * Adopt the bare pointer into the newly created sk_sp.
274 * No call to ref() or unref() will be made.
275 */
276 explicit sk_sp(T* obj) : fPtr(obj) {}
277
278 /**
279 * Calls unref() on the underlying object pointer.
280 */
281 ~sk_sp() {
282 SkSafeUnref(fPtr);
bungemanc901c112016-03-08 08:35:23 -0800283 SkDEBUGCODE(fPtr = nullptr);
reedbb7b0432016-03-01 07:28:51 -0800284 }
285
halcanarycb6cb382016-03-02 08:11:26 -0800286 sk_sp<T>& operator=(std::nullptr_t) { this->reset(); return *this; }
reedbb7b0432016-03-01 07:28:51 -0800287
288 /**
289 * Shares the underlying object referenced by the argument by calling ref() on it. If this
290 * sk_sp previously had a reference to an object (i.e. not null) it will call unref() on that
291 * object.
292 */
293 sk_sp<T>& operator=(const sk_sp<T>& that) {
294 this->reset(SkSafeRef(that.get()));
295 return *this;
296 }
bungeman801b44c2016-04-28 11:18:07 -0700297 template <typename U, typename = skstd::enable_if_t<std::is_convertible<U*, T*>::value>>
halcanarycb6cb382016-03-02 08:11:26 -0800298 sk_sp<T>& operator=(const sk_sp<U>& that) {
299 this->reset(SkSafeRef(that.get()));
300 return *this;
301 }
reedbb7b0432016-03-01 07:28:51 -0800302
303 /**
304 * Move the underlying object from the argument to the sk_sp. If the sk_sp previously held
305 * a reference to another object, unref() will be called on that object. No call to ref()
306 * will be made.
307 */
308 sk_sp<T>& operator=(sk_sp<T>&& that) {
309 this->reset(that.release());
310 return *this;
311 }
bungeman801b44c2016-04-28 11:18:07 -0700312 template <typename U, typename = skstd::enable_if_t<std::is_convertible<U*, T*>::value>>
halcanarycb6cb382016-03-02 08:11:26 -0800313 sk_sp<T>& operator=(sk_sp<U>&& that) {
314 this->reset(that.release());
315 return *this;
316 }
reedbb7b0432016-03-01 07:28:51 -0800317
bungemanbeab9e72016-03-03 07:50:49 -0800318 T& operator*() const {
319 SkASSERT(this->get() != nullptr);
320 return *this->get();
321 }
322
halcanary89bd99b2016-03-02 08:21:39 -0800323 // MSVC 2013 does not work correctly with explicit operator bool.
324 // https://chromium-cpp.appspot.com/#core-blacklist
bungemanbeab9e72016-03-03 07:50:49 -0800325 // When explicit operator bool can be used, remove operator! and operator unspecified_bool_type.
halcanary89bd99b2016-03-02 08:21:39 -0800326 //explicit operator bool() const { return this->get() != nullptr; }
bungemanbeab9e72016-03-03 07:50:49 -0800327 operator unspecified_bool_type() const { return this->get() ? &sk_sp::fPtr : nullptr; }
halcanary89bd99b2016-03-02 08:21:39 -0800328 bool operator!() const { return this->get() == nullptr; }
reedbb7b0432016-03-01 07:28:51 -0800329
330 T* get() const { return fPtr; }
331 T* operator->() const { return fPtr; }
332
333 /**
334 * Adopt the new bare pointer, and call unref() on any previously held object (if not null).
335 * No call to ref() will be made.
336 */
337 void reset(T* ptr = nullptr) {
bungemanc901c112016-03-08 08:35:23 -0800338 // Calling fPtr->unref() may call this->~() or this->reset(T*).
339 // http://wg21.cmeerw.net/lwg/issue998
340 // http://wg21.cmeerw.net/lwg/issue2262
341 T* oldPtr = fPtr;
reed941da9d2016-03-06 13:54:00 -0800342 fPtr = ptr;
bungemanc901c112016-03-08 08:35:23 -0800343 SkSafeUnref(oldPtr);
reedbb7b0432016-03-01 07:28:51 -0800344 }
345
346 /**
347 * Return the bare pointer, and set the internal object pointer to nullptr.
348 * The caller must assume ownership of the object, and manage its reference count directly.
349 * No call to unref() will be made.
350 */
351 T* SK_WARN_UNUSED_RESULT release() {
352 T* ptr = fPtr;
353 fPtr = nullptr;
354 return ptr;
355 }
356
bungemanc901c112016-03-08 08:35:23 -0800357 void swap(sk_sp<T>& that) /*noexcept*/ {
358 using std::swap;
359 swap(fPtr, that.fPtr);
360 }
361
reedbb7b0432016-03-01 07:28:51 -0800362private:
363 T* fPtr;
364};
365
bungemanc901c112016-03-08 08:35:23 -0800366template <typename T> inline void swap(sk_sp<T>& a, sk_sp<T>& b) /*noexcept*/ {
367 a.swap(b);
368}
369
370template <typename T, typename U> inline bool operator==(const sk_sp<T>& a, const sk_sp<U>& b) {
371 return a.get() == b.get();
372}
373template <typename T> inline bool operator==(const sk_sp<T>& a, std::nullptr_t) /*noexcept*/ {
374 return !a;
375}
376template <typename T> inline bool operator==(std::nullptr_t, const sk_sp<T>& b) /*noexcept*/ {
377 return !b;
378}
379
380template <typename T, typename U> inline bool operator!=(const sk_sp<T>& a, const sk_sp<U>& b) {
381 return a.get() != b.get();
382}
383template <typename T> inline bool operator!=(const sk_sp<T>& a, std::nullptr_t) /*noexcept*/ {
384 return static_cast<bool>(a);
385}
386template <typename T> inline bool operator!=(std::nullptr_t, const sk_sp<T>& b) /*noexcept*/ {
387 return static_cast<bool>(b);
388}
389
390template <typename T, typename U> inline bool operator<(const sk_sp<T>& a, const sk_sp<U>& b) {
391 // Provide defined total order on sk_sp.
392 // http://wg21.cmeerw.net/lwg/issue1297
393 // http://wg21.cmeerw.net/lwg/issue1401 .
394 return std::less<skstd::common_type_t<T*, U*>>()(a.get(), b.get());
395}
396template <typename T> inline bool operator<(const sk_sp<T>& a, std::nullptr_t) {
397 return std::less<T*>()(a.get(), nullptr);
398}
399template <typename T> inline bool operator<(std::nullptr_t, const sk_sp<T>& b) {
400 return std::less<T*>()(nullptr, b.get());
401}
402
403template <typename T, typename U> inline bool operator<=(const sk_sp<T>& a, const sk_sp<U>& b) {
404 return !(b < a);
405}
406template <typename T> inline bool operator<=(const sk_sp<T>& a, std::nullptr_t) {
407 return !(nullptr < a);
408}
409template <typename T> inline bool operator<=(std::nullptr_t, const sk_sp<T>& b) {
410 return !(b < nullptr);
411}
412
413template <typename T, typename U> inline bool operator>(const sk_sp<T>& a, const sk_sp<U>& b) {
414 return b < a;
415}
416template <typename T> inline bool operator>(const sk_sp<T>& a, std::nullptr_t) {
417 return nullptr < a;
418}
419template <typename T> inline bool operator>(std::nullptr_t, const sk_sp<T>& b) {
420 return b < nullptr;
421}
422
423template <typename T, typename U> inline bool operator>=(const sk_sp<T>& a, const sk_sp<U>& b) {
424 return !(a < b);
425}
426template <typename T> inline bool operator>=(const sk_sp<T>& a, std::nullptr_t) {
427 return !(a < nullptr);
428}
429template <typename T> inline bool operator>=(std::nullptr_t, const sk_sp<T>& b) {
430 return !(nullptr < b);
431}
432
halcanary217c0b32016-03-02 08:06:20 -0800433template <typename T, typename... Args>
434sk_sp<T> sk_make_sp(Args&&... args) {
435 return sk_sp<T>(new T(std::forward<Args>(args)...));
436}
437
reed647cc842016-03-08 12:54:48 -0800438/*
439 * Returns a sk_sp wrapping the provided ptr AND calls ref on it (if not null).
440 *
441 * This is different than the semantics of the constructor for sk_sp, which just wraps the ptr,
442 * effectively "adopting" it.
reed647cc842016-03-08 12:54:48 -0800443 */
444template <typename T> sk_sp<T> sk_ref_sp(T* obj) {
445 return sk_sp<T>(SkSafeRef(obj));
446}
447
448#endif