DUNE-FEM (unstable)

typeindexedtuple.hh
1#ifndef DUNE_FEM_COMMON_TYPEINDEXEDTUPLE_HH
2#define DUNE_FEM_COMMON_TYPEINDEXEDTUPLE_HH
3
4#include <tuple>
5
7#include <dune/fem/common/tupleutility.hh>
8
9namespace Dune
10{
11
12 // TypeIndexedTuple
13 // ----------------
14
15 /*
16 * \brief Please doc me.
17 */
18 template< class Tuple, class Types >
19 class TypeIndexedTuple
20 {
21 template< class T >
22 struct Position
23 {
24 static const int value = Dune::FirstTypeIndex< Types, T >::value;
25 };
26
27 // value selector for types that are contained
28 template< class T, bool contained >
29 struct ValueBase
30 {
31 typedef typename std::tuple_element< Position< T >::value, Tuple >::type Type;
32 static Type& at( Tuple& tuple )
33 {
34 return std::get< Position< T >::value >( tuple );
35 }
36 static const Type& at( const Tuple& tuple )
37 {
38 return std::get< Position< T >::value >( tuple );
39 }
40 };
41
42 // value selector for types that are not contained
43 template< class T >
44 struct ValueBase< T, false > // T is not contained in Tuple
45 {
46 typedef Tuple Type;
47 static Type& at( Tuple& tuple )
48 {
49 return tuple;
50 }
51 static const Type& at( const Tuple& tuple )
52 {
53 return tuple;
54 }
55 };
56
57 public:
58 template <class T>
59 struct Contains
60 {
61 static const bool value = ContainsType< Types, T >::value;
62 };
63
64 template< class T >
65 struct Value : public ValueBase< T, Contains< T >::value >
66 {
67 };
68
69 explicit TypeIndexedTuple ( const Tuple &tuple = Tuple() )
70 : tuple_( tuple )
71 {}
72
74 template< class T >
75 typename Value< T >::Type &at ()
76 {
77 return Value< T >::at( tuple_ );
78 }
79
81 template< class T >
82 const typename Value< T >::Type &at () const
83 {
84 return Value< T >::at( tuple_ );
85 }
86
88 template< class T >
89 typename Value< T >::Type &operator[] ( const T & )
90 {
91 return at< T >();
92 }
93
95 template< class T >
96 const typename Value< T >::Type &operator[] ( const T & ) const
97 {
98 return at< T >();
99 }
100
102 template< class T >
103 bool active( const T& ) const { return Contains< T >::value; }
104
106 operator Tuple & () { return tuple_; }
107
109 operator const Tuple & () const { return tuple_; }
110
111 private:
112 Tuple tuple_;
113 };
114
115
116
117 // get for TypeIndexedTuple
118 // ------------------------
119
120 template< int i, class Tuple, class Types >
121 typename std::tuple_element< i, Tuple >::type &
122 get ( Dune::TypeIndexedTuple< Tuple, Types > &tuple )
123 {
124 return std::get< i >( static_cast< Tuple & >( tuple ) );
125 }
126
127 template< int i, class Tuple, class Types >
128 const typename std::tuple_element< i, Tuple >::type &
129 get ( const Dune::TypeIndexedTuple< Tuple, Types > &tuple )
130 {
131 return std::get< i >( static_cast< const Tuple & >( tuple ) );
132 }
133
134} // namespace Dune
135
136
137// TODO please check this construction, later.
138// At the moment it is needed to make dune-fem-dg compile!
139namespace std
140{
141 template< size_t i, class Tuple, class Types >
142 class tuple_element< i, Dune::TypeIndexedTuple< Tuple, Types > >
143 {
144 public:
145 typedef typename std::tuple_element< i, Tuple >::type type;
146 };
147}
148
149#endif // #ifndef DUNE_FEM_COMMON_TYPEINDEXEDTUPLE_HH
Contains utility classes which can be used with std::tuple.
Dune namespace.
Definition: alignedallocator.hh:13
constexpr auto get(std::integer_sequence< T, II... >, std::integral_constant< std::size_t, pos >={})
Return the entry at position pos of the given sequence.
Definition: integersequence.hh:22
STL namespace.
Find the first occurrence of a type in a std::tuple.
Definition: tupleutility.hh:467
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Nov 13, 23:29, 2024)