siplasplas
typeinfo.hpp
1 #ifndef SIPLASPLAS_TYPEERASURE_TYPEINFO_HPP
2 #define SIPLASPLAS_TYPEERASURE_TYPEINFO_HPP
3 
4 #include "features/valuesemantics.hpp"
5 #include <siplasplas/utility/memory_manip.hpp>
6 #include <siplasplas/utility/function_traits.hpp>
7 #include <siplasplas/utility/typeinfo.hpp>
8 #include <siplasplas/constexpr/arrayview.hpp>
9 #include <siplasplas/constexpr/meta.hpp>
10 
11 namespace cpp
12 {
13 
14 namespace typeerasure
15 {
16 
17 namespace detail
18 {
19 
24 enum class ValueSemanticsOperation : std::size_t
25 {
26  DEFAULT_CONSTRUCT = 0,
27  COPY_CONSTRUCT = 1,
28  MOVE_CONSTRUCT = 2,
29  COPY_ASSIGN = 3,
30  MOVE_ASSIGN = 4,
31  DESTROY = 5,
32  END_OF_ENUM
33 };
34 
35 using ValueSemanticsOperationFunction = void(*)(void*, const void*);
36 
58 template<typename T>
59 ValueSemanticsOperationFunction valueSemanticsOperation(ValueSemanticsOperation operation)
60 {
61  static ValueSemanticsOperationFunction operations[static_cast<std::size_t>(ValueSemanticsOperation::END_OF_ENUM)] = {
62  [](void* object, const void*) {
63  features::DefaultConstructible::apply<T>(object);
64  },
65  [](void* object, const void* other) {
66  features::CopyConstructible::apply<T>(object, other);
67  },
68  [](void* object, const void* other) {
69  features::MoveConstructible::apply<T>(object, const_cast<void*>(other));
70  },
71  [](void* object, const void* other) {
72  features::CopyAssignable::apply<T>(object, other);
73  },
74  [](void* object, const void* other) {
75  features::MoveAssignable::apply<T>(object, const_cast<void*>(other));
76  },
77  [](void* object, const void*) {
78  features::Destructible::apply<T>(object);
79  }
80  };
81 
82  return operations[static_cast<std::size_t>(operation)];
83 }
84 
85 using ValueSemantics = decltype(&valueSemanticsOperation<int>);
86 
87 }
88 
115 class TypeInfo : public cpp::TypeInfo
116 {
117 public:
121  detail::ValueSemantics semantics() const
122  {
123  return _semantics;
124  }
125 
129  bool isPointer() const
130  {
131  return cpp::TypeInfo::isPointer();
132  }
133 
138  detail::ValueSemanticsOperationFunction semantics(detail::ValueSemanticsOperation operation) const
139  {
140  return semantics()(operation);
141  }
142 
149  void defaultConstruct(void* where) const
150  {
151  semantics(detail::ValueSemanticsOperation::DEFAULT_CONSTRUCT)(where, nullptr);
152  }
153 
161  void copyConstruct(void* where, const void* other) const
162  {
163  semantics(detail::ValueSemanticsOperation::COPY_CONSTRUCT)(where, other);
164  }
165 
174  void moveConstruct(void* where, void* other) const
175  {
176  semantics(detail::ValueSemanticsOperation::MOVE_CONSTRUCT)(where, const_cast<const void*>(other));
177  }
178 
186  void copyAssign(void* where, const void* other) const
187  {
188  semantics(detail::ValueSemanticsOperation::COPY_ASSIGN)(where, other);
189  }
190 
199  void moveAssign(void* where, void* other) const
200  {
201  semantics(detail::ValueSemanticsOperation::MOVE_ASSIGN)(where, const_cast<const void*>(other));
202  }
203 
210  void destroy(void* where) const
211  {
212  if(!isPointer())
213  {
214  semantics(detail::ValueSemanticsOperation::DESTROY)(where, nullptr);
215  }
216  }
217 
224  {
225  return _functionArgs;
226  }
227 
231  template<typename T>
232  static constexpr TypeInfo get()
233  {
234  return TypeInfo{meta::identity<T>()};
235  }
236 
240  template<typename T>
241  static constexpr TypeInfo get(const T&)
242  {
243  return TypeInfo::get<T>();
244  }
245 
246  friend constexpr bool operator==(const TypeInfo& lhs, const TypeInfo& rhs)
247  {
248  return lhs._semantics == rhs._semantics;
249  }
250 
251  friend constexpr bool operator!=(const TypeInfo& lhs, const TypeInfo& rhs)
252  {
253  return !(lhs == rhs);
254  }
255 
256 private:
257  struct TypeToTypeInfo
258  {
259  using value_type = TypeInfo;
260 
261  template<typename T>
262  static constexpr TypeInfo get()
263  {
264  return TypeInfo::get<T>();
265  }
266  };
267 
268  template<typename T>
269  constexpr TypeInfo(meta::identity<T>) :
270  cpp::TypeInfo{cpp::TypeInfo::get<T>()},
271  _semantics{detail::valueSemanticsOperation<std::decay_t<T>>},
272  _functionArgs{
274  cpp::function_arguments<T>,
275  TypeToTypeInfo
276  >::get()
277  }
278  {}
279 
280  detail::ValueSemantics _semantics;
282 
283 };
284 
285 }
286 
287 }
288 
289 #endif // SIPLASPLAS_TYPEERASURE_TYPEINFO_HPP
void moveConstruct(void *where, void *other) const
Move constructs values of the type If the passed arguments are not of the represented type...
Definition: typeinfo.hpp:174
ValueSemanticsOperation
Represents a value semantics operation.
Definition: typeinfo.hpp:24
Definition: arrayview.hpp:87
Definition: meta.hpp:31
Definition: messaging.hpp:8
void defaultConstruct(void *where) const
Default constructs a value of the type If the passed argument is not of the represented type...
Definition: typeinfo.hpp:149
void destroy(void *where) const
Destroys objects of the type If the passed arguments are not of the represented type, the behavior is undefined.
Definition: typeinfo.hpp:210
bool isPointer() const
Checks if the type is a pointer type.
Definition: typeinfo.hpp:129
ValueSemanticsOperationFunction valueSemanticsOperation(ValueSemanticsOperation operation)
Implements a type-erased interface for the value semantics features of a type T.
Definition: typeinfo.hpp:59
detail::ValueSemanticsOperationFunction semantics(detail::ValueSemanticsOperation operation) const
Returns the function implementing the given valuesemantics operation for the type.
Definition: typeinfo.hpp:138
Definition: test_util.hpp:13
void moveAssign(void *where, void *other) const
Move assigns values of the type If the passed arguments are not of the represented type...
Definition: typeinfo.hpp:199
Definition: meta.hpp:18
Contains minimal information to execute the value semantics operations of a type. ...
Definition: typeinfo.hpp:115
Definition: typeinfo.hpp:13
detail::ValueSemantics semantics() const
Retuns the type-erased semantics of the type.
Definition: typeinfo.hpp:121
constexpr cpp::constexp::ConstArrayView< TypeInfo > arguments() const
Returns the type information of the function arguments.
Definition: typeinfo.hpp:223
void copyConstruct(void *where, const void *other) const
Copy constructs values of the type If the passed arguments are not of the represented type...
Definition: typeinfo.hpp:161
void copyAssign(void *where, const void *other) const
Move assigns values of the type If the passed arguments are not of the represented type...
Definition: typeinfo.hpp:186