GDevelop Core
Core library for developing platforms and tools compatible with GDevelop.
ExpressionMetadata.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 "AbstractFunctionMetadata.h"
9 
10 #include <functional>
11 #include <memory>
12 
13 #include "GDCore/Events/Instruction.h"
14 #include "GDCore/Extensions/Metadata/InstructionMetadata.h"
15 #include "GDCore/String.h"
16 namespace gd {
17 class Layout;
18 }
19 
20 namespace gd {
25  public:
27  : staticFunction(false), hasCustomCodeGenerator(false){};
29 
30  // TODO Move these attributes to ExpressionMetadata.
31  bool staticFunction;
32  gd::String functionCallName;
33  bool hasCustomCodeGenerator;
34  std::function<gd::String(const std::vector<gd::Expression>& parameters,
35  gd::EventsCodeGenerator& codeGenerator,
37  customCodeGenerator;
38  std::vector<gd::String> includeFiles;
39 };
40 
48  public:
52  ExpressionMetadata(const gd::String& returnType,
53  const gd::String& extensionNamespace,
54  const gd::String& name,
55  const gd::String& fullname,
56  const gd::String& description,
57  const gd::String& group,
58  const gd::String& smallicon);
59 
66  : returnType("unknown"), shown(false), isPrivate(false), relevantContext("Any"){};
67 
68  virtual ~ExpressionMetadata(){};
69 
73  ExpressionMetadata& SetHidden() override;
74 
79  group = str;
80  return *this;
81  }
82 
87  const gd::String& GetHelpPath() const { return helpPath; }
88 
94  helpPath = path;
95  return *this;
96  }
97 
102  bool IsPrivate() const { return isPrivate; }
103 
109  isPrivate = true;
110  return *this;
111  }
112 
117  return relevantContext == "Any" || relevantContext == "Layout";
118  }
119 
124  return relevantContext == "Any" || relevantContext == "Function";
125  }
126 
131  return relevantContext == "Any" || relevantContext == "Function" ||
132  relevantContext == "AsynchronousFunction";
133  }
134 
139  return relevantContext == "Any" || relevantContext == "Object";
140  }
141 
146  relevantContext = "Layout";
147  return *this;
148  }
149 
154  relevantContext = "Function";
155  return *this;
156  }
157 
162  relevantContext = "AsynchronousFunction";
163  return *this;
164  }
165 
170  relevantContext = "Object";
171  return *this;
172  }
173 
178  AddParameter(const gd::String &type, const gd::String &label,
179  const gd::String &supplementaryInformation = "",
180  bool parameterIsOptional = false) override;
181 
186  AddCodeOnlyParameter(const gd::String &type,
187  const gd::String &supplementaryInformation) override;
188 
195  ExpressionMetadata &SetDefaultValue(const gd::String &defaultValue) override {
196  if (parameters.GetParametersCount() > 0) {
197  parameters.GetInternalVector().back()->SetDefaultValue(defaultValue);
198  }
199  return *this;
200  };
201 
209  SetParameterLongDescription(const gd::String &longDescription) override {
210  if (parameters.GetParametersCount() > 0) {
211  parameters.GetInternalVector().back()->SetLongDescription(longDescription);
212  }
213  return *this;
214  };
215 
224  const gd::String &extraInfo) override {
225  if (parameters.GetParametersCount() > 0) {
226  parameters.GetInternalVector().back()->SetExtraInfo(extraInfo);
227  }
228  return *this;
229  }
230 
237  ExpressionMetadata& SetRequiresBaseObjectCapability(
238  const gd::String& capability);
239 
245  return requiredBaseObjectCapability;
246  };
247 
248  bool IsShown() const { return shown; }
249  const gd::String& GetReturnType() const { return returnType; }
250  const gd::String& GetFullName() const { return fullname; }
251  const gd::String& GetDescription() const { return description; }
252  const gd::String& GetGroup() const { return group; }
253  const gd::String& GetSmallIconFilename() const { return smallIconFilename; }
254  const gd::ParameterMetadata& GetParameter(std::size_t id) const {
255  return parameters.GetParameter(id);
256  };
257  gd::ParameterMetadata& GetParameter(std::size_t id) {
258  return parameters.GetParameter(id);
259  };
260  std::size_t GetParametersCount() const { return parameters.GetParametersCount(); };
261  const gd::ParameterMetadataContainer& GetParameters() const {
262  return parameters;
263  };
264 
270  const gd::String& functionName) override {
271  codeExtraInformation.functionCallName = functionName;
272  return *this;
273  }
274 
275 
280  return codeExtraInformation.functionCallName;
281  }
286  codeExtraInformation.staticFunction = true;
287  return *this;
288  }
289 
296  const gd::String& includeFile) override {
297  codeExtraInformation.includeFiles.clear();
298  codeExtraInformation.includeFiles.push_back(includeFile);
299  return *this;
300  }
301 
306  const gd::String& includeFile) override {
307  if (std::find(codeExtraInformation.includeFiles.begin(), codeExtraInformation.includeFiles.end(), includeFile) ==
308  codeExtraInformation.includeFiles.end())
309  codeExtraInformation.includeFiles.push_back(includeFile);
310 
311  return *this;
312  }
313 
317  const std::vector<gd::String>& GetIncludeFiles() const override {
318  return codeExtraInformation.includeFiles;
319  };
320 
326  std::function<gd::String(const std::vector<gd::Expression>& parameters,
327  gd::EventsCodeGenerator& codeGenerator,
329  codeGenerator) {
330  codeExtraInformation.hasCustomCodeGenerator = true;
331  codeExtraInformation.customCodeGenerator = codeGenerator;
332  return *this;
333  }
334 
335  ExpressionMetadata& RemoveCustomCodeGenerator() {
336  codeExtraInformation.hasCustomCodeGenerator = false;
337  std::function<gd::String(const std::vector<gd::Expression>& parameters,
338  gd::EventsCodeGenerator& codeGenerator,
340  emptyFunction;
341  codeExtraInformation.customCodeGenerator = emptyFunction;
342  return *this;
343  }
344 
345  bool HasCustomCodeGenerator() const { return codeExtraInformation.hasCustomCodeGenerator; }
346 
354  return *this;
355  }
356 
357  ExpressionCodeGenerationInformation codeExtraInformation;
358 
359  private:
360  gd::String returnType;
361  gd::String fullname;
362  gd::String description;
363  gd::String helpPath;
364  gd::String group;
365  bool shown;
366 
367  gd::String smallIconFilename;
368  gd::String extensionNamespace;
369  bool isPrivate;
370  gd::String requiredBaseObjectCapability;
371  gd::String relevantContext;
372 
374 };
375 
376 } // namespace gd
Describe user-friendly information about an expression or an instruction (action or condition),...
Definition: AbstractFunctionMetadata.h:36
Used to manage the context when generating code for events.
Definition: EventsCodeGenerationContext.h:27
Internal class used to generate code from events.
Definition: EventsCodeGenerator.h:41
Information about how generate code for an expression.
Definition: ExpressionMetadata.h:24
Describe user-friendly information about an expression, its parameters and the function name as well ...
Definition: ExpressionMetadata.h:47
ExpressionMetadata & SetStatic()
Set that the function is static.
Definition: ExpressionMetadata.h:285
ExpressionMetadata & SetParameterExtraInfo(const gd::String &extraInfo) override
Set the additional information, used for some parameters with special type (for example,...
Definition: ExpressionMetadata.h:223
ExpressionMetadata & SetHelpPath(const gd::String &path)
Definition: ExpressionMetadata.h:93
bool IsRelevantForCustomObjectEvents() const
Definition: ExpressionMetadata.h:138
ExpressionMetadata & GetCodeExtraInformation()
Return the structure containing the information about code generation for the expression.
Definition: ExpressionMetadata.h:353
const std::vector< gd::String > & GetIncludeFiles() const override
Get the files that must be included to use the instruction.
Definition: ExpressionMetadata.h:317
bool IsPrivate() const
Definition: ExpressionMetadata.h:102
bool IsRelevantForLayoutEvents() const
Definition: ExpressionMetadata.h:116
ExpressionMetadata & SetRelevantForCustomObjectEventsOnly() override
Definition: ExpressionMetadata.h:169
ExpressionMetadata & SetDefaultValue(const gd::String &defaultValue) override
Definition: ExpressionMetadata.h:195
ExpressionMetadata & SetRelevantForAsynchronousFunctionEventsOnly() override
Definition: ExpressionMetadata.h:161
ExpressionMetadata & SetRelevantForLayoutEventsOnly() override
Definition: ExpressionMetadata.h:145
ExpressionMetadata & SetCustomCodeGenerator(std::function< gd::String(const std::vector< gd::Expression > &parameters, gd::EventsCodeGenerator &codeGenerator, gd::EventsCodeGenerationContext &context)> codeGenerator)
Set that the function must be generated using a custom code generator.
Definition: ExpressionMetadata.h:325
const gd::String & GetFunctionName()
Return the name of the function which will be called in the generated code.
Definition: ExpressionMetadata.h:279
ExpressionMetadata & SetIncludeFile(const gd::String &includeFile) override
Erase any existing include file and add the specified include.
Definition: ExpressionMetadata.h:295
bool IsRelevantForFunctionEvents() const
Definition: ExpressionMetadata.h:123
ExpressionMetadata()
Definition: ExpressionMetadata.h:65
bool IsRelevantForAsynchronousFunctionEvents() const
Definition: ExpressionMetadata.h:130
ExpressionMetadata & SetGroup(const gd::String &str)
Set the group of the instruction in the IDE.
Definition: ExpressionMetadata.h:78
const gd::String & GetRequiredBaseObjectCapability() const
Get the required specified capability for this (object) expression, or an empty string if there is no...
Definition: ExpressionMetadata.h:244
ExpressionMetadata & SetFunctionName(const gd::String &functionName) override
Set the function name which will be used when generating the code.
Definition: ExpressionMetadata.h:269
ExpressionMetadata & SetRelevantForFunctionEventsOnly() override
Definition: ExpressionMetadata.h:153
const gd::String & GetHelpPath() const
Definition: ExpressionMetadata.h:87
ExpressionMetadata & SetParameterLongDescription(const gd::String &longDescription) override
Set the long description shown in the editor for the last added parameter.
Definition: ExpressionMetadata.h:209
ExpressionMetadata & AddIncludeFile(const gd::String &includeFile) override
Add a file to the already existing include files.
Definition: ExpressionMetadata.h:305
ExpressionMetadata & SetPrivate() override
Definition: ExpressionMetadata.h:108
Used as a base class for classes that will own events-backed functions.
Definition: ParameterMetadataContainer.h:26
Describe a parameter of an instruction (action, condition) or of an expression: type,...
Definition: ParameterMetadata.h:27
String represents an UTF8 encoded string.
Definition: String.h:33
Definition: CommonTools.h:24