Skip to content

Commit 683010b

Browse files
committed
Add rvalue copy and assignment to MemoryPoolAllocator and StdAllocator.
1 parent 49aa0fc commit 683010b

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

include/rapidjson/allocators.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,18 +222,42 @@ class MemoryPoolAllocator {
222222
{
223223
RAPIDJSON_NOEXCEPT_ASSERT(rhs.shared_->refcount > 0);
224224
++rhs.shared_->refcount;
225+
this->~MemoryPoolAllocator();
226+
baseAllocator_ = rhs.baseAllocator_;
227+
chunk_capacity_ = rhs.chunk_capacity_;
228+
shared_ = rhs.shared_;
229+
return *this;
230+
}
225231

232+
#if RAPIDJSON_HAS_CXX11_RVALUE_REFS
233+
MemoryPoolAllocator(MemoryPoolAllocator&& rhs) RAPIDJSON_NOEXCEPT :
234+
chunk_capacity_(rhs.chunk_capacity_),
235+
baseAllocator_(rhs.baseAllocator_),
236+
shared_(rhs.shared_)
237+
{
238+
RAPIDJSON_NOEXCEPT_ASSERT(rhs.shared_->refcount > 0);
239+
rhs.shared_ = 0;
240+
}
241+
MemoryPoolAllocator& operator=(MemoryPoolAllocator&& rhs) RAPIDJSON_NOEXCEPT
242+
{
243+
RAPIDJSON_NOEXCEPT_ASSERT(rhs.shared_->refcount > 0);
226244
this->~MemoryPoolAllocator();
227245
baseAllocator_ = rhs.baseAllocator_;
228246
chunk_capacity_ = rhs.chunk_capacity_;
229247
shared_ = rhs.shared_;
248+
rhs.shared_ = 0;
230249
return *this;
231250
}
251+
#endif
232252

233253
//! Destructor.
234254
/*! This deallocates all memory chunks, excluding the user-supplied buffer.
235255
*/
236256
~MemoryPoolAllocator() RAPIDJSON_NOEXCEPT {
257+
if (!shared_) {
258+
// do nothing if moved
259+
return;
260+
}
237261
if (shared_->refcount > 1) {
238262
--shared_->refcount;
239263
return;
@@ -449,6 +473,17 @@ class StdAllocator :
449473
baseAllocator_(rhs.baseAllocator_)
450474
{ }
451475

476+
#if RAPIDJSON_HAS_CXX11_RVALUE_REFS
477+
StdAllocator(StdAllocator&& rhs) RAPIDJSON_NOEXCEPT :
478+
allocator_type(std::move(rhs)),
479+
baseAllocator_(std::move(rhs.baseAllocator_))
480+
{ }
481+
#endif
482+
#if RAPIDJSON_HAS_CXX11
483+
using propagate_on_container_move_assignment = std::true_type;
484+
using propagate_on_container_swap = std::true_type;
485+
#endif
486+
452487
/* implicit */
453488
StdAllocator(const BaseAllocator& allocator) RAPIDJSON_NOEXCEPT :
454489
allocator_type(),
@@ -549,6 +584,10 @@ class StdAllocator :
549584
deallocate<value_type>(p, n);
550585
}
551586

587+
#if RAPIDJSON_HAS_CXX11
588+
using is_always_equal = std::is_empty<BaseAllocator>;
589+
#endif
590+
552591
template<typename U>
553592
bool operator==(const StdAllocator<U, BaseAllocator>& rhs) const RAPIDJSON_NOEXCEPT
554593
{
@@ -561,6 +600,7 @@ class StdAllocator :
561600
}
562601

563602
//! rapidjson Allocator concept
603+
static const bool kNeedFree = BaseAllocator::kNeedFree;
564604
void* Malloc(size_t size)
565605
{
566606
return baseAllocator_.Malloc(size);

0 commit comments

Comments
 (0)