-
Notifications
You must be signed in to change notification settings - Fork 2
/
type_traits.hpp
329 lines (277 loc) · 9 KB
/
type_traits.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/**
* MIT License
*
* Copyright (c) 2022 Victor Moncada <vtr.moncada@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef SEQ_TYPE_TRAITS_HPP
#define SEQ_TYPE_TRAITS_HPP
#include <iostream>
#include <memory>
#include <type_traits>
#include <functional>
#include <limits>
#include <cstdint>
namespace std
{
#if defined(__GNUG__) && (__GNUC__ < 5)
// Reimplement the wheel for older gcc
template<class T>
struct is_trivially_copyable
{
static constexpr bool value = __has_trivial_copy(T);
};
template< class T>
struct is_trivially_move_assignable : is_trivially_copyable<T> {};
//: std::is_trivially_assignable< typename std::add_lvalue_reference<T>::type,
// typename std::add_rvalue_reference<T>::type> {};
#endif
}
namespace seq
{
namespace detail
{
template <class T>
struct make_void {
using type = void;
};
}
/// @brief Compute integer type maximum value at compile time
template <class T, bool Signed = std::is_signed<T>::value>
struct integer_max
{
static constexpr T value = std::numeric_limits<T>::max();//static_cast<T>( ~(static_cast < T>(1) << (static_cast<T>(sizeof(T) * 8) - static_cast < T>(1))) );
};
template <class T>
struct integer_max<T,false>
{
static constexpr T value = static_cast<T>(-1);
};
/// @brief Compute integer type minimum value at compile time
template <class T, bool Signed = std::is_signed<T>::value>
struct integer_min
{
static constexpr T value = (-integer_max<T>::value) - static_cast < T>(1) ;
};
template <class T>
struct integer_min<T,false>
{
static constexpr T value = static_cast < T>(0);
};
/// @brief Define the return type of seq::negate_if_signed and seq::abs
template<class T, bool Signed = std::is_signed<T>::value, size_t Size = sizeof(T)>
struct integer_abs_return
{
using type = T;
};
template<class T>
struct integer_abs_return<T,true,1>
{
using type = std::uint16_t;
};
template<class T>
struct integer_abs_return<T, true, 2>
{
using type = std::uint32_t;
};
template<class T>
struct integer_abs_return<T, true, 4>
{
using type = std::uint64_t;
};
template<class T>
struct integer_abs_return<T, true, 8>
{
using type = std::uint64_t;
};
namespace detail
{
template<class T, bool Signed = std::is_signed<T>::value>
struct IntegerAbs
{
using type = typename integer_abs_return<T>::type;
static inline auto neg_if_signed(T v) -> type { return static_cast<type>(-v); }
static inline auto abs(T v) -> type { return static_cast<type>(v < 0 ? -v : v); }
};
template<class T>
struct IntegerAbs<T,false>
{
using type = T;
static inline auto neg_if_signed(T v) -> T { return v; }
static inline auto abs(T v) -> T { return v; }
};
}
/// @brief Returns -v if v is signed, v otherwise.
template<class T>
auto negate_if_signed(T v) -> typename integer_abs_return<T>::type { return detail::IntegerAbs<T>::neg_if_signed(v); }
/// @brief Returns absolute value of v.
template<class T>
auto abs(T v) -> typename integer_abs_return<T>::type { return detail::IntegerAbs<T>::abs(v); }
namespace detail
{
template<class T>
struct unique_ptr_traits {};
template<class T, class Del>
struct unique_ptr_traits<std::unique_ptr<T, Del> >
{
using value_type = T;
using pointer = T*;
using const_pointer = const T*;
using reference = T&;
using const_reference = const T&;
};
template<class T, class Del>
struct unique_ptr_traits<const std::unique_ptr<T, Del> >
{
using value_type = T;
using pointer = const T*;
using const_pointer = const T*;
using reference = const T&;
using const_reference = const T&;
};
}
/// @brief Inherits std::true_type is T is of type std::unique_ptr<...>, false otherwise
template<class T>
struct is_unique_ptr : std::false_type {};
template<class T, class Del>
struct is_unique_ptr<std::unique_ptr<T, Del> > : std::true_type {};
/// @brief Type trait telling if a class is relocatable or not.
///
/// A type is considered relocatable if these consecutive calls
/// \code{.cpp}
/// new(new_place) T(std::move(old_place));
/// old_place.~T();
/// \endcode
/// can be replaced by
/// \code{.cpp}
/// memcpy(&new_place, &old_place, sizeof(T));
/// \endcode
///
/// This property is used to optimize containers like seq::devector, seq::tiered_vector, seq::flat_(map/set/multimap/multiset).
///
template<class T>
struct is_relocatable {
static constexpr bool value = std::is_trivially_copyable<T>::value && std::is_trivially_destructible<T>::value;
};
// Specilizations for unique_ptr, shared_ptr and pair
template<class T, class D>
struct is_relocatable<std::unique_ptr<T, D> > {
static constexpr bool value = is_relocatable<D>::value;
};
template<class T>
struct is_relocatable<std::shared_ptr<T> > {
static constexpr bool value = true;
};
template<class T, class V>
struct is_relocatable<std::pair<T, V> > {
static constexpr bool value = is_relocatable<T>::value && is_relocatable<V>::value;
};
template<class T>
struct is_relocatable<std::allocator<T> > {
static constexpr bool value = true;
};
/// @brief Tells if given type is hashable with std::hash.
/// True by default, optimistically assume that all types are hashable.
/// Used by seq::hold_any.
template<class T>
struct is_hashable : std::true_type {};
/// @brief Tells if given type can be streamed to a std::ostream object
template<class T, class = void>
struct is_ostreamable : std::false_type {};
template<class T>
struct is_ostreamable<T, typename detail::make_void<decltype(std::declval<std::ostream&>() << std::declval<const T&>())>::type > : std::true_type {};
/// @brief Tells if given type can be read from a std::istream object
template<class T, class = void>
struct is_istreamable : std::false_type {};
template<class T>
struct is_istreamable<T, typename detail::make_void<decltype(std::declval<std::istream&>() >> std::declval<T&>())>::type > : std::true_type {};
/// @brief Tells if given type supports equality comparison with operator ==
template<class T>
class is_equal_comparable
{
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wfloat-equal"
#endif
template<class TT>
static auto test(int)
-> decltype(std::declval<TT&>() == std::declval<TT&>(), std::true_type());
#ifdef __clang__
#pragma clang diagnostic pop
#endif
template<class>
static auto test(...)->std::false_type;
public:
static constexpr bool value = decltype(test<T>(0))::value;
};
/// @brief Tells if given type supports comparison with operator <
template<class T>
class is_less_comparable
{
template<class TT>
static auto test(int)
-> decltype(std::declval<TT&>() < std::declval<TT&>(), std::true_type());
template<class>
static auto test(...)->std::false_type;
public:
static constexpr bool value = decltype(test<T>(0))::value;
};
/// @brief Tells if given type supports invocation with signature void(Args ...)
/// Equivalent to C++17 std::is_invocable
template <typename F, typename... Args>
struct is_invocable :
std::is_constructible<
std::function<void(Args ...)>,
std::reference_wrapper<typename std::remove_reference<F>::type>
>
{
};
/// @brief Tells if given type supports invocation with signature R(Args ...)
/// Equivalent to C++17 std::is_invocable_r
template <typename R, typename F, typename... Args>
struct is_invocable_r :
std::is_constructible<
std::function<R(Args ...)>,
std::reference_wrapper<typename std::remove_reference<F>::type>
>
{
};
namespace detail
{
template <class T, class = void>
struct has_iterator : std::false_type {};
template <class T>
struct has_iterator<T,
typename make_void<typename T::iterator>::type>
: std::true_type {};
template <class T, class = void>
struct has_value_type : std::false_type {};
template <class T>
struct has_value_type<T,
typename make_void<typename T::value_type>::type>
: std::true_type {};
}
template<class C>
struct is_iterable
{
static constexpr bool value = detail::has_iterator<C>::value && detail::has_value_type<C>::value;
};
}
#endif