CeresEngine 0.2.0
A game development framework
Loading...
Searching...
No Matches
TypeTraits.hpp
Go to the documentation of this file.
1//
2// CeresEngine - A game development framework
3//
4// Created by Rogiel Sulzbach.
5// Copyright (c) 2018-2022 Rogiel Sulzbach. All rights reserved.
6//
7
8#pragma once
9
10#include <functional>
11#include <type_traits>
12
13namespace CeresEngine {
14
15 namespace impl {
17 template<typename Type, typename... Arguments>
18 struct is_explicitly_constructible_t : std::bool_constant<std::is_constructible<Type, Arguments...>::value> {};
19
21 template<typename Type, typename Argument, typename... Arguments>
23 : std::bool_constant<std::is_constructible<Type, Argument, Arguments...>::value && !std::is_convertible<Argument, Type>::value> {};
24 } // namespace impl
25
27 template<typename Type, typename... Arguments> constexpr bool is_explicitly_constructible = impl::is_explicitly_constructible_t<Type, Arguments...>::value;
28
29 namespace impl {
31 template<typename Type, typename... Arguments>
32 struct is_implicitly_constructible_t : std::bool_constant<std::is_constructible<Type, Arguments...>::value> {};
33
35 template<typename Type, typename Argument, typename... Arguments>
37 : std::bool_constant<std::is_constructible<Type, Argument, Arguments...>::value && std::is_convertible<Argument, Type>::value> {};
38 } // namespace impl
39
41 template<typename Type, typename... Arguments> constexpr bool is_implicitly_constructible = impl::is_implicitly_constructible_t<Type, Arguments...>::value;
42
43 // Non-capturing lambdas have a very interesting property : they can convert to an adequate function pointer,
44 // but they can also do so explicitly when you apply unary operator+ to them
45
46 template<typename T, typename = void> struct is_capturing_lambda : std::true_type {};
47 template<typename T> struct is_capturing_lambda<T, std::void_t<decltype(+std::declval<T>())>> : std::false_type {};
48
49 // ---------------------------------------------------------------------------------------------
50
51 template<typename T> struct size_of_t : std::integral_constant<std::size_t, sizeof(T)> {};
52 template<typename R, typename... Args> struct size_of_t<R (*)(Args...)> : std::integral_constant<std::size_t, sizeof(void*)> {};
53 template<> struct size_of_t<void> : std::integral_constant<std::size_t, 0> {};
54 template<typename T> constexpr auto size_of = size_of_t<T>::value;
55
56 // ---------------------------------------------------------------------------------------------
57
58 template<typename T, bool = std::is_pointer_v<T>> struct pointer_arity_t;
59 template<typename T> constexpr auto pointer_arity = pointer_arity_t<T>::value;
60
61 template<typename T> struct pointer_arity_t<T, false> : std::integral_constant<std::size_t, 0> {};
62 template<typename T> struct pointer_arity_t<T, true> : std::integral_constant<std::size_t, pointer_arity<std::remove_pointer_t<T>> + 1> {};
63
64 // ---------------------------------------------------------------------------------------------
65
66 template<typename T> struct array_length_t : std::integral_constant<std::size_t, 1> {};
67 template<typename T> constexpr auto array_length = array_length_t<T>::value;
68 template<typename T, std::size_t N> struct array_length_t<T[N]> : std::integral_constant<std::size_t, N * array_length<T>> {};
69
70 // ---------------------------------------------------------------------------------------------
71
72 template<typename T, bool = std::is_reference_v<T>, bool = std::is_pointer_v<T>, bool = std::is_array_v<T>> struct remove_all_cv_t;
73
74 template<typename T> using remove_all_cv = typename remove_all_cv_t<T>::type;
75
76 template<typename T> struct remove_all_cv_t<T, false, false, false> { using type = std::remove_cv_t<T>; };
77
78 template<typename T> struct remove_all_cv_t<T, true, false, false> {
79 constexpr static auto lref = std::is_lvalue_reference_v<T>;
81 using type = std::conditional_t<lref, std::add_lvalue_reference_t<U>, std::add_rvalue_reference_t<U>>;
82 };
83
84 template<typename T> struct remove_all_cv_t<T, false, true, false> {
86 using type = std::add_pointer_t<U>;
87 };
88
89 template<typename T> struct remove_all_cv_t<T, false, false, true> {
90 constexpr static auto extent = std::extent_v<T>;
92 using type = U[extent];
93 };
94
95 // ---------------------------------------------------------------------------------------------
96
97 template<typename T> using full_decay = remove_all_cv<std::decay_t<T>>;
98
99 // ---------------------------------------------------------------------------------------------
100
101 template<typename T, typename... Args> struct is_converting_constructor_t : std::false_type {};
102 template<typename T, typename Arg>
104 : std::conditional_t<!std::is_same_v<T, full_decay<Arg>> && std::is_convertible_v<Arg, T>, std::true_type, std::false_type> {};
105 template<typename T, typename... Args> constexpr auto is_converting_constructor = is_converting_constructor_t<T, Args...>::value;
106
107 // ---------------------------------------------------------------------------------------------
108
109 template<typename T> constexpr auto is_lvalue_const_reference = std::is_lvalue_reference_v<T>&& std::is_const_v<std::remove_reference_t<T>>;
110
111} // namespace CeresEngine
Represents a reflected C++ type. Can be used to get metadata from a C++ type.
Definition Type.hpp:32
Definition Application.hpp:19
constexpr bool is_implicitly_constructible
A type trait type that checks if Type is implicitly constructible from Arguments.
Definition TypeTraits.hpp:41
typename remove_all_cv_t< T >::type remove_all_cv
Definition TypeTraits.hpp:74
constexpr auto is_lvalue_const_reference
Definition TypeTraits.hpp:109
remove_all_cv< std::decay_t< T > > full_decay
Definition TypeTraits.hpp:97
constexpr auto array_length
Definition TypeTraits.hpp:67
constexpr bool is_explicitly_constructible
A type trait type that checks if Type is explicitly constructible from Arguments.
Definition TypeTraits.hpp:27
constexpr auto is_converting_constructor
Definition TypeTraits.hpp:105
constexpr auto size_of
Definition TypeTraits.hpp:54
constexpr size_t hash(const T &v)
Generates a hash for the provided type.
Definition Hash.hpp:25
constexpr auto pointer_arity
Definition TypeTraits.hpp:59
Definition Span.hpp:668
Definition TypeTraits.hpp:66
A type trait type that checks if Type is explicitly constructible from Arguments.
Definition TypeTraits.hpp:18
A type trait type that checks if Type is implicitly constructible from Arguments.
Definition TypeTraits.hpp:32
Definition TypeTraits.hpp:46
Definition TypeTraits.hpp:101
Definition TypeTraits.hpp:58
std::remove_cv_t< T > type
Definition TypeTraits.hpp:76
U[extent] type
Definition TypeTraits.hpp:92
remove_all_cv< std::remove_extent_t< T > > U
Definition TypeTraits.hpp:91
std::add_pointer_t< U > type
Definition TypeTraits.hpp:86
remove_all_cv< std::remove_pointer_t< T > > U
Definition TypeTraits.hpp:85
remove_all_cv< std::remove_reference_t< T > > U
Definition TypeTraits.hpp:80
std::conditional_t< lref, std::add_lvalue_reference_t< U >, std::add_rvalue_reference_t< U > > type
Definition TypeTraits.hpp:81
Definition TypeTraits.hpp:72
Definition TypeTraits.hpp:51