diff options
author | ylavic <ylavic.dev@gmail.com> | 2021-04-02 15:35:29 +0300 |
---|---|---|
committer | ylavic <ylavic.dev@gmail.com> | 2021-04-04 13:51:47 +0300 |
commit | 683010b02d407953d73609cd7973e2999342e97b (patch) | |
tree | d8adf3b7e37515a998ac5713a5be209dd8cae28d | |
parent | 49aa0fc15d63a2132ecf3ba0eda1ecf6fef215a1 (diff) |
Add rvalue copy and assignment to MemoryPoolAllocator and StdAllocator.
-rw-r--r-- | include/rapidjson/allocators.h | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/include/rapidjson/allocators.h b/include/rapidjson/allocators.h index 711ac308..cf8e75a3 100644 --- a/include/rapidjson/allocators.h +++ b/include/rapidjson/allocators.h @@ -222,18 +222,42 @@ public: { RAPIDJSON_NOEXCEPT_ASSERT(rhs.shared_->refcount > 0); ++rhs.shared_->refcount; + this->~MemoryPoolAllocator(); + baseAllocator_ = rhs.baseAllocator_; + chunk_capacity_ = rhs.chunk_capacity_; + shared_ = rhs.shared_; + 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<value_type>(p, n); } +#if RAPIDJSON_HAS_CXX11 + using is_always_equal = std::is_empty<BaseAllocator>; +#endif + template<typename U> bool operator==(const StdAllocator<U, BaseAllocator>& 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); |