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 #pragma once
8 
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  Unknown,
36 
37  // Primitive types
38  String,
39  Number,
40  Boolean,
41 
42  // Collection types
43  Structure,
44  Array
45  };
46 
50  static bool IsPrimitive(const Type type);
51 
55  Variable() : value(0), type(Type::Number), hasMixedValues(false) {};
56  Variable(const Variable&);
57  virtual ~Variable(){};
58 
59  Variable& operator=(const Variable& rhs);
60 
64  Type GetType() const { return type; }
65 
69  void CastTo(const Type newType);
70 
74  void CastTo(const gd::String& type) { return CastTo(StringAsType(type)); };
75 
80 
84  const gd::String& GetString() const;
85 
89  void SetString(const gd::String& newStr) {
90  str = newStr;
91  type = Type::String;
92  hasMixedValues = false;
93  }
94 
98  double GetValue() const;
99 
103  void SetValue(double val) {
104  value = val;
105  // NaN values are not supported by GDevelop nor the serializer.
106  if (std::isnan(value)) value = 0.0;
107  type = Type::Number;
108  hasMixedValues = false;
109  }
110 
114  bool GetBool() const;
115 
119  void SetBool(bool val) {
120  boolVal = val;
121  type = Type::Boolean;
122  hasMixedValues = false;
123  }
124 
129  bool HasMixedValues() const {
130  return hasMixedValues;
131  }
132 
137  void MarkAsMixedValues();
138 
139  // Operators are overloaded to allow accessing to variable using a simple
140  // int-like semantic.
141  void operator=(double val) { SetValue(val); };
142  void operator+=(double val) { SetValue(val + GetValue()); }
143  void operator-=(double val) { SetValue(GetValue() - val); }
144  void operator*=(double val) { SetValue(val * GetValue()); }
145  void operator/=(double val) { SetValue(GetValue() / val); }
146 
147  bool operator<=(double val) const { return GetValue() <= val; };
148  bool operator>=(double val) const { return GetValue() >= val; };
149  bool operator<(double val) const { return GetValue() < val; };
150  bool operator>(double val) const { return GetValue() > val; };
151  bool operator==(double val) const { return GetValue() == val; };
152  bool operator!=(double val) const { return GetValue() != val; };
153 
154  // Avoid ambiguous operators
155  void operator=(int val) { SetValue(val); };
156  void operator+=(int val) { SetValue(val + GetValue()); }
157  void operator-=(int val) { SetValue(GetValue() - val); }
158  void operator*=(int val) { SetValue(val * GetValue()); }
159  void operator/=(int val) { SetValue(GetValue() / val); }
160 
161  bool operator<=(int val) const { return GetValue() <= val; };
162  bool operator>=(int val) const { return GetValue() >= val; };
163  bool operator<(int val) const { return GetValue() < val; };
164  bool operator>(int val) const { return GetValue() > val; };
165  bool operator==(int val) const { return GetValue() == val; };
166  bool operator!=(int val) const { return GetValue() != val; };
167 
168  // Operators are overloaded to allow accessing to variable using a simple
169  // string-like semantic.
170  void operator=(const gd::String& val) { SetString(val); };
171  void operator+=(const gd::String& val) { SetString(GetString() + val); }
172 
173  bool operator==(const gd::String& val) const { return GetString() == val; };
174  bool operator!=(const gd::String& val) const { return GetString() != val; };
175 
176  // Avoid ambiguous operators
177  void operator=(const char* val) { SetString(val); };
178  void operator+=(const char* val) { SetString(GetString() + val); }
179 
180  bool operator==(const char* val) const { return GetString() == val; };
181  bool operator!=(const char* val) const { return GetString() != val; };
182 
183  // Operators are overloaded to allow accessing to variable using a simple
184  // bool-like semantic.
185  void operator=(const bool val) { SetBool(val); };
186 
187  bool operator==(const bool val) const { return GetBool() == val; };
188  bool operator!=(const bool val) const { return GetBool() != val; };
189 
190  bool operator==(const gd::Variable& variable) const;
191  bool operator!=(const gd::Variable& variable) const;
192 
194 
199 
203  void ClearChildren() {
204  children.clear();
205  childrenArray.clear();
206  };
207 
211  size_t GetChildrenCount() const {
212  return type == Type::Structure ? children.size()
213  : type == Type::Array ? childrenArray.size()
214  : 0;
215  };
216 
221 
225  bool HasChild(const gd::String& name) const;
226 
233  Variable& GetChild(const gd::String& name);
234 
241  const Variable& GetChild(const gd::String& name) const;
242 
249  void RemoveChild(const gd::String& name);
250 
258  bool RenameChild(const gd::String& oldName, const gd::String& newName);
259 
263  std::vector<gd::String> GetAllChildrenNames() const;
264 
268  const std::map<gd::String, std::shared_ptr<Variable>>& GetAllChildren()
269  const {
270  return children;
271  }
272 
276  bool Contains(const gd::Variable& variableToSearch, bool recursive) const;
277 
281  void RemoveRecursively(const gd::Variable& variableToRemove);
283 
288 
295  Variable& GetAtIndex(const size_t index);
296 
303  const Variable& GetAtIndex(const size_t index) const;
304 
308  Variable& PushNew();
309 
315  void RemoveAtIndex(const size_t index);
316 
320  void MoveChildInArray(const size_t oldIndex, const size_t newIndex);
321 
325  bool InsertAtIndex(const gd::Variable& variable, const size_t index);
326 
330  bool InsertChild(const gd::String& name, const gd::Variable& variable);
331 
335  const std::vector<std::shared_ptr<Variable>>& GetAllChildrenArray() const {
336  return childrenArray;
337  }
338 
342  void SetFolded(bool fold = true) { folded = fold; }
343 
347  bool IsFolded() const { return folded; }
348 
351 
356 
359  void SerializeTo(SerializerElement& element) const;
360 
364  void UnserializeFrom(const SerializerElement& element);
365 
370  Variable& ResetPersistentUuid();
371 
376  Variable& ClearPersistentUuid();
377 
382  const gd::String& GetPersistentUuid() const { return persistentUuid; };
384 
385  private:
389  static gd::String TypeAsString(Type t);
390 
394  static Type StringAsType(const gd::String& str);
395 
396  bool folded = false;
397  mutable Type type;
398  mutable gd::String str;
399  mutable double value;
400  mutable bool boolVal = false;
401  mutable bool hasMixedValues;
402  mutable std::map<gd::String, std::shared_ptr<Variable>>
403  children;
404  mutable std::vector<std::shared_ptr<Variable>>
405  childrenArray;
407  mutable gd::String persistentUuid;
409 
414  void CopyChildren(const Variable& other);
415 };
416 
417 } // namespace gd
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:33
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:89
Type
Definition: Variable.h:32
@ MixedTypes
Definition: Variable.h:35
size_t GetChildrenCount() const
Get the count of children that the variable has.
Definition: Variable.h:211
const std::map< gd::String, std::shared_ptr< Variable > > & GetAllChildren() const
Get the map containing all the children.
Definition: Variable.h:268
void SetValue(double val)
Change the content of the variable, considered as a number.
Definition: Variable.h:103
Variable()
Default constructor creating a variable with 0 as value.
Definition: Variable.h:55
bool IsFolded() const
True if the children should be folded in the variables editor.
Definition: Variable.h:347
bool HasMixedValues() const
Return true when objects of a group have different values for a variable.
Definition: Variable.h:129
Type GetType() const
Get the type of the variable.
Definition: Variable.h:64
void SetBool(bool val)
Change the content of the variable, considered as a boolean.
Definition: Variable.h:119
const gd::String & GetPersistentUuid() const
Get the persistent UUID used to recognize the same variable between serialization.
Definition: Variable.h:382
void CastTo(const gd::String &type)
Converts the variable to a new type.
Definition: Variable.h:74
void ClearChildren()
Remove all the children.
Definition: Variable.h:203
const std::vector< std::shared_ptr< Variable > > & GetAllChildrenArray() const
Get the vector containing all the children.
Definition: Variable.h:335
void SetFolded(bool fold=true)
Set if the children must be folded.
Definition: Variable.h:342
Definition: CommonTools.h:24
Type
Type of JSON value.
Definition: rapidjson.h:603