KTL
global.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "../utility/aligned_malloc.h"
4 #include "../utility/alignment.h"
5 #include "../utility/empty_base.h"
6 #include "../utility/meta.h"
7 #include "global_fwd.h"
8 
9 #include <memory>
10 #include <type_traits>
11 
12 namespace ktl
13 {
14  template<typename Alloc>
15  class global
16  {
17  private:
18  static_assert(detail::has_no_value_type_v<Alloc>, "Building on top of typed allocators is not allowed. Use allocators without a type");
19 
20  public:
22 
26  global() noexcept {}
27 
28  global(const global& other) noexcept {}
29 
30  global(global&& other) noexcept {}
31 
32  global& operator=(const global& rhs) noexcept
33  {
34  return *this;
35  }
36 
37  global& operator=(global&& rhs) noexcept
38  {
39  return *this;
40  }
41 
42  bool operator==(const global& rhs) const
43  {
44  return true;
45  }
46 
47  bool operator!=(const global& rhs) const
48  {
49  return true;
50  }
51 
52 #pragma region Allocation
53  void* allocate(size_t n)
54  noexcept(detail::has_nothrow_allocate_v<Alloc>)
55  {
56  return s_Alloc.allocate(n);
57  }
58 
59  void deallocate(void* p, size_t n)
60  noexcept(detail::has_nothrow_deallocate_v<Alloc>)
61  {
62  s_Alloc.deallocate(p, n);
63  }
64 #pragma endregion
65 
66 #pragma region Construction
67  template<typename T, typename... Args>
68  typename std::enable_if<detail::has_construct_v<Alloc, T*, Args...>, void>::type
69  construct(T* p, Args&&... args)
70  noexcept(detail::has_nothrow_construct_v<Alloc, T*, Args...>)
71  {
72  s_Alloc.construct(p, std::forward<Args>(args)...);
73  }
74 
75  template<typename T>
76  typename std::enable_if<detail::has_destroy_v<Alloc, T*>, void>::type
77  destroy(T* p)
78  noexcept(detail::has_nothrow_destroy_v<Alloc, T*>)
79  {
80  s_Alloc.destroy(p);
81  }
82 #pragma endregion
83 
84 #pragma region Utility
85  template<typename A = Alloc>
86  typename std::enable_if<detail::has_max_size_v<A>, size_type>::type
87  max_size() const
88  noexcept(detail::has_nothrow_max_size_v<A>)
89  {
90  return s_Alloc.max_size();
91  }
92 
93  template<typename A = Alloc>
94  typename std::enable_if<detail::has_owns_v<A>, bool>::type
95  owns(void* p) const
96  noexcept(detail::has_nothrow_owns_v<A>)
97  {
98  return s_Alloc.owns(p);
99  }
100 #pragma endregion
101 
102  void set_allocator(Alloc&& value) noexcept
103  {
104  s_Alloc = std::move(value);
105  }
106 
107  Alloc& get_allocator() noexcept
108  {
109  return s_Alloc;
110  }
111 
112  const Alloc& get_allocator() const noexcept
113  {
114  return s_Alloc;
115  }
116 
117  private:
118  static inline Alloc s_Alloc{};
119  };
120 }
Definition: global.h:16
bool operator==(const global &rhs) const
Definition: global.h:42
const Alloc & get_allocator() const noexcept
Definition: global.h:112
bool operator!=(const global &rhs) const
Definition: global.h:47
global & operator=(const global &rhs) noexcept
Definition: global.h:32
detail::get_size_type_t< Alloc > size_type
Definition: global.h:18
void set_allocator(Alloc &&value) noexcept
Definition: global.h:102
global(global &&other) noexcept
Definition: global.h:30
void * allocate(size_t n) noexcept(detail::has_nothrow_allocate_v< Alloc >)
Definition: global.h:53
std::enable_if< detail::has_max_size_v< A >, size_type >::type max_size() const noexcept(detail::has_nothrow_max_size_v< A >)
Definition: global.h:87
global & operator=(global &&rhs) noexcept
Definition: global.h:37
std::enable_if< detail::has_destroy_v< Alloc, T * >, void >::type destroy(T *p) noexcept(detail::has_nothrow_destroy_v< Alloc, T * >)
Definition: global.h:77
global() noexcept
Constructor for forwarding any arguments to the underlying allocator.
Definition: global.h:26
global(const global &other) noexcept
Definition: global.h:28
std::enable_if< detail::has_construct_v< Alloc, T *, Args... >, void >::type construct(T *p, Args &&... args) noexcept(detail::has_nothrow_construct_v< Alloc, T *, Args... >)
Definition: global.h:69
std::enable_if< detail::has_owns_v< A >, bool >::type owns(void *p) const noexcept(detail::has_nothrow_owns_v< A >)
Definition: global.h:95
void deallocate(void *p, size_t n) noexcept(detail::has_nothrow_deallocate_v< Alloc >)
Definition: global.h:59
Alloc & get_allocator() noexcept
Definition: global.h:107
constexpr bool has_construct_v
Definition: meta.h:41
typename get_size_type< Alloc, void >::type get_size_type_t
Definition: meta.h:31
constexpr bool has_nothrow_max_size_v
Definition: meta.h:122
Definition: cascading.h:15