Newer
Older
* Copyright (c) 2012-2025, OpenGeoSys Community (http://www.opengeosys.org)
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
* Distributed under a Modified BSD License.
* See accompanying file LICENSE.txt or
* http://www.opengeosys.org/project/license
*
*/
#pragma once
#include <algorithm>
#include <array>
#include <utility>
#include "Algorithm.h"
namespace BaseLib::TMP
{
// Concat ----------------------------------------------------------------------
/**
* Concatenates two lists of types.
*/
template <typename List1, typename List2>
struct Concat;
template <template <typename... /*Types*/> typename List, typename... Types1,
typename... Types2>
struct Concat<List<Types1...>, List<Types2...>>
{
using type = List<Types1..., Types2...>;
};
/// \copydoc BaseLib::TMP::Concat
template <typename List1, typename List2>
using Concat_t = typename Concat<List1, List2>::type;
// filter- ---------------------------------------------------------------------
/// A list of types.
template <typename...>
struct List
{
};
namespace detail
{
template <typename Pred, template <typename...> typename SomeListOfTypes>
constexpr List<> filter(Pred, SomeListOfTypes<>*)
{
return {};
}
template <typename Pred, template <typename...> typename SomeListOfTypes,
typename Head, typename... Tail>
constexpr decltype(auto) filter(Pred pred, SomeListOfTypes<Head, Tail...>*)
{
constexpr auto tail_arg = static_cast<List<Tail...>*>(nullptr);
using TailFiltered = decltype(filter(pred, tail_arg));
using Result =
std::conditional_t<pred((Head*)nullptr),
Concat_t<List<Head>, TailFiltered>, TailFiltered>;
return Result{};
}
} // namespace detail
/**
* Filters the given list of types via the given predicate.
*
* Keeps all elements for which \c pred is true.
*
* The function signature of \c pred should be <tt>bool pred(Type*)</tt>, where
* \c Type is a member of the given list of types.
*/
template <typename List, typename Pred>
decltype(auto) filter(Pred pred)
{
return detail::filter(pred, static_cast<List*>(nullptr));
}
// Map -------------------------------------------------------------------------
/**
* Turns a list of types into a list of different types via the provided type
* mapping.
*/
template <template <typename /*FromType*/> typename MapFromTypeToType,
typename List>
struct Map;
template <template <typename /*FromType*/> typename MapFromTypeToType,
template <typename... /*Types*/> typename List, typename... Types>
struct Map<MapFromTypeToType, List<Types...>>
{
using type = List<MapFromTypeToType<Types>...>;
};
/// \copydoc BaseLib::TMP::Map
template <template <typename /*FromType*/> typename MapFromTypeToType,
typename List>
using Map_t = typename Map<MapFromTypeToType, List>::type;
// map_to_array ----------------------------------------------------------------
namespace detail
{
template <template <typename... /*Types*/> typename List, typename Function,
typename Head, typename... Tail>
constexpr decltype(auto) map_to_array(Function&& f, List<Head, Tail...>*)
{
using CommonType =
std::common_type_t<std::invoke_result_t<Function, Head*>,
std::invoke_result_t<Function, Tail*>...>;
return std::array<CommonType, 1 + sizeof...(Tail)>{f((Head*)nullptr),
f((Tail*)nullptr)...};
}
template <template <typename... /*Types*/> typename List, typename Function>
constexpr std::array<std::nullptr_t, 0> map_to_array(Function&& /*f*/, List<>*)
{
return {};
}
} // namespace detail
/**
* Applies the given function to a list of types returning the array of results.
*
* The function signature should be <tt>ReturnType f(Type*)</tt>, where \c Type
* is a member of the given list of types and \c ReturnType is the same for all
* overloads of \c f.
*/
template <typename List, typename Function>
constexpr decltype(auto) map_to_array(Function&& f)
{
return detail::map_to_array(std::forward<Function>(f), (List*)nullptr);
}
// foreach ---------------------------------------------------------------------
namespace detail
{
template <template <typename... /*Types*/> typename List, typename Function,
typename... Types>
void foreach (Function&& f, List<Types...>*)
{
(..., f((Types*)nullptr));
}
} // namespace detail
/**
* Applies the given function to a list of types.
*
* The function signature should be <tt>void f(Type*)</tt>, where \c Type is a
* member of the given list of types.
*/
template <typename List, typename Function>
void foreach (Function&& f)
{
detail::foreach (std::forward<Function>(f), (List*)nullptr);
}
// contains --------------------------------------------------------------------
/// Returns if \c Type is contained in the given \c List of types.
template <typename List, typename Type>
constexpr bool contains()
{
auto pred_is_same = []<typename OtherType>(OtherType*)
{ return std::is_same_v<Type, OtherType>; };
return any_of(map_to_array<List>(pred_is_same));
}
} // namespace BaseLib::TMP