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 
243  if (parameters.GetParametersCount() > 0) {
244  parameters.GetInternalVector().back()->SetHint(hint);
245  }
246  return *this;
247  };
248 
257  const gd::String &extraInfo) override {
258  if (parameters.GetParametersCount() > 0) {
259  parameters.GetInternalVector().back()->SetExtraInfo(extraInfo);
260  }
261  return *this;
262  }
263 
270  ExpressionMetadata& SetRequiresBaseObjectCapability(
271  const gd::String& capability);
272 
278  return requiredBaseObjectCapability;
279  };
280 
281  bool IsShown() const { return shown; }
282  const gd::String& GetReturnType() const { return returnType; }
283  const gd::String& GetFullName() const { return fullname; }
284  const gd::String& GetDescription() const { return description; }
285  const gd::String& GetGroup() const { return group; }
286  const gd::String& GetSmallIconFilename() const { return smallIconFilename; }
287  const gd::ParameterMetadata& GetParameter(std::size_t id) const {
288  return parameters.GetParameter(id);
289  };
290  gd::ParameterMetadata& GetParameter(std::size_t id) {
291  return parameters.GetParameter(id);
292  };
293  std::size_t GetParametersCount() const { return parameters.GetParametersCount(); };
294  const gd::ParameterMetadataContainer& GetParameters() const {
295  return parameters;
296  };
297 
303  const gd::String& functionName) override {
304  codeExtraInformation.functionCallName = functionName;
305  return *this;
306  }
307 
308 
313  return codeExtraInformation.functionCallName;
314  }
319  codeExtraInformation.staticFunction = true;
320  return *this;
321  }
322 
329  const gd::String& includeFile) override {
330  codeExtraInformation.includeFiles.clear();
331  codeExtraInformation.includeFiles.push_back(includeFile);
332  return *this;
333  }
334 
339  const gd::String& includeFile) override {
340  if (std::find(codeExtraInformation.includeFiles.begin(), codeExtraInformation.includeFiles.end(), includeFile) ==
341  codeExtraInformation.includeFiles.end())
342  codeExtraInformation.includeFiles.push_back(includeFile);
343 
344  return *this;
345  }
346 
350  const std::vector<gd::String>& GetIncludeFiles() const override {
351  return codeExtraInformation.includeFiles;
352  };
353 
359  std::function<gd::String(const std::vector<gd::Expression>& parameters,
360  gd::EventsCodeGenerator& codeGenerator,
362  codeGenerator) {
363  codeExtraInformation.hasCustomCodeGenerator = true;
364  codeExtraInformation.customCodeGenerator = codeGenerator;
365  return *this;
366  }
367 
368  ExpressionMetadata& RemoveCustomCodeGenerator() {
369  codeExtraInformation.hasCustomCodeGenerator = false;
370  std::function<gd::String(const std::vector<gd::Expression>& parameters,
371  gd::EventsCodeGenerator& codeGenerator,
373  emptyFunction;
374  codeExtraInformation.customCodeGenerator = emptyFunction;
375  return *this;
376  }
377 
378  bool HasCustomCodeGenerator() const { return codeExtraInformation.hasCustomCodeGenerator; }
379 
387  return *this;
388  }
389 
390  ExpressionCodeGenerationInformation codeExtraInformation;
391 
392  private:
393  gd::String returnType;
394  gd::String fullname;
395  gd::String description;
396  gd::String helpPath;
397  gd::String group;
398  bool shown;
399 
400  gd::String smallIconFilename;
401  gd::String extensionNamespace;
402  bool isPrivate;
403  gd::String requiredBaseObjectCapability;
404  gd::String relevantContext;
405  gd::String deprecationMessage;
406 
408 };
409 
410 } // 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:318
ExpressionMetadata & SetParameterExtraInfo(const gd::String &extraInfo) override
Set the additional information, used for some parameters with special type (for example,...
Definition: ExpressionMetadata.h:256
ExpressionMetadata & SetHelpPath(const gd::String &path)
Definition: ExpressionMetadata.h:113
ExpressionMetadata & SetParameterHint(const gd::String &hint) override
Set a hint attached to the last added parameter. See gd::InstructionMetadata::SetParameterHint.
Definition: ExpressionMetadata.h:242
bool IsRelevantForCustomObjectEvents() const
Definition: ExpressionMetadata.h:158
ExpressionMetadata & GetCodeExtraInformation()
Return the structure containing the information about code generation for the expression.
Definition: ExpressionMetadata.h:386
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:350
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:358
const gd::String & GetFunctionName()
Return the name of the function which will be called in the generated code.
Definition: ExpressionMetadata.h:312
ExpressionMetadata & SetIncludeFile(const gd::String &includeFile) override
Erase any existing include file and add the specified include.
Definition: ExpressionMetadata.h:328
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:277
ExpressionMetadata & SetFunctionName(const gd::String &functionName) override
Set the function name which will be used when generating the code.
Definition: ExpressionMetadata.h:302
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:338
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