Game Develop Core: gd::EventsCodeGenerator Class Reference

| Game Develop Core Core library for developing platforms and tools compatible with Game Develop.

- Main Page
- Modules
- Classes

- Class List
- Class Index
- Class Hierarchy

All Classes Files Functions Variables Typedefs Enumerations Macros Groups Pages
Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | Friends | List of all members
gd::EventsCodeGenerator Class Reference

Internal class used to generate code from events. More...

`#include <EventsCodeGenerator.h>`

## Public Member Functions

| EventsCodeGenerator (gd::Project &project_, const gd::Layout &layout, const gd::Platform &platform_)
| Construct a code generator for the specified platform/project/layout.
void | PreprocessEventList (std::vector< gd::BaseEventSPtr > &listEvent)
| Preprocess an events list ( Replacing for example links with the linked event ). More...
std::string | GenerateEventsListCode (std::vector< gd::BaseEventSPtr > &events, const EventsCodeGenerationContext &context)
| Generate code for executing an event list. More...
virtual std::string | GenerateConditionsListCode (std::vector< gd::Instruction > &conditions, EventsCodeGenerationContext &context)
| Generate code for executing a condition list. More...
virtual std::string | GenerateActionsListCode (std::vector< gd::Instruction > &actions, EventsCodeGenerationContext &context)
| Generate code for executing an action list. More...
std::vector< std::string > | GenerateParametersCodes (std::vector< gd::Expression > parameters, const std::vector< gd::ParameterMetadata > &parametersInfo, EventsCodeGenerationContext &context, std::vector< std::pair< std::string, std::string > > *supplementaryParametersTypes=0)
| Generate the code for a parameter of an action/condition/expression. More...
std::string | GenerateConditionCode (gd::Instruction &condition, std::string returnBoolean, EventsCodeGenerationContext &context)
| Generate code for a single condition. More...
std::string | GenerateActionCode (gd::Instruction &action, EventsCodeGenerationContext &context)
| Generate code for a single action. More...
virtual std::string | GenerateObjectsDeclarationCode (EventsCodeGenerationContext &context)
| Generate code for declaring objects lists. More...
virtual std::string | ConvertToString (std::string plainString)
| Must convert a plain string ( with line feed, quotes ) to a string that can be inserted into code. More...
virtual std::string | ConvertToStringExplicit (std::string plainString)
| Convert a plain string ( with line feed, quotes ) to a string that can be inserted into code. The string construction must be explicit : for example, quotes must be added if the target language need quotes. More...
void | AddIncludeFile (std::string file)
| Declare an include file to be added. More...
void | AddIncludeFiles (std::vector< std::string > files)
| Declare a list of include files to be added. More...
void | AddGlobalDeclaration (std::string declaration)
| Add a declaration which will be inserted after includes.
void | AddCustomCodeOutsideMain (std::string code)
| Add some code before events outside the main function.
void | AddCustomCodeInMain (std::string code)
| Add some code before events in the main function.
const std::set< std::string > & | GetIncludeFiles () const
| Get the set containing the include files.
const std::string & | GetCustomCodeOutsideMain () const
| Get the custom code to be inserted outside main.
const std::string & | GetCustomCodeInMain () const
| Get the custom code to be inserted inside main function.
const std::set< std::string > & | GetCustomGlobalDeclaration () const
| Get the custom declaration to be inserted after includes.
bool | GenerateCodeForRuntime ()
| Return true if code generation is made for runtime only.
void | SetGenerateCodeForRuntime (bool compilationForRuntime_)
| Set if the code generated is meant to be used for runtime only and not in the IDE.
void | ReportError ()
| Report that an error occurred during code generation ( Event code won't be generated )
bool | ErrorOccurred () const
| Return true if an error has occurred during code generation ( in this case, generated code is not usable )
gd::Project & | GetProject () const
| Get the project the code is being generated for.
const gd::Layout & | GetLayout () const
| Get the layout the code is being generated for.
const gd::Platform & | GetPlatform () const
| Get the platform the code is being generated for.
std::vector< std::string > | ExpandObjectsName (const std::string &objectName, const EventsCodeGenerationContext &context) const
| Convert a group name to the full list of objects contained in the group. More...
size_t | GetMaxCustomConditionsDepth () const
| Get the maximum depth of custom conditions reached during code generation.
size_t | GetMaxConditionsListsSize () const
| Get the maximum size of a list of conditions.
virtual std::string | GenerateBooleanFullName (const std::string &boolName, const gd::EventsCodeGenerationContext &context)
| Generate the full name for accessing to a boolean variable used for conditions. More...
virtual std::string | GenerateBooleanInitializationToFalse (const std::string &boolName, const gd::EventsCodeGenerationContext &context)
| Must create a boolean. Its value must be false. More...
virtual std::string | GetObjectListName (const std::string &name, const gd::EventsCodeGenerationContext &context)
| Get the full name for accessing to a list of objects. More...

## Static Public Member Functions

static void | DeleteUselessEvents (std::vector< gd::BaseEventSPtr > &events)

## Protected Member Functions

virtual std::string | GenerateParameterCodes (const std::string &parameter, const gd::ParameterMetadata &metadata, gd::EventsCodeGenerationContext &context, const std::string &previousParameter, std::vector< std::pair< std::string, std::string > > *supplementaryParametersTypes)
| Generate the code for a single parameter. More...
virtual std::string | GenerateObjectFunctionCall (std::string objectListName, const ObjectMetadata &objMetadata, const gd::ExpressionCodeGenerationInformation &codeInfo, std::string parametersStr, std::string defaultOutput, gd::EventsCodeGenerationContext &context)
| Call a function of the current object. More...
virtual std::string | GenerateObjectAutomatismFunctionCall (std::string objectListName, std::string automatismName, const gd::AutomatismMetadata &autoInfo, const gd::ExpressionCodeGenerationInformation &codeInfo, std::string parametersStr, std::string defaultOutput, gd::EventsCodeGenerationContext &context)
| Call a function of an automatism of the current object. More...
virtual std::string | GenerateScopeBegin (gd::EventsCodeGenerationContext &context, const std::string &extraVariable="")
| Called when a new scope must be entered. More...
virtual std::string | GenerateScopeEnd (gd::EventsCodeGenerationContext &context, const std::string &extraVariable="")
| Called when a new must be ended. More...
virtual std::string | GenerateNegatedPredicat (const std::string &predicat) const
| Must negate a predicat. More...
virtual std::string | GenerateReferenceToUpperScopeBoolean (const std::string &referenceName, const std::string &referencedBoolean, gd::EventsCodeGenerationContext &context)
| Must create a boolean which is a reference to a boolean declared in the parent scope. More...
virtual std::string | GenerateFreeCondition (const std::vector< std::string > &arguments, const gd::InstructionMetadata &instrInfos, const std::string &returnBoolean, bool conditionInverted, gd::EventsCodeGenerationContext &context)
virtual std::string | GenerateObjectCondition (const std::string &objectName, const gd::ObjectMetadata &objInfo, const std::vector< std::string > &arguments, const gd::InstructionMetadata &instrInfos, const std::string &returnBoolean, bool conditionInverted, gd::EventsCodeGenerationContext &context)
virtual std::string | GenerateAutomatismCondition (const std::string &objectName, const std::string &automatismName, const gd::AutomatismMetadata &autoInfo, const std::vector< std::string > &arguments, const gd::InstructionMetadata &instrInfos, const std::string &returnBoolean, bool conditionInverted, gd::EventsCodeGenerationContext &context)
virtual std::string | GenerateFreeAction (const std::vector< std::string > &arguments, const gd::InstructionMetadata &instrInfos, gd::EventsCodeGenerationContext &context)
virtual std::string | GenerateObjectAction (const std::string &objectName, const gd::ObjectMetadata &objInfo, const std::vector< std::string > &arguments, const gd::InstructionMetadata &instrInfos, gd::EventsCodeGenerationContext &context)
virtual std::string | GenerateAutomatismAction (const std::string &objectName, const std::string &automatismName, const gd::AutomatismMetadata &autoInfo, const std::vector< std::string > &arguments, const gd::InstructionMetadata &instrInfos, gd::EventsCodeGenerationContext &context)
std::string | GenerateRelationalOperatorCall (const gd::InstructionMetadata &instrInfos, const std::vector< std::string > &arguments, const std::string &callStartString, unsigned int startFromArgument=0)
std::string | GenerateOperatorCall (const gd::InstructionMetadata &instrInfos, const std::vector< std::string > &arguments, const std::string &callStartString, const std::string &getterStartString, unsigned int startFromArgument=0)
std::string | GenerateCompoundOperatorCall (const gd::InstructionMetadata &instrInfos, const std::vector< std::string > &arguments, const std::string &callStartString, unsigned int startFromArgument=0)
std::string | GenerateTrue () const
| Must return an expression whose value is true.
std::string | GenerateFalse () const
| Must return an expression whose value is false.

## Protected Attributes

gd::Project & | project
| The project being used.
const gd::Layout & | scene
| The scene being generated.
const gd::Platform & | platform
| The platform being used.
bool | errorOccurred
| Must be set to true if an error occured.
bool | compilationForRuntime
| Is set to true if the code generation is made for runtime only.
std::set< std::string > | includeFiles
| List of headers files used by instructions. A (shared) pointer is used so as context created from another one can share the same list.
std::string | customCodeOutsideMain
| Custom code inserted before events ( and not in events function )
std::string | customCodeInMain
| Custom code inserted before events ( in main function )
std::set< std::string > | customGlobalDeclaration
| Custom global C++ declarations inserted after includes.
size_t | maxCustomConditionsDepth
| The maximum depth value for all the custom conditions created.
size_t | maxConditionsListsSize
| The maximum size of a list of conditions.

## Friends

class | CallbacksForGeneratingExpressionCode

## Detailed Description

Internal class used to generate code from events.

Todo:
  For now, this class generates only C++ code for GD C++ Platform.

See Also
  CallbacksForGeneratingExpressionCode

## Member Function Documentation

void gd::EventsCodeGenerator::AddIncludeFile | ( | std::string | file) |  |

inline

Declare an include file to be added.

Note
  The way includes files are used may vary depending on the platform:
  - On GD C++ Platform, the includes files are added in the #include directives of the generated code.
  - On GD JS Platform, the includes files are added in the list of JS files in the index file.

void gd::EventsCodeGenerator::AddIncludeFiles | ( | std::vector< std::string > | files) |  |

inline

Declare a list of include files to be added.

See Also
  gd::EventsCodeGenerator::AddIncludeFile

std::string gd::EventsCodeGenerator::ConvertToString | ( | std::string | plainString) |  |

virtual

Must convert a plain string ( with line feed, quotes ) to a string that can be inserted into code.

Note
  It is the caller responsibility to add proper code need to create a full string.

Usage example :

code += "std::string(\""+codeGenerator.ConvertToString(name)+"\")";
/

Parameters

  plainString | The string to convert

Returns
  plainString which can be included into the generated code.

std::string gd::EventsCodeGenerator::ConvertToStringExplicit | ( | std::string | plainString) |  |

virtual

Convert a plain string ( with line feed, quotes ) to a string that can be inserted into code. The string construction must be explicit : for example, quotes must be added if the target language need quotes.

Usage example :

code += codeGenerator.ConvertToStringExplicit(name);

Note
  The default implementation simply call ConvertToString and add quotes

Parameters

  plainString | The string to convert

Returns
  plainString which can be included into the generated code.

void gd::EventsCodeGenerator::DeleteUselessEvents | ( | std::vector< gd::BaseEventSPtr > & | events) |  |

static

Remove events not executed

std::vector< std::string > gd::EventsCodeGenerator::ExpandObjectsName | ( | const std::string & | objectName,
|  | const EventsCodeGenerationContext & | context
| ) |  | const

Convert a group name to the full list of objects contained in the group.

Get a list containing the "real" objects name when the events refers to objectName :
If objectName if really an object, the list will only contains objectName unchanged.
If objectName is a group, the list will contains all the objects of the group.
If objectName is the "current" object in the context ( i.e: The object being used for launching an action... ), none of the two rules below apply, and the list will only contains the context "current" object name.

std::string gd::EventsCodeGenerator::GenerateActionCode | ( | gd::Instruction & | action,
|  | EventsCodeGenerationContext & | context
| ) |  |

Generate code for a single action.

The generation is really done in GenerateFreeAction/GenerateObjectAction or GenerateAutomatismAction.

Parameters

  condition | instruction to be done.
  context | Context used for generation

Returns
  Code

Generate code for an action.

string gd::EventsCodeGenerator::GenerateActionsListCode | ( | std::vector< gd::Instruction > & | actions,
|  | EventsCodeGenerationContext & | context
| ) |  |

virtual

Generate code for executing an action list.

The default implementation just calls repeatedly GenerateActionCode.

Parameters

  game | Game used
  scene | Scene used
  actions | std::vector of actions
  context | Context used for generation

Returns
  Code

Generate actions code.

virtual std::string gd::EventsCodeGenerator::GenerateBooleanFullName | ( | const std::string & | boolName,
|  | const gd::EventsCodeGenerationContext & | context
| ) |  |

inlinevirtual

Generate the full name for accessing to a boolean variable used for conditions.

Default implementation just returns the boolean name passed as argument.

virtual std::string gd::EventsCodeGenerator::GenerateBooleanInitializationToFalse | ( | const std::string & | boolName,
|  | const gd::EventsCodeGenerationContext & | context
| ) |  |

inlinevirtual

Must create a boolean. Its value must be false.

The default implementation generates C-style code.

string gd::EventsCodeGenerator::GenerateCompoundOperatorCall | ( | const gd::InstructionMetadata & | instrInfos,
|  | const std::vector< std::string > & | arguments,
|  | const std::string & | callStartString,
|  | unsigned int | startFromArgument = `0`
| ) |  |

protected

Generate call using a compound assignment operators ( =,+=,-=,*=,/= ). Operator position is deduced from parameters type. Expression is assumed to be placed just before the operator.

Parameters

  Information | about the instruction
  Arguments,in | their C++ form.
  String | to be placed at the start of the call ( the function to be called typically ). Example : MyObject->Set
  Arguments | will be generated starting from this number. For example, set this to 1 to skip the first argument.

std::string gd::EventsCodeGenerator::GenerateConditionCode | ( | gd::Instruction & | condition,
|  | std::string | returnBoolean,
|  | EventsCodeGenerationContext & | context
| ) |  |

Generate code for a single condition.

The generation is really done in GenerateFreeCondition/GenerateObjectCondition or GenerateAutomatismCondition.

Parameters

  condition | instruction to be done.
  returnBoolean | The name of the boolean that must contains the condition result.
  context | Context used for generation

Returns
  Code

string gd::EventsCodeGenerator::GenerateConditionsListCode | ( | std::vector< gd::Instruction > & | conditions,
|  | EventsCodeGenerationContext & | context
| ) |  |

virtual

Generate code for executing a condition list.

The default implementation create the condition calls using C-style ifs and booleans.

Parameters

  game | Game used
  scene | Scene used
  conditions | std::vector of conditions
  context | Context used for generation

Returns
  Code. Boolean containing conditions result are name conditionXIsTrue, with X = the number of the condition, starting from 0.

Generate code for a list of conditions. Bools containing conditions results are named conditionXIsTrue.

string gd::EventsCodeGenerator::GenerateEventsListCode | ( | std::vector< gd::BaseEventSPtr > & | events,
|  | const EventsCodeGenerationContext & | context
| ) |  |

Generate code for executing an event list.

Parameters

  events | std::vector of events
  context | Context used for generation

Returns
  C++ code

Generate events list code.

virtual std::string gd::EventsCodeGenerator::GenerateNegatedPredicat | ( | const std::string & | predicat) |  | const

inlineprotectedvirtual

Must negate a predicat.

The default implementation generates C-style code : It wraps the predicat inside parenthesis and add a !.

std::string gd::EventsCodeGenerator::GenerateObjectAutomatismFunctionCall | ( | std::string | objectListName,
|  | std::string | automatismName,
|  | const gd::AutomatismMetadata & | autoInfo,
|  | const gd::ExpressionCodeGenerationInformation & | codeInfo,
|  | std::string | parametersStr,
|  | std::string | defaultOutput,
|  | gd::EventsCodeGenerationContext & | context
| ) |  |

protectedvirtual

Call a function of an automatism of the current object.

Note
  The current object is the object being manipulated by a condition or an action.

Parameters

  objectListName | The full name of the object list being used
  automatismName | The full name of the automatism to be used
  objMetadata | Metadata about the automatism being used.
  functionCallName | The function to be called on this object.
  parametersStr | The parameters of the function
  context | The context : May be used to get information about the current scope.

std::string gd::EventsCodeGenerator::GenerateObjectFunctionCall | ( | std::string | objectListName,
|  | const ObjectMetadata & | objMetadata,
|  | const gd::ExpressionCodeGenerationInformation & | codeInfo,
|  | std::string | parametersStr,
|  | std::string | defaultOutput,
|  | gd::EventsCodeGenerationContext & | context
| ) |  |

protectedvirtual

Call a function of the current object.

Note
  The current object is the object being manipulated by a condition or an action.

Parameters

  objectListName | The full name of the object list being used
  objMetadata | Metadata about the object being used.
  functionCallName | The function to be called on this object.
  parametersStr | The parameters of the function
  context | The context : May be used to get information about the current scope.

std::string gd::EventsCodeGenerator::GenerateObjectsDeclarationCode | ( | EventsCodeGenerationContext & | context) |  |

virtual

Generate code for declaring objects lists.

This method is used for each event.

Parameters

  context | The context to be used.

string gd::EventsCodeGenerator::GenerateOperatorCall | ( | const gd::InstructionMetadata & | instrInfos,
|  | const std::vector< std::string > & | arguments,
|  | const std::string & | callStartString,
|  | const std::string & | getterStartString,
|  | unsigned int | startFromArgument = `0`
| ) |  |

protected

Generate call using an operator ( =,+,-,*,/ ). Operator position is deduced from parameters type. Expression is assumed to be placed just before the operator.

Parameters

  Information | about the instruction
  Arguments,in | their C++ form.
  String | to be placed at the start of the call ( the function to be called typically ). Example : MyObject->Set
  String | to be placed at the start of the call of the getter ( the "getter" function to be called typically ). Example : MyObject->Get
  Arguments | will be generated starting from this number. For example, set this to 1 to skip the first argument.

std::string gd::EventsCodeGenerator::GenerateParameterCodes | ( | const std::string & | parameter,
|  | const gd::ParameterMetadata & | metadata,
|  | gd::EventsCodeGenerationContext & | context,
|  | const std::string & | previousParameter,
|  | std::vector< std::pair< std::string, std::string > > * | supplementaryParametersTypes
| ) |  |

protectedvirtual

Generate the code for a single parameter.

Standard supported parameters type, and how they are used in code:

- object : Object name -> string
- expression : Mathematical expression -> number (double)
- string : Text expression -> string
- layer, color, file, joyaxis : Same as string
- relationalOperator : Used to make a comparison between the function resturn value and value of the parameter preceding the relationOperator parameter -> string
- operator : Used to update a value using a setter and a getter -> string
- key, mouse, objectvar, scenevar, globalvar, password, musicfile, soundfile, police -> string
- trueorfalse, yesorno -> boolean ( See GenerateTrue/GenerateFalse ).

"Code only" parameters types:

- inlineCode: supplementary information associated with the parameter is directly pasted in the code without change.

Other standard parameters type that should be implemented by platforms:

- currentScene: Reference to the current runtime scene.
- objectList : a map containing lists of objects which are specified by the object name in another parameter. (C++: std::map <std::string, std::vector<RuntimeObject*> *>). Example:
  AddExpression("Count", _("Object count"), _("Count the number of picked objects"), _("Objects"), "res/conditions/nbObjet.png")
  .AddParameter("objectList", _("Object"))
  .codeExtraInformation.SetFunctionName("PickedObjectsCount").SetIncludeFile("GDCpp/BuiltinExtensions/ObjectTools.h");
- objectListWithoutPicking : Same as objectList but do not pick object if they are not already picked.
- objectPtr : Return a pointer to object specified by the object name in another parameter ( C++: RuntimeObject* ). Example:
  .AddParameter("object", _("Object"))
  .AddParameter("objectPtr", _("Target object"))
  //The called function will be called with this signature on the C++ platform: Function(std::string, RuntimeObject*)

vector< string > gd::EventsCodeGenerator::GenerateParametersCodes | ( | std::vector< gd::Expression > | parameters,
|  | const std::vector< gd::ParameterMetadata > & | parametersInfo,
|  | EventsCodeGenerationContext & | context,
|  | std::vector< std::pair< std::string, std::string > > * | supplementaryParametersTypes = `0`
| ) |  |

Generate the code for a parameter of an action/condition/expression.

This method uses GenerateParameterCodes to generate the parameters code.

Parameters

  scene | Scene used
  parameters | std::vector of actual parameters.
  parametersInfo | std::vector of information about parameters
  context | Context used for generation
  supplementaryParametersTypes | Optional std::vector of new parameters types ( std::vector of pair<std::string,std::string>("type", "valueToBeInserted") )

virtual std::string gd::EventsCodeGenerator::GenerateReferenceToUpperScopeBoolean | ( | const std::string & | referenceName,
|  | const std::string & | referencedBoolean,
|  | gd::EventsCodeGenerationContext & | context
| ) |  |

inlineprotectedvirtual

Must create a boolean which is a reference to a boolean declared in the parent scope.

The default implementation generates C-style code.

string gd::EventsCodeGenerator::GenerateRelationalOperatorCall | ( | const gd::InstructionMetadata & | instrInfos,
|  | const std::vector< std::string > & | arguments,
|  | const std::string & | callStartString,
|  | unsigned int | startFromArgument = `0`
| ) |  |

protected

Generate call using a relational operator. Relational operator position is deduced from parameters type. Rhs hand side expression is assumed to be placed just before the relational operator.

Parameters

  Information | about the instruction
  Arguments,in | their C++ form.
  String | to be placed at the start of the call ( the function to be called typically ). Example : MyObject->Get
  Arguments | will be generated starting from this number. For example, set this to 1 to skip the first argument.

virtual std::string gd::EventsCodeGenerator::GenerateScopeBegin | ( | gd::EventsCodeGenerationContext & | context,
|  | const std::string & | extraVariable = `""`
| ) |  |

inlineprotectedvirtual

Called when a new scope must be entered.

Parameters

  context | The context : Internal events of the scope have been generated, but GenerateObjectsDeclarationCode was not called.
  extraVariable | An optional supplementary variable that should be inherited from the parent scope.

virtual std::string gd::EventsCodeGenerator::GenerateScopeEnd | ( | gd::EventsCodeGenerationContext & | context,
|  | const std::string & | extraVariable = `""`
| ) |  |

inlineprotectedvirtual

Called when a new must be ended.

Parameters

  context | The context : Internal events of the scope have been generated, but GenerateObjectsDeclarationCode was not called.
  extraVariable | An optional supplementary variable that should be inherited from the parent scope.

std::string gd::EventsCodeGenerator::GetObjectListName | ( | const std::string & | name,
|  | const gd::EventsCodeGenerationContext & | context
| ) |  |

virtual

Get the full name for accessing to a list of objects.

Default implementation simply returns the name mangled using gd::EventsCodeNameMangler.

void gd::EventsCodeGenerator::PreprocessEventList | ( | std::vector< gd::BaseEventSPtr > & | listEvent) |  |

Preprocess an events list ( Replacing for example links with the linked event ).

This should be called before any code generation.

Call preprocessing method of each event

The documentation for this class was generated from the following files:

- EventsCodeGenerator.h
- EventsCodeGenerator.cpp