From 2e018052981f7570ced5c162abf5490b802c6e18 Mon Sep 17 00:00:00 2001 From: Hal Canary Date: Wed, 3 Oct 2018 16:30:09 -0400 Subject: [PATCH] SkDynamicMemoryWStream: make moveable Motivation: using this in an experiment of mine (I want a std::vector.) Change-Id: I28e2c468156ace2f9e7d5e09937e05bc8b7e8200 Reviewed-on: https://skia-review.googlesource.com/c/159326 Reviewed-by: Ben Wagner Commit-Queue: Hal Canary --- include/core/SkStream.h | 12 ++++++------ src/core/SkStream.cpp | 20 +++++++++++++++++--- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/include/core/SkStream.h b/include/core/SkStream.h index c37a113b5d..f8f0fdbbc0 100644 --- a/include/core/SkStream.h +++ b/include/core/SkStream.h @@ -278,9 +278,7 @@ public: static int SizeOfPackedUInt(size_t value); private: - SkWStream(SkWStream&&) = delete; SkWStream(const SkWStream&) = delete; - SkWStream& operator=(SkWStream&&) = delete; SkWStream& operator=(const SkWStream&) = delete; }; @@ -459,7 +457,9 @@ private: class SK_API SkDynamicMemoryWStream : public SkWStream { public: - SkDynamicMemoryWStream(); + SkDynamicMemoryWStream() = default; + SkDynamicMemoryWStream(SkDynamicMemoryWStream&&); + SkDynamicMemoryWStream& operator=(SkDynamicMemoryWStream&&); ~SkDynamicMemoryWStream() override; bool write(const void* buffer, size_t size) override; @@ -488,9 +488,9 @@ public: void padToAlign4(); private: struct Block; - Block* fHead; - Block* fTail; - size_t fBytesWrittenBeforeTail; + Block* fHead = nullptr; + Block* fTail = nullptr; + size_t fBytesWrittenBeforeTail = 0; #ifdef SK_DEBUG void validate() const; diff --git a/src/core/SkStream.cpp b/src/core/SkStream.cpp index f8f43502bb..b80efbd9e8 100644 --- a/src/core/SkStream.cpp +++ b/src/core/SkStream.cpp @@ -476,9 +476,23 @@ struct SkDynamicMemoryWStream::Block { } }; -SkDynamicMemoryWStream::SkDynamicMemoryWStream() - : fHead(nullptr), fTail(nullptr), fBytesWrittenBeforeTail(0) -{} +SkDynamicMemoryWStream::SkDynamicMemoryWStream(SkDynamicMemoryWStream&& other) + : fHead(other.fHead) + , fTail(other.fTail) + , fBytesWrittenBeforeTail(other.fBytesWrittenBeforeTail) +{ + other.fHead = nullptr; + other.fTail = nullptr; + other.fBytesWrittenBeforeTail = 0; +} + +SkDynamicMemoryWStream& SkDynamicMemoryWStream::operator=(SkDynamicMemoryWStream&& other) { + if (this != &other) { + this->~SkDynamicMemoryWStream(); + new (this) SkDynamicMemoryWStream(std::move(other)); + } + return *this; +} SkDynamicMemoryWStream::~SkDynamicMemoryWStream() { this->reset();