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

Unified Diff: include/core/SkRefCnt.h

Issue 1760453004: Add operator* and operator safe-bool to sk_sp. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Test all the things. 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 cea72cda66f9a6bdad350a4c398d67d654c56385..9e8f3be61c33456de32384af7a25f84326d30989 100644
--- a/include/core/SkRefCnt.h
+++ b/include/core/SkRefCnt.h
@@ -238,6 +238,8 @@ private:
* may have its ref/unref be thread-safe, but that is not assumed/imposed by sk_sp.
*/
template <typename T> class sk_sp {
+ /** Supports safe bool idiom. Obsolete with explicit operator bool. */
+ using unspecified_bool_type = T* sk_sp::*;
public:
sk_sp() : fPtr(nullptr) {}
sk_sp(std::nullptr_t) : fPtr(nullptr) {}
@@ -316,10 +318,16 @@ public:
template <typename U>
bool operator!=(const sk_sp<U>& that) const { return this->get() != that.get(); }
+ T& operator*() const {
+ SkASSERT(this->get() != nullptr);
+ return *this->get();
+ }
+
// MSVC 2013 does not work correctly with explicit operator bool.
// https://chromium-cpp.appspot.com/#core-blacklist
+ // When explicit operator bool can be used, remove operator! and operator unspecified_bool_type.
//explicit operator bool() const { return this->get() != nullptr; }
-
+ operator unspecified_bool_type() const { return this->get() ? &sk_sp::fPtr : nullptr; }
bool operator!() const { return this->get() == nullptr; }
T* get() const { return fPtr; }
« 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