Skip to content

Commit b996a23

Browse files
authored
Merge pull request Tencent#1866 from ylavic/std_allocator_traits
Make StdAllocator C++17-20 compatible.
2 parents cd5ee4d + 08cf9a5 commit b996a23

File tree

3 files changed

+84
-26
lines changed

3 files changed

+84
-26
lines changed

include/rapidjson/allocators.h

Lines changed: 55 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919

2020
#include <memory>
2121

22+
#if RAPIDJSON_HAS_CXX11
23+
#include <type_traits>
24+
#endif
25+
2226
RAPIDJSON_NAMESPACE_BEGIN
2327

2428
///////////////////////////////////////////////////////////////////////////////
@@ -420,6 +424,11 @@ class StdAllocator :
420424
public std::allocator<T>
421425
{
422426
typedef std::allocator<T> allocator_type;
427+
#if RAPIDJSON_HAS_CXX11
428+
typedef std::allocator_traits<allocator_type> traits_type;
429+
#else
430+
typedef allocator_type traits_type;
431+
#endif
423432

424433
public:
425434
typedef BaseAllocator BaseAllocatorType;
@@ -449,30 +458,52 @@ class StdAllocator :
449458
~StdAllocator() RAPIDJSON_NOEXCEPT
450459
{ }
451460

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-
460461
template<typename U>
461462
struct rebind {
462463
typedef StdAllocator<U, BaseAllocator> other;
463464
};
464465

466+
typedef typename traits_type::size_type size_type;
467+
typedef typename traits_type::difference_type difference_type;
468+
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
471-
size_t max_size() const RAPIDJSON_NOEXCEPT
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
472479
{
473-
return allocator_type::max_size();
480+
return std::addressof(r);
481+
}
482+
const_pointer address(const_reference r) const RAPIDJSON_NOEXCEPT
483+
{
484+
return std::addressof(r);
485+
}
486+
487+
size_type max_size() const RAPIDJSON_NOEXCEPT
488+
{
489+
return traits_type::max_size(*this);
490+
}
491+
492+
template <typename ...Args>
493+
void construct(pointer p, Args&&... args)
494+
{
495+
traits_type::construct(*this, p, std::forward<Args>(args)...);
496+
}
497+
void destroy(pointer p)
498+
{
499+
traits_type::destroy(*this, p);
474500
}
475501

502+
#else // !RAPIDJSON_HAS_CXX11
503+
504+
typedef typename allocator_type::reference reference;
505+
typedef typename allocator_type::const_reference const_reference;
506+
476507
pointer address(reference r) const RAPIDJSON_NOEXCEPT
477508
{
478509
return allocator_type::address(r);
@@ -482,6 +513,11 @@ class StdAllocator :
482513
return allocator_type::address(r);
483514
}
484515

516+
size_type 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)
@@ -578,13 +615,13 @@ class StdAllocator<void, BaseAllocator> :
578615
~StdAllocator() RAPIDJSON_NOEXCEPT
579616
{ }
580617

581-
typedef typename allocator_type::value_type value_type;
582-
583618
template<typename U>
584619
struct rebind {
585620
typedef StdAllocator<U, BaseAllocator> other;
586621
};
587622

623+
typedef typename allocator_type::value_type value_type;
624+
588625
private:
589626
template <typename, typename>
590627
friend class StdAllocator; // access to StdAllocator<!T>.*

include/rapidjson/rapidjson.h

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,19 @@
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+
138+
//!@endcond
139+
127140
///////////////////////////////////////////////////////////////////////////////
128141
// RAPIDJSON_HAS_STDSTRING
129142

@@ -411,7 +424,7 @@ RAPIDJSON_NAMESPACE_END
411424

412425
// Prefer C++11 static_assert, if available
413426
#ifndef RAPIDJSON_STATIC_ASSERT
414-
#if __cplusplus >= 201103L || ( defined(_MSC_VER) && _MSC_VER >= 1800 )
427+
#if RAPIDJSON_CPLUSPLUS >= 201103L || ( defined(_MSC_VER) && _MSC_VER >= 1800 )
415428
#define RAPIDJSON_STATIC_ASSERT(x) \
416429
static_assert(x, RAPIDJSON_STRINGIFY(x))
417430
#endif // C++11
@@ -542,7 +555,7 @@ RAPIDJSON_NAMESPACE_END
542555
// C++11 features
543556

544557
#ifndef RAPIDJSON_HAS_CXX11
545-
#define RAPIDJSON_HAS_CXX11 (__cplusplus >= 201103L)
558+
#define RAPIDJSON_HAS_CXX11 (RAPIDJSON_CPLUSPLUS >= 201103L)
546559
#endif
547560

548561
#ifndef RAPIDJSON_HAS_CXX11_RVALUE_REFS
@@ -610,9 +623,17 @@ RAPIDJSON_NAMESPACE_END
610623
///////////////////////////////////////////////////////////////////////////////
611624
// C++17 features
612625

613-
#if defined(__has_cpp_attribute)
614-
# if __has_cpp_attribute(fallthrough)
615-
# define RAPIDJSON_DELIBERATE_FALLTHROUGH [[fallthrough]]
626+
#ifndef RAPIDJSON_HAS_CXX17
627+
#define RAPIDJSON_HAS_CXX17 (RAPIDJSON_CPLUSPLUS >= 201703L)
628+
#endif
629+
630+
#if RAPIDJSON_HAS_CXX17
631+
# define RAPIDJSON_DELIBERATE_FALLTHROUGH [[fallthrough]]
632+
#elif defined(__has_cpp_attribute)
633+
# if __has_cpp_attribute(clang::fallthrough)
634+
# define RAPIDJSON_DELIBERATE_FALLTHROUGH [[clang::fallthrough]]
635+
# elif __has_cpp_attribute(fallthrough)
636+
# define RAPIDJSON_DELIBERATE_FALLTHROUGH __attribute__((fallthrough))
616637
# else
617638
# define RAPIDJSON_DELIBERATE_FALLTHROUGH
618639
# 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)