GDevelop Core
Core library for developing platforms and tools compatible with GDevelop.
VariablesContainer.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 #include <memory>
9 #include <vector>
10 #include "GDCore/Project/Variable.h"
11 #include "GDCore/String.h"
12 namespace gd {
13 class SerializerElement;
14 }
15 
16 namespace gd {
17 
28 class GD_CORE_API VariablesContainer {
29  public:
30  enum SourceType {
31  Unknown,
32  Global,
33  Scene,
34  Object,
35  Local,
36  ExtensionGlobal,
37  ExtensionScene
38  };
39 
41  VariablesContainer(const SourceType sourceType);
43  virtual ~VariablesContainer(){};
44 
45  VariablesContainer& operator=(const VariablesContainer& rhs);
46 
47  SourceType GetSourceType() const { return sourceType; }
48 
53 
57  bool Has(const gd::String& name) const;
58 
62  Variable& Get(const gd::String& name);
63 
67  const Variable& Get(const gd::String& name) const;
68 
73  Variable& Get(std::size_t index);
74 
79  const Variable& Get(std::size_t index) const;
80 
89  Variable& Insert(const gd::String& name,
90  const Variable& variable,
91  std::size_t position);
92 
96  std::size_t Count() const { return variables.size(); };
97 
101  const gd::String& GetNameAt(std::size_t index) const;
102 
107  std::size_t GetPosition(const gd::String& name) const;
108 
116  Variable& InsertNew(const gd::String& name, std::size_t position = -1);
117 
122  void Remove(const gd::String& name);
123 
127  void RemoveRecursively(const gd::Variable& variable);
128 
133  bool Rename(const gd::String& oldName, const gd::String& newName);
134 
138  void Swap(std::size_t firstVariableIndex, std::size_t secondVariableIndex);
139 
143  void Move(std::size_t oldIndex, std::size_t newIndex);
144 
148  inline void Clear() { variables.clear(); }
149 
153  void ForEachVariableMatchingSearch(const gd::String& search, std::function<void(const gd::String& name, const gd::Variable& variable)> fn) const;
155 
160 
163  void SerializeTo(SerializerElement& element) const;
164 
168  void UnserializeFrom(const SerializerElement& element);
169 
174  VariablesContainer& ResetPersistentUuid();
175 
180  VariablesContainer& ClearPersistentUuid();
181 
186  const gd::String& GetPersistentUuid() const { return persistentUuid; };
188 
189  private:
190  SourceType sourceType = Unknown;
191  std::vector<std::pair<gd::String, std::shared_ptr<gd::Variable>>> variables;
192  mutable gd::String persistentUuid;
194  static gd::Variable badVariable;
195  static gd::String badName;
196 
201  void Init(const VariablesContainer& other);
202 };
203 
204 } // namespace gd
Represent a layout ( also called a scene ) of a project.
Definition: Layout.h:40
Represent an object of a platform.
Definition: Object.h:37
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
Class defining a container for gd::Variable.
Definition: VariablesContainer.h:28
const gd::String & GetPersistentUuid() const
Get the persistent UUID used to recognize the same variables between serialization.
Definition: VariablesContainer.h:186
void Clear()
Clear all variables of the container.
Definition: VariablesContainer.h:148
std::size_t Count() const
Return the number of variables.
Definition: VariablesContainer.h:96
Definition: CommonTools.h:24