EnTT 3.15.0
Loading...
Searching...
No Matches
type_traits.hpp
1#ifndef ENTT_CORE_TYPE_TRAITS_HPP
2#define ENTT_CORE_TYPE_TRAITS_HPP
3
4#include <cstddef>
5#include <iterator>
6#include <tuple>
7#include <type_traits>
8#include <utility>
9#include "../config/config.h"
10#include "fwd.hpp"
11
12namespace entt {
13
18template<std::size_t N>
20 // unfortunately, doxygen cannot parse such a construct
21 : choice_t<N - 1>
22{};
23
25template<>
26struct choice_t<0> {};
27
32template<std::size_t N>
33inline constexpr choice_t<N> choice{};
34
43template<typename Type>
46 using type = Type;
47};
48
53template<typename Type>
55
60template<typename Type, typename = void>
61struct size_of: std::integral_constant<std::size_t, 0u> {};
62
64template<typename Type>
65struct size_of<Type, std::void_t<decltype(sizeof(Type))>>
66 // NOLINTNEXTLINE(bugprone-sizeof-expression)
67 : std::integral_constant<std::size_t, sizeof(Type)> {};
68
73template<typename Type>
74inline constexpr std::size_t size_of_v = size_of<Type>::value;
75
81template<typename Type, typename>
82using unpack_as_type = Type;
83
89template<auto Value, typename>
90inline constexpr auto unpack_as_value = Value;
91
96template<auto Value>
97using integral_constant = std::integral_constant<decltype(Value), Value>;
98
103template<id_type Value>
105
110template<typename... Type>
111struct type_list {
115 static constexpr auto size = sizeof...(Type);
116};
117
119template<std::size_t, typename>
121
128template<std::size_t Index, typename First, typename... Other>
129struct type_list_element<Index, type_list<First, Other...>>
130 : type_list_element<Index - 1u, type_list<Other...>> {};
131
137template<typename First, typename... Other>
138struct type_list_element<0u, type_list<First, Other...>> {
140 using type = First;
141};
142
148template<std::size_t Index, typename List>
150
152template<typename, typename>
154
161template<typename Type, typename First, typename... Other>
162struct type_list_index<Type, type_list<First, Other...>> {
164 using value_type = std::size_t;
166 static constexpr value_type value = 1u + type_list_index<Type, type_list<Other...>>::value;
167};
168
174template<typename Type, typename... Other>
175struct type_list_index<Type, type_list<Type, Other...>> {
176 static_assert(type_list_index<Type, type_list<Other...>>::value == sizeof...(Other), "Non-unique type");
178 using value_type = std::size_t;
180 static constexpr value_type value = 0u;
181};
182
187template<typename Type>
188struct type_list_index<Type, type_list<>> {
190 using value_type = std::size_t;
192 static constexpr value_type value = 0u;
193};
194
200template<typename Type, typename List>
202
209template<typename... Type, typename... Other>
211 return {};
212}
213
215template<typename...>
217
219template<>
223};
224
231template<typename... Type, typename... Other, typename... List>
232struct type_list_cat<type_list<Type...>, type_list<Other...>, List...> {
234 using type = typename type_list_cat<type_list<Type..., Other...>, List...>::type;
235};
236
241template<typename... Type>
242struct type_list_cat<type_list<Type...>> {
244 using type = type_list<Type...>;
245};
246
251template<typename... List>
252using type_list_cat_t = typename type_list_cat<List...>::type;
253
255namespace internal {
256
257template<typename...>
258struct type_list_unique;
259
260template<typename First, typename... Other, typename... Type>
261struct type_list_unique<type_list<First, Other...>, Type...>
262 : std::conditional_t<(std::is_same_v<First, Type> || ...), type_list_unique<type_list<Other...>, Type...>, type_list_unique<type_list<Other...>, Type..., First>> {};
263
264template<typename... Type>
265struct type_list_unique<type_list<>, Type...> {
266 using type = type_list<Type...>;
267};
268
269} // namespace internal
271
276template<typename List>
279 using type = typename internal::type_list_unique<List>::type;
280};
281
286template<typename List>
288
295template<typename List, typename Type>
297
303template<typename... Type, typename Other>
304struct type_list_contains<type_list<Type...>, Other>
305 : std::bool_constant<(std::is_same_v<Type, Other> || ...)> {};
306
312template<typename List, typename Type>
314
316template<typename...>
318
324template<typename... Type, typename... Other>
325struct type_list_diff<type_list<Type...>, type_list<Other...>> {
327 using type = type_list_cat_t<std::conditional_t<type_list_contains_v<type_list<Other...>, Type>, type_list<>, type_list<Type>>...>;
328};
329
334template<typename... List>
335using type_list_diff_t = typename type_list_diff<List...>::type;
336
338template<typename, template<typename...> class>
340
346template<typename... Type, template<typename...> class Op>
347struct type_list_transform<type_list<Type...>, Op> {
349 // NOLINTNEXTLINE(modernize-type-traits)
351};
352
358template<typename List, template<typename...> class Op>
360
365template<auto... Value>
370 static constexpr auto size = sizeof...(Value);
371};
372
374template<std::size_t, typename>
376
383template<std::size_t Index, auto Value, auto... Other>
384struct value_list_element<Index, value_list<Value, Other...>>
385 : value_list_element<Index - 1u, value_list<Other...>> {};
386
392template<auto Value, auto... Other>
393struct value_list_element<0u, value_list<Value, Other...>> {
395 using type = decltype(Value);
397 static constexpr auto value = Value;
398};
399
405template<std::size_t Index, typename List>
407
413template<std::size_t Index, typename List>
415
417template<auto, typename>
419
426template<auto Value, auto First, auto... Other>
427struct value_list_index<Value, value_list<First, Other...>> {
429 using value_type = std::size_t;
431 static constexpr value_type value = 1u + value_list_index<Value, value_list<Other...>>::value;
432};
433
439template<auto Value, auto... Other>
440struct value_list_index<Value, value_list<Value, Other...>> {
441 static_assert(value_list_index<Value, value_list<Other...>>::value == sizeof...(Other), "Non-unique type");
443 using value_type = std::size_t;
445 static constexpr value_type value = 0u;
446};
447
452template<auto Value>
453struct value_list_index<Value, value_list<>> {
455 using value_type = std::size_t;
457 static constexpr value_type value = 0u;
458};
459
465template<auto Value, typename List>
467
474template<auto... Value, auto... Other>
476 return {};
477}
478
480template<typename...>
482
484template<>
488};
489
496template<auto... Value, auto... Other, typename... List>
497struct value_list_cat<value_list<Value...>, value_list<Other...>, List...> {
499 using type = typename value_list_cat<value_list<Value..., Other...>, List...>::type;
500};
501
506template<auto... Value>
507struct value_list_cat<value_list<Value...>> {
509 using type = value_list<Value...>;
510};
511
516template<typename... List>
517using value_list_cat_t = typename value_list_cat<List...>::type;
518
520template<typename>
522
528template<auto Value, auto... Other>
529struct value_list_unique<value_list<Value, Other...>> {
531 using type = std::conditional_t<
532 ((Value == Other) || ...),
533 typename value_list_unique<value_list<Other...>>::type,
535};
536
538template<>
543
548template<typename Type>
550
557template<typename List, auto Value>
559
565template<auto... Value, auto Other>
566struct value_list_contains<value_list<Value...>, Other>
567 : std::bool_constant<((Value == Other) || ...)> {};
568
574template<typename List, auto Value>
576
578template<typename...>
580
586template<auto... Value, auto... Other>
587struct value_list_diff<value_list<Value...>, value_list<Other...>> {
589 using type = value_list_cat_t<std::conditional_t<value_list_contains_v<value_list<Other...>, Value>, value_list<>, value_list<Value>>...>;
590};
591
596template<typename... List>
597using value_list_diff_t = typename value_list_diff<List...>::type;
598
600template<typename, typename>
601struct is_applicable: std::false_type {};
602
609template<typename Func, template<typename...> class Tuple, typename... Args>
610struct is_applicable<Func, Tuple<Args...>>: std::is_invocable<Func, Args...> {};
611
618template<typename Func, template<typename...> class Tuple, typename... Args>
619struct is_applicable<Func, const Tuple<Args...>>: std::is_invocable<Func, Args...> {};
620
626template<typename Func, typename Args>
628
630template<typename, typename, typename>
631struct is_applicable_r: std::false_type {};
632
640template<typename Ret, typename Func, typename... Args>
641struct is_applicable_r<Ret, Func, std::tuple<Args...>>: std::is_invocable_r<Ret, Func, Args...> {};
642
650template<typename Ret, typename Func, typename Args>
652
658template<typename Type, typename = void>
659struct is_complete: std::false_type {};
660
662template<typename Type>
663struct is_complete<Type, std::void_t<decltype(sizeof(Type))>>: std::true_type {};
664
669template<typename Type>
671
677template<typename Type, typename = void>
678struct is_iterator: std::false_type {};
679
681namespace internal {
682
683template<typename, typename = void>
684struct has_iterator_category: std::false_type {};
685
686template<typename Type>
687struct has_iterator_category<Type, std::void_t<typename std::iterator_traits<Type>::iterator_category>>: std::true_type {};
688
689} // namespace internal
691
693template<typename Type>
694struct is_iterator<Type, std::enable_if_t<!std::is_void_v<std::remove_cv_t<std::remove_pointer_t<Type>>>>>
695 : internal::has_iterator_category<Type> {};
696
701template<typename Type>
703
709template<typename Type>
711 : std::bool_constant<std::is_empty_v<Type> && !std::is_final_v<Type>> {};
712
717template<typename Type>
719
725template<typename Type, typename = void>
726struct is_transparent: std::false_type {};
727
729template<typename Type>
730struct is_transparent<Type, std::void_t<typename Type::is_transparent>>: std::true_type {};
731
736template<typename Type>
738
740namespace internal {
741
742template<typename, typename = void>
743struct has_tuple_size_value: std::false_type {};
744
745template<typename Type>
746struct has_tuple_size_value<Type, std::void_t<decltype(std::tuple_size<const Type>::value)>>: std::true_type {};
747
748template<typename, typename = void>
749struct has_value_type: std::false_type {};
750
751template<typename Type>
752struct has_value_type<Type, std::void_t<typename Type::value_type>>: std::true_type {};
753
754template<typename>
755[[nodiscard]] constexpr bool dispatch_is_equality_comparable();
756
757template<typename Type, std::size_t... Index>
758[[nodiscard]] constexpr bool unpack_maybe_equality_comparable(std::index_sequence<Index...>) {
759 return (dispatch_is_equality_comparable<std::tuple_element_t<Index, Type>>() && ...);
760}
761
762template<typename>
763[[nodiscard]] constexpr bool maybe_equality_comparable(char) {
764 return false;
765}
766
767template<typename Type>
768[[nodiscard]] constexpr auto maybe_equality_comparable(int) -> decltype(std::declval<Type>() == std::declval<Type>()) {
769 return true;
770}
771
772template<typename Type>
773[[nodiscard]] constexpr bool dispatch_is_equality_comparable() {
774 // NOLINTBEGIN(modernize-use-transparent-functors)
775 if constexpr(std::is_array_v<Type>) {
776 return false;
778 if constexpr(has_tuple_size_value<Type>::value) {
779 return maybe_equality_comparable<Type>(0) && unpack_maybe_equality_comparable<Type>(std::make_index_sequence<std::tuple_size<Type>::value>{});
780 } else {
781 return maybe_equality_comparable<Type>(0);
782 }
783 } else if constexpr(has_value_type<Type>::value) {
784 if constexpr(is_iterator_v<Type> || std::is_same_v<typename Type::value_type, Type> || dispatch_is_equality_comparable<typename Type::value_type>()) {
785 return maybe_equality_comparable<Type>(0);
786 } else {
787 return false;
788 }
789 } else {
790 return maybe_equality_comparable<Type>(0);
791 }
792 // NOLINTEND(modernize-use-transparent-functors)
793}
794
795} // namespace internal
797
803template<typename Type>
804struct is_equality_comparable: std::bool_constant<internal::dispatch_is_equality_comparable<Type>()> {};
805
807template<typename Type>
809
814template<typename Type>
816
822template<typename To, typename From>
825 using type = std::remove_const_t<To>;
826};
827
829template<typename To, typename From>
830struct constness_as<To, const From> {
832 using type = const To;
833};
834
840template<typename To, typename From>
842
847template<typename Member>
849 static_assert(std::is_member_pointer_v<Member>, "Invalid pointer type to non-static member object or function");
850
851 template<typename Class, typename Ret, typename... Args>
852 static Class *clazz(Ret (Class::*)(Args...));
853
854 template<typename Class, typename Ret, typename... Args>
855 static Class *clazz(Ret (Class::*)(Args...) const);
856
857 template<typename Class, typename Type>
858 static Class *clazz(Type Class::*);
859
860public:
862 using type = std::remove_pointer_t<decltype(clazz(std::declval<Member>()))>;
863};
864
869template<typename Member>
871
877template<std::size_t Index, typename Candidate>
879 template<typename Ret, typename... Args>
880 static constexpr type_list<Args...> pick_up(Ret (*)(Args...));
881
882 template<typename Ret, typename Class, typename... Args>
883 static constexpr type_list<Args...> pick_up(Ret (Class ::*)(Args...));
884
885 template<typename Ret, typename Class, typename... Args>
886 static constexpr type_list<Args...> pick_up(Ret (Class ::*)(Args...) const);
887
888 template<typename Type, typename Class>
889 static constexpr type_list<Type> pick_up(Type Class ::*);
890
891 template<typename Type>
892 static constexpr decltype(pick_up(&Type::operator())) pick_up(Type &&);
893
894public:
897};
898
904template<std::size_t Index, typename Candidate>
906
907} // namespace entt
908
909template<typename... Type>
910struct std::tuple_size<entt::type_list<Type...>>: std::integral_constant<std::size_t, entt::type_list<Type...>::size> {};
911
912template<std::size_t Index, typename... Type>
913struct std::tuple_element<Index, entt::type_list<Type...>>: entt::type_list_element<Index, entt::type_list<Type...>> {};
914
915template<auto... Value>
916struct std::tuple_size<entt::value_list<Value...>>: std::integral_constant<std::size_t, entt::value_list<Value...>::size> {};
917
918template<std::size_t Index, auto... Value>
919struct std::tuple_element<Index, entt::value_list<Value...>>: entt::value_list_element<Index, entt::value_list<Value...>> {};
920
921#endif
Extracts the class of a non-static member object or function.
std::remove_pointer_t< decltype(clazz(std::declval< Member >()))> type
The class of the given non-static member object or function.
Extracts the n-th argument of a callable type.
type_list_element_t< Index, decltype(pick_up(std::declval< Candidate >()))> type
N-th argument of the callable type.
EnTT default namespace.
Definition dense_map.hpp:22
typename member_class< Member >::type member_class_t
Helper type.
constexpr bool is_equality_comparable_v
Helper variable template.
constexpr choice_t< N > choice
Variable template for the choice trick.
std::integral_constant< decltype(Value), Value > integral_constant
Wraps a static constant.
typename constness_as< To, From >::type constness_as_t
Alias template to facilitate the transcription of the constness.
constexpr std::size_t size_of_v
Helper variable template.
typename nth_argument< Index, Candidate >::type nth_argument_t
Helper type.
typename type_list_cat< List... >::type type_list_cat_t
Helper type.
typename type_list_transform< List, Op >::type type_list_transform_t
Helper type.
typename value_list_unique< Type >::type value_list_unique_t
Helper type.
constexpr std::size_t type_list_index_v
Helper variable template.
constexpr bool is_iterator_v
Helper variable template.
typename value_list_cat< List... >::type value_list_cat_t
Helper type.
constexpr auto value_list_element_v
Helper type.
typename type_list_diff< List... >::type type_list_diff_t
Helper type.
constexpr bool is_applicable_v
Helper variable template.
constexpr bool is_transparent_v
Helper variable template.
typename type_identity< Type >::type type_identity_t
Helper type.
constexpr bool is_complete_v
Helper variable template.
constexpr bool type_list_contains_v
Helper variable template.
typename type_list_element< Index, List >::type type_list_element_t
Helper type.
constexpr std::size_t value_list_index_v
Helper variable template.
typename type_list_unique< List >::type type_list_unique_t
Helper type.
constexpr type_list< Type..., Other... > operator+(type_list< Type... >, type_list< Other... >)
Concatenates multiple type lists.
typename value_list_element< Index, List >::type value_list_element_t
Helper type.
constexpr bool value_list_contains_v
Helper variable template.
typename value_list_diff< List... >::type value_list_diff_t
Helper type.
Type unpack_as_type
Using declaration to be used to repeat the same type a number of times equal to the size of a given p...
integral_constant< Value > tag
Alias template to facilitate the creation of named values.
constexpr bool is_applicable_r_v
Helper variable template.
constexpr auto unpack_as_value
Helper variable template to be used to repeat the same value a number of times equal to the size of a...
constexpr bool is_ebco_eligible_v
Helper variable template.
Utility class to disambiguate overloaded functions.
const To type
The type resulting from the transcription of the constness.
Transcribes the constness of a type to another type.
std::remove_const_t< To > type
The type resulting from the transcription of the constness.
Same as std::is_invocable_r, but with tuples for arguments.
Same as std::is_invocable, but with tuples.
Provides the member constant value to true if a given type is complete, false otherwise.
Provides the member constant value to true if a given type is both an empty and non-final class,...
Provides the member constant value to true if a given type is equality comparable,...
Provides the member constant value to true if a given type is an iterator, false otherwise.
Provides the member constant value to true if Type::is_transparent is valid and denotes a type,...
A type-only sizeof wrapper that returns 0 where sizeof complains.
Identity type trait.
Type type
Identity type.
typename type_list_cat< type_list< Type..., Other... >, List... >::type type
A type list composed by the types of all the type lists.
type_list< Type... > type
A type list composed by the types of all the type lists.
Concatenates multiple type lists.
type_list<> type
A type list composed by the types of all the type lists.
Primary template isn't defined on purpose.
Provides the member constant value to true if a type list contains a given type, false otherwise.
type_list_cat_t< std::conditional_t< type_list_contains_v< type_list< Other... >, Type >, type_list<>, type_list< Type > >... > type
A type list that is the difference between the two type lists.
Primary template isn't defined on purpose.
Primary template isn't defined on purpose.
static constexpr value_type value
Compile-time position of the given type in the sublist.
static constexpr value_type value
Compile-time position of the given type in the sublist.
static constexpr value_type value
Compile-time position of the given type in the sublist.
std::size_t value_type
Unsigned integer type.
Primary template isn't defined on purpose.
type_list< typename Op< Type >::type... > type
Resulting type list after applying the transform function.
Primary template isn't defined on purpose.
Removes duplicates types from a type list.
typename internal::type_list_unique< List >::type type
A type list without duplicate types.
A class to use to push around lists of types, nothing more.
static constexpr auto size
type_list type
Type list type.
typename value_list_cat< value_list< Value..., Other... >, List... >::type type
A value list composed by the values of all the value lists.
value_list< Value... > type
A value list composed by the values of all the value lists.
Concatenates multiple value lists.
value_list<> type
A value list composed by the values of all the value lists.
Primary template isn't defined on purpose.
Provides the member constant value to true if a value list contains a given value,...
value_list_cat_t< std::conditional_t< value_list_contains_v< value_list< Other... >, Value >, value_list<>, value_list< Value > >... > type
A value list that is the difference between the two value lists.
Primary template isn't defined on purpose.
Primary template isn't defined on purpose.
static constexpr value_type value
Compile-time position of the given value in the sublist.
static constexpr value_type value
Compile-time position of the given value in the sublist.
std::size_t value_type
Unsigned integer type.
static constexpr value_type value
Compile-time position of the given type in the sublist.
Primary template isn't defined on purpose.
std::conditional_t<((Value==Other)||...), typename value_list_unique< value_list< Other... > >::type, value_list_cat_t< value_list< Value >, typename value_list_unique< value_list< Other... > >::type > > type
A value list without duplicate types.
value_list<> type
A value list without duplicate types.
Primary template isn't defined on purpose.
A class to use to push around lists of constant values, nothing more.
static constexpr auto size
value_list type
Value list type.