From e819e5f81e76fa88955b19467edc83b7ca6a4ff9 Mon Sep 17 00:00:00 2001 From: Ben Wagner Date: Tue, 28 Nov 2017 16:44:10 -0500 Subject: [PATCH] Update SkTLazy for move only types. Change-Id: Id812d4f6ac47ae41e5a938310aa8f30eaf33a42d Reviewed-on: https://skia-review.googlesource.com/76901 Reviewed-by: Mike Klein Commit-Queue: Ben Wagner --- include/core/SkTLazy.h | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/include/core/SkTLazy.h b/include/core/SkTLazy.h index cb08387bb9..39ebdbef4a 100644 --- a/include/core/SkTLazy.h +++ b/include/core/SkTLazy.h @@ -24,7 +24,8 @@ public: explicit SkTLazy(const T* src) : fPtr(src ? new (fStorage.get()) T(*src) : nullptr) {} - SkTLazy(const SkTLazy& src) : fPtr(nullptr) { *this = src; } + SkTLazy(const SkTLazy& that) : fPtr(nullptr) { *this = that; } + SkTLazy(SkTLazy&& that) : fPtr(nullptr) { *this = std::move(that); } ~SkTLazy() { if (this->isValid()) { @@ -32,9 +33,18 @@ public: } } - SkTLazy& operator=(const SkTLazy& src) { - if (src.isValid()) { - this->set(*src.get()); + SkTLazy& operator=(const SkTLazy& that) { + if (that.isValid()) { + this->set(*that.get()); + } else { + this->reset(); + } + return *this; + } + + SkTLazy& operator=(SkTLazy&& that) { + if (that.isValid()) { + this->set(std::move(*that.get())); } else { this->reset(); } @@ -70,6 +80,15 @@ public: return fPtr; } + T* set(T&& src) { + if (this->isValid()) { + *fPtr = std::move(src); + } else { + fPtr = new (SkTCast(fStorage.get())) T(std::move(src)); + } + return fPtr; + } + /** * Destroy the lazy object (if it was created via init() or set()) */