GDevelop Core
Core library for developing platforms and tools compatible with GDevelop.
Variable.h
1 /*
2  * GDevelop Core
3  * Copyright 2008-2016 Florian Rival ([email protected]). All rights
4  * reserved. This project is released under the MIT License.
5  */
6 
7 #ifndef GDCORE_VARIABLE_H
8 #define GDCORE_VARIABLE_H
9 #include <cmath>
10 #include <map>
11 #include <memory>
12 #include <vector>
13 
14 #include "GDCore/String.h"
15 namespace gd {
16 class SerializerElement;
17 }
18 
19 namespace gd {
20 
29 class GD_CORE_API Variable {
30  public:
31  static gd::Variable badVariable;
32  enum Type {
33  // Primitive types
34  String,
35  Number,
36  Boolean,
37 
38  // Collection types
39  Structure,
40  Array
41  };
42 
46  static bool IsPrimitive(const Type type);
47 
51  Variable() : value(0), type(Type::Number){};
52  Variable(const Variable&);
53  virtual ~Variable(){};
54 
55  Variable& operator=(const Variable& rhs);
56 
60  Type GetType() const { return type; }
61 
65  void CastTo(const Type newType);
66 
70  void CastTo(const gd::String& type) { return CastTo(StringAsType(type)); };
71 
76 
80  const gd::String& GetString() const;
81 
85  void SetString(const gd::String& newStr) {
86  str = newStr;
87  type = Type::String;
88  }
89 
93  double GetValue() const;
94 
98  void SetValue(double val) {
99  value = val;
100  // NaN values are not supported by GDevelop nor the serializer.
101  if (std::isnan(value)) value = 0.0;
102  type = Type::Number;
103  }
104 
108  bool GetBool() const;
109 
113  void SetBool(bool val) {
114  boolVal = val;
115  type = Type::Boolean;
116  }
117 
118  // Operators are overloaded to allow accessing to variable using a simple
119  // int-like semantic.
120  void operator=(double val) { SetValue(val); };
121  void operator+=(double val) { SetValue(val + GetValue()); }
122  void operator-=(double val) { SetValue(GetValue() - val); }
123  void operator*=(double val) { SetValue(val * GetValue()); }
124  void operator/=(double val) { SetValue(GetValue() / val); }
125 
126  bool operator<=(double val) const { return GetValue() <= val; };
127  bool operator>=(double val) const { return GetValue() >= val; };
128  bool operator<(double val) const { return GetValue() < val; };
129  bool operator>(double val) const { return GetValue() > val; };
130  bool operator==(double val) const { return GetValue() == val; };
131  bool operator!=(double val) const { return GetValue() != val; };
132 
133  // Avoid ambiguous operators
134  void operator=(int val) { SetValue(val); };
135  void operator+=(int val) { SetValue(val + GetValue()); }
136  void operator-=(int val) { SetValue(GetValue() - val); }
137  void operator*=(int val) { SetValue(val * GetValue()); }
138  void operator/=(int val) { SetValue(GetValue() / val); }
139 
140  bool operator<=(int val) const { return GetValue() <= val; };
141  bool operator>=(int val) const { return GetValue() >= val; };
142  bool operator<(int val) const { return GetValue() < val; };
143  bool operator>(int val) const { return GetValue() > val; };
144  bool operator==(int val) const { return GetValue() == val; };
145  bool operator!=(int val) const { return GetValue() != val; };
146 
147  // Operators are overloaded to allow accessing to variable using a simple
148  // string-like semantic.
149  void operator=(const gd::String& val) { SetString(val); };
150  void operator+=(const gd::String& val) { SetString(GetString() + val); }
151 
152  bool operator==(const gd::String& val) const { return GetString() == val; };
153  bool operator!=(const gd::String& val) const { return GetString() != val; };
154 
155  // Avoid ambiguous operators
156  void operator=(const char* val) { SetString(val); };
157  void operator+=(const char* val) { SetString(GetString() + val); }
158 
159  bool operator==(const char* val) const { return GetString() == val; };
160  bool operator!=(const char* val) const { return GetString() != val; };
161 
162  // Operators are overloaded to allow accessing to variable using a simple
163  // bool-like semantic.
164  void operator=(const bool val) { SetBool(val); };
165 
166  bool operator==(const bool val) const { return GetBool() == val; };
167  bool operator!=(const bool val) const { return GetBool() != val; };
168 
170 
175 
179  void ClearChildren() {
180  children.clear();
181  childrenArray.clear();
182  };
183 
187  size_t GetChildrenCount() const {
188  return type == Type::Structure ? children.size()
189  : type == Type::Array ? childrenArray.size()
190  : 0;
191  };
192 
197 
201  bool HasChild(const gd::String& name) const;
202 
209  Variable& GetChild(const gd::String& name);
210 
217  const Variable& GetChild(const gd::String& name) const;
218 
225  void RemoveChild(const gd::String& name);
226 
234  bool RenameChild(const gd::String& oldName, const gd::String& newName);
235 
239  std::vector<gd::String> GetAllChildrenNames() const;
240 
244  const std::map<gd::String, std::shared_ptr<Variable>>& GetAllChildren()
245  const {
246  return children;
247  }
248 
252  bool Contains(const gd::Variable& variableToSearch, bool recursive) const;
253 
257  void RemoveRecursively(const gd::Variable& variableToRemove);
259 
264 
271  Variable& GetAtIndex(const size_t index);
272 
279  const Variable& GetAtIndex(const size_t index) const;
280 
284  Variable& PushNew();
285 
291  void RemoveAtIndex(const size_t index);
292 
296  void MoveChildInArray(const size_t oldIndex, const size_t newIndex);
297 
301  bool InsertAtIndex(const gd::Variable& variable, const size_t index);
302 
306  bool InsertChild(const gd::String& name, const gd::Variable& variable);
307 
311  const std::vector<std::shared_ptr<Variable>>& GetAllChildrenArray() const {
312  return childrenArray;
313  }
314 
318  void SetFolded(bool fold = true) { folded = fold; }
319 
323  bool IsFolded() const { return folded; }
324 
327 
332 
335  void SerializeTo(SerializerElement& element) const;
336 
340  void UnserializeFrom(const SerializerElement& element);
341 
346  Variable& ResetPersistentUuid();
347 
352  Variable& ClearPersistentUuid() { persistentUuid = ""; return *this; };
353 
358  const gd::String& GetPersistentUuid() const { return persistentUuid; };
360 
361  private:
365  static gd::String TypeAsString(Type t);
366 
370  static Type StringAsType(const gd::String& str);
371 
372  bool folded;
373  mutable Type type;
374  mutable gd::String str;
375  mutable double value;
376  mutable bool boolVal;
377  mutable std::map<gd::String, std::shared_ptr<Variable>>
378  children;
379  mutable std::vector<std::shared_ptr<Variable>>
380  childrenArray;
382  mutable gd::String persistentUuid;
384 
389  void CopyChildren(const Variable& other);
390 };
391 
392 } // namespace gd
393 
394 #endif // GDCORE_VARIABLE_H
A generic container that can represent a value ( containing a string, double, bool or int),...
Definition: SerializerElement.h:37
String represents an UTF8 encoded string.
Definition: String.h:31
Defines a variable which can be used by an object, a layout or a project.
Definition: Variable.h:29
void SetString(const gd::String &newStr)
Change the content of the variable, considered as a string.
Definition: Variable.h:85
Variable & ClearPersistentUuid()
Remove the persistent UUID - when the variable no longer needs to be recognized between serialization...
Definition: Variable.h:352
size_t GetChildrenCount() const
Get the count of children that the variable has.
Definition: Variable.h:187
const std::map< gd::String, std::shared_ptr< Variable > > & GetAllChildren() const
Get the map containing all the children.
Definition: Variable.h:244
void SetValue(double val)
Change the content of the variable, considered as a number.
Definition: Variable.h:98
Variable()
Default constructor creating a variable with 0 as value.
Definition: Variable.h:51
bool IsFolded() const
True if the children should be folded in the variables editor.
Definition: Variable.h:323
Type GetType() const
Get the type of the variable.
Definition: Variable.h:60
void SetBool(bool val)
Change the content of the variable, considered as a boolean.
Definition: Variable.h:113
const gd::String & GetPersistentUuid() const
Get the persistent UUID used to recognize the same variable between serialization.
Definition: Variable.h:358
void CastTo(const gd::String &type)
Converts the variable to a new type.
Definition: Variable.h:70
void ClearChildren()
Remove all the children.
Definition: Variable.h:179
const std::vector< std::shared_ptr< Variable > > & GetAllChildrenArray() const
Get the vector containing all the children.
Definition: Variable.h:311
void SetFolded(bool fold=true)
Set if the children must be folded.
Definition: Variable.h:318
Definition: CommonTools.h:24
Type
Type of JSON value.
Definition: rapidjson.h:603