Skip to content

Commit

Permalink
Merge 858a9b3 into 01950eb
Browse files Browse the repository at this point in the history
  • Loading branch information
ylavic committed May 2, 2019
2 parents 01950eb + 858a9b3 commit fd6dbc0
Show file tree
Hide file tree
Showing 9 changed files with 1,056 additions and 74 deletions.
502 changes: 502 additions & 0 deletions bin/types/alotofkeys.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions example/sortkeys/sortkeys.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,8 @@ int main() {
// C++11 supports std::move() of Value so it always have no problem for std::sort().
// Some C++03 implementations of std::sort() requires copy constructor which causes compilation error.
// Needs a sorting function only depends on std::swap() instead.
#if __cplusplus >= 201103L || !defined(__GLIBCXX__)
#if __cplusplus >= 201103L || (!defined(__GLIBCXX__) && (!defined(_MSC_VER) || _MSC_VER >= 1900))
std::sort(d.MemberBegin(), d.MemberEnd(), NameComparator());
#endif

printIt(d);

Expand All @@ -59,4 +58,5 @@ int main() {
"zeta": false
}
*/
#endif
}
182 changes: 182 additions & 0 deletions include/rapidjson/allocators.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@

#include "rapidjson.h"

#include <memory>
#if RAPIDJSON_HAS_CXX11
#include <type_traits>
#endif

RAPIDJSON_NAMESPACE_BEGIN

///////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -279,6 +284,183 @@ class MemoryPoolAllocator {
BaseAllocator* ownBaseAllocator_; //!< base allocator created by this object.
};

#ifdef __GNUC__
RAPIDJSON_DIAG_PUSH
RAPIDJSON_DIAG_OFF(effc++) // std::allocator can safely be inherited
#endif

template <typename Value, typename BaseAllocator = CrtAllocator>
struct StdAllocator: public std::allocator<Value> {
typedef Value ValueType;
typedef BaseAllocator BaseAllocatorType;

StdAllocator(BaseAllocatorType &allocator) RAPIDJSON_NOEXCEPT :
std::allocator<ValueType>(), baseAllocator_(&allocator) {
}

StdAllocator(const StdAllocator& other) RAPIDJSON_NOEXCEPT :
std::allocator<ValueType>(other), baseAllocator_(other.baseAllocator_) {
}

template<typename T>
StdAllocator(const StdAllocator<T, BaseAllocatorType>& other) RAPIDJSON_NOEXCEPT :
std::allocator<ValueType>(other), baseAllocator_(other.baseAllocator_) {
}

~StdAllocator() RAPIDJSON_NOEXCEPT {
}


// std::allocator concept

typedef std::allocator<ValueType> allocator_type;

typedef typename allocator_type::value_type value_type;
typedef typename allocator_type::pointer pointer;
typedef typename allocator_type::const_pointer const_pointer;
typedef typename allocator_type::reference reference;
typedef typename allocator_type::const_reference const_reference;
typedef typename allocator_type::size_type size_type;
typedef typename allocator_type::difference_type difference_type;

template<typename T>
struct rebind {
typedef StdAllocator<T, BaseAllocatorType> other;
};

#if RAPIDJSON_HAS_CXX11
using is_always_equal = std::is_empty<BaseAllocatorType>;
using propagate_on_container_move_assignment = std::true_type;
using propagate_on_container_swap = std::true_type;
#endif
template<typename T>
bool operator==(const StdAllocator<T, BaseAllocatorType>& other) RAPIDJSON_NOEXCEPT {
#if RAPIDJSON_HAS_CXX11
if (is_always_equal::value)
return true;
#endif
return baseAllocator_ == other.baseAllocator_;
}
template<typename T>
bool operator!=(const StdAllocator<T, BaseAllocatorType>& other) RAPIDJSON_NOEXCEPT {
return !operator==(other);
}

using allocator_type::address;
using allocator_type::max_size;

// Allocate using baseAllocator_
pointer allocate(size_type n, const void* = 0) {
return Allocate(n);
}

// Deallocate using baseAllocator_
void deallocate(pointer p, size_type n) {
Deallocate(p, n);
}
template<typename T>
void deallocate(T* p, size_type n) {
Deallocate(p, n);
}

using allocator_type::construct;
using allocator_type::destroy;


// Rapidjson concept

typedef pointer PointerType;
typedef const_pointer ConstPointerType;
typedef reference ReferenceType;
typedef const_reference ConstReferenceType;
typedef size_type SizeType;
typedef difference_type DifferenceType;

template<typename T>
struct Rebind {
typedef typename rebind<T>::other Other;
};

SizeType MaxSize() const {
return max_size();
}

template<typename T>
T *Allocate(SizeType n = 1) {
return Reallocate<T>(NULL, 0, n);
}
PointerType Allocate(SizeType n = 1) {
return Reallocate<ValueType>(NULL, 0, n);
}

template<typename T>
T* Reallocate(T* old_p, SizeType old_n, SizeType new_n) {
RAPIDJSON_ASSERT(sizeof(T) == 1 || new_n <= MaxSize());
return static_cast<T*>(baseAllocator_->Realloc(old_p, old_n * sizeof(T),
new_n * sizeof(T)));
}

template<typename T>
void Deallocate(T *p, SizeType n = 1) {
static_cast<void>(Reallocate(p, n, 0));
}

// Contruct using method from std::allocator
#if RAPIDJSON_HAS_CXX11
template<typename T, typename... Args>
T *Construct(T* p, Args&&... args) {
construct(p, std::forward<Args>(args)...);
return p;
}
#else
PointerType Construct(PointerType p, ConstReferenceType r) {
construct(p, r);
return p;
}
#if RAPIDJSON_HAS_CXX11_RVALUE_REFS
PointerType Construct(PointerType p, ValueType&& r) {
construct(p, std::move(r));
return p;
}
#endif
#endif

// Destroy using method from std::allocator
template<typename T>
T *Destroy(T *p) {
destroy(p);
return p;
}

// New/Delete onto/from the allocator (C++11 only..)
#if RAPIDJSON_HAS_CXX11
template<typename T, typename... Args>
T *New(Args&&... args) {
typename Rebind<T>::Other other(*this);
return other.Construct(other.Allocate(), std::forward<Args>(args)...);
}
template<typename T>
void Delete(T *p) {
if (!p) // NULL safe
return;
typename Rebind<T>::Other other(*this);
other.Deallocate(other.Destroy(p));
}
#endif

private:
BaseAllocatorType *baseAllocator_;

StdAllocator(); // baseAllocator_ can't be NULL

template <typename, typename>
friend struct StdAllocator; // self access to other.baseAllocator_
};

#ifdef __GNUC__
RAPIDJSON_DIAG_POP
#endif

RAPIDJSON_NAMESPACE_END

#endif // RAPIDJSON_ENCODINGS_H_
Loading

0 comments on commit fd6dbc0

Please sign in to comment.