17 #include <type_traits>
21 namespace json_details {
22 template<
typename Alloc,
bool >
23 class AllocatorWrapperBase {
24 using allocator_t = std::remove_reference_t<Alloc>;
25 std::optional<allocator_t const *> allocator_ptr;
28 explicit AllocatorWrapperBase( ) =
default;
29 explicit constexpr AllocatorWrapperBase(
30 allocator_t
const &alloc ) noexcept
31 : allocator_ptr( &alloc ) {}
33 [[nodiscard]] constexpr allocator_t
const &get_allocator( )
const {
35 ErrorReason::UnexpectedNull );
36 return **allocator_ptr;
40 template<
typename Alloc>
41 class AllocatorWrapperBase<Alloc, true > {
42 using has_stateless_allocator = void;
43 using allocator_t = std::remove_reference_t<Alloc>;
44 static constexpr allocator_t allocator{ };
47 explicit AllocatorWrapperBase( ) =
default;
48 explicit constexpr AllocatorWrapperBase(
49 allocator_t
const & ) noexcept {}
51 [[nodiscard]] constexpr allocator_t
const &get_allocator( )
const {
57 allocator_has_rebind_v,
58 typename std::allocator_traits<T>::template rebind_traits<U>::type );
60 template<
typename Alloc>
61 struct AllocatorWrapper
62 : AllocatorWrapperBase<Alloc, std::is_empty_v<Alloc>> {
63 using allocator_type = std::remove_reference_t<Alloc>;
65 using AllocatorWrapperBase<Alloc,
66 std::is_empty_v<Alloc>>::get_allocator;
68 explicit AllocatorWrapper( ) =
default;
69 explicit constexpr AllocatorWrapper(
70 allocator_type
const &alloc ) noexcept
71 : AllocatorWrapperBase<allocator_type,
72 std::is_empty_v<allocator_type>>( alloc ) {}
74 static constexpr
bool has_allocator =
true;
76 template<
typename A,
typename T>
77 struct allocator_type_as_rebind {
79 typename std::allocator_traits<A>::template rebind_alloc<T>;
82 template<
typename A,
typename T>
83 static constexpr
bool has_rebind_v = allocator_has_rebind_v<A, T>;
87 using allocator_type_as =
typename std::allocator_traits<
88 allocator_type>::template rebind_alloc<T>;
91 [[nodiscard]] constexpr
auto get_allocator_for( )
const {
92 return static_cast<allocator_type_as<T>
>( this->get_allocator( ) );
96 struct NoAllocator {};
99 class AllocatorWrapper<NoAllocator> {
101 explicit AllocatorWrapper( ) =
default;
102 explicit constexpr AllocatorWrapper( NoAllocator
const & ) noexcept {}
104 static constexpr
bool has_allocator =
false;
106 using allocator_type = std::allocator<char>;
109 using allocator_type_as = std::allocator<T>;
112 [[nodiscard]] constexpr
static std::allocator<T> get_allocator_for( ) {
113 return std::allocator<T>( );
116 [[nodiscard]] DAW_CONSTEVAL
static NoAllocator get_allocator( ) {
#define daw_json_ensure(Bool,...)
Ensure that Bool is true. If false pass rest of args to daw_json_error.
#define DAW_JSON_MAKE_REQ_TYPE_ALIAS_TRAIT2(Name,...)
Customization point traits.
#define DAW_JSON_VER
The version string used in namespace definitions. Must be a valid namespace name.