wuzp
Wrong Unit? Zero Points! Compile-time checks for units, coordinate systems and points vs. vectors.
Loading...
Searching...
No Matches
helpers.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <array>
4#include <concepts>
5#include <cstddef>
6#include <tuple>
7#include <type_traits>
8#include <utility>
9
10namespace wuzp {
11
20template <typename T>
21struct FailOnInstantiation : public std::false_type {};
22
23template <int Size, typename T>
24using HomogeneousTuple = decltype(std::tuple_cat(std::declval<std::array<T, std::size_t{Size}>>()));
25
26template <typename... Tuples>
27struct Cat {
28 using type = decltype(std::tuple_cat(std::declval<Tuples>()...));
29};
30
31template <template <typename...> typename X, typename T>
33template <template <typename...> typename X, typename... Ts>
34struct ExtractAndApply<X, std::tuple<Ts...>> {
35 using type = typename X<Ts...>::type;
36};
37
38template <bool>
39struct SelectIf {
40 template <typename T>
41 using type = std::tuple<T>;
42};
43template <>
44struct SelectIf<false> {
45 template <typename T>
46 using type = std::tuple<>;
47};
48
50template <bool ChooseTypeA, typename TypeA, typename TypeB>
52template <typename TypeA, typename TypeB>
53struct IfThenElse<true, TypeA, TypeB> {
54 using type = TypeA;
55};
56template <typename TypeA, typename TypeB>
57struct IfThenElse<false, TypeA, TypeB> {
58 using type = TypeB;
59};
60
62template <template <typename...> typename Template, typename... Args>
63concept Specializable = requires { typename Template<Args...>; };
64
68template <typename Class, typename... Args>
69concept CopiesOrMoves = sizeof...(Args) == 1 && (std::same_as<std::remove_cvref_t<Args>, Class> && ...);
70
73template <typename T, template <typename...> typename Template>
74struct IsSpecialization : std::false_type {};
75// Template2<Args...> is only formed if it is valid.
76template <template <typename...> typename Template1, template <typename...> typename Template2, typename... Args>
77 requires Specializable<Template2, Args...>
78struct IsSpecialization<Template1<Args...>, Template2> : std::is_same<Template1<Args...>, Template2<Args...>> {};
79template <int Start, int End, int Increment, typename Tuple, typename IndexSequence>
81template <int Start, int End, int Increment, typename... Ts, size_t... Is>
82struct GetSubsetImpl<Start, End, Increment, std::tuple<Ts...>, std::index_sequence<Is...>> {
83 using type = typename Cat<typename SelectIf<(Is >= Start) && (Is < End) && ((Is - Start) % Increment == 0)>::template type<Ts>...>::type;
84};
85
86template <int Start, int End, int Increment, typename Tuple>
88
89template <typename T, typename... Ts>
90inline constexpr bool areSame = std::conjunction_v<std::is_same<T, Ts>...>;
91
96template <typename ScalarT_, typename ValueT_>
97inline constexpr bool isConvertedToValueType = std::is_same_v<std::common_type_t<ScalarT_, ValueT_>, ValueT_>;
98
99template <typename... Ts>
101 static constexpr bool value = areSame<Ts...>;
102};
103template <typename... Ts>
104struct IsHomogeneous<std::tuple<Ts...>> {
105 static constexpr bool value = IsHomogeneous<Ts...>::value;
106};
107
109template <typename T>
111
113template <typename Implementation>
115
116namespace internal {
117
122template <typename DerivedT_, typename BaseT_>
123class Crtp : public BaseT_ {
124 protected:
125 const DerivedT_& derived() const { return static_cast<const DerivedT_&>(*this); }
126};
127
128} // namespace internal
129
133template <typename First, typename... Rest>
134void applyCommas(First&& first, Rest&&... rest) {
135 static_cast<void>((std::forward<First>(first), ..., std::forward<Rest>(rest)));
136}
137
138} // namespace wuzp
Whether constructing a Class from the arguments copies or moves it, i.e. whether they are a single Cl...
Definition helpers.hpp:69
Whether the template can be specialized with the arguments, e.g. whether their number fits and they s...
Definition helpers.hpp:63
GetSubsetImpl< Start, End, Increment, Tuple, std::make_index_sequence< std::tuple_size< Tuple >::value > > GetSubset
Definition helpers.hpp:87
decltype(std::tuple_cat(std::declval< std::array< T, std::size_t{Size}> >())) HomogeneousTuple
Definition helpers.hpp:24
constexpr bool isConvertedToValueType
Whether an arithmetic scalar of the type ScalarT_ is converted into the value type ValueT_: if their ...
Definition helpers.hpp:97
constexpr bool areSame
Definition helpers.hpp:90
void applyCommas(First &&first, Rest &&... rest)
Applies the comma operator from left to right: applyCommas(a, b, c) evaluates ((a,...
Definition helpers.hpp:134
Type wrapper which is used to access traits partially specialized in the implementation header files.
Definition helpers.hpp:114
decltype(std::tuple_cat(std::declval< Tuples >()...)) type
Definition helpers.hpp:28
False for every type, so that a static_assert on it fails only when the template that names it is ins...
Definition helpers.hpp:21
typename Cat< typename SelectIf<(Is >=Start) &&(Is< End) &&((Is - Start) % Increment==0)>::template type< Ts >... >::type type
Definition helpers.hpp:83
Trait to select one type or another depending on a boolean. Note that both types must be defined even...
Definition helpers.hpp:51
static constexpr bool value
Definition helpers.hpp:101
Whether T is a specialization of Template.
Definition helpers.hpp:74
std::tuple< T > type
Definition helpers.hpp:41
Wrapper for partial specializations of trait for values types.
Definition helpers.hpp:110