This repository has been archived by the owner on Feb 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 63
/
TemplateUtil.hpp
235 lines (216 loc) · 7.72 KB
/
TemplateUtil.hpp
1
2
3
4
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
**/
#ifndef QUICKSTEP_UTILITY_TEMPLATE_UTIL_HPP_
#define QUICKSTEP_UTILITY_TEMPLATE_UTIL_HPP_
#include <cstddef>
#include <tuple>
#include <utility>
namespace quickstep {
/** \addtogroup Utility
* @{
*/
namespace template_util_inner {
/**
* @brief Represents a compile-time sequence of integers.
*
* Sequence is defined here for C++11 compatibility. For C++14 and above,
* std::integer_sequence can be used to achieve the same functionality.
*
* TODO(jianqiao): directly use std::integer_sequence if having C++14 support.
*/
template<std::size_t ...>
struct Sequence {};
/**
* @brief The helper class for creating Sequence. MakeSequence<N>::type is
* equivalent to Sequence<1,2,...,N>.
*
* MakeSequence is defined here for C++11 compatibility. For C++14 and above,
* std::make_index_sequence can be used to achieve the same functionality.
*
* TODO(jianqiao): directly use std::make_index_sequence if having C++14 support.
*/
template<std::size_t N, std::size_t ...S>
struct MakeSequence : MakeSequence<N-1, N-1, S...> {};
template<std::size_t ...S>
struct MakeSequence<0, S...> {
typedef Sequence<S...> type;
};
/**
* @brief Final step of CreateBoolInstantiatedInstance. Now all bool_values are
* ready. Instantiate the template and create (i.e. new) an instance.
*/
template <template <bool ...> class T, class ReturnT,
bool ...bool_values, std::size_t ...i,
typename Tuple>
inline ReturnT* CreateBoolInstantiatedInstanceInner(Tuple &&args,
Sequence<i...> &&indices) {
return new T<bool_values...>(std::get<i>(std::forward<Tuple>(args))...);
}
/**
* @brief Invoke the functor with the compile-time bool values wrapped as
* integral_constant types.
*/
template <typename FunctorT, bool ...bool_values>
inline auto InvokeOnBoolsInner(const FunctorT &functor) {
return functor(std::integral_constant<bool, bool_values>()...);
}
/**
* @brief Recursive dispatching.
*/
template <typename FunctorT, bool ...bool_values, typename ...Bools>
inline auto InvokeOnBoolsInner(const FunctorT &functor,
const bool tparam,
const Bools ...rest_params) {
if (tparam) {
return InvokeOnBoolsInner<FunctorT, bool_values..., true>(
functor, rest_params...);
} else {
return InvokeOnBoolsInner<FunctorT, bool_values..., false>(
functor, rest_params...);
}
}
/**
* @brief Move the functor to the first position in argument list.
*/
template <std::size_t last, std::size_t ...i, typename TupleT>
inline auto InvokeOnBoolsInner(TupleT &&args, Sequence<i...> &&indices) {
return InvokeOnBoolsInner(std::get<last>(std::forward<TupleT>(args)),
std::get<i>(std::forward<TupleT>(args))...);
}
} // namespace template_util_inner
/**
* @brief Edge case of the recursive CreateBoolInstantiatedInstance function
* when all bool variables have been branched and replaced with compile-time
* bool constants.
*/
template <template <bool ...> class T, class ReturnT,
bool ...bool_values,
typename Tuple>
inline ReturnT* CreateBoolInstantiatedInstance(Tuple &&args) {
// Note that the constructor arguments have been forwarded as a tuple (args).
// Here we generate a compile-time index sequence (i.e. typename MakeSequence<n_args>::type())
// for the tuple, so that the tuple can be unpacked as a sequence of constructor
// parameters in CreateBoolInstantiatedInstanceInner.
constexpr std::size_t n_args = std::tuple_size<Tuple>::value;
return template_util_inner::CreateBoolInstantiatedInstanceInner<
T, ReturnT, bool_values...>(
std::forward<Tuple>(args),
typename template_util_inner::MakeSequence<n_args>::type());
}
/**
* @brief A helper function for creating bool branched templates.
*
* The scenario for using this helper function is that, suppose we have a class
* where all template parameters are bools:
* --
* template <bool c1, bool c2, bool c3>
* class SomeClass : public BaseClass {
* // This simple function will be invoked in computationally-intensive loops.
* inline SomeType someSimpleFunction(...) {
* if (c1) {
* doSomeThing1();
* }
* if (c2) {
* doSomeThing2();
* }
* if (c3) {
* doSomeThing3();
* }
* }
* };
* --
* Typically, this bool-paramterized template is for performance consideration.
* That is, we would like to make a copy of code for each configuration of bool
* values, so that there will be no branchings in someSimpleFunction().
*
* The problem is that, to conditionally instantiate the template, given bool
* variables c1, c2, c3, we have to do something like this:
* --
* if (c1) {
* if (c2) {
* if (c3) {
* return new SomeClass<true, true, true>(some_args...);
* } else {
* return new SomeClass<true, true, false>(some_args...);
* }
* } else {
* if (c3) {
* return new SomeClass<true, false, true>(some_args...);
* } else {
* return new SomeClass<true, false, false>(some_args...);
* }
* } else {
* ...
* }
* --
* Then there will be power(2,N) branches if the template has N bool parameters,
* making it tedious to do the instantiating.
*
* Now, this helper function can achieve the branched instantiation in one
* statement as:
* --
* return CreateBoolInstantiatedInstance<SomeClass,BaseClass>(
* std::forward_as_tuple(some_args...), c1, c2, c3);
* --
*/
template <template <bool ...> class T, class ReturnT,
bool ...bool_values, typename ...Bools,
typename Tuple>
inline ReturnT* CreateBoolInstantiatedInstance(Tuple &&args,
const bool tparam,
const Bools ...rest_tparams) {
if (tparam) {
return CreateBoolInstantiatedInstance<T, ReturnT, bool_values..., true>(
std::forward<Tuple>(args), rest_tparams...);
} else {
return CreateBoolInstantiatedInstance<T, ReturnT, bool_values..., false>(
std::forward<Tuple>(args), rest_tparams...);
}
}
/**
* @brief A helper function for bool branched template specialization.
*
* Usage example:
* --
* bool c1 = true, c2 = false;
*
* InvokeOnBools(
* c1, c2,
* [&](auto c1, auto c2) -> SomeBaseClass* {
* using T1 = decltype(c1); // T1 == std::true_type
* using T2 = decltype(c2); // T2 == std::false_type
*
* constexpr bool cv1 = T1::value; // cv1 == true
* constexpr bool cv2 = T2::value; // cv2 == false
*
* SomeFunction<cv1, cv2>(...);
* return new SomeClass<cv1, cv2>(...);
* });
* --
*/
template <typename ...ArgTypes>
inline auto InvokeOnBools(ArgTypes ...args) {
constexpr std::size_t last = sizeof...(args) - 1;
return template_util_inner::InvokeOnBoolsInner<last>(
std::forward_as_tuple(args...),
typename template_util_inner::MakeSequence<last>::type());
}
/** @} */
} // namespace quickstep
#endif // QUICKSTEP_UTILITY_TEMPLATE_UTIL_HPP_