SUNphi  1.0
TupleOrder.hpp
Go to the documentation of this file.
1 #ifndef _TUPLEORDER_HPP
2 #define _TUPLEORDER_HPP
3 
4 /// \file TupleOrder.hpp
5 ///
6 /// \brief Defines filtering and reordering of Tuple
7 
8 #include <ints/Ranges.hpp>
9 #include <tuple/TupleClass.hpp>
10 #include <utility/Position.hpp>
11 
12 namespace SUNphi
13 {
14  /// Gets the position of a type in a tuple
15  ///
16  /// Internal implementation, empty case
17  template <bool assertIfNotPresent,
18  typename T>
19  constexpr int _posOfType(T,Tuple<>)
20  {
21  // Return NOT_PRESENT or assert if empty
22  static_assert(assertIfNotPresent==DO_NOT_ASSERT_IF_NOT_PRESENT,"Type not found in the list");
23 
24  return NOT_PRESENT;
25  }
26 
27  /// Gets the position of a type in a tuple
28  ///
29  /// Internal implementation, matching case
30  template <bool assertIfNotPresent,
31  typename T,
32  typename Head,
33  typename...Tail>
34  constexpr int _posOfType(T,Tuple<Head,Tail...>)
35  {
36  /// Check if T is of the same type of Head
37  constexpr bool isHead=
38  isSame<T,Head>;
39 
40  /// Position in the nested list
41  constexpr int posInTail=
42  _posOfType<DO_NOT_ASSERT_IF_NOT_PRESENT>(T{},Tuple<Tail...>{});
43 
44  /// Mark if present in the Tail
45  constexpr bool isInTail=
46  posInTail!=NOT_PRESENT;
47 
48  // Matching case
49  if constexpr(isHead)
50  {
51  static_assert(not isInTail,"Multiple case present!");
52 
53  return 0;
54  }
55  else
56  if constexpr(isInTail)
57  return 1+posInTail;
58  else
59  return NOT_PRESENT;
60  };
61 
62  /// Gets the position of a type in a tuple or list, asserting if not present
63  ///
64  /// Wraps the actual implementation
65  ///
66  /// \code
67  /// int a=posOfType<int,int,double,char>; //0
68  /// int b=posOfType<int,Tuple<int,double,char>>; //0
69  /// int c=posOfType<int,char,double,char>; //static_assert (not found)
70  /// int d=posOfType<int,int,int,char>; //static_assert (multiple occurency)
71  /// \endcode
72  template <typename T,
73  typename Tp>
74  [[ maybe_unused ]]
75  constexpr int posOfType=
76  _posOfType<ASSERT_IF_NOT_PRESENT>(T{},Tp{});
77 
78  /// Gets the position of a type in a tuple or list, not asserting if not present
79  ///
80  /// Wraps the actual implementation
81  ///
82  /// \code
83  /// int a=posOfType<int,int,double,char>; //0
84  /// int b=posOfType<int,Tuple<int,double,char>>; //0
85  /// int c=posOfType<int,char,double,char>; //NOT_PRESENT
86  /// int d=posOfType<int,int,int,char>; //static_assert (multiple occurency)
87  /// \endcode
88  template <typename T,
89  typename Tp>
90  [[ maybe_unused ]]
91  constexpr int posOfTypeNotAsserting=
92  _posOfType<DO_NOT_ASSERT_IF_NOT_PRESENT>(T{},Tp{});
93 
94  /////////////////////////////////////////////////////////////////
95 
96  /// Position of the types \c T in the Tuple \c TP, asserting or not
97  template <bool assertIfNotPresent, // Flag determining whether has to be assert or return NOT_PRESENT
98  typename...T, // Types to be searched
99  typename TP> // Type of the \c Tuple where to search
100  auto _posOfTypes(Tuple<T...> t, ///< Holds the types to be searched
101  TP tp) ///< \c Tuple where to search
102  -> IntSeq<_posOfType<assertIfNotPresent>(T{},TP{})...>;
103 
104  /// Position of the Tuple types \c TpToSearch in the Tuple \c TpToProbe, asserting
105  ///
106  /// An expection is issued if the type is not founs
107  template <typename TpToSearch, // Tuple containing the types to be searched
108  typename TpToProbe> // Tuple where to search
109  using PosOfTypes=
110  decltype(_posOfTypes<ASSERT_IF_NOT_PRESENT>(TpToSearch{},TpToProbe{}));
111 
112  /// Position of the Tuple types \c TpToSearch in the Tuple \c TpToProbe, not asserting
113  ///
114  /// NOT_PRESENT is returned if the type is not present
115  template <typename TpToSearch, // Tuple containing the types to be searched
116  typename TpToProbe> // Tuple where to search
117  using PosOfTypesNotAsserting=
118  decltype(_posOfTypes<DO_NOT_ASSERT_IF_NOT_PRESENT>(TpToSearch{},TpToProbe{}));
119 }
120 
121 #endif
constexpr int posOfTypeNotAsserting
Definition: TupleOrder.hpp:91
auto _posOfTypes(Tuple< T... > t, TP tp) -> IntSeq< _posOfType< assertIfNotPresent >(T
Position of the types T in the Tuple TP, asserting or not.
Definition: TupleOrder.hpp:100
constexpr int posOfType
Definition: TupleOrder.hpp:75