MIRA
UserDefinedMacro.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2025 by
3  * MetraLabs GmbH (MLAB), GERMANY
4  * and
5  * Neuroinformatics and Cognitive Robotics Labs (NICR) at TU Ilmenau, GERMANY
6  * All rights reserved.
7  *
8  * Contact: info@mira-project.org
9  *
10  * Commercial Usage:
11  * Licensees holding valid commercial licenses may use this file in
12  * accordance with the commercial license agreement provided with the
13  * software or, alternatively, in accordance with the terms contained in
14  * a written agreement between you and MLAB or NICR.
15  *
16  * GNU General Public License Usage:
17  * Alternatively, this file may be used under the terms of the GNU
18  * General Public License version 3.0 as published by the Free Software
19  * Foundation and appearing in the file LICENSE.GPL3 included in the
20  * packaging of this file. Please review the following information to
21  * ensure the GNU General Public License version 3.0 requirements will be
22  * met: http://www.gnu.org/copyleft/gpl.html.
23  * Alternatively you may (at your option) use any later version of the GNU
24  * General Public License if such license has been publicly approved by
25  * MLAB and NICR (or its successors, if any).
26  *
27  * IN NO EVENT SHALL "MLAB" OR "NICR" BE LIABLE TO ANY PARTY FOR DIRECT,
28  * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF
29  * THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF "MLAB" OR
30  * "NICR" HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * "MLAB" AND "NICR" SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING,
33  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
34  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
35  * ON AN "AS IS" BASIS, AND "MLAB" AND "NICR" HAVE NO OBLIGATION TO
36  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS OR MODIFICATIONS.
37  */
38 
47 #ifndef _MIRA_XML_MACROS_USER_DEFINED_MACRO_H_
48 #define _MIRA_XML_MACROS_USER_DEFINED_MACRO_H_
49 
50 #include <xml/macros/IMacro.h>
51 #include <xml/macros/Scope.h>
52 #include <xml/macros/Utils.h>
53 
54 namespace mira::xmlmacros {
55 
59 struct Parameter
60 {
61  std::string name;
62  std::optional<CopyableXMLDom> defaultValue;
63 
64  void reflect(XMLDeserializer& r);
65 }; // struct Parameter
66 
70 struct Parameters
71 {
72  std::vector<Parameter> parameters;
73 
74  void reflect(XMLDeserializer& r);
75 }; // struct Parameters
76 
80 struct Definition
81 {
82  std::string name;
83 
85 
87 
89 
90  void reflect(XMLDeserializer& r);
91 
92 }; // struct Definition
93 
102 {
103  const Definition* const definition;
104 
105  struct Argument
106  {
108  bool isDefault = false;
110 
112  {
113  source.file = r.getNode().uri();
114  source.line = r.getNode().line();
115  r.delegate(value);
116  }
117  };
118 
119  std::vector<Argument> arguments;
120 
122 
123  void reflect(XMLDeserializer& r);
124 }; // struct Invocation
125 
129 class UserDefinedMacro : public IMacro
130 {
131 public:
132  UserDefinedMacro(Definition definition, std::shared_ptr<Scope> scope)
133  : mDefinition(std::move(definition)), mScope(std::move(scope))
134  {}
135 
136  XMLNode expand(XMLMacroProcessor& processor, XMLNode node) final;
137 
138  [[nodiscard]] const Definition& getDefinition() const
139  {
140  return mDefinition;
141  }
142 
143  [[nodiscard]] ScopePtr getScope()
144  {
145  return mScope;
146  }
147 
148  [[nodiscard]] std::optional<SourceInfo> getSource() const override
149  {
150  return mDefinition.source;
151  }
152 
153  [[nodiscard]] bool canCoerceToString() const final
154  {
155  for (const auto& param : mDefinition.parameters.parameters) {
156  if (!param.defaultValue.has_value()) {
157  return false;
158  }
159  }
160 
161  return true;
162  }
163 
164  void coerceToString(XMLMacroProcessor& processor, std::ostream& ss) final;
165 
166 protected:
168 
169  // Scope the macro was defined in.
171 
172 }; // class UserDefinedMacro
173 
177 class XMLValueMacro : public IMacro
178 {
179 public:
181  : mScope(std::move(scope)), mValue(std::move(value)), mSource(std::move(source))
182  {}
183 
184  XMLNode expand(XMLMacroProcessor& processor, XMLNode node) final;
185 
186  [[nodiscard]] const XMLDom& getValue() const
187  {
188  return mValue;
189  }
190 
191  [[nodiscard]] bool canCoerceToString() const final
192  {
193  return true;
194  }
195 
196  void coerceToString(XMLMacroProcessor&, std::ostream& ss) final;
197 
198  [[nodiscard]] CopyableXMLDom evaluate(XMLMacroProcessor&) const;
199 
200 protected:
204 }; // class XMLValueMacro
205 
206 } // namespace mira::xmlmacros
207 
208 #endif // _MIRA_XML_MACROS_USER_DEFINED_MACRO_H_
ScopePtr getScope()
Definition: UserDefinedMacro.h:143
A STL conform wrapper for libxml2 to read XML files as DOM.
Definition: XMLDom.h:74
Definition: UserDefinedMacro.h:105
bool canCoerceToString() const final
Definition: UserDefinedMacro.h:153
std::vector< Argument > arguments
Definition: UserDefinedMacro.h:119
std::string uri() const
Return the uri of the node.
void reflect(XMLDeserializer &r)
Definition: UserDefinedMacro.h:111
Definition: Utils.h:56
void reflect(XMLDeserializer &r)
const Definition *const definition
Definition: UserDefinedMacro.h:103
SourceInfo mSource
Definition: UserDefinedMacro.h:203
<Parameters> of a macro definition.
Definition: UserDefinedMacro.h:70
void reflect(XMLDeserializer &r)
void coerceToString(XMLMacroProcessor &processor, std::ostream &ss) final
const XMLDom & getValue() const
Definition: UserDefinedMacro.h:186
Definition: Types.h:67
STL namespace.
XMLNode expand(XMLMacroProcessor &processor, XMLNode node) final
Expands the macro.
std::optional< CopyableXMLDom > defaultValue
Definition: UserDefinedMacro.h:62
const Definition & getDefinition() const
Definition: UserDefinedMacro.h:138
Replaces the input node with a different node.
Definition: UserDefinedMacro.h:177
Parameters parameters
Definition: UserDefinedMacro.h:84
Invocation(const Definition &definition)
Definition: UserDefinedMacro.h:121
UserDefinedMacro(Definition definition, std::shared_ptr< Scope > scope)
Definition: UserDefinedMacro.h:132
CopyableXMLDom value
Definition: UserDefinedMacro.h:107
std::string name
Definition: UserDefinedMacro.h:82
bool canCoerceToString() const final
Definition: UserDefinedMacro.h:191
Invocation of a user defined macro:
Definition: UserDefinedMacro.h:101
ScopePtr mScope
Definition: UserDefinedMacro.h:170
Data structure for storing macro definitions.
void delegate(T &member, ReflectCtrlFlags flags=REFLECT_CTRLFLAG_NONE)
Delegates the serialization directly to the specified member, without creating a separate compound fo...
Definition: RecursiveMemberReflector.h:1099
ScopePtr mScope
Definition: UserDefinedMacro.h:201
std::vector< Parameter > parameters
Definition: UserDefinedMacro.h:72
uint32 line() const
Gets the underlying line number of the loaded document for this node.
Iterator for iterating over xml nodes that have the same parent (sibligs)
Definition: XMLDom.h:850
std::string name
Definition: UserDefinedMacro.h:61
A single parameter inside <Parameters> of a macro definition.
Definition: UserDefinedMacro.h:59
std::optional< SourceInfo > getSource() const override
Definition: UserDefinedMacro.h:148
void reflect(XMLDeserializer &r)
Dynamically added to the processor by the DefineMacro macro.
Definition: UserDefinedMacro.h:129
CopyableXMLDom mValue
Definition: UserDefinedMacro.h:202
XMLNode expand(XMLMacroProcessor &processor, XMLNode node) final
Expands the macro.
void coerceToString(XMLMacroProcessor &, std::ostream &ss) final
CopyableXMLDom evaluate(XMLMacroProcessor &) const
Interface for implementing macro definitions.
Definition: IMacro.h:69
Macro definition.
Definition: UserDefinedMacro.h:80
Definition: XMLMacroProcessor.h:62
Scope::Ptr ScopePtr
Definition: Scope.h:155
Interface for implementing XML macros.
Definition mDefinition
Definition: UserDefinedMacro.h:167
std::string file
Definition: Types.h:69
SourceInfo source
Definition: UserDefinedMacro.h:109
void reflect(XMLDeserializer &r)
Definition: Builtins.h:56
SourceInfo source
Definition: UserDefinedMacro.h:88
CopyableXMLDom body
Definition: UserDefinedMacro.h:86
XMLValueMacro(ScopePtr scope, CopyableXMLDom value, SourceInfo source=SourceInfo())
Definition: UserDefinedMacro.h:180
bool isDefault
Definition: UserDefinedMacro.h:108
uint32 line
Definition: Types.h:70
XMLDom::const_iterator getNode()
Definition: XMLSerializer.h:602
Deserializer for serializing objects from XML format.
Definition: XMLSerializer.h:314