diff options
author | reed <reed@google.com> | 2016-03-08 12:54:48 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2016-03-08 12:54:48 -0800 |
commit | 647cc8474828202c98d540f799742e3074a2aace (patch) | |
tree | 927cedce7eab0ee033068777652ccf30b8b62c76 | |
parent | 9904c9212074279380e21f96575078734dbbd308 (diff) |
Add sk_ref_sp helper function.
Review URL: https://codereview.chromium.org/1773943004
-rw-r--r-- | include/core/SkRefCnt.h | 18 | ||||
-rw-r--r-- | tests/RefCntTest.cpp | 13 |
2 files changed, 31 insertions, 0 deletions
diff --git a/include/core/SkRefCnt.h b/include/core/SkRefCnt.h index c3f724fa8d..f159e3b1b7 100644 --- a/include/core/SkRefCnt.h +++ b/include/core/SkRefCnt.h @@ -14,6 +14,8 @@ #include <functional> #include <utility> +#define SK_SUPPORT_TRANSITION_TO_SP_INTERFACES + /** \class SkRefCntBase SkRefCntBase is the base class for objects that may be shared by multiple @@ -430,4 +432,20 @@ sk_sp<T> sk_make_sp(Args&&... args) { return sk_sp<T>(new T(std::forward<Args>(args)...)); } +#ifdef SK_SUPPORT_TRANSITION_TO_SP_INTERFACES + +/* + * Returns a sk_sp wrapping the provided ptr AND calls ref on it (if not null). + * + * This is different than the semantics of the constructor for sk_sp, which just wraps the ptr, + * effectively "adopting" it. + * + * This function may be helpful while we convert callers from ptr-based to sk_sp-based parameters. + */ +template <typename T> sk_sp<T> sk_ref_sp(T* obj) { + return sk_sp<T>(SkSafeRef(obj)); +} + +#endif + #endif diff --git a/tests/RefCntTest.cpp b/tests/RefCntTest.cpp index 2932913c3d..a9b461f352 100644 --- a/tests/RefCntTest.cpp +++ b/tests/RefCntTest.cpp @@ -385,3 +385,16 @@ DEF_TEST(sk_sp_reset, r) { sp.reset(rc); // this should transfer our ownership over to sp REPORTER_ASSERT(r, rc->unique()); } + +DEF_TEST(sk_sp_ref, r) { + SkRefCnt* rc = new SkRefCnt; + REPORTER_ASSERT(r, rc->unique()); + + { + sk_sp<SkRefCnt> sp = sk_ref_sp(rc); + REPORTER_ASSERT(r, !rc->unique()); + } + + REPORTER_ASSERT(r, rc->unique()); + rc->unref(); +} |