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  template <typename T, typename Config>
53  class TableAs;
54  template <typename T, typename Config>
56 
57  class DelegatedParameter;
59 }
60 
61 namespace tt {
62 
64 
65  template <bool b, typename T = void>
66  using disable_if = std::enable_if<!b, T>;
67 
68  template <bool b, typename T = void>
70 
71  template <typename T>
72  struct is_int
73  : std::bool_constant<std::is_integral_v<T> && !std::is_unsigned_v<T>> {};
74 
75  template <typename T>
76  using is_numeric = std::is_arithmetic<T>;
77 
78  template <typename T>
79  using is_uint = std::is_unsigned<T>;
80 
81  template <typename T, typename = void>
82  struct is_callable : std::false_type {};
83 
84  template <typename T>
85  struct is_callable<
86  T,
87  enable_if_function_exists_t<std::set<std::string> (T::*)(), &T::operator()>>
88  : std::true_type {};
89 
90  template <typename T>
91  struct is_callable<
92  T,
93  enable_if_function_exists_t<std::set<std::string> (T::*)() const,
94  &T::operator()>> : std::true_type {};
95 
96  //=======================================================
97  // Enforce (non)const-ness
98  //
100 
101  template <typename T, const_flavor C>
102  struct maybe_const {
103  using type = T;
104  };
105 
106  template <typename T>
107  struct maybe_const<T, const_flavor::require_const> {
108  using type = std::add_const_t<T>;
109  };
110 
111  template <typename T, const_flavor C>
113 
114  //=======================================================
115  // Check if sequence type
116  //
117  template <typename Container>
118  struct is_sequence_type : std::false_type {};
119 
120  template <typename ARG, std::size_t SZ>
121  struct is_sequence_type<std::array<ARG, SZ>> : std::true_type {};
122  template <typename... ARGS>
123  struct is_sequence_type<std::tuple<ARGS...>> : std::true_type {};
124  template <typename... ARGS>
125  struct is_sequence_type<std::vector<ARGS...>> : std::true_type {};
126 
127  template <typename T>
129 
130  //=======================================================
131  // Check if Table<>
132  //
133  template <typename T>
134  struct is_table : std::false_type {};
135 
136  template <typename T, typename KeysToIgnore>
137  struct is_table<fhicl::Table<T, KeysToIgnore>> : std::true_type {};
138 
139  template <typename T>
140  inline constexpr bool is_table_v{is_table<T>::value};
141 
142  //=======================================================
143  // Check if TableFragment<>
144  //
145  template <typename T>
146  struct is_table_fragment : std::false_type {};
147 
148  template <typename T>
149  struct is_table_fragment<fhicl::TableFragment<T>> : std::true_type {};
150 
151  template <typename T>
153 
154  //=======================================================
155  // Check if optional parameter
156  //
157  template <typename T>
158  struct is_optional_parameter : std::false_type {};
159 
160  template <typename T>
161  struct is_optional_parameter<fhicl::OptionalTable<T>> : std::true_type {};
162 
163  template <typename T>
164  struct is_optional_parameter<fhicl::OptionalAtom<T>> : std::true_type {};
165 
166  template <typename T, std::size_t SZ>
167  struct is_optional_parameter<fhicl::OptionalSequence<T, SZ>>
168  : std::true_type {};
169 
170  template <typename... TYPES>
171  struct is_optional_parameter<fhicl::OptionalTuple<TYPES...>>
172  : std::true_type {};
173 
174  template <typename T, typename... ARGS>
175  struct is_optional_parameter<fhicl::OptionalTupleAs<T(ARGS...)>>
176  : std::true_type {};
177 
178  template <typename T>
179  inline constexpr bool is_optional_parameter_v{
181 
182  //=======================================================
183  // Check if delegated parameter
184  //
185  template <typename T>
186  struct is_delegated_parameter : std::false_type {};
187 
188  template <>
189  struct is_delegated_parameter<fhicl::DelegatedParameter> : std::true_type {};
190 
191  template <>
192  struct is_delegated_parameter<fhicl::OptionalDelegatedParameter>
193  : std::true_type {};
194 
195  template <typename T>
196  inline constexpr bool is_delegated_parameter_v{
198 
199  //=======================================================
200  // Check if fhicl type -- i.e. Atom<>, Table<>, etc.
201  //
202  template <typename T>
203  struct is_fhicl_type : std::false_type {};
204 
205  // ... Table
206  template <typename T, typename KeysToIgnore>
207  struct is_fhicl_type<fhicl::Table<T, KeysToIgnore>> : std::true_type {};
208 
209  template <typename T>
210  struct is_fhicl_type<fhicl::OptionalTable<T>> : std::true_type {};
211 
212  // ... Atom
213  template <typename T>
214  struct is_fhicl_type<fhicl::Atom<T>> : std::true_type {};
215 
216  template <typename T>
217  struct is_fhicl_type<fhicl::OptionalAtom<T>> : std::true_type {};
218 
219  // ... Sequence
220  template <typename T, std::size_t SZ>
221  struct is_fhicl_type<fhicl::Sequence<T, SZ>> : std::true_type {};
222 
223  template <typename T, std::size_t SZ>
224  struct is_fhicl_type<fhicl::OptionalSequence<T, SZ>> : std::true_type {};
225 
226  // ... Tuple
227  template <typename... TYPES>
228  struct is_fhicl_type<fhicl::Tuple<TYPES...>> : std::true_type {};
229 
230  template <typename... TYPES>
231  struct is_fhicl_type<fhicl::OptionalTuple<TYPES...>> : std::true_type {};
232 
233  // ... TupleAs
234  template <typename T, typename... ARGS>
235  struct is_fhicl_type<fhicl::TupleAs<T(ARGS...)>> : std::true_type {};
236 
237  template <typename T, typename... ARGS>
238  struct is_fhicl_type<fhicl::OptionalTupleAs<T(ARGS...)>> : std::true_type {};
239 
240  // ... TableAs
241  template <typename T, typename Config>
242  struct is_fhicl_type<fhicl::TableAs<T, Config>> : std::true_type {};
243 
244  template <typename T, typename Config>
245  struct is_fhicl_type<fhicl::OptionalTableAs<T, Config>> : std::true_type {};
246 
247  template <typename T>
248  inline constexpr bool is_fhicl_type_v{is_fhicl_type<T>::value};
249 
250  //=======================================================
251  // Get FHiCL types: can be Atom, Sequence, Tuple, or Table
252  //
253  template <typename T>
256  };
257 
258  template <typename T, std::size_t SZ>
259  struct fhicl_type_impl<fhicl::Sequence<T, SZ>> {
261  };
262 
263  template <typename T, typename KeysToIgnore>
264  struct fhicl_type_impl<fhicl::Table<T, KeysToIgnore>> {
266  };
267 
268  template <typename... ARGS>
269  struct fhicl_type_impl<fhicl::Tuple<ARGS...>> {
270  using type = fhicl::Tuple<ARGS...>;
271  };
272 
273  template <typename T, typename Config>
274  struct fhicl_type_impl<fhicl::TableAs<T, Config>> {
276  };
277 
278  template <typename T, typename... ARGS>
279  struct fhicl_type_impl<fhicl::TupleAs<T(ARGS...)>> {
280  using type = fhicl::TupleAs<T(ARGS...)>;
281  };
282 
283  template <typename T>
284  struct fhicl_type_impl<fhicl::OptionalAtom<T>> {
286  };
287 
288  template <typename T, std::size_t SZ>
289  struct fhicl_type_impl<fhicl::OptionalSequence<T, SZ>> {
291  };
292 
293  template <typename T>
294  struct fhicl_type_impl<fhicl::OptionalTable<T>> {
296  };
297 
298  template <typename... ARGS>
299  struct fhicl_type_impl<fhicl::OptionalTuple<ARGS...>> {
300  using type = fhicl::OptionalTuple<ARGS...>;
301  };
302 
303  template <typename T, typename Config>
304  struct fhicl_type_impl<fhicl::OptionalTableAs<T, Config>> {
306  };
307 
308  template <typename T, typename... ARGS>
309  struct fhicl_type_impl<fhicl::OptionalTupleAs<T(ARGS...)>> {
310  using type = fhicl::OptionalTupleAs<T(ARGS...)>;
311  };
312 
313  // The alias
314  template <typename T>
316 
317  //=======================================================
318  // Get Return types
319  //
320  template <typename T>
322  using value_type = T;
323  };
324 
325  template <typename T>
326  struct return_type_impl<fhicl::Atom<T>> {
328  };
329 
330  template <typename T, std::size_t SZ>
331  struct return_type_impl<fhicl::Sequence<T, SZ>> {
333  };
334 
335  template <typename S, typename KeysToIgnore>
336  struct return_type_impl<fhicl::Table<S, KeysToIgnore>> {
338  };
339 
340  template <typename... ARGS>
341  struct return_type_impl<fhicl::Tuple<ARGS...>> {
342  using value_type = typename fhicl::Tuple<ARGS...>::value_type;
343  };
344 
345  template <typename T, typename Config>
346  struct return_type_impl<fhicl::TableAs<T, Config>> {
348  };
349 
350  template <typename T, typename... ARGS>
351  struct return_type_impl<fhicl::TupleAs<T(ARGS...)>> {
352  using value_type = typename fhicl::TupleAs<T(ARGS...)>::value_type;
353  };
354 
355  // The alias
356  template <typename... ARGS>
357  using return_type = typename return_type_impl<ARGS...>::value_type;
358 }
359 
360 #endif /* fhiclcpp_type_traits_h */
361 
362 // Local Variables:
363 // mode: c++
364 // End:
std::is_unsigned< T > is_uint
Definition: type_traits.h:79
std::is_arithmetic< T > is_numeric
Definition: type_traits.h:76
std::array< tt::return_type< T >, N > value_type
Definition: Sequence.h:85
constexpr bool is_fhicl_type_v
Definition: type_traits.h:248
typename fhicl::Tuple< ARGS... >::value_type value_type
Definition: type_traits.h:342
Definition: 044_section.h:5
STL namespace.
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:352
void * Tuple
Definition: DBFolder.h:13
std::integral_constant< bool, Value > bool_constant
Definition: ProviderPack.h:314
Definition: type_traits.h:61
typename fhicl::Sequence< T, SZ >::value_type value_type
Definition: type_traits.h:332
typename fhicl::TableAs< T, Config >::value_type value_type
Definition: type_traits.h:347
constexpr bool is_sequence_type_v
Definition: type_traits.h:128
typename fhicl_type_impl< T >::type fhicl_type
Definition: type_traits.h:315
typename fhicl::Table< S, KeysToIgnore >::value_type value_type
Definition: type_traits.h:337
typename fhicl::Atom< T >::value_type value_type
Definition: type_traits.h:327
T value_type
Definition: Atom.h:56
constexpr bool is_table_fragment_v
Definition: type_traits.h:152
auto array(Array const &a)
Returns a manipulator which will print the specified array.
Definition: DumpUtils.h:228
std::enable_if<!b, T > disable_if
Definition: type_traits.h:66
constexpr bool is_optional_parameter_v
Definition: type_traits.h:179
typename disable_if< b, T >::type disable_if_t
Definition: type_traits.h:69
typename maybe_const< T, C >::type maybe_const_t
Definition: type_traits.h:112
constexpr bool is_table_v
Definition: type_traits.h:140
const_flavor
Definition: type_traits.h:99
typename return_type_impl< ARGS... >::value_type return_type
Definition: type_traits.h:357
constexpr bool is_delegated_parameter_v
Definition: type_traits.h:196