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/MemoryTrackedRegistry.h"
11 #include "GDCore/Project/Variable.h"
12 #include "GDCore/String.h"
13 namespace gd {
14 class SerializerElement;
15 }
16 
17 namespace gd {
18 
29 class GD_CORE_API VariablesContainer {
30  public:
31  enum SourceType {
32  Unknown,
33  Global,
34  Scene,
35  Object,
36  Local,
37  ExtensionGlobal,
38  ExtensionScene,
39  Parameters,
40  Properties,
41  };
42 
44  VariablesContainer(const SourceType sourceType);
46  virtual ~VariablesContainer(){};
47 
48  VariablesContainer& operator=(const VariablesContainer& rhs);
49 
50  SourceType GetSourceType() const { return sourceType; }
51 
56 
60  bool Has(const gd::String& name) const;
61 
65  Variable& Get(const gd::String& name);
66 
70  const Variable& Get(const gd::String& name) const;
71 
76  Variable& Get(std::size_t index);
77 
82  const Variable& Get(std::size_t index) const;
83 
92  Variable& Insert(const gd::String& name,
93  const Variable& variable,
94  std::size_t position);
95 
99  std::size_t Count() const { return variables.size(); };
100 
104  const gd::String& GetNameAt(std::size_t index) const;
105 
110  std::size_t GetPosition(const gd::String& name) const;
111 
119  Variable& InsertNew(const gd::String& name, std::size_t position = -1);
120 
125  void Remove(const gd::String& name);
126 
130  void RemoveRecursively(const gd::Variable& variable);
131 
136  bool Rename(const gd::String& oldName, const gd::String& newName);
137 
141  void Swap(std::size_t firstVariableIndex, std::size_t secondVariableIndex);
142 
146  void Move(std::size_t oldIndex, std::size_t newIndex);
147 
151  inline void Clear() { variables.clear(); }
152 
156  void ForEachVariableMatchingSearch(const gd::String& search, std::function<void(const gd::String& name, const gd::Variable& variable)> fn) const;
158 
163 
166  void SerializeTo(SerializerElement& element) const;
167 
171  void UnserializeFrom(const SerializerElement& element);
172 
177  VariablesContainer& ResetPersistentUuid();
178 
183  VariablesContainer& ClearPersistentUuid();
184 
189  const gd::String& GetPersistentUuid() const { return persistentUuid; };
191 
192  private:
193  SourceType sourceType = Unknown;
194  std::vector<std::pair<gd::String, std::shared_ptr<gd::Variable>>> variables;
195  mutable gd::String persistentUuid;
197  static gd::Variable badVariable;
198  static gd::String badName;
199  gd::MemoryTracked _memoryTracked{this, "VariablesContainer"};
200 
205  void Init(const VariablesContainer& other);
206 };
207 
208 } // namespace gd
Represent a layout ( also called a scene ) of a project.
Definition: Layout.h:41
A non-copyable, non-movable member object that registers/unregisters its owner with MemoryTrackedRegi...
Definition: MemoryTrackedRegistry.h:238
Represent an object of a platform.
Definition: Object.h:39
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:29
const gd::String & GetPersistentUuid() const
Get the persistent UUID used to recognize the same variables between serialization.
Definition: VariablesContainer.h:189
void Clear()
Clear all variables of the container.
Definition: VariablesContainer.h:151
std::size_t Count() const
Return the number of variables.
Definition: VariablesContainer.h:99
Definition: CommonTools.h:24