GDevelop Core
Core library for developing platforms and tools compatible with GDevelop.
EventsCodeGenerationContext.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 #ifndef EVENTSCODEGENERATIONCONTEXT_H
7 #define EVENTSCODEGENERATIONCONTEXT_H
8 #include <map>
9 #include <memory>
10 #include <set>
11 
12 #include "GDCore/String.h"
13 
14 namespace gd {
15 
27 class GD_CORE_API EventsCodeGenerationContext {
28  friend class EventsCodeGenerator;
29 
30  public:
36  EventsCodeGenerationContext(unsigned int* maxDepthLevel_ = nullptr)
37  : maxDepthLevel(maxDepthLevel_){};
38  virtual ~EventsCodeGenerationContext(){};
39 
45  void InheritsFrom(EventsCodeGenerationContext& parent);
46 
51  void InheritsAsAsyncCallbackFrom(EventsCodeGenerationContext& parent);
52 
59  void Reuse(EventsCodeGenerationContext& parent);
60 
68  void ForbidReuse() { reuseExplicitlyForbidden = true; }
69 
74  bool CanReuse() const {
75  return !reuseExplicitlyForbidden && parent != nullptr;
76  }
77 
84  size_t GetContextDepth() const { return contextDepth; }
85 
91  const EventsCodeGenerationContext* GetParentContext() const { return parent; }
92 
96  void SetCurrentObject(const gd::String& objectName) {
97  currentObject = objectName;
98  };
99 
103  void SetNoCurrentObject() { currentObject = ""; };
104 
108  const gd::String& GetCurrentObject() const { return currentObject; };
109 
117  void ObjectsListNeeded(const gd::String& objectName);
118 
127  void ObjectsListNeededOrEmptyIfJustDeclared(const gd::String& objectName);
128 
137  void EmptyObjectsListNeeded(const gd::String& objectName);
138 
142  bool ObjectAlreadyDeclaredByParents(const gd::String& objectName) const {
143  return (alreadyDeclaredObjectsLists.find(objectName) !=
144  alreadyDeclaredObjectsLists.end());
145  };
146 
151  std::set<gd::String> GetAllObjectsToBeDeclared() const;
152 
156  const std::set<gd::String>& GetObjectsListsToBeDeclared() const {
157  return objectsListsToBeDeclared;
158  };
159 
164  const std::set<gd::String>& GetObjectsListsToBeEmptyIfJustDeclared()
165  const {
166  return objectsListsOrEmptyToBeDeclared;
167  };
168 
174  const std::set<gd::String>& GetObjectsListsToBeDeclaredEmpty() const {
175  return emptyObjectsListsToBeDeclared;
176  };
177 
182  const std::set<gd::String>& GetObjectsListsAlreadyDeclaredByParents() const {
183  return alreadyDeclaredObjectsLists;
184  };
185 
193  unsigned int GetLastDepthObjectListWasNeeded(
194  const gd::String& objectName) const;
195 
202  bool IsSameObjectsList(const gd::String& objectName,
203  const EventsCodeGenerationContext& otherContext) const;
204 
208  void EnterCustomCondition() { customConditionDepth++; };
209 
213  void LeaveCustomCondition() { customConditionDepth--; };
214 
223  size_t GetCurrentConditionDepth() const { return customConditionDepth; }
224 
233  const std::set<gd::String>& GetAllDeclaredObjectsAcrossChildren() {
234  return allObjectsListToBeDeclaredAcrossChildren;
235  };
236 
243  bool ShouldUseAsyncObjectsList(const gd::String& objectName) const;
244 
250  bool IsInsideAsync() const { return asyncDepth != 0; };
251 
256  bool IsAsyncCallback() const { return parent != nullptr && parent->asyncDepth != asyncDepth; }
257 
262  bool IsToBeDeclared(const gd::String& objectName) {
263  return objectsListsToBeDeclared.find(objectName) !=
264  objectsListsToBeDeclared.end() ||
265  objectsListsOrEmptyToBeDeclared.find(objectName) !=
266  objectsListsOrEmptyToBeDeclared.end() ||
267  emptyObjectsListsToBeDeclared.find(objectName) !=
268  emptyObjectsListsToBeDeclared.end();
269  };
270 
271  private:
272  void NotifyAsyncParentsAboutDeclaredObject(const gd::String& objectName);
273 
274  std::set<gd::String>
275  alreadyDeclaredObjectsLists;
277  std::set<gd::String>
278  objectsListsToBeDeclared;
280  std::set<gd::String>
281  objectsListsOrEmptyToBeDeclared;
285  std::set<gd::String>
286  emptyObjectsListsToBeDeclared;
291  std::set<gd::String>
292  allObjectsListToBeDeclaredAcrossChildren;
299 
300  std::map<gd::String, unsigned int>
301  depthOfLastUse;
302  gd::String
303  currentObject;
304  unsigned int contextDepth = 0;
307  unsigned int customConditionDepth =
308  0;
309  unsigned int asyncDepth =
310  0;
315  unsigned int* maxDepthLevel;
317  const EventsCodeGenerationContext* parent =
318  nullptr;
319  EventsCodeGenerationContext* nearestAsyncParent =
320  nullptr;
322  bool reuseExplicitlyForbidden =
323  false;
325 };
326 
327 } // namespace gd
328 #endif // EVENTSCODEGENERATIONCONTEXT_H
Used to manage the context when generating code for events.
Definition: EventsCodeGenerationContext.h:27
bool CanReuse() const
Return false if the object lists of the context can not be reused in a child context.
Definition: EventsCodeGenerationContext.h:74
const EventsCodeGenerationContext * GetParentContext() const
Get the parent context, if any.
Definition: EventsCodeGenerationContext.h:91
size_t GetCurrentConditionDepth() const
Get the current condition depth : The depth is increased each time a custom condition code is generat...
Definition: EventsCodeGenerationContext.h:223
bool ObjectAlreadyDeclaredByParents(const gd::String &objectName) const
Definition: EventsCodeGenerationContext.h:142
bool IsToBeDeclared(const gd::String &objectName)
Returns true if the given object is already going to be declared in this context (either as a traditi...
Definition: EventsCodeGenerationContext.h:262
const std::set< gd::String > & GetObjectsListsToBeDeclaredEmpty() const
Definition: EventsCodeGenerationContext.h:174
const std::set< gd::String > & GetObjectsListsToBeEmptyIfJustDeclared() const
Definition: EventsCodeGenerationContext.h:164
const std::set< gd::String > & GetAllDeclaredObjectsAcrossChildren()
Returns the list of all objects declared in this context and subcontexts.
Definition: EventsCodeGenerationContext.h:233
EventsCodeGenerationContext(unsigned int *maxDepthLevel_=nullptr)
Definition: EventsCodeGenerationContext.h:36
void EnterCustomCondition()
Called when a custom condition code is generated.
Definition: EventsCodeGenerationContext.h:208
size_t GetContextDepth() const
Returns the depth of the inheritance of the context.
Definition: EventsCodeGenerationContext.h:84
void LeaveCustomCondition()
Called when a custom condition code generation is over.
Definition: EventsCodeGenerationContext.h:213
void SetCurrentObject(const gd::String &objectName)
Definition: EventsCodeGenerationContext.h:96
const gd::String & GetCurrentObject() const
Definition: EventsCodeGenerationContext.h:108
void SetNoCurrentObject()
Definition: EventsCodeGenerationContext.h:103
bool IsInsideAsync() const
Definition: EventsCodeGenerationContext.h:250
const std::set< gd::String > & GetObjectsListsAlreadyDeclaredByParents() const
Definition: EventsCodeGenerationContext.h:182
const std::set< gd::String > & GetObjectsListsToBeDeclared() const
Definition: EventsCodeGenerationContext.h:156
bool IsAsyncCallback() const
Definition: EventsCodeGenerationContext.h:256
void ForbidReuse()
Forbid any optimization that would reuse and modify the object list from this context in children con...
Definition: EventsCodeGenerationContext.h:68
Internal class used to generate code from events.
Definition: EventsCodeGenerator.h:40
String represents an UTF8 encoded string.
Definition: String.h:31
Definition: CommonTools.h:24