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
13
14#include <type_traits>
15
16namespace CeresEngine {
17
18 namespace internal {
19
20 template<typename F> struct function_traits_helper : function_traits_helper<decltype(&F::operator())> {};
21
22 template<typename R, typename... Args> struct function_traits_helper<R (*)(Args...)> {
24
25 using is_const = std::false_type;
26 using is_volatile = std::false_type;
27 using is_lrefthis = std::false_type;
28 using is_rrefthis = std::false_type;
29 using is_noexcept = std::false_type;
30
31 using result_type = R;
32 using args = MPL::TypeList<Args...>;
33 static constexpr std::size_t arity = MPL::size<args>();
34 template<std::size_t i> using arg = MPL::Nth<i, args>;
35
36 using base_signature = R(Args...);
37 using uniformSignature = R(Args...);
38 };
39
40 template<typename R, typename... Args> struct function_traits_helper<R (*)(Args...) noexcept> : function_traits_helper<R (*)(Args...)> {
41 using is_noexcept = std::true_type;
42 using base_signature = R(Args...) noexcept;
44 };
45
47 using class_type = C;
48 using uniformSignature = R(C&, Args...);
49 };
50
51 template<typename C, typename R, typename... Args>
52 struct function_traits_helper<R (C::*)(Args...) noexcept> : function_traits_helper<R (*)(Args...) noexcept> {
53 using class_type = C;
54 using uniformSignature = R(C&, Args...) noexcept;
55 };
56
58 using is_lrefthis = std::true_type;
59 };
60
61 template<typename C, typename R, typename... Args> struct function_traits_helper<R (C::*)(Args...) &&> : function_traits_helper<R (C::*)(Args...)> {
62 using is_rrefthis = std::true_type;
63 using uniformSignature = R(C&&, Args...);
64 };
65
66 template<typename C, typename R, typename... Args> struct function_traits_helper<R (C::*)(Args...) const> : function_traits_helper<R (*)(Args...)> {
67 using class_type = C;
68 using is_const = std::true_type;
69 using uniformSignature = R(C const&, Args...);
70 };
71
72 template<typename C, typename R, typename... Args>
73 struct function_traits_helper<R (C::*)(Args...) const noexcept> : function_traits_helper<R (*)(Args...) noexcept> {
74 using class_type = C;
75 using is_const = std::true_type;
76 using uniformSignature = R(C const&, Args...) noexcept;
77 };
78
79 template<typename C, typename R, typename... Args>
80 struct function_traits_helper<R (C::*)(Args...) const&> : function_traits_helper<R (C::*)(Args...) const> {
81 using is_lrefthis = std::true_type;
82 };
83
84 template<typename C, typename R, typename... Args>
85 struct function_traits_helper<R (C::*)(Args...) const&&> : function_traits_helper<R (C::*)(Args...) const> {
86 using is_rrefthis = std::true_type;
87 using uniformSignature = R(C const&&, Args...);
88 };
89
90 template<typename C, typename R, typename... Args> struct function_traits_helper<R (C::*)(Args...) volatile> : function_traits_helper<R (*)(Args...)> {
91 using class_type = C;
92 using is_volatile = std::true_type;
93 using uniformSignature = R(volatile C&, Args...);
94 };
95
96 template<typename C, typename R, typename... Args>
97 struct function_traits_helper<R (C::*)(Args...) volatile&> : function_traits_helper<R (C::*)(Args...) volatile> {
98 using is_lrefthis = std::true_type;
99 };
100
101 template<typename C, typename R, typename... Args>
102 struct function_traits_helper<R (C::*)(Args...) volatile&&> : function_traits_helper<R (C::*)(Args...) volatile> {
103 using is_rrefthis = std::true_type;
104 using uniformSignature = R(volatile C&&, Args...);
105 };
106
107 template<typename C, typename R, typename... Args>
108 struct function_traits_helper<R (C::*)(Args...) const volatile> : function_traits_helper<R (*)(Args...)> {
109 using class_type = C;
110 using is_const = std::true_type;
111 using is_volatile = std::true_type;
112 using uniformSignature = R(C const volatile&, Args...);
113 };
114
115 template<typename C, typename R, typename... Args>
116 struct function_traits_helper<R (C::*)(Args...) const volatile&> : function_traits_helper<R (C::*)(Args...) const volatile> {
117 using is_lrefthis = std::true_type;
118 };
119
120 template<typename C, typename R, typename... Args>
121 struct function_traits_helper<R (C::*)(Args...) const volatile&&> : function_traits_helper<R (C::*)(Args...) const volatile> {
122 using is_rrefthis = std::true_type;
123 using uniformSignature = R(C const volatile&&, Args...);
124 };
125
126 template<typename F> struct function_traits_helper<std::function<F>> : function_traits_helper<F> {};
127
128 } // namespace internal
129
130 template<typename F> struct function_traits : internal::function_traits_helper<std::decay_t<F>> {};
131
132 // Factory
133 template<typename F> using function_type = std::function<typename function_traits<F>::base_signature>;
134 template<typename F> inline function_type<F> make_function(F&& f) { return function_type<F>{std::forward<F>(f)}; }
135
136 // ---------------------------------------------------------------------------------------------
137
138 namespace mpl {
139
140 // identity
141 template<typename T> struct identity {
142 using type = T;
143 };
144
145 // index sequence
146 template<std::size_t... Ints> struct index_sequence {
147 using size = std::integral_constant<std::size_t, sizeof...(Ints)>;
148 };
149
150 template<std::size_t N, std::size_t... Ints> struct make_index_sequence : make_index_sequence<N - 1, N - 1, Ints...> {};
151 template<std::size_t... Ints> struct make_index_sequence<0, Ints...> : identity<index_sequence<Ints...>> {};
152 template<typename... Args> struct index_sequence_for : make_index_sequence<sizeof...(Args)> {};
153 template<typename... Args> struct index_sequence_for<MPL::TypeList<Args...>> : index_sequence_for<Args...> {};
154 template<typename... Args> struct index_sequence_for<std::tuple<Args...>> : index_sequence_for<Args...> {};
155 template<typename... Args> using index_sequence_for_t = typename index_sequence_for<Args...>::type;
156
157 } // namespace mpl
158
159 // ---------------------------------------------------------------------------------------------
160
161 namespace internal {
162 template<class T, class = decltype(std::declval<T>() == std::declval<T>())> std::true_type HasOperatorEqualsTest(const T&);
163 std::false_type HasOperatorEqualsTest(...);
164 } // namespace internal
165
166 template<class T> static constexpr bool HasOperatorEquals = decltype(internal::HasOperatorEqualsTest(std::declval<T>()))::value;
167
168} // namespace CeresEngine
std::tuple_element_t< TIndex, Tuple< TTypeList > > Nth
"Nth" element of a type list.
Definition TypeListOps.hpp:29
std::true_type HasOperatorEqualsTest(const T &)
typename index_sequence_for< Args... >::type index_sequence_for_t
Definition TypeTraits.hpp:155
Definition Application.hpp:19
std::function< typename function_traits< F >::base_signature > function_type
Definition TypeTraits.hpp:133
function_type< F > make_function(F &&f)
Definition TypeTraits.hpp:134
constexpr size_t hash(const T &v)
Generates a hash for the provided type.
Definition Hash.hpp:25
Definition Span.hpp:668
Compile-time list of types.
Definition TypeList.hpp:15
Definition TypeTraits.hpp:130
R(Args...) noexcept uniformSignature
Definition TypeTraits.hpp:43
R(Args...) noexcept base_signature
Definition TypeTraits.hpp:42
MPL::Nth< i, args > arg
Definition TypeTraits.hpp:34
std::false_type is_lrefthis
Definition TypeTraits.hpp:27
R(Args...) base_signature
Definition TypeTraits.hpp:36
std::false_type is_const
Definition TypeTraits.hpp:25
std::false_type is_noexcept
Definition TypeTraits.hpp:29
std::false_type is_volatile
Definition TypeTraits.hpp:26
R(Args...) uniformSignature
Definition TypeTraits.hpp:37
std::false_type is_rrefthis
Definition TypeTraits.hpp:28
R(C const volatile &&, Args...) uniformSignature
Definition TypeTraits.hpp:123
R(C &&, Args...) uniformSignature
Definition TypeTraits.hpp:63
std::true_type is_rrefthis
Definition TypeTraits.hpp:62
R(C const &, Args...) uniformSignature
Definition TypeTraits.hpp:69
R(C const &&, Args...) uniformSignature
Definition TypeTraits.hpp:87
R(C const &, Args...) noexcept uniformSignature
Definition TypeTraits.hpp:76
R(C const volatile &, Args...) uniformSignature
Definition TypeTraits.hpp:112
R(C &, Args...) noexcept uniformSignature
Definition TypeTraits.hpp:54
R(volatile C &, Args...) uniformSignature
Definition TypeTraits.hpp:93
R(volatile C &&, Args...) uniformSignature
Definition TypeTraits.hpp:104
R(C &, Args...) uniformSignature
Definition TypeTraits.hpp:48
std::true_type is_lrefthis
Definition TypeTraits.hpp:58
Definition TypeTraits.hpp:141
T type
Definition TypeTraits.hpp:142
Definition TypeTraits.hpp:152
Definition TypeTraits.hpp:146
std::integral_constant< std::size_t, sizeof...(Ints)> size
Definition TypeTraits.hpp:147
Definition TypeTraits.hpp:150