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 
80  deprecationMessage = message;
81  return *this;
82  }
83 
88  const gd::String& GetDeprecationMessage() const { return deprecationMessage; }
89 
93  bool IsDeprecated() const { return !deprecationMessage.empty(); }
94 
99  group = str;
100  return *this;
101  }
102 
107  const gd::String& GetHelpPath() const { return helpPath; }
108 
114  helpPath = path;
115  return *this;
116  }
117 
122  bool IsPrivate() const { return isPrivate; }
123 
129  isPrivate = true;
130  return *this;
131  }
132 
137  return relevantContext == "Any" || relevantContext == "Layout";
138  }
139 
144  return relevantContext == "Any" || relevantContext == "Function";
145  }
146 
151  return relevantContext == "Any" || relevantContext == "Function" ||
152  relevantContext == "AsynchronousFunction";
153  }
154 
159  return relevantContext == "Any" || relevantContext == "Object";
160  }
161 
166  relevantContext = "Layout";
167  return *this;
168  }
169 
174  relevantContext = "Function";
175  return *this;
176  }
177 
182  relevantContext = "AsynchronousFunction";
183  return *this;
184  }
185 
190  relevantContext = "Object";
191  return *this;
192  }
193 
198  AddParameter(const gd::String &type, const gd::String &label,
199  const gd::String &supplementaryInformation = "",
200  bool parameterIsOptional = false) override;
201 
206  AddCodeOnlyParameter(const gd::String &type,
207  const gd::String &supplementaryInformation) override;
208 
215  ExpressionMetadata &SetDefaultValue(const gd::String &defaultValue) override {
216  if (parameters.GetParametersCount() > 0) {
217  parameters.GetInternalVector().back()->SetDefaultValue(defaultValue);
218  }
219  return *this;
220  };
221 
229  SetParameterLongDescription(const gd::String &longDescription) override {
230  if (parameters.GetParametersCount() > 0) {
231  parameters.GetInternalVector().back()->SetLongDescription(longDescription);
232  }
233  return *this;
234  };
235 
244  const gd::String &extraInfo) override {
245  if (parameters.GetParametersCount() > 0) {
246  parameters.GetInternalVector().back()->SetExtraInfo(extraInfo);
247  }
248  return *this;
249  }
250 
257  ExpressionMetadata& SetRequiresBaseObjectCapability(
258  const gd::String& capability);
259 
265  return requiredBaseObjectCapability;
266  };
267 
268  bool IsShown() const { return shown; }
269  const gd::String& GetReturnType() const { return returnType; }
270  const gd::String& GetFullName() const { return fullname; }
271  const gd::String& GetDescription() const { return description; }
272  const gd::String& GetGroup() const { return group; }
273  const gd::String& GetSmallIconFilename() const { return smallIconFilename; }
274  const gd::ParameterMetadata& GetParameter(std::size_t id) const {
275  return parameters.GetParameter(id);
276  };
277  gd::ParameterMetadata& GetParameter(std::size_t id) {
278  return parameters.GetParameter(id);
279  };
280  std::size_t GetParametersCount() const { return parameters.GetParametersCount(); };
281  const gd::ParameterMetadataContainer& GetParameters() const {
282  return parameters;
283  };
284 
290  const gd::String& functionName) override {
291  codeExtraInformation.functionCallName = functionName;
292  return *this;
293  }
294 
295 
300  return codeExtraInformation.functionCallName;
301  }
306  codeExtraInformation.staticFunction = true;
307  return *this;
308  }
309 
316  const gd::String& includeFile) override {
317  codeExtraInformation.includeFiles.clear();
318  codeExtraInformation.includeFiles.push_back(includeFile);
319  return *this;
320  }
321 
326  const gd::String& includeFile) override {
327  if (std::find(codeExtraInformation.includeFiles.begin(), codeExtraInformation.includeFiles.end(), includeFile) ==
328  codeExtraInformation.includeFiles.end())
329  codeExtraInformation.includeFiles.push_back(includeFile);
330 
331  return *this;
332  }
333 
337  const std::vector<gd::String>& GetIncludeFiles() const override {
338  return codeExtraInformation.includeFiles;
339  };
340 
346  std::function<gd::String(const std::vector<gd::Expression>& parameters,
347  gd::EventsCodeGenerator& codeGenerator,
349  codeGenerator) {
350  codeExtraInformation.hasCustomCodeGenerator = true;
351  codeExtraInformation.customCodeGenerator = codeGenerator;
352  return *this;
353  }
354 
355  ExpressionMetadata& RemoveCustomCodeGenerator() {
356  codeExtraInformation.hasCustomCodeGenerator = false;
357  std::function<gd::String(const std::vector<gd::Expression>& parameters,
358  gd::EventsCodeGenerator& codeGenerator,
360  emptyFunction;
361  codeExtraInformation.customCodeGenerator = emptyFunction;
362  return *this;
363  }
364 
365  bool HasCustomCodeGenerator() const { return codeExtraInformation.hasCustomCodeGenerator; }
366 
374  return *this;
375  }
376 
377  ExpressionCodeGenerationInformation codeExtraInformation;
378 
379  private:
380  gd::String returnType;
381  gd::String fullname;
382  gd::String description;
383  gd::String helpPath;
384  gd::String group;
385  bool shown;
386 
387  gd::String smallIconFilename;
388  gd::String extensionNamespace;
389  bool isPrivate;
390  gd::String requiredBaseObjectCapability;
391  gd::String relevantContext;
392  gd::String deprecationMessage;
393 
395 };
396 
397 } // 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:305
ExpressionMetadata & SetParameterExtraInfo(const gd::String &extraInfo) override
Set the additional information, used for some parameters with special type (for example,...
Definition: ExpressionMetadata.h:243
ExpressionMetadata & SetHelpPath(const gd::String &path)
Definition: ExpressionMetadata.h:113
bool IsRelevantForCustomObjectEvents() const
Definition: ExpressionMetadata.h:158
ExpressionMetadata & GetCodeExtraInformation()
Return the structure containing the information about code generation for the expression.
Definition: ExpressionMetadata.h:373
ExpressionMetadata & SetDeprecationMessage(const gd::String &message) override
Set the deprecation message that explains why the expression is deprecated and what to use instead.
Definition: ExpressionMetadata.h:79
const std::vector< gd::String > & GetIncludeFiles() const override
Get the files that must be included to use the instruction.
Definition: ExpressionMetadata.h:337
bool IsPrivate() const
Definition: ExpressionMetadata.h:122
bool IsRelevantForLayoutEvents() const
Definition: ExpressionMetadata.h:136
bool IsDeprecated() const
Check if the expression is deprecated.
Definition: ExpressionMetadata.h:93
ExpressionMetadata & SetRelevantForCustomObjectEventsOnly() override
Definition: ExpressionMetadata.h:189
ExpressionMetadata & SetDefaultValue(const gd::String &defaultValue) override
Definition: ExpressionMetadata.h:215
ExpressionMetadata & SetRelevantForAsynchronousFunctionEventsOnly() override
Definition: ExpressionMetadata.h:181
ExpressionMetadata & SetRelevantForLayoutEventsOnly() override
Definition: ExpressionMetadata.h:165
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:345
const gd::String & GetFunctionName()
Return the name of the function which will be called in the generated code.
Definition: ExpressionMetadata.h:299
ExpressionMetadata & SetIncludeFile(const gd::String &includeFile) override
Erase any existing include file and add the specified include.
Definition: ExpressionMetadata.h:315
bool IsRelevantForFunctionEvents() const
Definition: ExpressionMetadata.h:143
ExpressionMetadata()
Definition: ExpressionMetadata.h:65
bool IsRelevantForAsynchronousFunctionEvents() const
Definition: ExpressionMetadata.h:150
ExpressionMetadata & SetGroup(const gd::String &str)
Set the group of the instruction in the IDE.
Definition: ExpressionMetadata.h:98
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:264
ExpressionMetadata & SetFunctionName(const gd::String &functionName) override
Set the function name which will be used when generating the code.
Definition: ExpressionMetadata.h:289
ExpressionMetadata & SetRelevantForFunctionEventsOnly() override
Definition: ExpressionMetadata.h:173
const gd::String & GetHelpPath() const
Definition: ExpressionMetadata.h:107
ExpressionMetadata & SetParameterLongDescription(const gd::String &longDescription) override
Set the long description shown in the editor for the last added parameter.
Definition: ExpressionMetadata.h:229
ExpressionMetadata & AddIncludeFile(const gd::String &includeFile) override
Add a file to the already existing include files.
Definition: ExpressionMetadata.h:325
const gd::String & GetDeprecationMessage() const
Get the deprecation message that explains why the expression is deprecated and what to use instead.
Definition: ExpressionMetadata.h:88
ExpressionMetadata & SetPrivate() override
Definition: ExpressionMetadata.h:128
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