Gorgon Game Engine
Data.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "../Any.h"
4 #include "../Utils/Assert.h"
5 #include "Exceptions.h"
6 
7 namespace Gorgon {
8 
9  namespace Scripting {
10 
11  class Type;
12 
22  class Data {
23  public:
24 
27  static Data Invalid() { return {}; }
28 
31  Data() {}
32 
34  Data(const Data &other);
35 
37  Data(Data &&other);
38 
40  Data(const Type *type, const Any &data, bool isreference=false, bool isconstant=false);
41 
43  Data(const Type &type, const Any &data, bool isreference=false, bool isconstant=false) : Data(&type, data, isreference, isconstant) {
44  }
45 
47  Data(const Type &type);
48 
51 
52  bool IsNull() const;
53 
55  template <class T_>
56  typename std::enable_if<!std::is_pointer<T_>::value, const typename std::remove_reference<T_>::type &>::type
57  GetValue() const {
58  if(isconstant) {
59  if(IsReference())
60  return *data.Get<typename std::remove_reference<const T_>::type*>();
61  else
62  return data.Get<typename std::remove_reference<const T_>::type>();
63  }
64  else {
65  if(IsReference())
66  return *data.Get<typename std::remove_const<typename std::remove_reference<T_>::type>::type*>();
67  else
68  return data.Get<typename std::remove_const<typename std::remove_reference<T_>::type>::type>();
69  }
70  }
71 
73  template <class T_>
74  typename std::enable_if<std::is_pointer<T_>::value && std::is_const<typename std::remove_pointer<T_>::type>::value, T_>::type
75  GetValue() const {
76  if(isconstant) {
77  if(IsReference())
78  return data.Get<const typename std::remove_pointer<T_>::type *>();
79  else
80  throw CastException("Value", "Reference");
81  }
82  else {
83  if(IsReference())
84  return data.Get<typename std::remove_const<typename std::remove_pointer<T_>::type>::type*>();
85  else
86  throw CastException("Value", "Reference");
87  }
88  }
89 
91  template <class T_>
92  typename std::enable_if<std::is_pointer<T_>::value && !std::is_const<typename std::remove_pointer<T_>::type>::value, T_>::type
93  GetValue() const {
94  if(isconstant) {
95  if(IsReference())
96  throw CastException("Const reference", "Reference");
97  else
98  throw CastException("Value", "Reference");
99  }
100  else {
101  if(IsReference())
102  return data.Get<typename std::remove_const<typename std::remove_pointer<T_>::type>::type*>();
103  else
104  throw CastException("Value", "Reference");
105  }
106  }
107 
110  template <class T_>
111  typename std::enable_if<std::is_pointer<T_>::value, T_>::type
112  ReferenceValue() const {
113  ASSERT(type, "Type is not set", 1, 2);
114 
115  ASSERT(IsReference(), "The data contained is not a reference", 1, 10);
116 
117  return data.Get<typename std::remove_pointer<T_>::type *>();
118  }
119 
122  template <class T_>
123  typename std::enable_if<!std::is_pointer<T_>::value, T_>::type
124  ReferenceValue() const {
125  ASSERT(type, "Type is not set", 1, 2);
126 
127  ASSERT(IsReference(), "The data contained is not a reference", 1, 10);
128 
129  return *data.Get<typename std::remove_reference<T_>::type*>();
130  }
131 
133  Any GetData() const {
134  return data;
135  }
136 
137  Data GetReference();
138 
139  Data DeReference();
140 
141  void SetParent(const Data data) {
142  delete parent;
143  parent=new Data(data);
144  }
145 
146  void RemoveParent() {
147  delete parent;
148  parent=nullptr;
149  }
150 
152  bool IsValid() const {
153  return type != nullptr;
154  }
155 
157  bool IsReference() const;
158 
160  bool IsConstant() const {
161  return isconstant;
162  }
163 
165  void MakeConstant();
166 
168  void Delete() const;
169 
170  bool operator==(const Data &r) const;
171 
173  const Type &GetType() const {
174  ASSERT(type, "Type is not set", 1, 2);
175 
176  return *type;
177  }
178 
179  std::string ToString() const;
180 
181  virtual ~Data();
182 
183  protected:
186 
188  const Type *type = nullptr;
189 
191  bool isreference = false;
192 
194  bool isconstant = false;
195 
196  Data *parent = nullptr;
197  private:
198 
199  void check();
200  };
201 
202 
203  }
204 }
Gorgon::Scripting::Data::isreference
bool isreference
Is a reference, data is a ptr to the original type.
Definition: Data.h:191
Gorgon::Scripting::Data::DeReference
Data DeReference()
Definition: Data.cpp:180
Gorgon::Scripting::Data::GetValue
std::enable_if< std::is_pointer< T_ >::value &&!std::is_const< typename std::remove_pointer< T_ >::type >::value, T_ >::type GetValue() const
Returns the value of this data in the requested format.
Definition: Data.h:93
Gorgon::Scripting::Data::data
Any data
Stored data.
Definition: Data.h:185
Gorgon::Scripting::Data::~Data
virtual ~Data()
Definition: Data.cpp:128
Gorgon::Scripting::Data::ReferenceValue
std::enable_if< std::is_pointer< T_ >::value, T_ >::type ReferenceValue() const
Returns the value of this data in the requested format.
Definition: Data.h:112
Gorgon::Scripting::Data::Data
Data()
Constructs an invalid data.
Definition: Data.h:31
Gorgon::Any
This class can hold any other information providing type erasure.
Definition: Any.h:32
Gorgon::Scripting::Data::GetValue
std::enable_if<!std::is_pointer< T_ >::value, const typename std::remove_reference< T_ >::type & >::type GetValue() const
Returns the value of this data in the requested format.
Definition: Data.h:57
Gorgon::Scripting::Data
Data describes a piece of data.
Definition: Data.h:22
Gorgon::Scripting::Data::ToString
std::string ToString() const
Definition: Data.cpp:90
Gorgon::Scripting::Data::GetValue
std::enable_if< std::is_pointer< T_ >::value &&std::is_const< typename std::remove_pointer< T_ >::type >::value, T_ >::type GetValue() const
Returns the value of this data in the requested format.
Definition: Data.h:75
Gorgon::Scripting::Data::GetType
const Type & GetType() const
Returns the type of the data.
Definition: Data.h:173
Gorgon::Scripting::Data::MakeConstant
void MakeConstant()
Makes this data a constant.
Definition: Data.cpp:199
Gorgon::Scripting::Type
This class stores information about types.
Definition: Reflection.h:1165
Gorgon
Root namespace for Gorgon Game Engine.
Definition: Any.h:19
Gorgon::Scripting::Data::RemoveParent
void RemoveParent()
Definition: Data.h:146
Gorgon::Scripting::Data::GetReference
Data GetReference()
Definition: Data.cpp:148
Gorgon::Scripting::Data::IsReference
bool IsReference() const
Returns if this data contains a reference.
Definition: Data.cpp:212
ASSERT
#define ASSERT(expression, message,...)
Replaces regular assert to allow messages and backtrace.
Definition: Assert.h:161
Gorgon::Scripting::Data::Invalid
static Data Invalid()
Constructs an invalid data object.
Definition: Data.h:27
Gorgon::Scripting::Data::operator==
bool operator==(const Data &r) const
Definition: Data.cpp:218
Gorgon::Scripting::Data::SetParent
void SetParent(const Data data)
Definition: Data.h:141
Gorgon::Scripting::Data::Delete
void Delete() const
Attempts to delete the data contained in this data.
Definition: Data.cpp:136
Gorgon::Scripting::Data::IsNull
bool IsNull() const
Definition: Data.cpp:118
Gorgon::Scripting::Data::GetData
Any GetData() const
Returns the data contained in this data element.
Definition: Data.h:133
Gorgon::Scripting::Data::operator=
Data & operator=(Data)
Assignment operator.
Definition: Data.cpp:96
Gorgon::Scripting::Data::parent
Data * parent
Definition: Data.h:196
Gorgon::Scripting::Data::ReferenceValue
std::enable_if<!std::is_pointer< T_ >::value, T_ >::type ReferenceValue() const
Returns the value of this data in the requested format.
Definition: Data.h:124
Gorgon::GL::UBOBindingPoint::Type
Type
Definition: Shader.h:14
Gorgon::Scripting::Data::type
const Type * type
Type of the data.
Definition: Data.h:188
Gorgon::Scripting::Data::IsConstant
bool IsConstant() const
Returns if this data is constant.
Definition: Data.h:160
Gorgon::Scripting::Data::Data
Data(const Type &type, const Any &data, bool isreference=false, bool isconstant=false)
Any constructor. Allows both data and type to be specified.
Definition: Data.h:43
Exceptions.h
Exceptions This file contains string related exceptions.
Gorgon::Any::Get
T_ & Get()
Returns the value contained with this any.
Definition: Any.h:258
Gorgon::Scripting::Data::IsValid
bool IsValid() const
Returns if the data is in a valid state.
Definition: Data.h:152
Gorgon::Scripting::Data::isconstant
bool isconstant
This data is a constant and should not be changed.
Definition: Data.h:194