StormByte C++ Library 0.0.9999
StormByte is a comprehensive, cross-platform C++ library aimed at easing system programming, configuration management, logging, and database handling tasks. This library provides a unified API that abstracts away the complexities and inconsistencies of different platforms (Windows, Linux).
Loading...
Searching...
No Matches
type_traits.hxx
1#pragma once
2
3#include <concepts>
4#include <optional>
5#include <string>
6#include <type_traits>
7#include <utility>
8#include <variant>
9
17namespace StormByte {
18 // Implementation detail: type traits for internal use only
19 namespace {
20 template<typename T>
21 struct is_string : std::bool_constant<std::is_same_v<T, std::string> || std::is_same_v<T, std::wstring>> {};
22
23 template<typename T, typename _ = void>
24 struct is_container : std::false_type {};
25
26 template<typename T>
27 struct is_container<T, std::void_t<decltype(std::declval<T>().begin()), decltype(std::declval<T>().end()), typename T::value_type>>
28 : std::bool_constant<!is_string<std::decay_t<T>>::value> {};
29
30 template<typename T, typename _ = void>
31 struct is_optional : std::false_type {};
32
33 template<typename T>
34 struct is_optional<T, std::void_t<typename T::value_type>>
35 : std::is_same<T, std::optional<typename T::value_type>> {};
36
37 template<typename T, typename _ = void>
38 struct is_pair : std::false_type {};
39
40 template<typename T>
41 struct is_pair<T, std::void_t<
42 decltype(std::declval<T>().first),
43 decltype(std::declval<T>().second)
44 >> : std::true_type {};
45
46 template<typename T>
47 struct is_variant : std::false_type {};
48
49 template<typename... Ts>
50 struct is_variant<std::variant<Ts...>> : std::true_type {};
51
52 template<typename VariantT, typename U, std::size_t... I>
53 constexpr bool variant_has_type_impl(std::index_sequence<I...>) noexcept {
54 return ((std::is_same_v<std::remove_cvref_t<U>, std::remove_cvref_t<std::variant_alternative_t<I, VariantT>>>) || ...);
55 }
56
57 template<typename VariantT, typename U>
58 struct variant_has_type : std::bool_constant<
59 variant_has_type_impl<VariantT, U>(std::make_index_sequence<std::variant_size_v<VariantT>>())
60 > {};
61 }
62
70 namespace Type {
81 template<typename T>
82 concept String = is_string<T>::value;
83
95 template<typename T>
96 concept Container = is_container<T>::value;
97
104 template<typename C>
105 concept HasPushBack = Container<C> && requires(C& c, typename C::value_type const& v) {
106 c.push_back(v);
107 };
108
112 template<typename C>
113 concept HasPushFront = Container<C> && requires(C& c, typename C::value_type const& v) {
114 c.push_front(v);
115 };
116
120 template<typename C>
121 concept HasInsert = Container<C> && requires(C& c, typename C::value_type const& v) {
122 c.insert(v);
123 };
124
131 template<typename C, typename U>
132 concept HasSubscript = Container<C> && requires(C& c, U const& u) {
133 { c[u] };
134 };
135
146 template<typename C>
147 concept HasKeyType = Container<C> && requires { typename C::key_type; };
148
155 template<typename C>
156 concept HasMappedType = Container<C> && requires { typename C::mapped_type; };
157
168 template<typename T>
169 concept Optional = is_optional<T>::value;
170
181 template<typename T>
182 concept Pair = is_pair<T>::value;
183
194 template<typename T>
195 concept Reference = std::is_reference_v<T>;
196
207 template<typename E>
208 concept Enum = std::is_enum_v<std::remove_cv_t<E>>;
209
220 template<typename E>
221 concept UnsignedEnum =
222 Enum<E> &&
223 std::is_unsigned_v<std::underlying_type_t<std::remove_cv_t<E>>>;
224
235 template<typename E>
236 concept ScopedEnum = std::is_scoped_enum_v<E>;
237
245 template<typename E>
246 requires Enum<E>
247 using UnderlyingType = std::underlying_type_t<std::remove_cv_t<E>>;
248
252 template<typename E>
253 requires Enum<E>
254 constexpr UnderlyingType<E> ToUnderlying(E e) noexcept {
255 return static_cast<UnderlyingType<E>>(e);
256 }
257
268 template<typename T>
269 concept Pointer = std::is_pointer_v<T>;
270
281 template<typename T>
282 concept Integral = std::is_integral_v<T>;
283
294 template<typename T>
295 concept FloatingPoint = std::is_floating_point_v<T>;
296
307 template<typename T>
308 concept Arithmetic = std::is_arithmetic_v<T>;
309
320 template<typename T>
321 concept Signed = std::is_signed_v<T>;
322
333 template<typename T>
334 concept Unsigned = std::is_unsigned_v<T>;
335
346 template<typename T>
347 concept Const = std::is_const_v<T>;
348
359 template<typename T>
360 concept Class = std::is_class_v<T>;
361
372 template<typename T>
373 concept Variant = is_variant<std::remove_cvref_t<T>>::value;
374
386 template<typename T, typename U>
387 concept VariantHasType = Variant<T> && variant_has_type<std::remove_cvref_t<T>, U>::value;
388
399 template<typename T>
400 concept TriviallyCopyable = std::is_trivially_copyable_v<T>;
401
412 template<typename T>
413 concept TriviallyDestructible = std::is_trivially_destructible_v<T>;
414
425 template<typename T>
426 concept DefaultConstructible = std::is_default_constructible_v<T>;
427
438 template<typename T>
439 concept CopyConstructible = std::is_copy_constructible_v<T>;
440
451 template<typename T>
452 concept MoveConstructible = std::is_move_constructible_v<T>;
453
466 template<typename F, typename... Args>
467 concept Callable = std::is_invocable_v<F, Args...>;
468
481 template<typename T, typename U>
482 concept SameAs = std::is_same_v<std::remove_cvref_t<T>, std::remove_cvref_t<U>>;
483
496 template<typename From, typename To>
497 concept ConvertibleTo = std::is_convertible_v<From, To>;
498 }
499}
Concept to check if a type is arithmetic.
Definition type_traits.hxx:308
Concept to check if a type is callable.
Definition type_traits.hxx:467
Concept to check if a type is a class or struct.
Definition type_traits.hxx:360
Concept to check if a type is const-qualified.
Definition type_traits.hxx:347
Concept to check if a type is a container (excluding strings).
Definition type_traits.hxx:96
Concept to check if one type is convertible to another.
Definition type_traits.hxx:497
Concept to check if a type is copy constructible.
Definition type_traits.hxx:439
Concept to check if a type is default constructible.
Definition type_traits.hxx:426
Concept to check if a type is an enumeration.
Definition type_traits.hxx:208
Concept to check if a type is a floating-point type.
Definition type_traits.hxx:295
Concept that checks whether a container supports insert with a value.
Definition type_traits.hxx:121
Concept to check if a container has a key_type member type.
Definition type_traits.hxx:147
Concept to check if a container has a mapped_type member type.
Definition type_traits.hxx:156
Concept that checks whether a container supports push_back.
Definition type_traits.hxx:105
Concept that checks whether a container supports push_front.
Definition type_traits.hxx:113
Concept that checks whether a container supports operator[] for a given key/index type U.
Definition type_traits.hxx:132
Concept to check if a type is an integral type.
Definition type_traits.hxx:282
Concept to check if a type is move constructible.
Definition type_traits.hxx:452
Concept to check if a type is an optional.
Definition type_traits.hxx:169
Concept to check if a type is a pair.
Definition type_traits.hxx:182
Concept to check if a type is a pointer.
Definition type_traits.hxx:269
Concept to check if a type is a reference.
Definition type_traits.hxx:195
Concept to check if two types are the same.
Definition type_traits.hxx:482
Concept to check if a type is a scoped enumeration (enum class).
Definition type_traits.hxx:236
Concept to check if a type is signed.
Definition type_traits.hxx:321
Concept to check if a type is std::string or std::wstring.
Definition type_traits.hxx:82
Concept to check if a type is trivially copyable.
Definition type_traits.hxx:400
Concept to check if a type has a trivial destructor.
Definition type_traits.hxx:413
Concept to check if a type is an unsigned enumeration.
Definition type_traits.hxx:221
Concept to check if a type is unsigned.
Definition type_traits.hxx:334
Concept to check if a variant type contains a specific type.
Definition type_traits.hxx:387
Concept to check if a type is a variant.
Definition type_traits.hxx:373
Main namespace for the StormByte library.
Modern C++20 concepts for type checking.