KTL
Loading...
Searching...
No Matches
meta.h
Go to the documentation of this file.
1#pragma once
2
3#include "source_location.h"
4
5#include <type_traits>
6
7namespace ktl::detail
8{
9 // has value_type
10 template<typename Alloc, typename = void>
11 struct has_no_value_type : std::true_type {};
12
13 template<typename Alloc>
14 struct has_no_value_type<Alloc, std::void_t<typename Alloc::value_type>> : std::false_type {};
15
16 template<typename Alloc>
18
19 // get size_type
20 template<typename Alloc, typename = void>
22 {
23 using type = size_t;
24 };
25
26 template<typename Alloc>
27 struct get_size_type<Alloc, std::void_t<typename Alloc::size_type>>
28 {
29 using type = typename Alloc::size_type;
30 };
31
32 template<typename Alloc, typename = void>
34
35 // has allocate(size_t)
36 template<typename Alloc, typename = void>
37 struct has_plain_allocate : std::false_type {};
38
39 template<typename Alloc>
40 struct has_plain_allocate<Alloc, std::void_t<decltype(std::declval<Alloc&>().allocate(std::declval<size_t>()))>> : std::true_type {};
41
42 template<typename Alloc>
44
45 // has construct(T*, Args&&...)
46 template<typename Void, typename... Types>
47 struct has_construct : std::false_type {};
48
49 template<typename Alloc, typename... Args>
50 struct has_construct<std::void_t<decltype(std::declval<Alloc&>().construct(std::declval<Args>()...))>, Alloc, Args...> : std::true_type {};
51
52 template<typename Alloc, typename... Args>
54
55 // has destroy(T*)
56 template<typename Alloc, typename Ptr, typename = void>
57 struct has_destroy : std::false_type {};
58
59 template<typename Alloc, typename Ptr>
60 struct has_destroy<Alloc, Ptr, std::void_t<decltype(std::declval<Alloc&>().destroy(std::declval<Ptr>()))>> : std::true_type {};
61
62 template<typename Alloc, typename Ptr>
64
65 // has max_size()
66 template<typename Alloc, typename = void>
67 struct has_max_size : std::false_type {};
68
69 template<typename Alloc>
70 struct has_max_size<Alloc, std::void_t<decltype(std::declval<Alloc&>().max_size())>> : std::true_type {};
71
72 template<typename Alloc>
74
75 // has owns(void*)
76 template<typename Alloc, typename = void>
77 struct has_owns : std::false_type {};
78
79 template<typename Alloc>
80 struct has_owns<Alloc, std::void_t<decltype(std::declval<Alloc&>().owns(std::declval<void*>()))>> : std::true_type {};
81
82 template<typename Alloc>
84
85
86
87 // has allocate(size_t) noexcept
88 template<typename Alloc, bool>
89 struct has_nothrow_allocate : std::false_type {};
90
91 template<typename Alloc>
93 : std::bool_constant<noexcept(std::declval<Alloc&>().allocate(std::declval<size_t>()))> {};
94
95 template<typename Alloc>
97 : std::bool_constant<noexcept(std::declval<Alloc&>().allocate(std::declval<size_t>(), std::declval<source_location>()))> {};
98
99 template<typename Alloc>
101
102 // has deallocate(void*, size_t) noexcept
103 template<typename Alloc>
104 constexpr bool has_nothrow_deallocate_v = noexcept(std::declval<Alloc&>().deallocate(std::declval<void*>(), std::declval<size_t>()));
105
106 // has T& == T& noexcept
107 template<typename T>
108 constexpr bool has_nothrow_equal_v = noexcept(std::declval<T&>() == std::declval<T&>());
109
110 // has T& != T& noexcept
111 template<typename T>
112 constexpr bool has_nothrow_not_equal_v = noexcept(std::declval<T&>() == std::declval<T&>());
113
114 // has construct(T*, Args&&...) noexcept
115 template<typename Void, typename... Types>
116 struct has_nothrow_construct : std::false_type {};
117
118 template<typename Alloc, typename... Args>
119 struct has_nothrow_construct<std::enable_if_t<has_construct_v<Alloc, Args...>>, Alloc, Args...>
120 : std::bool_constant<noexcept(std::declval<Alloc&>().construct(std::declval<Args>()...))> {};
121
122 template<typename Alloc, typename... Args>
124
125 // has destroy(T*) noexcept
126 template<typename Alloc, typename Ptr, typename = void>
127 struct has_nothrow_destroy : std::false_type {};
128
129 template<typename Alloc, typename Ptr>
130 struct has_nothrow_destroy<Alloc, Ptr, std::enable_if_t<has_destroy_v<Alloc, Ptr>>>
131 : std::bool_constant<noexcept(std::declval<Alloc&>().destroy(std::declval<Ptr>()))> {};
132
133 template<typename Alloc, typename Ptr>
135
136 // has max_size() noexcept
137 template<typename Alloc, typename = void>
138 struct has_nothrow_max_size : std::false_type {};
139
140 template<typename Alloc>
141 struct has_nothrow_max_size<Alloc, std::enable_if_t<has_max_size_v<Alloc>>>
142 : std::bool_constant<noexcept(std::declval<Alloc&>().max_size())> {};
143
144 template<typename Alloc>
146
147 // has owns(void*) noexcept
148 template<typename Alloc, typename = void>
149 struct has_nothrow_owns : std::false_type {};
150
151 template<typename Alloc>
152 struct has_nothrow_owns<Alloc, std::enable_if_t<has_owns_v<Alloc>>>
153 : std::bool_constant<noexcept(std::declval<Alloc&>().owns(std::declval<void*>()))> {};
154
155 template<typename Alloc>
157
158
159
160 template<typename Alloc>
161 void* allocate(Alloc& alloc, size_t n, const source_location source) noexcept(false)
162 {
163 if constexpr (has_plain_allocate_v<Alloc>)
164 return alloc.allocate(n);
165 else
166 return alloc.allocate(n, source);
167 }
168}
Definition fallback_fwd.h:23
typename get_size_type< Alloc, void >::type get_size_type_t
Definition meta.h:33
constexpr bool has_nothrow_construct_v
Definition meta.h:123
constexpr bool has_max_size_v
Definition meta.h:73
constexpr bool has_construct_v
Definition meta.h:53
constexpr bool has_nothrow_destroy_v
Definition meta.h:134
void * allocate(Alloc &alloc, size_t n, const source_location source) noexcept(false)
Definition meta.h:161
constexpr bool has_no_value_type_v
Definition meta.h:17
constexpr bool has_nothrow_equal_v
Definition meta.h:108
constexpr bool has_nothrow_deallocate_v
Definition meta.h:104
constexpr bool has_plain_allocate_v
Definition meta.h:43
constexpr bool has_destroy_v
Definition meta.h:63
constexpr bool has_nothrow_allocate_v
Definition meta.h:100
constexpr bool has_owns_v
Definition meta.h:83
constexpr bool has_nothrow_not_equal_v
Definition meta.h:112
constexpr bool has_nothrow_owns_v
Definition meta.h:156
constexpr bool has_nothrow_max_size_v
Definition meta.h:145
Definition meta.h:22
size_t type
Definition meta.h:23
Definition meta.h:47
Definition meta.h:57
Definition meta.h:67
Definition meta.h:11
Definition meta.h:149
Definition meta.h:77
Definition source_location.h:19