aboutsummaryrefslogtreecommitdiffhomepage
path: root/include/core/SkRefCnt.h
diff options
context:
space:
mode:
authorGravatar bungeman <bungeman@google.com>2016-03-03 07:50:49 -0800
committerGravatar Commit bot <commit-bot@chromium.org>2016-03-03 07:50:49 -0800
commitbeab9e7ba06ea8bd01fcbfd44f2ca2ed69c8a1d9 (patch)
tree149ae269dc91baac346deeba5ec4362c19062b55 /include/core/SkRefCnt.h
parentf655e9330e3e3cc3d56f19017c89d8309f2ddc36 (diff)
Add operator* and operator safe-bool to sk_sp.
This greatly reduces the need to use '.get()' in conditionals. Review URL: https://codereview.chromium.org/1760453004
Diffstat (limited to 'include/core/SkRefCnt.h')
-rw-r--r--include/core/SkRefCnt.h10
1 files changed, 9 insertions, 1 deletions
diff --git a/include/core/SkRefCnt.h b/include/core/SkRefCnt.h
index cea72cda66..9e8f3be61c 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; }