OpenVDB: TypeList.h Source File

OpenVDB 8.1.0

TypeList.h
Go to the documentation of this file.
1 // Copyright Contributors to the OpenVDB Project
2 // SPDX-License-Identifier: MPL-2.0
3
11
12 #ifndef OPENVDB_TYPELIST_HAS_BEEN_INCLUDED
13 #define OPENVDB_TYPELIST_HAS_BEEN_INCLUDED
14
15 #include "version.h"
16
17 #include <tuple>
18 #include <type_traits>
19
20 namespace openvdb {
21 OPENVDB_USE_VERSION_NAMESPACE
22 namespace OPENVDB_VERSION_NAME {
23
25
26 template<typename... Ts> struct TypeList; // forward declaration
27
28 namespace typelist_internal {
29
30 // Implementation details of @c TypeList
31
34 struct NullType {};
35
36
43 template<typename ListT, size_t Idx, typename = void> struct TSGetElementImpl;
44
48 template<typename... Ts, size_t Idx>
49 struct TSGetElementImpl<TypeList<Ts...>, Idx,
50 typename std::enable_if<(Idx < sizeof...(Ts) && sizeof...(Ts))>::type> {
51 using type = typename std::tuple_element<Idx, std::tuple<Ts...>>::type;
52 };
53
58 template<typename... Ts, size_t Idx>
59 struct TSGetElementImpl<TypeList<Ts...>, Idx,
60 typename std::enable_if<!(Idx < sizeof...(Ts) && sizeof...(Ts))>::type> {
61 using type = NullType;
62 };
63
64
77 template <typename ListT, typename T, size_t=0>
78 struct TSHasTypeImpl;
79
85 template <typename T, size_t Idx>
86 struct TSHasTypeImpl<TypeList<>, T, Idx> {
87 static constexpr bool Value = false;
88 static constexpr int64_t Index = -1;
89 };
90
98 template <typename U, typename T, typename... Ts, size_t Idx>
99 struct TSHasTypeImpl<TypeList<U, Ts...>, T, Idx> :
100 TSHasTypeImpl<TypeList<Ts...>, T, Idx+1> {};
101
107 template <typename T, typename... Ts, size_t Idx>
108 struct TSHasTypeImpl<TypeList<T, Ts...>, T, Idx>
109 {
110 static constexpr bool Value = true;
111 static constexpr int64_t Index = static_cast<int64_t>(Idx);
112 };
113
114
125 template <typename ListT, typename... Ts>
126 struct TSMakeUniqueImpl {
127 using type = ListT;
128 };
129
138 template <typename... Ts, typename U, typename... Us>
139 struct TSMakeUniqueImpl<TypeList<Ts...>, U, Us...>
140 {
141 using type = typename std::conditional<
142 TSHasTypeImpl<TypeList<Ts...>, U>::Value,
143 typename TSMakeUniqueImpl<TypeList<Ts...>, Us...>::type,
144 typename TSMakeUniqueImpl<TypeList<Ts..., U>, Us...>::type >::type;
145 };
146
147
152 template<typename ListT, typename... Ts> struct TSAppendImpl;
153
158 template<typename... Ts, typename... OtherTs>
159 struct TSAppendImpl<TypeList<Ts...>, OtherTs...> {
160 using type = TypeList<Ts..., OtherTs...>;
161 };
162
167 template<typename... Ts, typename... OtherTs>
168 struct TSAppendImpl<TypeList<Ts...>, TypeList<OtherTs...>> {
169 using type = TypeList<Ts..., OtherTs...>;
170 };
171
172
177 template<typename ListT, typename T> struct TSEraseImpl;
178
181 template<typename T>
182 struct TSEraseImpl<TypeList<>, T> { using type = TypeList<>; };
183
189 template<typename... Ts, typename T>
190 struct TSEraseImpl<TypeList<T, Ts...>, T> {
191 using type = typename TSEraseImpl<TypeList<Ts...>, T>::type;
192 };
193
200 template<typename T2, typename... Ts, typename T>
201 struct TSEraseImpl<TypeList<T2, Ts...>, T> {
202 using type = typename TSAppendImpl<TypeList<T2>,
203 typename TSEraseImpl<TypeList<Ts...>, T>::type>::type;
204 };
205
212 template<typename ListT, typename... Ts> struct TSRemoveImpl;
213
216 template<typename ListT>
217 struct TSRemoveImpl<ListT> { using type = ListT; };
218
224 template<typename ListT, typename T, typename... Ts>
225 struct TSRemoveImpl<ListT, T, Ts...> {
226 using type = typename TSRemoveImpl<typename TSEraseImpl<ListT, T>::type, Ts...>::type;
227 };
228
234 template<typename ListT, typename... Ts>
235 struct TSRemoveImpl<ListT, TypeList<Ts...>> {
236 using type = typename TSRemoveImpl<ListT, Ts...>::type;
237 };
238
243 template<typename T>
244 struct TSRemoveFirstImpl {
245 using type = TypeList<>;
246 };
247
252 template<typename T, typename... Ts>
253 struct TSRemoveFirstImpl<TypeList<T, Ts...>> {
254 using type = TypeList<Ts...>;
255 };
256
257
262 template<typename T>
263 struct TSRemoveLastImpl { using type = TypeList<>; };
264
271 template<typename T>
272 struct TSRemoveLastImpl<TypeList<T>> : TSRemoveLastImpl<T> {};
273
280 template<typename T, typename... Ts>
281 struct TSRemoveLastImpl<TypeList<T, Ts...>>
282 {
283 using type =
284 typename TypeList<T>::template
285 Append<typename TSRemoveLastImpl<TypeList<Ts...>>::type>;
286 };
287
288
301 template<typename ListT, size_t First, size_t Last, size_t Idx=0>
302 struct TSRemoveIndicesImpl;
303
308 template<size_t First, size_t Last, size_t Idx>
309 struct TSRemoveIndicesImpl<TypeList<>, First, Last, Idx> {
310 using type = TypeList<>;
311 };
312
318 template<typename T, size_t First, size_t Last, size_t Idx>
319 struct TSRemoveIndicesImpl<TypeList<T>, First, Last, Idx>
320 {
321 private:
322 static constexpr bool Remove = Idx >= First && Idx <= Last;
323 public:
324 using type = typename std::conditional<Remove, TypeList<>, TypeList<T>>::type;
325 };
326
339 template<typename T, typename... Ts, size_t First, size_t Last, size_t Idx>
340 struct TSRemoveIndicesImpl<TypeList<T, Ts...>, First, Last, Idx>
341 {
342 private:
343 using ThisList = typename TSRemoveIndicesImpl<TypeList<T>, First, Last, Idx>::type;
344 using NextList = typename TSRemoveIndicesImpl<TypeList<Ts...>, First, Last, Idx+1>::type;
345 public:
346 using type = typename ThisList::template Append<NextList>;
347 };
348
349
350 template<typename OpT> inline void TSForEachImpl(OpT) {}
351 template<typename OpT, typename T, typename... Ts>
352 inline void TSForEachImpl(OpT op) { op(T()); TSForEachImpl<OpT, Ts...>(op); }
353
354 } // namespace internal
355
357
358
364 template<typename... Ts>
365 struct TypeList
366 {
368 using Self = TypeList;
369
371 static constexpr size_t Size = sizeof...(Ts);
372
375 template<size_t N>
376 using Get = typename typelist_internal::TSGetElementImpl<Self, N>::type;
377 using Front = Get<0>;
378 using Back = Get<Size-1>;
379
392 template<typename T>
393 static constexpr bool Contains = typelist_internal::TSHasTypeImpl<Self, T>::Value;
394
408 template<typename T>
409 static constexpr int64_t Index = typelist_internal::TSHasTypeImpl<Self, T>::Index;
410
423 template<typename ListT = TypeList<>>
424 using Unique = typename typelist_internal::TSMakeUniqueImpl<ListT, Ts...>::type;
425
439 template<typename... TypesToAppend>
440 using Append = typename typelist_internal::TSAppendImpl<Self, TypesToAppend...>::type;
441
452 template<typename... TypesToRemove>
453 using Remove = typename typelist_internal::TSRemoveImpl<Self, TypesToRemove...>::type;
454
468 using PopFront = typename typelist_internal::TSRemoveFirstImpl<Self>::type;
469
483 using PopBack = typename typelist_internal::TSRemoveLastImpl<Self>::type;
484
499 template <size_t First, size_t Last>
500 using RemoveByIndex = typename typelist_internal::TSRemoveIndicesImpl<Self, First, Last>::type;
501
522 template<typename OpT>
523 static void foreach(OpT op) { typelist_internal::TSForEachImpl<OpT, Ts...>(op); }
524 };
525
526
527 } // namespace OPENVDB_VERSION_NAME
528 } // namespace openvdb
529
530
531 #endif // OPENVDB_TYPELIST_HAS_BEEN_INCLUDED
openvdb::v8_1::Index
Index32 Index
Definition: openvdb/Types.h:50
openvdb
Definition: openvdb/Exceptions.h:13
std
Definition: Coord.h:587
openvdb::v8_1::TypeList
A list of types (not necessarily unique)
Definition: TypeList.h:366
openvdb::v8_1::TypeList::Remove
typename typelist_internal::TSRemoveImpl< Self, TypesToRemove... >::type Remove
Remove all occurrences of one or more types, or the members of another TypeList, from this list.
Definition: TypeList.h:453
openvdb::v8_1::TypeList::Front
Get< 0 > Front
Definition: TypeList.h:377
openvdb::v8_1::TypeList::Back
Get< Size-1 > Back
Definition: TypeList.h:378
openvdb::v8_1::TypeList::Get
typename typelist_internal::TSGetElementImpl< Self, N >::type Get
Access a particular element of this type list. If the index is out of range, typelist_internal::NullT...
Definition: TypeList.h:376
openvdb::v8_1::TypeList::RemoveByIndex
typename typelist_internal::TSRemoveIndicesImpl< Self, First, Last >::type RemoveByIndex
Return a new list with types removed by their location within the list. If First is equal to Last,...
Definition: TypeList.h:500
openvdb::v8_1::TypeList::PopFront
typename typelist_internal::TSRemoveFirstImpl< Self >::type PopFront
Remove the first element of this type list. Has no effect if the type list is already empty.
Definition: TypeList.h:468
openvdb::v8_1::TypeList::PopBack
typename typelist_internal::TSRemoveLastImpl< Self >::type PopBack
Remove the last element of this type list. Has no effect if the type list is already empty.
Definition: TypeList.h:483
openvdb::v8_1::TypeList::Append
typename typelist_internal::TSAppendImpl< Self, TypesToAppend... >::type Append
Append types, or the members of another TypeList, to this list.
Definition: TypeList.h:440
openvdb::v8_1::TypeList::Unique
typename typelist_internal::TSMakeUniqueImpl< ListT, Ts... >::type Unique
Remove any duplicate types from this TypeList by rotating the next valid type left (maintains the ord...
Definition: TypeList.h:424
OPENVDB_VERSION_NAME
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h.in:116
OPENVDB_USE_VERSION_NAMESPACE
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h.in:178