From 41e5efecc1dbd2befbe8888e783930ac0854a330 Mon Sep 17 00:00:00 2001 From: ylavic Date: Thu, 11 Apr 2019 19:56:58 +0200 Subject: [PATCH] Address some compilation failures with various compilers. --- include/rapidjson/allocators.h | 50 ++++++++++++++++++++++++++++------ include/rapidjson/document.h | 4 +-- include/rapidjson/rapidjson.h | 4 +++ 3 files changed, 48 insertions(+), 10 deletions(-) diff --git a/include/rapidjson/allocators.h b/include/rapidjson/allocators.h index 6de4abd48..801558fd1 100644 --- a/include/rapidjson/allocators.h +++ b/include/rapidjson/allocators.h @@ -18,12 +18,17 @@ #include "rapidjson.h" #include -#if RAPIDJSON_HAS_CXX11_TYPETRAITS +#if RAPIDJSON_HAS_CXX11 #include #endif RAPIDJSON_NAMESPACE_BEGIN +RAPIDJSON_DIAG_PUSH +#ifdef __GNUC__ +RAPIDJSON_DIAG_OFF(effc++) +#endif + /////////////////////////////////////////////////////////////////////////////// // Allocator @@ -89,7 +94,7 @@ class StdBaseAllocator { }; StdBaseAllocator(Allocator &allocator) RAPIDJSON_NOEXCEPT : - allocator_(allocator) { + allocator_(&allocator) { } StdBaseAllocator(const StdBaseAllocator& other) RAPIDJSON_NOEXCEPT : @@ -101,6 +106,14 @@ class StdBaseAllocator { allocator_(other.allocator_) { } +#if RAPIDJSON_HAS_CXX11 + StdBaseAllocator(StdBaseAllocator&& other) RAPIDJSON_NOEXCEPT : + allocator_(other.allocator_) { + } + using propagate_on_container_move_assignment = std::true_type; + using is_always_equal = std::is_empty; +#endif + size_type max_size() const RAPIDJSON_NOEXCEPT { return static_cast(-1) / sizeof(value_type); } @@ -128,7 +141,7 @@ class StdBaseAllocator { void construct(pointer p, const_reference r) { ::new(static_cast(p)) value_type(r); } -#if RAPIDJSON_HAS_CXX11_RVALUE_REFS +#if RAPIDJSON_HAS_CXX11 template void construct(OtherType* p, Args&&... args) { ::new(static_cast(p)) OtherType(std::forward(args)...); @@ -156,10 +169,11 @@ class StdBaseAllocator { } pointer Reallocate(pointer old, size_type old_n, size_type new_n) { RAPIDJSON_ASSERT(sizeof(value_type) == 1 || new_n <= max_size()); - return static_cast(allocator_.Realloc(old, old_n * sizeof(value_type), - new_n * sizeof(value_type))); + return static_cast(allocator_->Realloc(old, old_n * sizeof(value_type), + new_n * sizeof(value_type))); } -#if 0 && RAPIDJSON_HAS_CXX11_RVALUE_REFS + +#if RAPIDJSON_HAS_CXX11 template OtherType *New(Args&&... args) { typename rebind::other a(*this); @@ -176,11 +190,29 @@ class StdBaseAllocator { #endif private: + Allocator *allocator_; + StdBaseAllocator() /* = delete */; - template friend class StdBaseAllocator; // OtherType::allocator_ - Allocator &allocator_; + + // For OtherType::allocator_ + template + friend class StdBaseAllocator; + template + friend bool operator==(const StdBaseAllocator& a1, + const StdBaseAllocator& a2) RAPIDJSON_NOEXCEPT; }; +template +bool operator==(const StdBaseAllocator& a1, + const StdBaseAllocator& a2) RAPIDJSON_NOEXCEPT { + return a1.allocator_ == a2.allocator_; +} +template +bool operator!=(const StdBaseAllocator& a1, + const StdBaseAllocator& a2) RAPIDJSON_NOEXCEPT { + return !(a1 == a2); +} + /////////////////////////////////////////////////////////////////////////////// // CrtAllocator @@ -406,6 +438,8 @@ class MemoryPoolAllocator : public StdBaseAllocator StringRef(const std::basic_string& s #ifdef RAPIDJSON_USE_MEMBERS_MAP template struct GenericMemberMap { - typedef Allocator AllocatorType; typedef typename Encoding::Ch Ch; typedef GenericStringRef KeyType; struct KeyLess { @@ -421,9 +420,10 @@ struct GenericMemberMap { return cmp < 0 || (cmp == 0 && s1.length < s2.length); } }; + typedef std::pair Pair; + typedef typename Allocator::template rebind::other AllocatorType; typedef std::multimap Map; typedef typename Map::iterator Iterator; - typedef typename Map::value_type Pair; }; #endif diff --git a/include/rapidjson/rapidjson.h b/include/rapidjson/rapidjson.h index 968dd8fac..240c2db15 100644 --- a/include/rapidjson/rapidjson.h +++ b/include/rapidjson/rapidjson.h @@ -535,6 +535,10 @@ RAPIDJSON_NAMESPACE_END /////////////////////////////////////////////////////////////////////////////// // C++11 features +#ifndef RAPIDJSON_HAS_CXX11 +#define RAPIDJSON_HAS_CXX11 (__cplusplus >= 201103L) +#endif + #ifndef RAPIDJSON_HAS_CXX11_RVALUE_REFS #if defined(__clang__) #if __has_feature(cxx_rvalue_references) && \