AMDiS  0.3
The Adaptive Multi-Dimensional Simulation Toolbox
TypeTraits.hpp
1 #pragma once
2 
3 #include <memory>
4 #include <type_traits>
5 
6 namespace AMDiS
7 {
9 
16  template <class T>
17  struct remove_cvref
18  {
19  using type = std::remove_cv_t<std::remove_reference_t<T>>;
20  };
21 
23  template <class T>
24  using remove_cvref_t = typename remove_cvref<T>::type;
25 
26  namespace Impl
27  {
28  template <class T>
29  struct UnderlyingType
30  {
31  using type = T;
32  };
33 
34  template <class T>
35  struct UnderlyingType<const T>
36  {
37  using type = typename UnderlyingType<T>::type;
38  };
39 
40  template <class T>
41  struct UnderlyingType<T&>
42  {
43  using type = typename UnderlyingType<T>::type;
44  };
45 
46  template <class T>
47  struct UnderlyingType<T*>
48  {
49  using type = typename UnderlyingType<T>::type;
50  };
51 
52  template <class T>
53  struct UnderlyingType<std::reference_wrapper<T>>
54  {
55  using type = typename UnderlyingType<T>::type;
56  };
57 
58  template <class T>
59  struct UnderlyingType<std::shared_ptr<T>>
60  {
61  using type = typename UnderlyingType<T>::type;
62  };
63 
64  template <class T>
65  struct UnderlyingType<std::unique_ptr<T>>
66  {
67  using type = typename UnderlyingType<T>::type;
68  };
69  }
70 
71  template <class T>
72  using Underlying_t = typename Impl::UnderlyingType<T>::type;
73 
74 
75  // overload for l-values
76  template <class T>
77  T& underlying(T& value)
78  {
79  return value;
80  }
81 
82  // overload for l-value pointers
83  template <class T>
84  T& underlying(T* value)
85  {
86  return *value;
87  }
88 
89  // overload for plain r-values is deleted
90  template <class T>
91  T underlying(T&& value) = delete;
92 
93  // overload for reference-wrappers
94  template <class T>
95  T& underlying(std::reference_wrapper<T> const& value)
96  {
97  return value.get();
98  }
99 
100  // overload for shared_ptr
101  template <class T>
102  T& underlying(std::shared_ptr<T> const& value)
103  {
104  return *value;
105  }
106 
107  // overload for unique_ptr
108  template <class T>
109  T& underlying(std::unique_ptr<T> const& value)
110  {
111  return *value;
112  }
113 
114 
116  #define FWD(...) static_cast<decltype(__VA_ARGS__)>(__VA_ARGS__)
117 
119  #define TYPEOF(...) AMDiS::remove_cvref_t<decltype(__VA_ARGS__)>
120 
122  #define VALUE(...) TYPEOF(__VA_ARGS__)::value
123 
124  // ---------------------------------------------------------------------------
125 
127  template <class... Ts>
128  struct Types {};
129 
130  template <class... Ts>
131  using Types_t = Types<remove_cvref_t<Ts>...>;
132 
133 
135  template <class T>
136  using owner = T;
137 
139  struct NoOp
140  {
141  template <class... T>
142  constexpr void operator()(T&&...) const { /* no nothing */ }
143  };
144 
146  template <class Obj>
147  auto makeUniquePtr(Obj&& obj)
148  {
149  return std::make_unique<TYPEOF(obj)>(FWD(obj));
150  }
151 
152  template <class Obj>
153  auto makeSharedPtr(Obj&& obj)
154  {
155  return std::make_shared<TYPEOF(obj)>(FWD(obj));
156  }
157 
158  template <bool... b>
159  using enable_if_all_t = std::enable_if_t<(b &&...)>;
160 
161 } // end namespace AMDiS
A functor with no operation.
Definition: TypeTraits.hpp:139
A variadic type list.
Definition: TypeTraits.hpp:128
Definition: AdaptBase.hpp:6
Remove cv and ref qualifiers of type T.
Definition: TypeTraits.hpp:17