Merge pull request #1870 from ylavic/allocators_rvalues
Add rvalue copy and assignment to MemoryPoolAllocator and StdAllocator.
This commit is contained in:
commit
03676c9bf5
14
.travis.yml
14
.travis.yml
@ -40,10 +40,10 @@ matrix:
|
|||||||
- env: CONF=debug ARCH=x86_64 CXX11=OFF CXX17=OFF
|
- env: CONF=debug ARCH=x86_64 CXX11=OFF CXX17=OFF
|
||||||
compiler: gcc
|
compiler: gcc
|
||||||
arch: amd64
|
arch: amd64
|
||||||
- env: CONF=debug ARCH=x86 CXX11=OFF CXX17=ON
|
- env: CONF=debug ARCH=x86 CXX11=OFF CXX17=ON CXX_FLAGS='-D_GLIBCXX_DEBUG'
|
||||||
compiler: gcc
|
compiler: gcc
|
||||||
arch: amd64
|
arch: amd64/
|
||||||
- env: CONF=debug ARCH=x86_64 CXX11=OFF CXX17=ON
|
- env: CONF=debug ARCH=x86_64 CXX11=OFF CXX17=ON CXX_FLAGS='-D_GLIBCXX_DEBUG'
|
||||||
compiler: gcc
|
compiler: gcc
|
||||||
arch: amd64
|
arch: amd64
|
||||||
- env: CONF=release ARCH=aarch64 CXX11=ON CXX17=OFF
|
- env: CONF=release ARCH=aarch64 CXX11=ON CXX17=OFF
|
||||||
@ -84,7 +84,7 @@ matrix:
|
|||||||
compiler: clang
|
compiler: clang
|
||||||
arch: arm64
|
arch: arm64
|
||||||
# coverage report
|
# coverage report
|
||||||
- env: CONF=debug ARCH=x86 CXX11=ON CXX17=OFF GCOV_FLAGS='--coverage'
|
- env: CONF=debug ARCH=x86 GCOV_FLAGS='--coverage' CXX_FLAGS='-O0' CXX11=OFF CXX17=OFF
|
||||||
compiler: gcc
|
compiler: gcc
|
||||||
arch: amd64
|
arch: amd64
|
||||||
cache:
|
cache:
|
||||||
@ -93,7 +93,7 @@ matrix:
|
|||||||
after_success:
|
after_success:
|
||||||
- pip install --user cpp-coveralls
|
- pip install --user cpp-coveralls
|
||||||
- coveralls -r .. --gcov-options '\-lp' -e thirdparty -e example -e test -e build/CMakeFiles -e include/rapidjson/msinttypes -e include/rapidjson/internal/meta.h -e include/rapidjson/error/en.h
|
- coveralls -r .. --gcov-options '\-lp' -e thirdparty -e example -e test -e build/CMakeFiles -e include/rapidjson/msinttypes -e include/rapidjson/internal/meta.h -e include/rapidjson/error/en.h
|
||||||
- env: CONF=debug ARCH=x86_64 GCOV_FLAGS='--coverage'
|
- env: CONF=debug ARCH=x86_64 GCOV_FLAGS='--coverage' CXX_FLAGS='-O0' CXX11=ON CXX17=OFF
|
||||||
compiler: gcc
|
compiler: gcc
|
||||||
arch: amd64
|
arch: amd64
|
||||||
cache:
|
cache:
|
||||||
@ -102,7 +102,7 @@ matrix:
|
|||||||
after_success:
|
after_success:
|
||||||
- pip install --user cpp-coveralls
|
- pip install --user cpp-coveralls
|
||||||
- coveralls -r .. --gcov-options '\-lp' -e thirdparty -e example -e test -e build/CMakeFiles -e include/rapidjson/msinttypes -e include/rapidjson/internal/meta.h -e include/rapidjson/error/en.h
|
- coveralls -r .. --gcov-options '\-lp' -e thirdparty -e example -e test -e build/CMakeFiles -e include/rapidjson/msinttypes -e include/rapidjson/internal/meta.h -e include/rapidjson/error/en.h
|
||||||
- env: CONF=debug ARCH=aarch64 GCOV_FLAGS='--coverage'
|
- env: CONF=debug ARCH=aarch64 GCOV_FLAGS='--coverage' CXX_FLAGS='-O0' CXX11=OFF CXX17=ON
|
||||||
compiler: gcc
|
compiler: gcc
|
||||||
arch: arm64
|
arch: arm64
|
||||||
cache:
|
cache:
|
||||||
@ -150,7 +150,7 @@ script:
|
|||||||
-DRAPIDJSON_BUILD_CXX17=$CXX17
|
-DRAPIDJSON_BUILD_CXX17=$CXX17
|
||||||
-DCMAKE_VERBOSE_MAKEFILE=ON
|
-DCMAKE_VERBOSE_MAKEFILE=ON
|
||||||
-DCMAKE_BUILD_TYPE=$CONF
|
-DCMAKE_BUILD_TYPE=$CONF
|
||||||
-DCMAKE_CXX_FLAGS="$ARCH_FLAGS $GCOV_FLAGS"
|
-DCMAKE_CXX_FLAGS="$ARCH_FLAGS $GCOV_FLAGS $CXX_FLAGS"
|
||||||
-DCMAKE_EXE_LINKER_FLAGS=$GCOV_FLAGS
|
-DCMAKE_EXE_LINKER_FLAGS=$GCOV_FLAGS
|
||||||
..)
|
..)
|
||||||
- cd build
|
- cd build
|
||||||
|
@ -222,7 +222,6 @@ public:
|
|||||||
{
|
{
|
||||||
RAPIDJSON_NOEXCEPT_ASSERT(rhs.shared_->refcount > 0);
|
RAPIDJSON_NOEXCEPT_ASSERT(rhs.shared_->refcount > 0);
|
||||||
++rhs.shared_->refcount;
|
++rhs.shared_->refcount;
|
||||||
|
|
||||||
this->~MemoryPoolAllocator();
|
this->~MemoryPoolAllocator();
|
||||||
baseAllocator_ = rhs.baseAllocator_;
|
baseAllocator_ = rhs.baseAllocator_;
|
||||||
chunk_capacity_ = rhs.chunk_capacity_;
|
chunk_capacity_ = rhs.chunk_capacity_;
|
||||||
@ -230,10 +229,35 @@ public:
|
|||||||
return *this;
|
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.
|
//! Destructor.
|
||||||
/*! This deallocates all memory chunks, excluding the user-supplied buffer.
|
/*! This deallocates all memory chunks, excluding the user-supplied buffer.
|
||||||
*/
|
*/
|
||||||
~MemoryPoolAllocator() RAPIDJSON_NOEXCEPT {
|
~MemoryPoolAllocator() RAPIDJSON_NOEXCEPT {
|
||||||
|
if (!shared_) {
|
||||||
|
// do nothing if moved
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (shared_->refcount > 1) {
|
if (shared_->refcount > 1) {
|
||||||
--shared_->refcount;
|
--shared_->refcount;
|
||||||
return;
|
return;
|
||||||
@ -449,6 +473,17 @@ public:
|
|||||||
baseAllocator_(rhs.baseAllocator_)
|
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 */
|
/* implicit */
|
||||||
StdAllocator(const BaseAllocator& allocator) RAPIDJSON_NOEXCEPT :
|
StdAllocator(const BaseAllocator& allocator) RAPIDJSON_NOEXCEPT :
|
||||||
allocator_type(),
|
allocator_type(),
|
||||||
@ -549,6 +584,10 @@ public:
|
|||||||
deallocate<value_type>(p, n);
|
deallocate<value_type>(p, n);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if RAPIDJSON_HAS_CXX11
|
||||||
|
using is_always_equal = std::is_empty<BaseAllocator>;
|
||||||
|
#endif
|
||||||
|
|
||||||
template<typename U>
|
template<typename U>
|
||||||
bool operator==(const StdAllocator<U, BaseAllocator>& rhs) const RAPIDJSON_NOEXCEPT
|
bool operator==(const StdAllocator<U, BaseAllocator>& rhs) const RAPIDJSON_NOEXCEPT
|
||||||
{
|
{
|
||||||
@ -561,6 +600,7 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
//! rapidjson Allocator concept
|
//! rapidjson Allocator concept
|
||||||
|
static const bool kNeedFree = BaseAllocator::kNeedFree;
|
||||||
void* Malloc(size_t size)
|
void* Malloc(size_t size)
|
||||||
{
|
{
|
||||||
return baseAllocator_.Malloc(size);
|
return baseAllocator_.Malloc(size);
|
||||||
|
@ -16,8 +16,10 @@
|
|||||||
|
|
||||||
#include "rapidjson/allocators.h"
|
#include "rapidjson/allocators.h"
|
||||||
|
|
||||||
|
#include <map>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <cstring>
|
#include <utility>
|
||||||
|
#include <functional>
|
||||||
|
|
||||||
using namespace rapidjson;
|
using namespace rapidjson;
|
||||||
|
|
||||||
@ -141,12 +143,34 @@ void TestStdAllocator(const Allocator& a) {
|
|||||||
|
|
||||||
typedef StdAllocator<char, Allocator> CharAllocator;
|
typedef StdAllocator<char, Allocator> CharAllocator;
|
||||||
typedef std::basic_string<char, std::char_traits<char>, CharAllocator> String;
|
typedef std::basic_string<char, std::char_traits<char>, CharAllocator> String;
|
||||||
|
#if RAPIDJSON_HAS_CXX11
|
||||||
|
String s(CharAllocator{a});
|
||||||
|
#else
|
||||||
CharAllocator ca(a);
|
CharAllocator ca(a);
|
||||||
String s(ca);
|
String s(ca);
|
||||||
|
#endif
|
||||||
for (int i = 0; i < 26; i++) {
|
for (int i = 0; i < 26; i++) {
|
||||||
s.push_back(static_cast<char>('A' + i));
|
s.push_back(static_cast<char>('A' + i));
|
||||||
}
|
}
|
||||||
EXPECT_TRUE(s == "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
|
EXPECT_TRUE(s == "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
|
||||||
|
|
||||||
|
typedef StdAllocator<std::pair<const int, bool>, Allocator> MapAllocator;
|
||||||
|
typedef std::map<int, bool, std::less<int>, MapAllocator> Map;
|
||||||
|
#if RAPIDJSON_HAS_CXX11
|
||||||
|
Map map(std::less<int>(), MapAllocator{a});
|
||||||
|
#else
|
||||||
|
MapAllocator ma(a);
|
||||||
|
Map map(std::less<int>(), ma);
|
||||||
|
#endif
|
||||||
|
for (int i = 0; i < 10; i++) {
|
||||||
|
map.insert(std::make_pair(i, (i % 2) == 0));
|
||||||
|
}
|
||||||
|
EXPECT_TRUE(map.size() == 10);
|
||||||
|
for (int i = 0; i < 10; i++) {
|
||||||
|
typename Map::iterator it = map.find(i);
|
||||||
|
EXPECT_TRUE(it != map.end());
|
||||||
|
EXPECT_TRUE(it->second == ((i % 2) == 0));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(Allocator, CrtAllocator) {
|
TEST(Allocator, CrtAllocator) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user