siplasplas
object.hpp
1 #ifndef SIPLASPLAS_REFLECTION_DYNAMIC_OBJECT_HPP
2 #define SIPLASPLAS_REFLECTION_DYNAMIC_OBJECT_HPP
3 
4 #include "type.hpp"
5 
6 #include <memory>
7 #include <cassert>
8 #include <type_traits>
9 
10 namespace cpp
11 {
12 
13 namespace dynamic_reflection
14 {
15 
16 class SIPLASPLAS_REFLECTION_DYNAMIC_EXPORT Object
17 {
18 public:
19  static constexpr bool ConstructReference = true;
20 
21  enum class Kind
22  {
23  VALUE,
24  REFERENCE,
25  POINTER
26  };
27 
28  Object();
30  Object(const cpp::dynamic_reflection::Type& type, void* fromRaw, bool isReference = false);
31  template<typename T, typename = std::enable_if_t<
32  !std::is_same<std::decay_t<T>, cpp::dynamic_reflection::Type>::value &&
33  !std::is_same<std::decay_t<T>, Object>::value &&
34  !std::is_pointer<std::decay_t<T>>::value
35  >>
36  Object(T&& value) :
37  _type{cpp::dynamic_reflection::Type::get<std::decay_t<T>>()},
38  _kind{Kind::VALUE},
39  _object{_type.copy_construct(&value)}
40  {}
41  template<typename T>
42  Object(const T* pointer) :
43  _type{cpp::dynamic_reflection::Type::get<T*>()},
44  _kind{Kind::POINTER},
45  _object{const_cast<T*>(pointer)}
46  {}
47 
48  Object(const Object& other);
49  Object(Object&& other);
50  Object& operator=(const Object& other);
51  Object& operator=(Object&& other);
52  ~Object();
53 
54  template<typename T>
55  Object& operator=(const T& value)
56  {
57  get<T>() = value;
58  return *this;
59  }
60 
61  template<typename T>
62  T& get() const
63  {
64  return *reinterpret_cast<std::remove_reference_t<T>*>(_object);
65  }
66 
67  template<typename T>
68  operator T&() const
69  {
70  return get<T>();
71  }
72 
73  cpp::dynamic_reflection::Type type() const;
74  bool isReference() const;
75  Kind kind() const;
76  bool empty() const;
77 
78  const void* raw() const;
79  void* raw();
80 
81  std::string toString() const;
82  static Object fromString(const std::string& typeName, const std::string& value);
83 
84 private:
86  Kind _kind;
87  void* _object;
88 
89  void destroy();
90 };
91 
92 }
93 }
94 
95 #endif // SIPLASPLAS_REFLECTION_DYNAMIC_OBJECT_HPP
Definition: meta.hpp:31
Definition: messaging.hpp:8
void destroy(T &object)
Invokes the destructor of an object.
Definition: destroy.hpp:16
Definition: type.hpp:45
Definition: object.hpp:16