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

Unified Diff: include/core/SkRefCnt.h

Issue 1752683002: isolate sk_sp from larger cl (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Created 4 years, 10 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | tests/RefCntTest.cpp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: include/core/SkRefCnt.h
diff --git a/include/core/SkRefCnt.h b/include/core/SkRefCnt.h
index 9d1e5f1f02c252c13c2091208903457403c0a825..6da6a277c175e3eaa2ea12f8887566c14f986896 100644
--- a/include/core/SkRefCnt.h
+++ b/include/core/SkRefCnt.h
@@ -227,4 +227,51 @@ private:
mutable int32_t fRefCnt;
};
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+template <typename T> class sk_sp {
+public:
fmalita_google_do_not_use 2016/03/01 05:23:25 Default ctor?
mtklein 2016/03/01 13:25:36 (T* obj = nullptr) serves as the default.
+ sk_sp(std::nullptr_t) : fPtr(nullptr) {}
+ sk_sp(const sk_sp<T>& that) : fPtr(SkSafeRef(that.get())) {}
+ sk_sp(sk_sp<T>&& that) : fPtr(that.release()) {}
+ explicit sk_sp(T* obj = nullptr) : fPtr(obj) {}
fmalita_google_do_not_use 2016/03/01 05:23:25 The adoption is not self documenting, and it's bou
f(malita) 2016/03/01 12:53:49 OTOH this makes sk_sp a drop-in replacment for SkA
mtklein 2016/03/01 13:25:36 Let's plan for very few bare pointers escape into
+ ~sk_sp() {
+ SkSafeUnref(fPtr);
+ }
+
+ sk_sp<T>& operator=(const sk_sp<T>& that) {
+ this->reset(SkSafeRef(that.get()));
+ return *this;
+ }
+ sk_sp<T>& operator=(sk_sp<T>&& that) {
+ this->reset(that.release());
+ return *this;
+ }
+
+ bool operator==(std::nullptr_t) const { return this->get() == nullptr; }
+ bool operator!=(std::nullptr_t) const { return this->get() != nullptr; }
+
+ bool operator==(const sk_sp<T>& that) const { return this->get() == that.get(); }
+ bool operator!=(const sk_sp<T>& that) const { return this->get() != that.get(); }
+
+ explicit operator bool() const { return this->get() != nullptr; }
+
+ T* get() const { return fPtr; }
+ T* operator->() const { return fPtr; }
+
+ void reset(T* ptr) {
+ SkSafeUnref(fPtr);
+ fPtr = ptr;
+ }
+
+ T* release() {
+ T* ptr = fPtr;
+ fPtr = nullptr;
+ return ptr;
+ }
+
+private:
+ T* fPtr;
+};
+
#endif
« no previous file with comments | « no previous file | tests/RefCntTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698