HOPS
HOPS class reference
MHO_Meta.hh
Go to the documentation of this file.
1 #ifndef MHO_Meta_HH__
2 #define MHO_Meta_HH__
3 
4 #include <complex>
5 #include <map>
6 #include <tuple>
7 #include <type_traits>
8 
9 namespace hops
10 {
11 
19 //null and empty types
21 {};
22 
27 {};
28 
29 //typelist
33 template< typename... T > struct MHO_Typelist
34 {};
35 
36 //typelist size
40 template< typename L > struct MHO_TypelistSizeImpl;
41 
42 //specialization for typelist
48 template< class... T > struct MHO_TypelistSizeImpl< MHO_Typelist< T... > >
49 {
50  using type = std::integral_constant< size_t, sizeof...(T) >;
51 };
52 
56 template< class L > using MHO_TypelistSize = typename MHO_TypelistSizeImpl< L >::type;
57 
61 template< class T, class U > struct is_same_count
62 {
63  constexpr static size_t value = 0;
64 };
65 
69 template< class T > struct is_same_count< T, T >
70 {
71  constexpr static size_t value = 1;
72 };
73 
77 template< typename XCheckType, size_t N, typename... T > struct count_instances_of_type;
78 
79 //terminating case is N=0
87 template< typename XCheckType, typename... T > struct count_instances_of_type< XCheckType, 0, T... >
88 {
89  using current_type = typename std::tuple_element< 0, std::tuple< T... > >::type;
91 };
92 
93 //N = sizeof...(T) - 1
97 template< typename XCheckType, size_t N, typename... T > struct count_instances_of_type
98 {
99  using current_type = typename std::tuple_element< N, std::tuple< T... > >::type;
100  constexpr static std::size_t value =
102 };
103 
105 
106 //functions needed to stream tuples/////////////////////////////////////////////
117 template< size_t N = 0, typename XStream, typename... T >
118 typename std::enable_if< (N >= sizeof...(T)), XStream& >::type ostream_tuple(XStream& s, const std::tuple< T... >&)
119 {
120  //terminating case, do nothing but return s
121  return s;
122 }
123 
134 template< size_t N = 0, typename XStream, typename... T >
135 typename std::enable_if< (N < sizeof...(T)), XStream& >::type ostream_tuple(XStream& s, const std::tuple< T... >& t)
136 {
137  //dump the element @ N
138  s << std::get< N >(t);
139  //recurse to next
140  return ostream_tuple< N + 1 >(s, t);
141 }
142 
143 //functions needed to stream tuples
154 template< size_t N = 0, typename XStream, typename... T >
155 typename std::enable_if< (N >= sizeof...(T)), XStream& >::type istream_tuple(XStream& s, std::tuple< T... >&)
156 {
157  //terminating case, do nothing but return s
158  return s;
159 }
160 
171 template< size_t N = 0, typename XStream, typename... T >
172 typename std::enable_if< (N < sizeof...(T)), XStream& >::type istream_tuple(XStream& s, std::tuple< T... >& t)
173 {
174  //in stream the element @ N
175  s >> std::get< N >(t);
176  //recurse to next
177  return istream_tuple< N + 1 >(s, t);
178 }
179 
181 
185 template< size_t NTypes > struct indexed_tuple_visit
186 {
193  template< typename XTupleType, typename XFunctorType > static void visit(XTupleType& tup, XFunctorType& functor)
194  {
195  //apply here and then recurse to the next type
196  functor(NTypes - 1, std::get< NTypes - 1 >(tup));
198  }
199 };
200 
201 //base case, terminates the recursion
205 template<> struct indexed_tuple_visit< 0 >
206 {
213  template< typename XTupleType, typename XFunctorType > static void visit(XTupleType& tup, XFunctorType& functor) {}
214 };
215 
217 
218 
222 template< size_t NTypes > struct apply_to_tuple
223 {
234  template< typename XTupleType, typename XFunctorType >
235  static void apply(XTupleType& tup, size_t index, XFunctorType& functor)
236  {
237  //if the index matches the current element, apply the functor
238  if(index == NTypes - 1)
239  {
240  functor(std::get< NTypes - 1 >(tup));
241  }
242  else
243  {
244  //else recurse to the next type
245  apply_to_tuple< NTypes - 1 >::apply(tup, index, functor);
246  }
247  }
248 };
249 
250 //base case, empty tuple with no elements (should never happen)
254 template<> struct apply_to_tuple< 0 >
255 {
266  template< typename XTupleType, typename XFunctorType >
267  static void apply(XTupleType& tup, size_t index, XFunctorType& functor)
268  {}
269 };
270 
271 //const access
280 template< typename XTupleType, typename XFunctorType > void apply_at(const XTupleType& tup, size_t index, XFunctorType& functor)
281 {
282  apply_to_tuple< std::tuple_size< XTupleType >::value >::apply(tup, index, functor);
283 }
284 
285 //non-const access
286 template< typename XTupleType, typename XFunctorType > void apply_at(XTupleType& tup, size_t index, XFunctorType& functor)
287 {
288  apply_to_tuple< std::tuple_size< XTupleType >::value >::apply(tup, index, functor);
289 }
290 
293 //same thing as above, but apply a two argument functor to two tuples of the same type
294 
295 //generic apply functor to tuple (for runtime-indexed access)
296 //XTupleType and XTupleType2 must be the same (except for const)
297 template< size_t NTypes > struct apply_to_tuple2
298 {
299  template< typename XTupleType, typename XTupleType2, typename XFunctorType >
300  static void apply(XTupleType& tup1, XTupleType2& tup2, size_t index, XFunctorType& functor)
301  {
302  //if the index matches the current element, apply the functor
303  if(index == NTypes - 1)
304  {
305  functor(std::get< NTypes - 1 >(tup1), std::get< NTypes - 1 >(tup2));
306  }
307  else
308  {
309  //else recurse to the next type
310  apply_to_tuple2< NTypes - 1 >::apply(tup1, tup2, index, functor);
311  }
312  }
313 };
314 
315 //base case, empty tuple with no elements (should never happen)
316 template<> struct apply_to_tuple2< 0 >
317 {
318  template< typename XTupleType, typename XTupleType2, typename XFunctorType >
319  static void apply(XTupleType& tup1, XTupleType2& tup2, size_t index, XFunctorType& functor)
320  {}
321 };
322 
323 //const access on first parameter
324 template< typename XTupleType, typename XTupleType2, typename XFunctorType >
325 void apply_at2(const XTupleType& tup1, XTupleType& tup2, size_t index, XFunctorType& functor)
326 {
327  apply_to_tuple2< std::tuple_size< XTupleType >::value >::apply(tup1, tup2, index, functor);
328 }
329 
330 //non-const access
331 template< typename XTupleType, typename XTupleType2, typename XFunctorType >
332 void apply_at2(XTupleType& tup1, XTupleType2& tup2, size_t index, XFunctorType& functor)
333 {
334  apply_to_tuple2< std::tuple_size< XTupleType >::value >::apply(tup1, tup2, index, functor);
335 }
336 
338 //check structs for complex floating point types
339 
340 template< typename XValueType > struct is_complex: std::false_type
341 {};
342 
343 template<> struct is_complex< std::complex< float > >: std::true_type
344 {};
345 
346 template<> struct is_complex< std::complex< double > >: std::true_type
347 {};
348 
349 template<> struct is_complex< std::complex< long double > >: std::true_type
350 {};
351 
352 
358 template< typename XContainer1, typename XContainer2 >
359 std::map< typename XContainer1::value_type, typename XContainer2::value_type > zip_into_map(const XContainer1& c1,
360  const XContainer2& c2)
361 {
362  auto it1 = c1.begin();
363  auto it2 = c2.begin();
364  std::map< typename XContainer1::value_type, typename XContainer2::value_type > zip;
365  while(it1 != c1.end() && it2 != c2.end())
366  {
367  zip[*it1] = *it2;
368  ++it1;
369  ++it2;
370  }
371  return zip;
372 }
373 
375 //empty classes for detecting classes derived from container types
376 //only needed for dependent template specializations
378 {};
379 
381 {};
382 
384 {}; //only needed for dependent template specializations
385 
387 {}; //only needed for dependent template specializations
388 
390 
391 } // namespace hops
392 
393 #endif
Definition: MHO_Meta.hh:378
Definition: MHO_Meta.hh:387
Definition: MHO_Meta.hh:381
Definition: MHO_Meta.hh:384
t
Definition: picking_aedit.py:14
Definition: MHO_ChannelLabeler.hh:17
std::enable_if<(N >=sizeof...(T)), XStream & >::type istream_tuple(XStream &s, std::tuple< T... > &)
Returns an XStream& without modification for terminating case.
Definition: MHO_Meta.hh:155
void apply_at2(const XTupleType &tup1, XTupleType &tup2, size_t index, XFunctorType &functor)
Definition: MHO_Meta.hh:325
typename MHO_TypelistSizeImpl< L >::type MHO_TypelistSize
Class MHO_TypelistSize - alias to MHO_TypelistSize, retrieve the value itself with value (element of ...
Definition: MHO_Meta.hh:56
std::map< typename XContainer1::value_type, typename XContainer2::value_type > zip_into_map(const XContainer1 &c1, const XContainer2 &c2)
zip elements of two iterable (probably STL) containers (which define a value_type) into a map which t...
Definition: MHO_Meta.hh:359
std::enable_if<(N >=sizeof...(T)), XStream & >::type ostream_tuple(XStream &s, const std::tuple< T... > &)
Terminating case for ostream_tuple, does nothing and returns s.
Definition: MHO_Meta.hh:118
std::integral_constant< size_t, sizeof...(T) > type
Definition: MHO_Meta.hh:50
void apply_at(const XTupleType &tup, size_t index, XFunctorType &functor)
Applies a functor to an element at a specified index in a tuple.
Definition: MHO_Meta.hh:280
Definition: MHO_Meta.hh:21
Class MHO_EmptyType.
Definition: MHO_Meta.hh:27
Class MHO_Typelist.
Definition: MHO_Meta.hh:34
Class MHO_TypelistSizeImpl.
Definition: MHO_Meta.hh:40
static void apply(XTupleType &tup1, XTupleType2 &tup2, size_t index, XFunctorType &functor)
Definition: MHO_Meta.hh:319
Definition: MHO_Meta.hh:298
static void apply(XTupleType &tup1, XTupleType2 &tup2, size_t index, XFunctorType &functor)
Definition: MHO_Meta.hh:300
static void apply(XTupleType &tup, size_t index, XFunctorType &functor)
Applies a functor to an element of a tuple at a given index.
Definition: MHO_Meta.hh:267
Class apply_to_tuple - generic apply functor to tuple element (for runtime-indexed access)
Definition: MHO_Meta.hh:223
static void apply(XTupleType &tup, size_t index, XFunctorType &functor)
Applies a functor to an element of a tuple at a specified index.
Definition: MHO_Meta.hh:235
typename std::tuple_element< 0, std::tuple< T... > >::type current_type
Definition: MHO_Meta.hh:89
Class count_instances_of_type - utility to count the instances of a particular type in a parameter pa...
Definition: MHO_Meta.hh:98
typename std::tuple_element< N, std::tuple< T... > >::type current_type
Definition: MHO_Meta.hh:99
constexpr static std::size_t value
Definition: MHO_Meta.hh:100
static void visit(XTupleType &tup, XFunctorType &functor)
Recursively applies functor to tuple elements and then itself.
Definition: MHO_Meta.hh:213
Class indexed_tuple_visit - generic apply functor (which takes and index value!) to all elements of a...
Definition: MHO_Meta.hh:186
static void visit(XTupleType &tup, XFunctorType &functor)
Applies a functor to all elements of an XTupleType tuple and recursively visits the next type.
Definition: MHO_Meta.hh:193
Definition: MHO_Meta.hh:341
Class is_same_count - utility to return 1 if two types are the same, zero otherwise.
Definition: MHO_Meta.hh:62
Definition: vex.h:175