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 
329  bool HasAttribute(const gd::String &name) const;
330 
334  const std::map<gd::String, SerializerValue> &GetAllAttributes() const {
335  return attributes;
336  };
338 
343 
350  void ConsiderAsArray() const { isArray = true; };
351 
359  bool ConsideredAsArray() const { return isArray; };
360 
371  void ConsiderAsArrayOf(const gd::String &name,
372  const gd::String &deprecatedName = "") const {
373  ConsiderAsArray();
374  arrayOf = name;
375  deprecatedArrayOf = deprecatedName;
376  };
377 
384  const gd::String &ConsideredAsArrayOf() const { return arrayOf; };
385 
392  SerializerElement &AddChild(gd::String name);
393 
400  SerializerElement &GetChild(gd::String name,
401  std::size_t index = 0,
402  gd::String deprecatedName = "") const;
403 
410  SerializerElement &GetChild(std::size_t index) const;
411 
423  std::size_t GetChildrenCount(gd::String name = "",
424  gd::String deprecatedName = "") const;
425 
431  bool HasChild(const gd::String &name, gd::String deprecatedName = "") const;
432 
438  void RemoveChild(const gd::String &name);
439 
443  const std::vector<std::pair<gd::String, std::shared_ptr<SerializerElement> > >
444  &GetAllChildren() const {
445  return children;
446  };
448 
449  static SerializerElement nullElement;
450 
451  private:
456  void Init(const gd::SerializerElement &other);
457 
458  bool valueUndefined;
459  SerializerValue elementValue;
460 
461  std::map<gd::String, SerializerValue> attributes;
462  std::vector<std::pair<gd::String, std::shared_ptr<SerializerElement> > >
463  children;
464  mutable bool isArray;
465  mutable gd::String arrayOf;
467  mutable gd::String deprecatedArrayOf;
468 };
469 
470 } // namespace gd
471 
472 #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:384
void ConsiderAsArray() const
Consider that the element is an array of elements, without specific name for the children element.
Definition: SerializerElement.h:350
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:334
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:444
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:371
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:359
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:31
Definition: CommonTools.h:24