type_traits.h
Go to the documentation of this file.
1 #ifndef fhiclcpp_type_traits_h
2 #define fhiclcpp_type_traits_h
3 // ======================================================================
4 //
5 // type traits
6 //
7 // ======================================================================
8 
10 
11 #include <array>
12 #include <complex>
13 #include <set>
14 #include <string>
15 #include <tuple>
16 #include <type_traits>
17 #include <utility>
18 #include <vector>
19 
20 // ======================================================================
21 
22 namespace fhicl {
23 
24  template <typename T>
25  class Atom;
26  template <typename T>
27  class OptionalAtom;
28 
29  template <typename T, typename KeysToIgnore>
30  class Table;
31  template <typename T>
33 
34  template <typename T>
36 
37  template <typename... ARGS>
38  class Tuple;
39  template <typename... ARGS>
41 
42  template <typename T, std::size_t SZ>
43  class Sequence;
44  template <typename T, std::size_t SZ>
46 
47  template <typename T, typename... ARGS>
48  class TupleAs;
49  template <typename T, typename... ARGS>
51 
52  class DelegatedParameter;
54 }
55 
56 namespace tt {
57 
59  using std::enable_if;
60  using std::is_floating_point;
61 
62  template <bool b, typename T = void>
63  using disable_if = std::enable_if<!b, T>;
64 
65  template <typename T>
66  struct is_int : std::integral_constant<bool,
67  std::is_integral<T>::value &&
68  !std::is_unsigned<T>::value> {};
69 
70  template <typename T>
71  using is_numeric = std::is_arithmetic<T>;
72 
73  template <typename T>
74  using is_uint = std::is_unsigned<T>;
75 
76  template <typename T, typename = void>
77  struct is_callable : std::false_type {};
78 
79  template <typename T>
80  struct is_callable<
81  T,
82  enable_if_function_exists_t<std::set<std::string> (T::*)(), &T::operator()>>
83  : std::true_type {};
84 
85  template <typename T>
86  struct is_callable<
87  T,
88  enable_if_function_exists_t<std::set<std::string> (T::*)() const,
89  &T::operator()>> : std::true_type {};
90 
91  //=======================================================
92  // Enforce (non)const-ness
93  //
95 
96  template <typename T, const_flavor C>
97  struct maybe_const {
98  using type = T;
99  };
100 
101  template <typename T>
102  struct maybe_const<T, const_flavor::require_const> {
103  using type = std::add_const_t<T>;
104  };
105 
106  template <typename T, const_flavor C>
108 
109  //=======================================================
110  // Check if sequence type
111  //
112  template <typename Container>
113  struct is_sequence_type : std::false_type {};
114 
115  template <typename ARG, std::size_t SZ>
116  struct is_sequence_type<std::array<ARG, SZ>> : std::true_type {};
117  template <typename... ARGS>
118  struct is_sequence_type<std::tuple<ARGS...>> : std::true_type {};
119  template <typename... ARGS>
120  struct is_sequence_type<std::vector<ARGS...>> : std::true_type {};
121 
122  //=======================================================
123  // Check if Table<>
124  //
125  template <typename T>
126  struct is_table : std::false_type {};
127 
128  template <typename T, typename KeysToIgnore>
129  struct is_table<fhicl::Table<T, KeysToIgnore>> : std::true_type {};
130 
131  //=======================================================
132  // Check if TableFragment<>
133  //
134  template <typename T>
135  struct is_table_fragment : std::false_type {};
136 
137  template <typename T>
138  struct is_table_fragment<fhicl::TableFragment<T>> : std::true_type {};
139 
140  //=======================================================
141  // Check if optional parameter
142  //
143  template <typename T>
144  struct is_optional_parameter : std::false_type {};
145 
146  template <typename T>
147  struct is_optional_parameter<fhicl::OptionalTable<T>> : std::true_type {};
148 
149  template <typename T>
150  struct is_optional_parameter<fhicl::OptionalAtom<T>> : std::true_type {};
151 
152  template <typename T, std::size_t SZ>
153  struct is_optional_parameter<fhicl::OptionalSequence<T, SZ>>
154  : std::true_type {};
155 
156  template <typename... TYPES>
157  struct is_optional_parameter<fhicl::OptionalTuple<TYPES...>>
158  : std::true_type {};
159 
160  template <typename T, typename... ARGS>
161  struct is_optional_parameter<fhicl::OptionalTupleAs<T(ARGS...)>>
162  : std::true_type {};
163 
164  //=======================================================
165  // Check if delegated parameter
166  //
167  template <typename T>
168  struct is_delegated_parameter : std::false_type {};
169 
170  template <>
171  struct is_delegated_parameter<fhicl::DelegatedParameter> : std::true_type {};
172 
173  template <>
174  struct is_delegated_parameter<fhicl::OptionalDelegatedParameter>
175  : std::true_type {};
176 
177  //=======================================================
178  // Check if fhicl type -- i.e. Atom<>, Table<>, etc.
179  //
180  template <typename T>
181  struct is_fhicl_type : std::false_type {};
182 
183  // ... Table
184  template <typename T, typename KeysToIgnore>
185  struct is_fhicl_type<fhicl::Table<T, KeysToIgnore>> : std::true_type {};
186 
187  template <typename T>
188  struct is_fhicl_type<fhicl::OptionalTable<T>> : std::true_type {};
189 
190  // ... Atom
191  template <typename T>
192  struct is_fhicl_type<fhicl::Atom<T>> : std::true_type {};
193 
194  template <typename T>
195  struct is_fhicl_type<fhicl::OptionalAtom<T>> : std::true_type {};
196 
197  // ... Sequence
198  template <typename T, std::size_t SZ>
199  struct is_fhicl_type<fhicl::Sequence<T, SZ>> : std::true_type {};
200 
201  template <typename T, std::size_t SZ>
202  struct is_fhicl_type<fhicl::OptionalSequence<T, SZ>> : std::true_type {};
203 
204  // ... Tuple
205  template <typename... TYPES>
206  struct is_fhicl_type<fhicl::Tuple<TYPES...>> : std::true_type {};
207 
208  template <typename... TYPES>
209  struct is_fhicl_type<fhicl::OptionalTuple<TYPES...>> : std::true_type {};
210 
211  // ... TupleAs
212  template <typename T, typename... ARGS>
213  struct is_fhicl_type<fhicl::TupleAs<T(ARGS...)>> : std::true_type {};
214 
215  template <typename T, typename... ARGS>
216  struct is_fhicl_type<fhicl::OptionalTupleAs<T(ARGS...)>> : std::true_type {};
217 
218  //=======================================================
219  // Get FHiCL types: can be Atom, Sequence, Tuple, or Table
220  //
221  template <typename T>
224  };
225 
226  template <typename T, std::size_t SZ>
227  struct fhicl_type_impl<fhicl::Sequence<T, SZ>> {
229  };
230 
231  template <typename T, typename KeysToIgnore>
232  struct fhicl_type_impl<fhicl::Table<T, KeysToIgnore>> {
234  };
235 
236  template <typename... ARGS>
237  struct fhicl_type_impl<fhicl::Tuple<ARGS...>> {
238  using type = fhicl::Tuple<ARGS...>;
239  };
240 
241  template <typename T, typename... ARGS>
242  struct fhicl_type_impl<fhicl::TupleAs<T(ARGS...)>> {
243  using type = fhicl::TupleAs<T(ARGS...)>;
244  };
245 
246  template <typename T>
247  struct fhicl_type_impl<fhicl::OptionalAtom<T>> {
249  };
250 
251  template <typename T, std::size_t SZ>
252  struct fhicl_type_impl<fhicl::OptionalSequence<T, SZ>> {
254  };
255 
256  template <typename T>
257  struct fhicl_type_impl<fhicl::OptionalTable<T>> {
259  };
260 
261  template <typename... ARGS>
262  struct fhicl_type_impl<fhicl::OptionalTuple<ARGS...>> {
263  using type = fhicl::OptionalTuple<ARGS...>;
264  };
265 
266  template <typename T, typename... ARGS>
267  struct fhicl_type_impl<fhicl::OptionalTupleAs<T(ARGS...)>> {
268  using type = fhicl::OptionalTupleAs<T(ARGS...)>;
269  };
270 
271  // The alias
272  template <typename T>
274 
275  //=======================================================
276  // Get Return types
277  //
278  template <typename T>
280  using value_type = T;
281  };
282 
283  template <typename T>
284  struct return_type_impl<fhicl::Atom<T>> {
286  };
287 
288  template <typename T, std::size_t SZ>
289  struct return_type_impl<fhicl::Sequence<T, SZ>> {
291  };
292 
293  template <typename S, typename KeysToIgnore>
294  struct return_type_impl<fhicl::Table<S, KeysToIgnore>> {
296  };
297 
298  template <typename... ARGS>
299  struct return_type_impl<fhicl::Tuple<ARGS...>> {
300  using value_type = typename fhicl::Tuple<ARGS...>::value_type;
301  };
302 
303  template <typename T, typename... ARGS>
304  struct return_type_impl<fhicl::TupleAs<T(ARGS...)>> {
305  using value_type = typename fhicl::TupleAs<T(ARGS...)>::value_type;
306  };
307 
308  // The alias
309  template <typename... ARGS>
310  using return_type = typename return_type_impl<ARGS...>::value_type;
311 }
312 
313 #endif /* fhiclcpp_type_traits_h */
314 
315 // Local Variables:
316 // mode: c++
317 // End:
std::is_unsigned< T > is_uint
Definition: type_traits.h:74
std::is_arithmetic< T > is_numeric
Definition: type_traits.h:71
std::array< tt::return_type< T >, N > value_type
Definition: Sequence.h:85
typename fhicl::Tuple< ARGS... >::value_type value_type
Definition: type_traits.h:300
enable_if_same_t< FT, decltype(f), R > enable_if_function_exists_t
typename fhicl::TupleAs< T(ARGS...)>::value_type value_type
Definition: type_traits.h:305
Definition: type_traits.h:56
typename fhicl::Sequence< T, SZ >::value_type value_type
Definition: type_traits.h:290
typename fhicl_type_impl< T >::type fhicl_type
Definition: type_traits.h:273
typename fhicl::Table< S, KeysToIgnore >::value_type value_type
Definition: type_traits.h:295
typename fhicl::Atom< T >::value_type value_type
Definition: type_traits.h:285
T value_type
Definition: Atom.h:57
std::enable_if<!b, T > disable_if
Definition: type_traits.h:63
typename maybe_const< T, C >::type maybe_const_t
Definition: type_traits.h:107
const_flavor
Definition: type_traits.h:94
typename return_type_impl< ARGS... >::value_type return_type
Definition: type_traits.h:310
double T
Definition: Xdiff_gwt.C:5