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

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: Correct signature, write test. 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') | tests/RefCntTest.cpp » ('J')
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..d9591c893210681f7e02de30f4c19ebb8aecc267 100644
--- a/include/core/SkRefCnt.h
+++ b/include/core/SkRefCnt.h
@@ -238,6 +238,7 @@ private:
* may have its ref/unref be thread-safe, but that is not assumed/imposed by sk_sp.
*/
template <typename T> class sk_sp {
+ using unspecified_bool_type = T* sk_sp::*;
mtklein 2016/03/02 20:05:10 // safe bool idiom. obsolete with explicit operat
bungeman-skia 2016/03/02 20:32:58 Done.
public:
sk_sp() : fPtr(nullptr) {}
sk_sp(std::nullptr_t) : fPtr(nullptr) {}
@@ -316,10 +317,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; }
mtklein 2016/03/02 20:05:10 ... ? &fPtr : nullptr ... ?
bungeman-skia 2016/03/02 20:32:58 So it turns out the type of '&fPtr' here is 'T*con
bool operator!() const { return this->get() == nullptr; }
T* get() const { return fPtr; }
« no previous file with comments | « no previous file | tests/RefCntTest.cpp » ('j') | tests/RefCntTest.cpp » ('J')

Powered by Google App Engine
This is Rietveld 408576698