Audacity 3.2.0
TypeList.cpp
Go to the documentation of this file.
1/**********************************************************************
2
3 Audacity: A Digital Audio Editor
4
5 @file TypeList.cpp
6 @brief Examples as a compile-time unit test of metaprogramming utilities
7
8 Paul Licameli
9
10**********************************************************************/
11#include "TypeList.h"
12#include <array>
13
14// Enough examples to test compilation of all the templates
15namespace { using namespace TypeList; using namespace std;
16
18
19static_assert(Null_v<Nil>);
20static_assert(!Null_v<Example>);
21
22static_assert(0 == Length_v<Nil>);
23static_assert(2 == Length_v<Example>);
24
25static_assert(is_same_v<int, Head_t<Example>>);
26
27// These compile even though Head_t<Nil> does not
29static_assert(is_same_v<int, Head_t<conditional_t<true, Example, Nil>>>);
30template <typename T> struct ShortCircuiting
31 : Head<conditional_t<Null_v<T>, Example, T>>{};
32static_assert(is_same_v<int, ShortCircuiting<Nil>::type>);
33static_assert(is_same_v<double, ShortCircuiting<List<double>>::type>);
34
35static_assert(is_same_v<List<double>, Tail_t<Example>>);
36
37static_assert(is_same_v<int, First_t<Example>>);
38static_assert(is_same_v<double, Second_t<Example>>);
39
40static_assert(is_same_v<Example, Cons_t<int, Cons_t<double, Nil>>>);
41static_assert(is_same_v<Example, PushFront_t<PushFront_t<Nil, double>, int>>);
42static_assert(is_same_v<Example, PushBack_t<PushBack_t<Nil, int>, double>>);
43
44static_assert(is_same_v<Nil, NonEmptyTails_t<Nil>>);
45static_assert(is_same_v<List<Example, List<double>>, NonEmptyTails_t<Example>>);
46
47static_assert(is_same_v<List<Nil>, Tails_t<Nil>>);
48static_assert(is_same_v<List<Example, List<double>, Nil>, Tails_t<Example>>);
49
50static_assert(is_same_v<double, Last_t<Example>>);
51static_assert(is_same_v<List<int>, ButLast_t<Example>>);
52
53static_assert(is_same_v<Example, Map_t<Identity, Example>>);
54static_assert(is_same_v<List<const int, const double>,
56static_assert(is_same_v<List<const int&, const double &>,
58static_assert(is_same_v<List<const int*&, const double *&>,
59 Map_t<
61 Example>>);
62static_assert(is_same_v<List<int *const &, double *const &>,
63 Map_t<
64 Compose<
67 Example>>);
68
69static_assert(is_same_v<Nil, MapList_t<Fn<Length_t>, Nil>>);
70static_assert(is_same_v<
71 List<integral_constant<size_t, 2>, integral_constant<size_t, 1>>,
73
74static_assert(is_same_v<tuple<int, double>, Apply_t<tuple, Example>>);
75static_assert(is_same_v<tuple<const int, const double>,
77
78static_assert(is_same_v<Example, Bind_t<tuple<int, double>>>);
79static_assert(is_same_v<Example, Bind_t<pair<int, double>>>);
80static_assert(is_same_v<List<int, int>, Bind_t<array<int, 2>>>);
81
82static_assert(is_same_v<Nil, Append_t<>>);
83static_assert(is_same_v<Example, Append_t<Example>>);
84static_assert(is_same_v<List<int, double, char>,
86
87static_assert(is_same_v<List<Example, List<int>>,
89
90static_assert(is_same_v<List<Example, List<double>>,
92
93static_assert(is_same_v<List<double, int>, Reverse_t<Example>>);
94
95static_assert(is_same_v<const int, Call_t<MaybeConst<true>, int>>);
96static_assert(is_same_v<int, Call_t<MaybeConst<false>, int>>);
97
98static_assert(is_same_v<List<const int, const double>,
100static_assert(is_same_v<Example, MapConst_t<false, Example>>);
101
102static_assert(Is_v<Fn<Null>, Nil>);
103static_assert(!Is_v<Fn<Null>, Example>);
104static_assert(!Is_v<Not<Fn<Null>>, Nil>);
105static_assert(Is_v<Not<Fn<Null>>, Example>);
106
108static_assert(Is_v<HeadIsInt, Example>);
109// static_assert(!Is_v<HeadIsInt, Nil>); // won't compile
110
113static_assert(!Is_v<StartsWithInt, Nil>);
114static_assert(Is_v<StartsWithInt, Example>);
115
117static_assert(Is_v<NullOrStartsWithInt, Nil>);
118static_assert(Is_v<NullOrStartsWithInt, Example>);
119
120static_assert(Every_v<Fn<is_arithmetic>, Example>);
121static_assert(is_base_of_v<true_type, Every<Fn<is_arithmetic>, Example>>);
122static_assert(!Every_v<Fn<is_integral>, Example>);
123static_assert(is_base_of_v<is_integral<double>,
125
126static_assert(Some_v<Fn<is_integral>, Example>);
127static_assert(is_base_of_v<is_integral<int>,
129static_assert(!Some_v<Fn<is_void>, Example>);
130static_assert(is_base_of_v<false_type, Some<Fn<is_void>, Example>>);
131
132static_assert(NotEvery_v<Fn<is_floating_point>, Example>);
133static_assert(NotAny_v<Fn<is_void>, Example>);
134
135static_assert(!In_v<void, Example>);
136static_assert(In_v<int, Example>);
137
138struct B {};
139struct D : B {};
140
141static_assert(HasBaseIn_v<D, List<B>>);
142static_assert(HasBaseIn_v<D, List<D>>);
143static_assert(HasBaseIn_v<B, List<B>>);
144static_assert(!HasBaseIn_v<B, List<D>>);
145static_assert(HasBaseIn_v<B, List<B, D>>);
146
147static_assert(is_same_v<List<Nil, Example>,
149static_assert(is_same_v<List<List<int>, List<double>>,
151static_assert(is_same_v<List<Example, Nil>,
153
154static_assert(is_same_v<Nil, Filter_t<Fn<is_void>, Example>>);
155static_assert(is_same_v<List<int>, Filter_t<Fn<is_integral>, Example>>);
156static_assert(is_same_v<Example, Filter_t<Fn<is_arithmetic>, Example>>);
157
159static_assert(is_same_v<List<unsigned>,
161static_assert(is_same_v<List<unsigned, double>,
163
164// To do: more cases
165}
Metaprogramming utilities for manipulating lists of types.
Utilities for compile-time type manipulation. Some terminology as in Lisp.
Definition: TypeList.h:22
typename NonEmptyTails< TypeList >::type NonEmptyTails_t
Definition: TypeList.h:173
typename Bind< TupleLike >::type Bind_t
Definition: TypeList.h:250
typename Tails< TypeList >::type Tails_t
Definition: TypeList.h:180
typename LeftFoldList< Op, TypeList, Initial >::type LeftFoldList_t
Definition: TypeList.h:329
typename StablePartition< Predicate, TypeList >::type StablePartition_t
Definition: TypeList.h:497
typename Tail< TypeList >::type Tail_t
Definition: TypeList.h:107
typename Filter< Predicate, TypeList >::type Filter_t
Definition: TypeList.h:504
typename ButLast< TypeList >::type ButLast_t
Definition: TypeList.h:190
typename Reverse< TypeList >::type Reverse_t
Definition: TypeList.h:368
typename Apply< Template, TypeList >::type Apply_t
Definition: TypeList.h:235
typename Map< Metafunction, TypeList >::type Map_t
Definition: TypeList.h:262
typename MapList< Metafunction, TypeList >::type MapList_t
Definition: TypeList.h:270
typename MapConst< Const, TypeList >::type MapConst_t
Definition: TypeList.h:388
typename Cons< Type, TypeList >::type Cons_t
Definition: TypeList.h:142
typename RightFoldList< Op, TypeList, Initial >::type RightFoldList_t
Definition: TypeList.h:365
typename Append< Lists... >::type Append_t
Definition: TypeList.h:273
Cons_t< unsigned, Example > Example2
Definition: TypeList.cpp:158
STL namespace.
Delays expansion of nested alias head; so Head<Nil> is legal.
Definition: TypeList.h:89
Empty specialization.
Definition: TypeList.h:63
Primary template for a list of arbitrary types.
Definition: TypeList.h:61