MIRA
Builtins.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_BUILTINS_H_
48 #define _MIRA_XML_MACROS_BUILTINS_H_
49 
50 #include <xml/macros/Utils.h>
51 #include <xml/macros/Types.h>
52 #include <xml/macros/IMacro.h>
53 
54 #include <serialization/adapters/std/vector>
55 
56 namespace mira::xmlmacros {
57 
74 class DefineMacro : public IMacro
75 {
76 public:
77  static constexpr const char* PREFIX = "Define_";
78 
79  XMLNode expand(XMLMacroProcessor& processor, XMLNode node) final;
80 
81  static bool isDefinition(const std::string& name)
82  {
83  return name.rfind(PREFIX, 0) == 0;
84  }
85 
86  static std::string extractName(XMLDom::const_iterator node);
87 }; // class DefineMacro
88 
92 class ImportInlineMacro : public IMacro
93 {
94 public:
95  XMLNode expand(XMLMacroProcessor& processor, XMLNode node) final;
96 
97 protected:
98  uint mNameCount;
99 
100  std::string createAnonymousName(const std::string originalName)
101  {
102  return "ImportedMacro_" + originalName + "_" + std::to_string(mNameCount++);
103  }
104 }; // class ImportMacro
105 
106 class ForMacro : public IMacro
107 {
108 public:
109  static constexpr const char* NAME = "For";
110 
111  XMLNode expand(XMLMacroProcessor& processor, XMLNode node) final;
112 
113 }; // class ForMacro
114 
118 class SplitMacro : public IMacro
119 {
120 public:
121 
122  static constexpr const char* NAME = "Split";
123 
124  XMLNode expand(XMLMacroProcessor& processor, XMLNode node) final;
125 
126 }; // class SplitMacro
127 
158 class ZipMacro : public IMacro
159 {
160 public:
161  struct ZipBody {
162 
163  struct ZipElement {
164  std::string name;
165  std::vector<CopyableXMLDom> items;
166 
168  name = *r.getNode();
169  r.delegate(items);
170  }
171  };
172 
173  std::vector<ZipElement> elements;
174 
176 
177  elements.clear();
178 
179  auto node = r.getNode();
180 
181  elements.reserve(std::distance(node.begin(), node.end()));
182 
183  for (auto it = node.begin(); it != node.end(); ++it) {
184  r.property((*it).c_str(), elements.emplace_back(), "");
185  }
186  }
187  }; // struct ZipBody
188 
189  static constexpr const char* NAME = "Zip";
190 
191  XMLNode expand(XMLMacroProcessor& processor, XMLNode node) final;
192 }; // class ZipMacro
193 
194 class NothingMacro : public IMacro
195 {
196 public:
197  static constexpr const char* NAME = "Nothing";
198 
199  XMLNode expand(XMLMacroProcessor& processor, XMLNode node) final
200  {
201  return node.remove();
202  }
203 
204  [[nodiscard]] bool canCoerceToString() const final
205  {
206  return true;
207  }
208 
209  void coerceToString(XMLMacroProcessor& processor, std::ostream& ss) final
210  {
211  ss << "";
212  }
213 }; // class NothingMacro
214 
218 class PrintXMLMacro : public IMacro
219 {
220 public:
221  static constexpr const char* NAME = "PrintXML";
222 
223  XMLNode expand(XMLMacroProcessor& processor, XMLNode node) final;
224 }; // class PrintXMLMacro
225 
226 }; // namespace mira::xmlmacros
227 
228 #endif // _MIRA_XML_MACROS_BUILTINS_H_
Splits content into words and inserts them as word nodes.
Definition: Builtins.h:118
Definition: Builtins.h:106
static constexpr const char * NAME
Definition: Builtins.h:122
XMLNode expand(XMLMacroProcessor &processor, XMLNode node) final
Expands the macro.
XMLNode expand(XMLMacroProcessor &processor, XMLNode node) final
Expands the macro.
Definition: Builtins.h:194
XMLDom::sibling_iterator remove()
Removes this node from the document.
void reflect(XMLDeserializer &r)
Definition: Builtins.h:175
void property(const char *name, T &member, const char *comment, PropertyHint &&hint=PropertyHint(), ReflectCtrlFlags flags=REFLECT_CTRLFLAG_NONE)
Definition: RecursiveMemberReflector.h:985
Const sibling_iterator for iterating over xml nodes that have the same parent (siblings) ...
Definition: XMLDom.h:763
std::vector< ZipElement > elements
Definition: Builtins.h:173
Evaluates children and prints the result to stdout for debugging.
Definition: Builtins.h:218
XMLNode expand(XMLMacroProcessor &processor, XMLNode node) final
Expands the macro.
Zips lists.
Definition: Builtins.h:158
static constexpr const char * PREFIX
Definition: Builtins.h:77
XMLNode expand(XMLMacroProcessor &processor, XMLNode node) final
Expands the macro.
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
Iterator for iterating over xml nodes that have the same parent (sibligs)
Definition: XMLDom.h:850
static constexpr const char * NAME
Definition: Builtins.h:197
uint mNameCount
Definition: Builtins.h:98
XMLNode expand(XMLMacroProcessor &processor, XMLNode node) final
Expands the macro.
Definition: Builtins.h:199
Interface for implementing macro definitions.
Definition: IMacro.h:69
bool canCoerceToString() const final
Definition: Builtins.h:204
void coerceToString(XMLMacroProcessor &processor, std::ostream &ss) final
Definition: Builtins.h:209
Definition: XMLMacroProcessor.h:62
static constexpr const char * NAME
Definition: Builtins.h:109
Definition: Builtins.h:161
void reflect(XMLDeserializer &r)
Definition: Builtins.h:167
Interface for implementing XML macros.
std::vector< CopyableXMLDom > items
Definition: Builtins.h:165
Definition: Builtins.h:56
Usage: <macro:MyImportedMacro From="file/that/defines/MyImportedMacro.xml"> ...
Definition: Builtins.h:92
XMLNode expand(XMLMacroProcessor &processor, XMLNode node) final
Expands the macro.
static constexpr const char * NAME
Definition: Builtins.h:189
std::string name
Definition: Builtins.h:164
static std::string extractName(XMLDom::const_iterator node)
static bool isDefinition(const std::string &name)
Definition: Builtins.h:81
static constexpr const char * NAME
Definition: Builtins.h:221
std::string createAnonymousName(const std::string originalName)
Definition: Builtins.h:100
XMLNode expand(XMLMacroProcessor &processor, XMLNode node) final
Expands the macro.
The "Define" macro.
Definition: Builtins.h:74
XMLDom::const_iterator getNode()
Definition: XMLSerializer.h:602
Deserializer for serializing objects from XML format.
Definition: XMLSerializer.h:314