GDevelop Core
Core library for developing platforms and tools compatible with GDevelop.
SerializableWithNameList.h
1 /*
2  * GDevelop Core
3  * Copyright 2008-present Florian Rival ([email protected]). All rights
4  * reserved. This project is released under the MIT License.
5  */
6 
7 #ifndef GDCORE_SerializableWithNameList
8 #define GDCORE_SerializableWithNameList
9 #include <memory>
10 #include <vector>
11 #include "GDCore/String.h"
12 namespace gd {
13 class Project;
14 class SerializerElement;
15 } // namespace gd
16 
17 namespace gd {
18 
32 template <typename T>
34  public:
37  virtual ~SerializableWithNameList(){};
38  SerializableWithNameList<T>& operator=(
39  const SerializableWithNameList<T>& rhs);
40 
50  T& Insert(const T& element, size_t position = (size_t)-1);
51 
55  void Insert(const SerializableWithNameList<T>& otherEvents,
56  size_t begin,
57  size_t end,
58  size_t position = (size_t)-1);
59 
70  T& InsertNew(const gd::String& name, size_t position = (size_t)-1);
71 
75  size_t GetCount() const { return elements.size(); };
76 
80  T& Get(const gd::String& name);
81 
85  const T& Get(const gd::String& name) const;
86 
91  T& Get(size_t index) { return *elements[index]; };
92 
97  const T& Get(size_t index) const { return *elements[index]; };
98 
102  void Remove(const gd::String& name);
103 
108  void Remove(size_t index);
109 
113  bool IsEmpty() const { return elements.empty(); };
114 
118  void Clear() { return elements.clear(); };
119 
125  void Move(std::size_t oldIndex, std::size_t newIndex);
126 
130  bool Has(const gd::String& name) const;
131 
135  std::size_t GetPosition(const T& element) const;
136 
142 
146  size_t size() const { return GetCount(); }
147 
152  bool empty() const { return IsEmpty(); }
153 
158  T& operator[](size_t index) { return Get(index); };
159 
164  const T& operator[](size_t index) const { return Get(index); };
165 
170  T& at(size_t index) { return Get(index); };
171 
176  const T& at(size_t index) const { return Get(index); };
178 
182 
185  const std::vector<std::unique_ptr<T>>& GetInternalVector() const {
186  return elements;
187  };
188 
192  std::vector<std::unique_ptr<T>>& GetInternalVector() { return elements; };
194 
198  void SerializeElementsTo(const gd::String& elementName,
199  SerializerElement& element) const;
200 
201  void UnserializeElementsFrom(const gd::String& elementName,
202  gd::Project& project,
203  const SerializerElement& element);
204 
205  void UnserializeElementsFrom(const gd::String& elementName,
206  const SerializerElement& element);
208 
209  protected:
210  std::vector<std::unique_ptr<T>> elements;
211 
216  void Init(const SerializableWithNameList<T>& other);
217 };
218 
219 } // namespace gd
220 
221 #include "SerializableWithNameList.inl"
222 
223 #endif
Base class representing a project (game), including all resources, scenes, objects,...
Definition: Project.h:50
A class template that store a list of elements that can be accessed by their names and serialized.
Definition: SerializableWithNameList.h:33
T & Get(size_t index)
Return a reference to the element at position index in the elements list.
Definition: SerializableWithNameList.h:91
T & at(size_t index)
Alias for Get()
Definition: SerializableWithNameList.h:170
std::vector< std::unique_ptr< T > > & GetInternalVector()
Provide a raw access to the vector containing the elements.
Definition: SerializableWithNameList.h:192
void Remove(const gd::String &name)
Remove the element with the specified name, destroying it.
Definition: SerializableWithNameList.inl:61
size_t size() const
Alias for GetCount()
Definition: SerializableWithNameList.h:146
const std::vector< std::unique_ptr< T > > & GetInternalVector() const
Provide a raw access to the vector containing the elements.
Definition: SerializableWithNameList.h:185
void Clear()
Clear the list of elements, destroying all of them.
Definition: SerializableWithNameList.h:118
bool IsEmpty() const
Return true if there isn't any element in the list.
Definition: SerializableWithNameList.h:113
bool Has(const gd::String &name) const
Return true if an element with the specified name exists.
Definition: SerializableWithNameList.inl:94
T & Insert(const T &element, size_t position=(size_t) -1)
Insert the specified element to the list.
Definition: SerializableWithNameList.inl:36
const T & at(size_t index) const
Alias for Get()
Definition: SerializableWithNameList.h:176
bool empty() const
Alias for IsEmpty()
Definition: SerializableWithNameList.h:152
std::size_t GetPosition(const T &element) const
Get the position of an element in the list.
Definition: SerializableWithNameList.inl:113
size_t GetCount() const
Return the number of elements.
Definition: SerializableWithNameList.h:75
void Init(const SerializableWithNameList< T > &other)
Definition: SerializableWithNameList.inl:169
const T & Get(size_t index) const
Return a reference to the element at position index in the elements list.
Definition: SerializableWithNameList.h:97
const T & operator[](size_t index) const
Alias for Get()
Definition: SerializableWithNameList.h:164
T & InsertNew(const gd::String &name, size_t position=(size_t) -1)
Insert a new element (constructed from its default constructor) with the given name.
Definition: SerializableWithNameList.inl:45
void Move(std::size_t oldIndex, std::size_t newIndex)
Move element at position oldIndex to position newIndex.
Definition: SerializableWithNameList.inl:103
T & operator[](size_t index)
Alias for Get()
Definition: SerializableWithNameList.h:158
T & Get(const gd::String &name)
Return a reference to the element with the specified name.
Definition: SerializableWithNameList.inl:74
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
Definition: CommonTools.h:24