From 02f42604bd8d1ca7a65fb981c520d61174ab7585 Mon Sep 17 00:00:00 2001 From: ylavic Date: Sun, 28 Mar 2021 12:31:52 +0200 Subject: [PATCH 1/2] Make StdAllocator C++17-20 compatible. --- include/rapidjson/allocators.h | 67 +++++++++++++++++++++++++------- include/rapidjson/rapidjson.h | 31 +++++++++++---- test/unittest/allocatorstest.cpp | 6 +-- 3 files changed, 78 insertions(+), 26 deletions(-) diff --git a/include/rapidjson/allocators.h b/include/rapidjson/allocators.h index 03b2dcc..4265214 100644 --- a/include/rapidjson/allocators.h +++ b/include/rapidjson/allocators.h @@ -19,6 +19,11 @@ #include +#if RAPIDJSON_HAS_CXX11 +#include +#include +#endif + RAPIDJSON_NAMESPACE_BEGIN /////////////////////////////////////////////////////////////////////////////// @@ -420,6 +425,11 @@ class StdAllocator : public std::allocator { typedef std::allocator allocator_type; +#if RAPIDJSON_HAS_CXX11 + typedef std::allocator_traits traits_type; +#else + typedef allocator_type traits_type; +#endif public: typedef BaseAllocator BaseAllocatorType; @@ -449,30 +459,51 @@ public: ~StdAllocator() RAPIDJSON_NOEXCEPT { } - 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 struct rebind { typedef StdAllocator other; }; + typedef typename traits_type::size_type size_type; + typedef typename traits_type::difference_type difference_type; + typedef typename traits_type::value_type value_type; + typedef typename traits_type::pointer pointer; + typedef typename traits_type::const_pointer const_pointer; + #if RAPIDJSON_HAS_CXX11 - using allocator_type::max_size; - using allocator_type::address; - using allocator_type::construct; - using allocator_type::destroy; -#else + + typedef typename std::add_lvalue_reference::type &reference; + typedef typename std::add_lvalue_reference::type>::type &const_reference; + + pointer address(reference r) const RAPIDJSON_NOEXCEPT + { + return std::addressof(r); + } + const_pointer address(const_reference r) const RAPIDJSON_NOEXCEPT + { + return std::addressof(r); + } + size_t max_size() const RAPIDJSON_NOEXCEPT { - return allocator_type::max_size(); + return std::numeric_limits::max() / sizeof(value_type); } + template + void construct(pointer p, Args &&...args) + { + ::new (static_cast(p)) value_type(std::forward(args)...); + } + void destroy(pointer p) + { + p->~T(); + } + +#else // !RAPIDJSON_HAS_CXX11 + + typedef typename allocator_type::reference reference; + typedef typename allocator_type::const_reference const_reference; + pointer address(reference r) const RAPIDJSON_NOEXCEPT { return allocator_type::address(r); @@ -482,6 +513,11 @@ public: return allocator_type::address(r); } + size_t max_size() const RAPIDJSON_NOEXCEPT + { + return allocator_type::max_size(); + } + void construct(pointer p, const_reference r) { allocator_type::construct(p, r); @@ -490,7 +526,8 @@ public: { allocator_type::destroy(p); } -#endif + +#endif // !RAPIDJSON_HAS_CXX11 template U* allocate(size_type n = 1, const void* = 0) diff --git a/include/rapidjson/rapidjson.h b/include/rapidjson/rapidjson.h index 8034c49..8dcb0a0 100644 --- a/include/rapidjson/rapidjson.h +++ b/include/rapidjson/rapidjson.h @@ -124,6 +124,17 @@ #define RAPIDJSON_NAMESPACE_END } #endif +/////////////////////////////////////////////////////////////////////////////// +// __cplusplus macro + +//!@cond RAPIDJSON_HIDDEN_FROM_DOXYGEN + +#if defined(_MSC_VER) +#define RAPIDJSON_CPLUSPLUS _MSVC_LANG +#else +#define RAPIDJSON_CPLUSPLUS __cplusplus +#endif + /////////////////////////////////////////////////////////////////////////////// // RAPIDJSON_HAS_STDSTRING @@ -411,7 +422,7 @@ RAPIDJSON_NAMESPACE_END // Prefer C++11 static_assert, if available #ifndef RAPIDJSON_STATIC_ASSERT -#if __cplusplus >= 201103L || ( defined(_MSC_VER) && _MSC_VER >= 1800 ) +#if RAPIDJSON_CPLUSPLUS >= 201103L || ( defined(_MSC_VER) && _MSC_VER >= 1800 ) #define RAPIDJSON_STATIC_ASSERT(x) \ static_assert(x, RAPIDJSON_STRINGIFY(x)) #endif // C++11 @@ -542,7 +553,7 @@ RAPIDJSON_NAMESPACE_END // C++11 features #ifndef RAPIDJSON_HAS_CXX11 -#define RAPIDJSON_HAS_CXX11 (__cplusplus >= 201103L) +#define RAPIDJSON_HAS_CXX11 (RAPIDJSON_CPLUSPLUS >= 201103L) #endif #ifndef RAPIDJSON_HAS_CXX11_RVALUE_REFS @@ -610,12 +621,16 @@ RAPIDJSON_NAMESPACE_END /////////////////////////////////////////////////////////////////////////////// // C++17 features -#if defined(__has_cpp_attribute) -# if __has_cpp_attribute(fallthrough) -# define RAPIDJSON_DELIBERATE_FALLTHROUGH [[fallthrough]] -# else -# define RAPIDJSON_DELIBERATE_FALLTHROUGH -# endif +#ifndef RAPIDJSON_HAS_CXX17 +#define RAPIDJSON_HAS_CXX17 (RAPIDJSON_CPLUSPLUS >= 201703L) +#endif + +#if RAPIDJSON_HAS_CXX17 +# define RAPIDJSON_DELIBERATE_FALLTHROUGH [[fallthrough]] +#elif defined(__has_cpp_attribute) && __has_cpp_attribute(fallthrough) +# define RAPIDJSON_DELIBERATE_FALLTHROUGH __attribute__((fallthrough)) +#elif defined(__has_cpp_attribute) && __has_cpp_attribute(clang::fallthrough) +# define RAPIDJSON_DELIBERATE_FALLTHROUGH [[clang::fallthrough]] #else # define RAPIDJSON_DELIBERATE_FALLTHROUGH #endif diff --git a/test/unittest/allocatorstest.cpp b/test/unittest/allocatorstest.cpp index b8a2191..76e34b5 100644 --- a/test/unittest/allocatorstest.cpp +++ b/test/unittest/allocatorstest.cpp @@ -107,12 +107,12 @@ void TestStdAllocator(const Allocator& a) { arr[i] = 0x0f0f0f0f; } ia.deallocate(arr, 10); - arr = (int *)ia.Malloc(10 * sizeof(int)); + arr = Malloc(ia, 10); EXPECT_TRUE(arr != 0); for (int i = 0; i < 10; ++i) { arr[i] = 0x0f0f0f0f; } - arr = (int *)ia.Realloc(arr, 10 * sizeof(int), 20 * sizeof(int)); + arr = Realloc(ia, arr, 10, 20); EXPECT_TRUE(arr != 0); for (int i = 0; i < 10; ++i) { EXPECT_EQ(arr[i], 0x0f0f0f0f); @@ -120,7 +120,7 @@ void TestStdAllocator(const Allocator& a) { for (int i = 10; i < 20; i++) { arr[i] = 0x0f0f0f0f; } - ia.Free(arr); + Free(ia, arr, 20); int cons = 0, dest = 0; StdAllocator da(a); From 08cf9a56c0f3d648ef615c3d5f60d08941ebd3ec Mon Sep 17 00:00:00 2001 From: ylavic Date: Mon, 29 Mar 2021 00:17:24 +0200 Subject: [PATCH 2/2] Make StdAllocator C++17-20 compatible. --- include/rapidjson/allocators.h | 18 +++++++++--------- include/rapidjson/rapidjson.h | 14 ++++++++++---- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/include/rapidjson/allocators.h b/include/rapidjson/allocators.h index 4265214..2871542 100644 --- a/include/rapidjson/allocators.h +++ b/include/rapidjson/allocators.h @@ -20,7 +20,6 @@ #include #if RAPIDJSON_HAS_CXX11 -#include #include #endif @@ -466,6 +465,7 @@ public: typedef typename traits_type::size_type size_type; typedef typename traits_type::difference_type difference_type; + typedef typename traits_type::value_type value_type; typedef typename traits_type::pointer pointer; typedef typename traits_type::const_pointer const_pointer; @@ -484,19 +484,19 @@ public: return std::addressof(r); } - size_t max_size() const RAPIDJSON_NOEXCEPT + size_type max_size() const RAPIDJSON_NOEXCEPT { - return std::numeric_limits::max() / sizeof(value_type); + return traits_type::max_size(*this); } template - void construct(pointer p, Args &&...args) + void construct(pointer p, Args&&... args) { - ::new (static_cast(p)) value_type(std::forward(args)...); + traits_type::construct(*this, p, std::forward(args)...); } void destroy(pointer p) { - p->~T(); + traits_type::destroy(*this, p); } #else // !RAPIDJSON_HAS_CXX11 @@ -513,7 +513,7 @@ public: return allocator_type::address(r); } - size_t max_size() const RAPIDJSON_NOEXCEPT + size_type max_size() const RAPIDJSON_NOEXCEPT { return allocator_type::max_size(); } @@ -615,13 +615,13 @@ public: ~StdAllocator() RAPIDJSON_NOEXCEPT { } - typedef typename allocator_type::value_type value_type; - template struct rebind { typedef StdAllocator other; }; + typedef typename allocator_type::value_type value_type; + private: template friend class StdAllocator; // access to StdAllocator.* diff --git a/include/rapidjson/rapidjson.h b/include/rapidjson/rapidjson.h index 8dcb0a0..1093581 100644 --- a/include/rapidjson/rapidjson.h +++ b/include/rapidjson/rapidjson.h @@ -135,6 +135,8 @@ #define RAPIDJSON_CPLUSPLUS __cplusplus #endif +//!@endcond + /////////////////////////////////////////////////////////////////////////////// // RAPIDJSON_HAS_STDSTRING @@ -627,10 +629,14 @@ RAPIDJSON_NAMESPACE_END #if RAPIDJSON_HAS_CXX17 # define RAPIDJSON_DELIBERATE_FALLTHROUGH [[fallthrough]] -#elif defined(__has_cpp_attribute) && __has_cpp_attribute(fallthrough) -# define RAPIDJSON_DELIBERATE_FALLTHROUGH __attribute__((fallthrough)) -#elif defined(__has_cpp_attribute) && __has_cpp_attribute(clang::fallthrough) -# define RAPIDJSON_DELIBERATE_FALLTHROUGH [[clang::fallthrough]] +#elif defined(__has_cpp_attribute) +# if __has_cpp_attribute(clang::fallthrough) +# define RAPIDJSON_DELIBERATE_FALLTHROUGH [[clang::fallthrough]] +# elif __has_cpp_attribute(fallthrough) +# define RAPIDJSON_DELIBERATE_FALLTHROUGH __attribute__((fallthrough)) +# else +# define RAPIDJSON_DELIBERATE_FALLTHROUGH +# endif #else # define RAPIDJSON_DELIBERATE_FALLTHROUGH #endif