dune-typetree 2.10
Loading...
Searching...
No Matches
typetraits.hh
Go to the documentation of this file.
1// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2// vi: set et ts=4 sw=2 sts=2:
3// SPDX-FileCopyrightInfo: Copyright © DUNE Project contributors, see file LICENSE.md in module root
4// SPDX-License-Identifier: LGPL-3.0-or-later OR LicenseRef-GPL-2.0-only-with-PDELab-exception
5
6#ifndef DUNE_TYPETREE_TYPETRAITS_HH
7#define DUNE_TYPETREE_TYPETRAITS_HH
8
9#include <type_traits>
10#include <dune/common/typetraits.hh>
11
14
15namespace Dune {
16
17 // Provide some more C++11 TMP helpers.
18 // These should be upstreamed to dune-common ASAP.
19
20 template<typename... T>
21 struct first_type;
22
23 template<typename T0, typename... T>
24 struct first_type<T0,T...>
25 {
26 typedef T0 type;
27 };
28
29 namespace TypeTree {
30
31 template<typename T>
33 {
34 struct yes { char dummy[1]; };
35 struct no { char dummy[2]; };
36
37 template<typename X>
38 static yes test(NodeTag<X> *);
39 template<typename X>
40 static no test(...);
41
43 constexpr static bool value = sizeof(test<T>(0)) == sizeof(yes);
44 };
45
46 template<typename T, typename V>
48 {
49 template<int N>
50 struct maybe { char dummy[N+1]; };
51 struct yes { char dummy[2]; };
52 struct no { char dummy[1]; };
53
54 template<typename X>
57 template<typename X>
58 static no test(...);
59
61 constexpr static bool value = sizeof(test<T>(0)) == sizeof(yes);
62 };
63
64 template<typename T>
66 {
67 struct yes { char dummy[1]; };
68 struct no { char dummy[2]; };
69
70 template<typename X>
72 template<typename X>
73 static no test(...);
74
76 constexpr static bool value = sizeof(test<T>(0)) == sizeof(yes);
77 };
78
79 template<typename T, typename V>
81 {
82 template<int N>
83 struct maybe { char dummy[N+1]; };
84 struct yes { char dummy[2]; };
85 struct no { char dummy[1]; };
86
87 template<typename X>
90 template<typename X>
91 static no test(...);
92
94 constexpr static bool value = sizeof(test<T>(0)) == sizeof(yes);
95 };
96
97 template<typename>
99 {
100 typedef void type;
101 };
102
103
105 template<typename T>
107
108
109 // Support for lazy evaluation of meta functions. This is required when doing
110 // nested tag dispatch without C++11-style typedefs (based on using syntax).
111 // The standard struct-based meta functions cause premature evaluation in a
112 // context that is not SFINAE-compatible. We thus have to return the meta function
113 // without evaluating it, placing that burden on the caller. On the other hand,
114 // the lookup will often directly be the target type, so here is some helper code
115 // to automatically do the additional evaluation if necessary.
116 // Too bad that the new syntax is GCC 4.6+...
117
118
120
123 struct meta_function {};
124
126 template<typename F>
128 {
129 typedef typename F::type type;
130 };
131
133 template<typename F>
135 {
136 typedef F type;
137 };
138
140 template<typename F>
142 {
143 typedef typename std::conditional<
144 std::is_base_of<meta_function,F>::value,
147 >::type::type type;
148 };
149
150 namespace impl {
151
152 // Check if type is a or is derived from one of the tree path types
153
154 // Default overload for types not representing a tree path
155 constexpr auto isTreePath(void*)
156 -> std::false_type
157 {
158 return std::false_type();
159 }
160
161 // Overload for instances of HybridTreePath<...>
162 template<class... I>
163 constexpr auto isTreePath(const HybridTreePath<I...>*)
164 -> std::true_type
165 {
166 return std::true_type();
167 }
168
169 }
170
181 template<class T>
182 struct IsTreePath :
183 public decltype(impl::isTreePath((typename std::decay<T>::type*)(nullptr)))
184 {};
185
192 template<class T>
193 constexpr auto isTreePath(const T&)
195 {
196 return IsTreePath<T>();
197 }
198
199
200 } // end namespace TypeTree
201} // end namespace Dune
202
203#endif // DUNE_TYPETREE_TYPETRAITS_HH
typename std::decay_t< Node >::NodeTag NodeTag
Returns the node tag of the given Node.
Definition nodeinterface.hh:70
typename std::decay_t< T >::ImplementationTag ImplementationTag
Returns the implementation tag of the given Node.
Definition nodeinterface.hh:74
Definition accumulate_static.hh:16
constexpr auto isTreePath(const T &) -> IsTreePath< T >
Check if given object represents a tree path.
Definition typetraits.hh:193
T * declptr()
Helper function for generating a pointer to a value of type T in an unevaluated operand setting.
constexpr auto isTreePath(void *) -> std::false_type
Definition typetraits.hh:155
A hybrid version of TreePath that supports both compile time and run time indices.
Definition treepath.hh:158
Definition typetraits.hh:21
T0 type
Definition typetraits.hh:26
Definition typetraits.hh:33
static constexpr bool value
True if class T defines a NodeTag.
Definition typetraits.hh:43
static yes test(NodeTag< X > *)
Definition typetraits.hh:34
char dummy[1]
Definition typetraits.hh:34
Definition typetraits.hh:35
char dummy[2]
Definition typetraits.hh:35
Definition typetraits.hh:48
static maybe< std::is_base_of< V, NodeTag< X > >::value > test(NodeTag< X > *a)
static constexpr bool value
True if class T defines a NodeTag of type V.
Definition typetraits.hh:61
char dummy[N+1]
Definition typetraits.hh:50
Definition typetraits.hh:51
char dummy[2]
Definition typetraits.hh:51
Definition typetraits.hh:52
char dummy[1]
Definition typetraits.hh:52
Definition typetraits.hh:66
static yes test(ImplementationTag< X > *)
static constexpr bool value
True if class T defines an ImplementationTag.
Definition typetraits.hh:76
char dummy[1]
Definition typetraits.hh:67
char dummy[2]
Definition typetraits.hh:68
static maybe< std::is_base_of< V, ImplementationTag< X > >::value > test(ImplementationTag< X > *a)
static constexpr bool value
True if class T defines an ImplementationTag of type V.
Definition typetraits.hh:94
char dummy[N+1]
Definition typetraits.hh:83
char dummy[2]
Definition typetraits.hh:84
char dummy[1]
Definition typetraits.hh:85
Definition typetraits.hh:99
void type
Definition typetraits.hh:100
Marker tag declaring a meta function.
Definition typetraits.hh:123
Helper meta function to delay evaluation of F.
Definition typetraits.hh:128
F::type type
Definition typetraits.hh:129
Identity function.
Definition typetraits.hh:135
F type
Definition typetraits.hh:136
Meta function that evaluates its argument iff it inherits from meta_function.
Definition typetraits.hh:142
std::conditional< std::is_base_of< meta_function, F >::value, lazy_evaluate< F >, lazy_identity< F > >::type::type type
Definition typetraits.hh:147
Check if type represents a tree path.
Definition typetraits.hh:184