DAW JSON Link
daw_json_allocator_wrapper.h
Go to the documentation of this file.
1 // Copyright (c) Darrell Wright
2 //
3 // Distributed under the Boost Software License, Version 1.0. (See accompanying
4 // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt)
5 //
6 // Official repository: https://github.com/beached/daw_json_link
7 //
8 
9 #pragma once
10 
11 #include "version.h"
12 
13 #include <daw/cpp_17.h>
14 #include <daw/daw_traits.h>
15 
16 #include <cstddef>
17 #include <memory>
18 #include <type_traits>
19 
20 namespace daw::json {
21  inline namespace DAW_JSON_VER {
22  namespace json_details {
23  template<typename Alloc, bool /*is_empty*/>
25  using allocator_t = std::remove_reference_t<Alloc>;
26  allocator_t *allocator_ptr;
27 
28  public:
29  explicit AllocatorWrapperBase( allocator_t &alloc ) noexcept
30  : allocator_ptr( &alloc ) {}
31 
32  allocator_t &get_allocator( ) const {
33  return *allocator_ptr;
34  }
35  };
36 
37  template<typename Alloc>
38  class AllocatorWrapperBase<Alloc, true /*is_empty*/> {
39  using allocator_t = std::remove_reference_t<Alloc>;
40  static constexpr allocator_t allocator{ };
41 
42  public:
43  constexpr AllocatorWrapperBase( ) = default;
44  explicit constexpr AllocatorWrapperBase( allocator_t & ) noexcept {}
45 
46  allocator_t &get_allocator( ) const {
47  return allocator;
48  }
49  };
50 
51  template<typename Alloc>
53  : AllocatorWrapperBase<Alloc, std::is_empty<Alloc>::value> {
54  using allocator_type = std::remove_reference_t<Alloc>;
55 
56  explicit AllocatorWrapper( allocator_type &alloc ) noexcept
58  std::is_empty<allocator_type>::value>(
59  alloc ) {}
60 
61  static constexpr bool has_allocator = true;
62 
63  template<typename A, typename T>
65  using type =
66  typename std::allocator_traits<A>::template rebind_alloc<T>;
67  };
68 
69  template<typename A, typename T>
71  typename std::allocator_traits<A>::template rebind_traits<T>::type;
72 
73  template<typename A, typename T>
74  static inline constexpr bool has_rebind_v =
75  daw::is_detected<has_allocator_type_as_rebind, A, T>::value;
76 
77  // DAW FIX
78  template<typename T>
80  std::conditional_t<has_rebind_v<allocator_type, T>,
83 
84  template<typename T>
85  auto get_allocator_for( template_param<T> ) const {
86  return static_cast<allocator_type_as<T>>( this->get_allocator( ) );
87  }
88  };
89 
90  struct NoAllocator {};
91  template<>
93  public:
94  constexpr AllocatorWrapper( ) noexcept = default;
95  static constexpr bool has_allocator = false;
96 
97  using allocator_type = std::allocator<char>;
98 
99  template<typename T>
100  using allocator_type_as = std::allocator<T>;
101 
102  template<typename T>
103  [[maybe_unused]] inline std::allocator<T>
104  get_allocator_for( template_param<T> ) const {
105  return std::allocator<T>( );
106  }
107  };
108  } // namespace json_details
109  } // namespace DAW_JSON_VER
110 } // namespace daw::json
std::allocator< char > allocator_type
Definition: daw_json_allocator_wrapper.h:97
std::allocator< T > allocator_type_as
Definition: daw_json_allocator_wrapper.h:100
allocator_t & get_allocator() const
Definition: daw_json_allocator_wrapper.h:46
constexpr AllocatorWrapperBase(allocator_t &) noexcept
Definition: daw_json_allocator_wrapper.h:44
Definition: daw_json_allocator_wrapper.h:24
AllocatorWrapperBase(allocator_t &alloc) noexcept
Definition: daw_json_allocator_wrapper.h:29
allocator_t & get_allocator() const
Definition: daw_json_allocator_wrapper.h:32
Definition: daw_from_json.h:22
typename std::allocator_traits< A >::template rebind_alloc< T > type
Definition: daw_json_allocator_wrapper.h:66
Definition: daw_json_allocator_wrapper.h:53
std::conditional_t< has_rebind_v< allocator_type, T >, allocator_type_as_rebind< allocator_type, T >, allocator_type > allocator_type_as
Definition: daw_json_allocator_wrapper.h:82
typename std::allocator_traits< A >::template rebind_traits< T >::type has_allocator_type_as_rebind
Definition: daw_json_allocator_wrapper.h:71
std::remove_reference_t< Alloc > allocator_type
Definition: daw_json_allocator_wrapper.h:54
AllocatorWrapper(allocator_type &alloc) noexcept
Definition: daw_json_allocator_wrapper.h:56
static constexpr bool has_rebind_v
Definition: daw_json_allocator_wrapper.h:74
auto get_allocator_for(template_param< T >) const
Definition: daw_json_allocator_wrapper.h:85
static constexpr bool has_allocator
Definition: daw_json_allocator_wrapper.h:61
Definition: daw_json_allocator_wrapper.h:90
#define DAW_JSON_VER
Definition: version.h:11