GDevelop Core
Core library for developing platforms and tools compatible with GDevelop.
SerializerElement.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_SERIALIZERELEMENT_H
8 #define GDCORE_SERIALIZERELEMENT_H
9 #include <map>
10 #include <memory>
11 #include <string>
12 #include <vector>
13 
14 #include "GDCore/Serialization/SerializerValue.h"
15 #include "GDCore/String.h"
16 
17 namespace gd {
18 
37 class GD_CORE_API SerializerElement {
38  public:
44 
48  SerializerElement(const SerializerValue &value);
49 
53  SerializerElement(const gd::SerializerElement &object) { Init(object); };
54 
59  if ((this) != &object) Init(object);
60  return *this;
61  }
62 
63  virtual ~SerializerElement();
64 
69 
72  void SetValue(const SerializerValue &value) {
73  valueUndefined = false;
74  elementValue = value;
75  }
76 
80  void SetValue(bool val) {
81  valueUndefined = false;
82  elementValue.SetBool(val);
83  }
84 
88  void SetBoolValue(bool val) { SetValue(val); }
89 
93  void SetValue(const gd::String &val) {
94  valueUndefined = false;
95  elementValue.SetString(val);
96  }
97 
101  void SetStringValue(const gd::String &val) { SetValue(val); }
102 
106  void SetValue(int val) {
107  valueUndefined = false;
108  elementValue.SetInt(val);
109  }
110 
114  void SetIntValue(int val) { SetValue(val); }
115 
119  void SetValue(unsigned int val) {
120  valueUndefined = false;
121  elementValue.SetInt((int)val);
122  }
123 
128  void SetValue(double val) {
129  valueUndefined = false;
130  elementValue.SetDouble(val);
131  }
132 
137  void SetDoubleValue(double val) { SetValue(val); }
138 
142  void SetValue(float val) { SetValue((double)val); }
143 
147  void SetFloatValue(float val) { SetValue(val); }
148 
155  const SerializerValue &GetValue() const;
156 
160  bool GetBoolValue() const { return GetValue().GetBool(); };
161 
165  gd::String GetStringValue() const { return GetValue().GetString(); };
166 
170  int GetIntValue() const { return GetValue().GetInt(); };
171 
175  double GetDoubleValue() const { return GetValue().GetDouble(); };
176 
180  bool IsValueUndefined() const { return valueUndefined; }
181 
186  void SetMultilineStringValue(const gd::String &value);
187 
192  gd::String GetMultilineStringValue();
194 
205 
210  SerializerElement &SetAttribute(const gd::String &name, bool value);
211 
217  SerializerElement &SetBoolAttribute(const gd::String &name, bool value) {
218  return SetAttribute(name, value);
219  }
220 
226  SerializerElement &SetAttribute(const gd::String &name,
227  const gd::String &value);
228 
235  const gd::String &value) {
236  return SetAttribute(name, value);
237  }
238 
244  SerializerElement &SetAttribute(const gd::String &name, const char *value) {
245  if (value) SetAttribute(name, gd::String(value));
246  return *this;
247  };
248 
254  SerializerElement &SetAttribute(const gd::String &name, int value);
255 
261  SerializerElement &SetIntAttribute(const gd::String &name, int value) {
262  return SetAttribute(name, value);
263  }
264 
270  SerializerElement &SetAttribute(const gd::String &name, double value);
271 
277  SerializerElement &SetDoubleAttribute(const gd::String &name, double value) {
278  return SetAttribute(name, value);
279  }
280 
288  bool GetBoolAttribute(const gd::String &name,
289  bool defaultValue = false,
290  gd::String deprecatedName = "") const;
291 
299  gd::String GetStringAttribute(const gd::String &name,
300  gd::String defaultValue = "",
301  gd::String deprecatedName = "") const;
302 
310  int GetIntAttribute(const gd::String &name,
311  int defaultValue = 0,
312  gd::String deprecatedName = "") const;
313 
321  double GetDoubleAttribute(const gd::String &name,
322  double defaultValue = 0.0,
323  gd::String deprecatedName = "") const;
324 
330  bool HasAttribute(const gd::String &name) const;
331 
335  const std::map<gd::String, SerializerValue> &GetAllAttributes() const {
336  return attributes;
337  };
339 
344 
351  void ConsiderAsArray() const { isArray = true; };
352 
360  bool ConsideredAsArray() const { return isArray; };
361 
372  void ConsiderAsArrayOf(const gd::String &name,
373  const gd::String &deprecatedName = "") const {
374  ConsiderAsArray();
375  arrayOf = name;
376  deprecatedArrayOf = deprecatedName;
377  };
378 
385  const gd::String &ConsideredAsArrayOf() const { return arrayOf; };
386 
393  SerializerElement &AddChild(gd::String name);
394 
401  SerializerElement &GetChild(gd::String name,
402  std::size_t index = 0,
403  gd::String deprecatedName = "") const;
404 
411  SerializerElement &GetChild(std::size_t index) const;
412 
424  std::size_t GetChildrenCount(gd::String name = "",
425  gd::String deprecatedName = "") const;
426 
432  bool HasChild(const gd::String &name, gd::String deprecatedName = "") const;
433 
439  void RemoveChild(const gd::String &name);
440 
444  const std::vector<std::pair<gd::String, std::shared_ptr<SerializerElement> > >
445  &GetAllChildren() const {
446  return children;
447  };
449 
450  static SerializerElement nullElement;
451 
452  private:
457  void Init(const gd::SerializerElement &other);
458 
459  bool valueUndefined = true;
460  SerializerValue elementValue;
461 
462  std::map<gd::String, SerializerValue> attributes;
463  std::vector<std::pair<gd::String, std::shared_ptr<SerializerElement> > >
464  children;
465  mutable bool isArray = false;
466  mutable gd::String arrayOf;
468  mutable gd::String deprecatedArrayOf;
469 };
470 
471 } // namespace gd
472 
473 #endif
A generic container that can represent a value ( containing a string, double, bool or int),...
Definition: SerializerElement.h:37
bool GetBoolValue() const
Get the value, its type being a boolean.
Definition: SerializerElement.h:160
void SetIntValue(int val)
Set the value of the element, as an integer.
Definition: SerializerElement.h:114
const gd::String & ConsideredAsArrayOf() const
Return the name of the children the element is considered an array of.
Definition: SerializerElement.h:385
void ConsiderAsArray() const
Consider that the element is an array of elements, without specific name for the children element.
Definition: SerializerElement.h:351
void SetValue(unsigned int val)
Set the value of the element, as an unsigned integer.
Definition: SerializerElement.h:119
SerializerElement & operator=(const gd::SerializerElement &object)
Definition: SerializerElement.h:58
gd::String GetStringValue() const
Get the value, its type being a gd::String.
Definition: SerializerElement.h:165
void SetValue(const gd::String &val)
Set the value of the element, as a string.
Definition: SerializerElement.h:93
void SetFloatValue(float val)
Set the value of the element, as a floating point number.
Definition: SerializerElement.h:147
const std::map< gd::String, SerializerValue > & GetAllAttributes() const
Return all the attributes of the element.
Definition: SerializerElement.h:335
SerializerElement & SetAttribute(const gd::String &name, const char *value)
Set the string value of an attribute of the element.
Definition: SerializerElement.h:244
SerializerElement & SetDoubleAttribute(const gd::String &name, double value)
Set the double precision floating point number value of an attribute of the element.
Definition: SerializerElement.h:277
SerializerElement & SetBoolAttribute(const gd::String &name, bool value)
Set the boolean value of an attribute of the element.
Definition: SerializerElement.h:217
void SetValue(bool val)
Set the value of the element, as a boolean.
Definition: SerializerElement.h:80
void SetValue(int val)
Set the value of the element, as an integer.
Definition: SerializerElement.h:106
SerializerElement & SetIntAttribute(const gd::String &name, int value)
Set the integer value of an attribute of the element.
Definition: SerializerElement.h:261
bool IsValueUndefined() const
Return true if no value was set for the element.
Definition: SerializerElement.h:180
SerializerElement & SetStringAttribute(const gd::String &name, const gd::String &value)
Set the string value of an attribute of the element.
Definition: SerializerElement.h:234
void SetDoubleValue(double val)
Set the value of the element, as a double precision floating point number.
Definition: SerializerElement.h:137
void SetValue(float val)
Set the value of the element, as a floating point number.
Definition: SerializerElement.h:142
const std::vector< std::pair< gd::String, std::shared_ptr< SerializerElement > > > & GetAllChildren() const
Return all the children of the element.
Definition: SerializerElement.h:445
SerializerElement(const gd::SerializerElement &object)
Definition: SerializerElement.h:53
int GetIntValue() const
Get the value, its type being an int.
Definition: SerializerElement.h:170
void SetBoolValue(bool val)
Set the value of the element, as a boolean.
Definition: SerializerElement.h:88
void ConsiderAsArrayOf(const gd::String &name, const gd::String &deprecatedName="") const
Consider that the element is an array for elements with the given name.
Definition: SerializerElement.h:372
void SetStringValue(const gd::String &val)
Set the value of the element, as a string.
Definition: SerializerElement.h:101
void SetValue(const SerializerValue &value)
Set the value of the element.
Definition: SerializerElement.h:72
bool ConsideredAsArray() const
Check if the element is considered as an array containing its children.
Definition: SerializerElement.h:360
void SetValue(double val)
Set the value of the element, as a double precision floating point number.
Definition: SerializerElement.h:128
double GetDoubleValue() const
Get the value, its type being a double.
Definition: SerializerElement.h:175
A value stored inside a gd::SerializerElement.
Definition: SerializerValue.h:20
String represents an UTF8 encoded string.
Definition: String.h:33
Definition: CommonTools.h:24