DUNE PDELab (git)

typetraits.hh
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>
11
12#include <dune/typetree/treepath.hh>
13#include <dune/typetree/nodeinterface.hh>
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>
32 struct has_node_tag
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>
47 struct has_node_tag_value
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>
55 static maybe<std::is_base_of<V, NodeTag<X>>::value>
56 test(NodeTag<X> * a);
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>
65 struct has_implementation_tag
66 {
67 struct yes { char dummy[1]; };
68 struct no { char dummy[2]; };
69
70 template<typename X>
71 static yes test(ImplementationTag<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>
80 struct has_implementation_tag_value
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>
88 static maybe<std::is_base_of<V, ImplementationTag<X>>::value>
89 test(ImplementationTag<X> * a);
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>
98 struct AlwaysVoid
99 {
100 typedef void type;
101 };
102
103
105 template<typename T>
106 T* declptr();
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
Traits for type conversions and type information.
Dune namespace.
Definition: alignedallocator.hh:13
Check if type represents a tree path.
Definition: typetraits.hh:184
Meta function that evaluates its argument iff it inherits from meta_function.
Definition: typetraits.hh:142
Helper meta function to delay evaluation of F.
Definition: typetraits.hh:128
Identity function.
Definition: typetraits.hh:135
Marker tag declaring a meta function.
Definition: typetraits.hh:123
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Jul 15, 22:36, 2024)