Dune Core Modules (2.3.1)

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 #ifndef DUNE_TYPETRAITS_HH
4 #define DUNE_TYPETRAITS_HH
5 
6 #if defined HAVE_TYPE_TRAITS
7 #include <type_traits>
8 #elif defined HAVE_TR1_TYPE_TRAITS
9 #include <tr1/type_traits>
10 #endif
11 
13 
14 namespace Dune
15 {
16 
30  struct Empty {};
31 
42  template <typename T>
43  class TypeTraits
44  {
45  private:
46  template <class U>
47  struct PointerTraits {
48  enum { result = false };
49  typedef Empty PointeeType;
50  };
51 
52  template <class U>
53  struct PointerTraits<U*> {
54  enum { result = true };
55  typedef U PointeeType;
56  };
57 
58  template <class U> struct ReferenceTraits
59  {
60  enum { result = false };
61  typedef U ReferredType;
62  };
63 
64  template <class U> struct ReferenceTraits<U&>
65  {
66  enum { result = true };
67  typedef U ReferredType;
68  };
69 
70  public:
71  enum { isPointer = PointerTraits<T>::result };
72  typedef typename PointerTraits<T>::PointeeType PointeeType DUNE_DEPRECATED_MSG("Use remove_pointer instead!");
73 
74  enum { isReference = ReferenceTraits<T>::result };
75  typedef typename ReferenceTraits<T>::ReferredType ReferredType DUNE_DEPRECATED_MSG("Use remove_reference instead!");
76  };
77 
82  template<typename T>
84  {
85  enum {
87  isVolatile=false,
89  isConst=false
90  };
91 
93  typedef T UnqualifiedType;
95  typedef const T ConstType;
97  typedef const volatile T ConstVolatileType;
98  };
99 
100  template<typename T>
101  struct ConstantVolatileTraits<const T>
102  {
103  enum {
104  isVolatile=false, isConst=true
105  };
106  typedef T UnqualifiedType;
107  typedef const UnqualifiedType ConstType;
108  typedef const volatile UnqualifiedType ConstVolatileType;
109  };
110 
111 
112  template<typename T>
113  struct ConstantVolatileTraits<volatile T>
114  {
115  enum {
116  isVolatile=true, isConst=false
117  };
118  typedef T UnqualifiedType;
119  typedef const UnqualifiedType ConstType;
120  typedef const volatile UnqualifiedType ConstVolatileType;
121  };
122 
123  template<typename T>
124  struct ConstantVolatileTraits<const volatile T>
125  {
126  enum {
127  isVolatile=true, isConst=true
128  };
129  typedef T UnqualifiedType;
130  typedef const UnqualifiedType ConstType;
131  typedef const volatile UnqualifiedType ConstVolatileType;
132  };
133 
135  template<typename T>
136  struct IsVolatile
137  {
138  enum {
141  };
142  };
143 
145  template<typename T>
146  struct IsConst
147  {
148  enum {
151  };
152  };
153 
154  template<typename T, bool isVolatile>
155  struct RemoveConstHelper
156  {
157  typedef typename ConstantVolatileTraits<T>::UnqualifiedType Type;
158  };
159 
160  template<typename T>
161  struct RemoveConstHelper<T,true>
162  {
163  typedef volatile typename ConstantVolatileTraits<T>::UnqualifiedType Type;
164  };
165 
166 #if defined HAVE_TYPE_TRAITS
167  using std::remove_const;
168 #elif defined HAVE_TR1_TYPE_TRAITS
169  using std::tr1::remove_const;
170 #else
174  template<typename T>
176  {
177  typedef typename RemoveConstHelper<T, IsVolatile<T>::value>::Type type;
178  };
179 #endif
180 
181 #if defined HAVE_TYPE_TRAITS
182  using std::remove_reference;
183 #elif defined HAVE_TR1_TYPE_TRAITS
184  using std::tr1::remove_reference;
185 #else
187 
191  template<typename T> struct remove_reference {
193  typedef T type;
194  };
195 # ifndef DOXYGEN
196  template<typename T> struct remove_reference<T&> {
197  typedef T type;
198  };
199 # endif // ! defined(DOXYGEN)
200 #endif
201 
211  template<class From, class To>
213  {
214  typedef char Small;
215  struct Big {char dummy[2];};
216  static Small test(To);
217  static Big test(...);
218  static typename remove_reference< From >::type &makeFrom ();
219 
220  public:
221  enum {
223  exists = sizeof(test(makeFrom())) == sizeof(Small),
227  sameType = false
228  };
229  Conversion(){}
230 
231  };
232 
233  template <class From>
234  class Conversion<From, void>
235  {
236  public:
237  enum {
238  exists = false,
239  isTwoWay = false,
240  sameType = false
241  };
242  };
243 
244  template <class To>
245  class Conversion<void, To>
246  {
247  public:
248  enum {
249  exists = false,
250  isTwoWay = false,
251  sameType = false
252  };
253  };
254 
255  template<>
256  class Conversion< int, double >
257  {
258  public:
259  enum {
260  exists = true,
261  isTwoWay = false,
262  sameType = false
263  };
264  };
265 
266  template<class T>
267  class Conversion<T,T>{
268  public:
269  enum { exists=true, isTwoWay=true, sameType=true};
270  };
271 
281  template <class Base, class Derived>
282  class IsBaseOf
283  {
284  typedef typename ConstantVolatileTraits< typename remove_reference< Base >::type >::UnqualifiedType RawBase;
285  typedef typename ConstantVolatileTraits< typename remove_reference< Derived >::type >::UnqualifiedType RawDerived;
286  typedef char Small;
287  struct Big {char dummy[2];};
288  static Small test(RawBase*);
289  static Big test(...);
290  static RawDerived* &makePtr ();
291  public:
292  enum {
294  value = sizeof(test(makePtr())) == sizeof(Small)
295  };
296  IsBaseOf(){}
297 
298  };
299 
306  template<class T1, class T2>
308  {
309  enum {
315  };
316  };
317 
318 #ifdef HAVE_TYPE_TRAITS
319  using std::enable_if;
320 #else
327  template<bool b, typename T=void>
328  struct enable_if
329  {
330  typedef T type;
331  };
332 
333  template<typename T>
334  struct enable_if<false,T>
335  {};
336 #endif
337 
338 
344  template<class T1, class T2, class Type>
346  : public enable_if<IsInteroperable<T1,T2>::value, Type>
347  {};
348 
349 #if defined HAVE_TYPE_TRAITS
350  using std::is_same;
351 #elif defined HAVE_TR1_TYPE_TRAITS
352  using std::tr1::is_same;
353 #else
358  template<typename T1, typename T2>
359  struct is_same
360  {
362  enum {
363  /* @brief Whether T1 is the same type as T2. */
364  value=false
365  };
366  };
367 
368 
369  template<typename T>
370  struct is_same<T,T>
371  {
372  enum { value=true};
373  };
374 #endif
375 
386  template<bool first, class T1, class T2>
387  struct SelectType
388  {
395  typedef T1 Type DUNE_DEPRECATED_MSG("Use Dune::conditional::type instead");
396  } DUNE_DEPRECATED;
397 
398  template<class T1, class T2>
399  struct SelectType<false,T1,T2>
400  {
401  typedef T2 Type DUNE_DEPRECATED_MSG("Use Dune::conditional::type instead");
402  };
403 
404 #if DOXYGEN || !HAVE_STD_CONDITIONAL
405 
417  template<bool first, class T1, class T2>
418  struct conditional
419  {
426  typedef T1 type;
427  };
428 
429  template<class T1, class T2>
430  struct conditional<false,T1,T2>
431  {
432  typedef T2 type;
433  };
434 
435 #else // DOXYGEN || !HAVE_STD_CONDITIONAL
436 
437  // pull in default implementation
438  using std::conditional;
439 
440 #endif // DOXYGEN || !HAVE_STD_CONDITIONAL
441 
443  //
444  // integral_constant (C++0x 20.7.3 "Helper classes")
445  //
446 #if HAVE_INTEGRAL_CONSTANT
447  using std::integral_constant;
448  using std::true_type;
449  using std::false_type;
450 #else // #if HAVE_INTEGRAL_CONSTANT
452 
456  template <class T, T v>
459  static const T value = v;
461  typedef T value_type;
465  operator value_type() { return value; }
466  };
467 
472 #endif // #else // #if HAVE_INTEGRAL_CONSTANT
473 
474  template<typename>
475  struct __is_pointer_helper
476  : public false_type { };
477 
478  template<typename T>
479  struct __is_pointer_helper<T*>
480  : public true_type { };
481 
483  template<typename T>
484  struct is_pointer
485  : public integral_constant<bool, (__is_pointer_helper<T>::value)>
486  { };
487 
488  // Helper class for is_lvalue_reference
489  template<typename>
490  struct __is_lvalue_reference_helper
491  : public false_type { };
492 
493  template<typename T>
494  struct __is_lvalue_reference_helper<T&>
495  : public true_type { };
496 
498  template<typename T>
500  : public integral_constant<bool, (__is_lvalue_reference_helper<T>::value)>
501  { };
502 
503  template<typename _Tp>
504  struct __remove_pointer_helper
505  { typedef _Tp type; };
506 
507  template<typename _Tp>
508  struct __remove_pointer_helper<_Tp*>
509  { typedef _Tp type; };
510 
516  template<typename _Tp>
518  : public __remove_pointer_helper<typename remove_const<_Tp>::type >
519  { };
520 
522 }
523 #endif
Checks wether a type is convertible to another.
Definition: typetraits.hh:213
@ sameType
True if To and From are the same type.
Definition: typetraits.hh:227
@ exists
True if the conversion exists.
Definition: typetraits.hh:223
@ isTwoWay
Whether the conversion exists in both ways.
Definition: typetraits.hh:225
Checks wether a type is derived from another.
Definition: typetraits.hh:283
@ value
True if Base is a base class of Derived.
Definition: typetraits.hh:294
General type traits class to check whether type is reference or pointer type.
Definition: typetraits.hh:44
Definition of the DUNE_DEPRECATED macro for the case that config.h is not available.
integral_constant< bool, true > true_type
type for true
Definition: typetraits.hh:469
#define DUNE_DEPRECATED_MSG(text)
Mark some entity as deprecated.
Definition: deprecated.hh:169
#define DUNE_DEPRECATED
Mark some entity as deprecated.
Definition: deprecated.hh:84
integral_constant< bool, false > false_type
type for false
Definition: typetraits.hh:471
Dune namespace.
Definition: alignment.hh:14
Determines wether a type is const or volatile and provides the unqualified types.
Definition: typetraits.hh:84
const volatile T ConstVolatileType
The const volatile type.
Definition: typetraits.hh:97
T UnqualifiedType
The unqualified type.
Definition: typetraits.hh:93
@ isConst
True if T has a const qualifier.
Definition: typetraits.hh:89
@ isVolatile
True if T has a volatile specifier.
Definition: typetraits.hh:87
const T ConstType
The const type.
Definition: typetraits.hh:95
Just an empty class.
Definition: typetraits.hh:30
Enable typedef if two types are interoperable.
Definition: typetraits.hh:347
Tests wether a type is constant.
Definition: typetraits.hh:147
@ value
True if The type is constant.
Definition: typetraits.hh:150
Checks wether two types are interoperable.
Definition: typetraits.hh:308
@ value
True if either a conversion from T1 to T2 or vice versa exists.
Definition: typetraits.hh:314
Tests wether a type is volatile.
Definition: typetraits.hh:137
@ value
True if The type is volatile.
Definition: typetraits.hh:140
Select a type based on a condition.
Definition: typetraits.hh:388
T1 Type DUNE_DEPRECATED_MSG("Use Dune::conditional::type instead")
The selected type.
Definition: typetraits.hh:395
Select a type based on a condition.
Definition: typetraits.hh:419
T1 type
The selected type.
Definition: typetraits.hh:426
Enable typedef if condition is met.
Definition: typetraits.hh:329
Generate a type for a given integral constant.
Definition: typetraits.hh:457
static const T value
value this type was generated for
Definition: typetraits.hh:459
T value_type
type of value
Definition: typetraits.hh:461
integral_constant< T, v > type
type of this class itself
Definition: typetraits.hh:463
Determine whether a type is a lvalue reference type.
Definition: typetraits.hh:501
is_pointer
Definition: typetraits.hh:486
Compile time test for testing whether two types are the same.
Definition: typetraits.hh:360
Removes a const qualifier while preserving others.
Definition: typetraits.hh:176
Return the type a pointer type points to.
Definition: typetraits.hh:519
Remove a reference from a type.
Definition: typetraits.hh:191
T type
T with references removed.
Definition: typetraits.hh:193
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.80.0 (May 15, 22:30, 2024)