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 #pragma once
7 
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 
263  void SetFollowedByElseEvent(bool followed) {
264  followedByElseEvent = followed;
265  }
266 
270  bool IsFollowedByElseEvent() const { return followedByElseEvent; }
271 
276  bool IsToBeDeclared(const gd::String& objectName) {
277  return objectsListsToBeDeclared.find(objectName) !=
278  objectsListsToBeDeclared.end() ||
279  objectsListsOrEmptyToBeDeclared.find(objectName) !=
280  objectsListsOrEmptyToBeDeclared.end() ||
281  emptyObjectsListsToBeDeclared.find(objectName) !=
282  emptyObjectsListsToBeDeclared.end();
283  };
284 
285  private:
286  void NotifyAsyncParentsAboutDeclaredObject(const gd::String& objectName);
287 
288  std::set<gd::String>
289  alreadyDeclaredObjectsLists;
291  std::set<gd::String>
292  objectsListsToBeDeclared;
294  std::set<gd::String>
295  objectsListsOrEmptyToBeDeclared;
299  std::set<gd::String>
300  emptyObjectsListsToBeDeclared;
305  std::set<gd::String>
306  allObjectsListToBeDeclaredAcrossChildren;
313 
314  std::map<gd::String, unsigned int>
315  depthOfLastUse;
316  gd::String
317  currentObject;
318  unsigned int contextDepth = 0;
321  unsigned int customConditionDepth =
322  0;
323  unsigned int asyncDepth =
324  0;
329  unsigned int* maxDepthLevel;
331  const EventsCodeGenerationContext* parent =
332  nullptr;
333  EventsCodeGenerationContext* nearestAsyncParent =
334  nullptr;
336  bool reuseExplicitlyForbidden =
337  false;
339  bool followedByElseEvent =
340  false;
343 };
344 
345 } // namespace gd
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
void SetFollowedByElseEvent(bool followed)
Set whether this event is followed by an Else event in the events list. Used by Standard event code g...
Definition: EventsCodeGenerationContext.h:263
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:276
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
bool IsFollowedByElseEvent() const
Return true if this event is followed by an Else event.
Definition: EventsCodeGenerationContext.h:270
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:41
String represents an UTF8 encoded string.
Definition: String.h:33
Definition: CommonTools.h:24