From 683010b02d407953d73609cd7973e2999342e97b Mon Sep 17 00:00:00 2001 From: ylavic Date: Fri, 2 Apr 2021 14:35:29 +0200 Subject: [PATCH] Add rvalue copy and assignment to MemoryPoolAllocator and StdAllocator. --- include/rapidjson/allocators.h | 42 +++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/include/rapidjson/allocators.h b/include/rapidjson/allocators.h index 711ac30..cf8e75a 100644 --- a/include/rapidjson/allocators.h +++ b/include/rapidjson/allocators.h @@ -222,7 +222,6 @@ public: { RAPIDJSON_NOEXCEPT_ASSERT(rhs.shared_->refcount > 0); ++rhs.shared_->refcount; - this->~MemoryPoolAllocator(); baseAllocator_ = rhs.baseAllocator_; chunk_capacity_ = rhs.chunk_capacity_; @@ -230,10 +229,35 @@ public: return *this; } +#if RAPIDJSON_HAS_CXX11_RVALUE_REFS + MemoryPoolAllocator(MemoryPoolAllocator&& rhs) RAPIDJSON_NOEXCEPT : + chunk_capacity_(rhs.chunk_capacity_), + baseAllocator_(rhs.baseAllocator_), + shared_(rhs.shared_) + { + RAPIDJSON_NOEXCEPT_ASSERT(rhs.shared_->refcount > 0); + rhs.shared_ = 0; + } + MemoryPoolAllocator& operator=(MemoryPoolAllocator&& rhs) RAPIDJSON_NOEXCEPT + { + RAPIDJSON_NOEXCEPT_ASSERT(rhs.shared_->refcount > 0); + this->~MemoryPoolAllocator(); + baseAllocator_ = rhs.baseAllocator_; + chunk_capacity_ = rhs.chunk_capacity_; + shared_ = rhs.shared_; + rhs.shared_ = 0; + return *this; + } +#endif + //! Destructor. /*! This deallocates all memory chunks, excluding the user-supplied buffer. */ ~MemoryPoolAllocator() RAPIDJSON_NOEXCEPT { + if (!shared_) { + // do nothing if moved + return; + } if (shared_->refcount > 1) { --shared_->refcount; return; @@ -449,6 +473,17 @@ public: baseAllocator_(rhs.baseAllocator_) { } +#if RAPIDJSON_HAS_CXX11_RVALUE_REFS + StdAllocator(StdAllocator&& rhs) RAPIDJSON_NOEXCEPT : + allocator_type(std::move(rhs)), + baseAllocator_(std::move(rhs.baseAllocator_)) + { } +#endif +#if RAPIDJSON_HAS_CXX11 + using propagate_on_container_move_assignment = std::true_type; + using propagate_on_container_swap = std::true_type; +#endif + /* implicit */ StdAllocator(const BaseAllocator& allocator) RAPIDJSON_NOEXCEPT : allocator_type(), @@ -549,6 +584,10 @@ public: deallocate(p, n); } +#if RAPIDJSON_HAS_CXX11 + using is_always_equal = std::is_empty; +#endif + template bool operator==(const StdAllocator& rhs) const RAPIDJSON_NOEXCEPT { @@ -561,6 +600,7 @@ public: } //! rapidjson Allocator concept + static const bool kNeedFree = BaseAllocator::kNeedFree; void* Malloc(size_t size) { return baseAllocator_.Malloc(size);