Skip to content

Commit 02f4260

Browse files
committed
Make StdAllocator C++17-20 compatible.
1 parent cd5ee4d commit 02f4260

File tree

3 files changed

+78
-26
lines changed

3 files changed

+78
-26
lines changed

include/rapidjson/allocators.h

Lines changed: 52 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@
1919

2020
#include <memory>
2121

22+
#if RAPIDJSON_HAS_CXX11
23+
#include <limits>
24+
#include <type_traits>
25+
#endif
26+
2227
RAPIDJSON_NAMESPACE_BEGIN
2328

2429
///////////////////////////////////////////////////////////////////////////////
@@ -420,6 +425,11 @@ class StdAllocator :
420425
public std::allocator<T>
421426
{
422427
typedef std::allocator<T> allocator_type;
428+
#if RAPIDJSON_HAS_CXX11
429+
typedef std::allocator_traits<allocator_type> traits_type;
430+
#else
431+
typedef allocator_type traits_type;
432+
#endif
423433

424434
public:
425435
typedef BaseAllocator BaseAllocatorType;
@@ -449,29 +459,50 @@ class StdAllocator :
449459
~StdAllocator() RAPIDJSON_NOEXCEPT
450460
{ }
451461

452-
typedef typename allocator_type::value_type value_type;
453-
typedef typename allocator_type::pointer pointer;
454-
typedef typename allocator_type::const_pointer const_pointer;
455-
typedef typename allocator_type::reference reference;
456-
typedef typename allocator_type::const_reference const_reference;
457-
typedef typename allocator_type::size_type size_type;
458-
typedef typename allocator_type::difference_type difference_type;
459-
460462
template<typename U>
461463
struct rebind {
462464
typedef StdAllocator<U, BaseAllocator> other;
463465
};
464466

467+
typedef typename traits_type::size_type size_type;
468+
typedef typename traits_type::difference_type difference_type;
469+
typedef typename traits_type::value_type value_type;
470+
typedef typename traits_type::pointer pointer;
471+
typedef typename traits_type::const_pointer const_pointer;
472+
465473
#if RAPIDJSON_HAS_CXX11
466-
using allocator_type::max_size;
467-
using allocator_type::address;
468-
using allocator_type::construct;
469-
using allocator_type::destroy;
470-
#else
474+
475+
typedef typename std::add_lvalue_reference<value_type>::type &reference;
476+
typedef typename std::add_lvalue_reference<typename std::add_const<value_type>::type>::type &const_reference;
477+
478+
pointer address(reference r) const RAPIDJSON_NOEXCEPT
479+
{
480+
return std::addressof(r);
481+
}
482+
const_pointer address(const_reference r) const RAPIDJSON_NOEXCEPT
483+
{
484+
return std::addressof(r);
485+
}
486+
471487
size_t max_size() const RAPIDJSON_NOEXCEPT
472488
{
473-
return allocator_type::max_size();
489+
return std::numeric_limits<size_type>::max() / sizeof(value_type);
490+
}
491+
492+
template <typename ...Args>
493+
void construct(pointer p, Args &&...args)
494+
{
495+
::new (static_cast<void*>(p)) value_type(std::forward<Args>(args)...);
474496
}
497+
void destroy(pointer p)
498+
{
499+
p->~T();
500+
}
501+
502+
#else // !RAPIDJSON_HAS_CXX11
503+
504+
typedef typename allocator_type::reference reference;
505+
typedef typename allocator_type::const_reference const_reference;
475506

476507
pointer address(reference r) const RAPIDJSON_NOEXCEPT
477508
{
@@ -482,6 +513,11 @@ class StdAllocator :
482513
return allocator_type::address(r);
483514
}
484515

516+
size_t max_size() const RAPIDJSON_NOEXCEPT
517+
{
518+
return allocator_type::max_size();
519+
}
520+
485521
void construct(pointer p, const_reference r)
486522
{
487523
allocator_type::construct(p, r);
@@ -490,7 +526,8 @@ class StdAllocator :
490526
{
491527
allocator_type::destroy(p);
492528
}
493-
#endif
529+
530+
#endif // !RAPIDJSON_HAS_CXX11
494531

495532
template <typename U>
496533
U* allocate(size_type n = 1, const void* = 0)

include/rapidjson/rapidjson.h

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,17 @@
124124
#define RAPIDJSON_NAMESPACE_END }
125125
#endif
126126

127+
///////////////////////////////////////////////////////////////////////////////
128+
// __cplusplus macro
129+
130+
//!@cond RAPIDJSON_HIDDEN_FROM_DOXYGEN
131+
132+
#if defined(_MSC_VER)
133+
#define RAPIDJSON_CPLUSPLUS _MSVC_LANG
134+
#else
135+
#define RAPIDJSON_CPLUSPLUS __cplusplus
136+
#endif
137+
127138
///////////////////////////////////////////////////////////////////////////////
128139
// RAPIDJSON_HAS_STDSTRING
129140

@@ -411,7 +422,7 @@ RAPIDJSON_NAMESPACE_END
411422

412423
// Prefer C++11 static_assert, if available
413424
#ifndef RAPIDJSON_STATIC_ASSERT
414-
#if __cplusplus >= 201103L || ( defined(_MSC_VER) && _MSC_VER >= 1800 )
425+
#if RAPIDJSON_CPLUSPLUS >= 201103L || ( defined(_MSC_VER) && _MSC_VER >= 1800 )
415426
#define RAPIDJSON_STATIC_ASSERT(x) \
416427
static_assert(x, RAPIDJSON_STRINGIFY(x))
417428
#endif // C++11
@@ -542,7 +553,7 @@ RAPIDJSON_NAMESPACE_END
542553
// C++11 features
543554

544555
#ifndef RAPIDJSON_HAS_CXX11
545-
#define RAPIDJSON_HAS_CXX11 (__cplusplus >= 201103L)
556+
#define RAPIDJSON_HAS_CXX11 (RAPIDJSON_CPLUSPLUS >= 201103L)
546557
#endif
547558

548559
#ifndef RAPIDJSON_HAS_CXX11_RVALUE_REFS
@@ -610,12 +621,16 @@ RAPIDJSON_NAMESPACE_END
610621
///////////////////////////////////////////////////////////////////////////////
611622
// C++17 features
612623

613-
#if defined(__has_cpp_attribute)
614-
# if __has_cpp_attribute(fallthrough)
615-
# define RAPIDJSON_DELIBERATE_FALLTHROUGH [[fallthrough]]
616-
# else
617-
# define RAPIDJSON_DELIBERATE_FALLTHROUGH
618-
# endif
624+
#ifndef RAPIDJSON_HAS_CXX17
625+
#define RAPIDJSON_HAS_CXX17 (RAPIDJSON_CPLUSPLUS >= 201703L)
626+
#endif
627+
628+
#if RAPIDJSON_HAS_CXX17
629+
# define RAPIDJSON_DELIBERATE_FALLTHROUGH [[fallthrough]]
630+
#elif defined(__has_cpp_attribute) && __has_cpp_attribute(fallthrough)
631+
# define RAPIDJSON_DELIBERATE_FALLTHROUGH __attribute__((fallthrough))
632+
#elif defined(__has_cpp_attribute) && __has_cpp_attribute(clang::fallthrough)
633+
# define RAPIDJSON_DELIBERATE_FALLTHROUGH [[clang::fallthrough]]
619634
#else
620635
# define RAPIDJSON_DELIBERATE_FALLTHROUGH
621636
#endif

test/unittest/allocatorstest.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,20 +107,20 @@ void TestStdAllocator(const Allocator& a) {
107107
arr[i] = 0x0f0f0f0f;
108108
}
109109
ia.deallocate(arr, 10);
110-
arr = (int *)ia.Malloc(10 * sizeof(int));
110+
arr = Malloc<int>(ia, 10);
111111
EXPECT_TRUE(arr != 0);
112112
for (int i = 0; i < 10; ++i) {
113113
arr[i] = 0x0f0f0f0f;
114114
}
115-
arr = (int *)ia.Realloc(arr, 10 * sizeof(int), 20 * sizeof(int));
115+
arr = Realloc<int>(ia, arr, 10, 20);
116116
EXPECT_TRUE(arr != 0);
117117
for (int i = 0; i < 10; ++i) {
118118
EXPECT_EQ(arr[i], 0x0f0f0f0f);
119119
}
120120
for (int i = 10; i < 20; i++) {
121121
arr[i] = 0x0f0f0f0f;
122122
}
123-
ia.Free(arr);
123+
Free<int>(ia, arr, 20);
124124

125125
int cons = 0, dest = 0;
126126
StdAllocator<TestStdAllocatorData, Allocator> da(a);

0 commit comments

Comments
 (0)