preCICE v3.2.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
XMLTag.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <Eigen/Core>
4#include <map>
5#include <memory>
6#include <optional>
7#include <string>
8#include <variant>
9#include <vector>
10#include "logging/Logger.hpp"
11#include "xml/ConfigParser.hpp"
12#include "xml/XMLAttribute.hpp"
13
14namespace precice::xml {
15class ConfigParser;
16}
17
18namespace precice::xml {
19
26
28class XMLTag {
30
31public:
33
35
42
44
46 struct Listener {
47
49
50 virtual ~Listener() = default;
57 virtual void xmlTagCallback(ConfigurationContext const &context, XMLTag &callingTag) = 0;
58
66 virtual void xmlEndTagCallback(ConfigurationContext const &context, XMLTag &callingTag) = 0;
67 };
68
77
79
88 XMLTag(
89 Listener &listener,
91 Occurrence occurrence,
92 std::string xmlNamespace = "");
93
100
102 {
103 return _doc;
104 };
105
112 XMLTag &addNamespace(const std::string &namespaceName);
113
115 XMLTag &addSubtag(const XMLTag &tag);
116
117 const Subtags &getSubtags() const
118 {
119 return _subtags;
120 };
121
123 XMLTag &addAttribute(const XMLAttribute<double> &attribute);
124
125 // Adds a XML attribute by making a copy of the given attribute.
126 XMLTag &addAttribute(const XMLAttribute<int> &attribute);
127
130
132 XMLTag &addAttribute(const XMLAttribute<bool> &attribute);
133
136
139
140 bool hasAttribute(const std::string &attributeName) const;
141
142 template <typename Container>
143 void addSubtags(const Container &subtags)
144 {
145 std::for_each(subtags.begin(), subtags.end(), [this](auto &s) { this->addSubtag(s); });
146 }
147
153 const std::string &getName() const
154 {
155 return _name;
156 }
157
160 {
161 return _namespace;
162 }
163
165 {
166 return _namespaces;
167 };
168
171 {
172 return _fullName;
173 }
174
175 double getDoubleAttributeValue(const std::string &name, std::optional<double> default_value = std::nullopt) const;
176
177 int getIntAttributeValue(const std::string &name, std::optional<int> default_value = std::nullopt) const;
178
180
182
183 Eigen::VectorXd getEigenVectorXdAttributeValue(const std::string &name) const;
184
186
188 {
189 return _attributes;
190 };
191
192 bool isConfigured() const
193 {
194 return _configured;
195 }
196
198 {
199 return _occurrence;
200 }
201
203 void readAttributes(const std::map<std::string, std::string> &aAttributes);
204
205private:
206 mutable logging::Logger _log{"xml::XMLTag"};
207
209
212
215
218
220
221 bool _configured = false;
222
224
226
228
230
232
234
235 void areAllSubtagsConfigured() const;
236
237 void resetAttributes();
238};
239
241std::string getName(const XMLTag::Attribute &attribute);
242
243// ------------------------------------------------------ HEADER IMPLEMENTATION
244
247 void xmlTagCallback(ConfigurationContext const &context, XMLTag &callingTag) override {}
248 void xmlEndTagCallback(ConfigurationContext const &context, XMLTag &callingTag) override {}
249};
250
256XMLTag getRootTag();
257
260 XMLTag &tag,
262 std::string_view configurationFilename);
263
264} // namespace precice::xml
std::string name
This class provides a lightweight logger.
Definition Logger.hpp:17
Represents an XML tag to be configured automatically.
Definition XMLTag.hpp:28
Eigen::VectorXd getEigenVectorXdAttributeValue(const std::string &name) const
Definition XMLTag.cpp:173
const std::string & getNamespace() const
Returns xml namespace.
Definition XMLTag.hpp:159
bool isConfigured() const
Definition XMLTag.hpp:192
void addSubtags(const Container &subtags)
Definition XMLTag.hpp:143
std::string getDocumentation() const
Definition XMLTag.hpp:101
std::string _fullName
Combination of name and namespace: _namespace + ":" + _name.
Definition XMLTag.hpp:217
std::map< std::string, bool > _configuredNamespaces
Definition XMLTag.hpp:229
bool hasAttribute(const std::string &attributeName) const
Definition XMLTag.cpp:112
std::string getStringAttributeValue(const std::string &name, std::optional< std::string > default_value=std::nullopt) const
Definition XMLTag.cpp:145
Occurrence _occurrence
Definition XMLTag.hpp:223
bool getBooleanAttributeValue(const std::string &name, std::optional< bool > default_value=std::nullopt) const
Definition XMLTag.cpp:159
Occurrence getOccurrence() const
Definition XMLTag.hpp:197
void readAttributes(const std::map< std::string, std::string > &aAttributes)
reads all attributes of this tag
Definition XMLTag.cpp:184
typename std::vector< std::string > Namespaces
Definition XMLTag.hpp:32
Namespaces _namespaces
Definition XMLTag.hpp:225
std::string _namespace
XML namespace of the tag.
Definition XMLTag.hpp:214
const Namespaces & getNamespaces() const
Definition XMLTag.hpp:164
Listener & _listener
Definition XMLTag.hpp:208
XMLTag & setDocumentation(std::string_view documentation)
Adds a description of the purpose of this XML tag.
Definition XMLTag.cpp:29
static std::string_view getOccurrenceString(Occurrence occurrence)
Definition XMLTag.cpp:302
const std::string & getName() const
Returns name (without namespace).
Definition XMLTag.hpp:153
typename std::vector< Attribute > Attributes
Definition XMLTag.hpp:43
const std::string & getFullName() const
Returns full name consisting of xml namespace + ":" + name.
Definition XMLTag.hpp:170
Occurrence
Types of occurrences of an XML tag.
Definition XMLTag.hpp:70
void addAttributeHint(std::string name, std::string message)
Adds a hint for missing attributes, which will be displayed along the error message.
Definition XMLTag.cpp:98
XMLTag(Listener &listener, std::string name, Occurrence occurrence, std::string xmlNamespace="")
Standard constructor.
Definition XMLTag.cpp:11
std::map< std::string, std::string > _attributeHints
Definition XMLTag.hpp:233
std::string _name
Name of the tag.
Definition XMLTag.hpp:211
typename std::vector< std::shared_ptr< XMLTag > > Subtags
Definition XMLTag.hpp:34
int getIntAttributeValue(const std::string &name, std::optional< int > default_value=std::nullopt) const
Definition XMLTag.cpp:131
const Attributes & getAttributes() const
Definition XMLTag.hpp:187
std::string _doc
Definition XMLTag.hpp:219
XMLTag & addNamespace(const std::string &namespaceName)
Adds a namespace to the tag.
Definition XMLTag.cpp:35
const Subtags & getSubtags() const
Definition XMLTag.hpp:117
std::vector< std::string > getAttributeNames() const
Definition XMLTag.cpp:220
logging::Logger _log
Definition XMLTag.hpp:206
Attributes _attributes
Definition XMLTag.hpp:231
double getDoubleAttributeValue(const std::string &name, std::optional< double > default_value=std::nullopt) const
Definition XMLTag.cpp:117
XMLTag & addAttribute(const XMLAttribute< double > &attribute)
Adds a XML attribute by making a copy of the given attribute.
Definition XMLTag.cpp:53
XMLTag & addSubtag(const XMLTag &tag)
Adds an XML tag as subtag by making a copy of the given tag.
Definition XMLTag.cpp:41
void areAllSubtagsConfigured() const
Definition XMLTag.cpp:229
T for_each(T... args)
T make_unique(T... args)
contains the XML configuration parser.
XMLTag getRootTag()
Returns an empty root tag with name "configuration".
Definition XMLTag.cpp:278
std::string getName(const XMLTag::Attribute &attribute)
Returns the name of an Attribute.
Definition XMLTag.cpp:273
std::string configure(XMLTag &tag, const precice::xml::ConfigurationContext &context, std::string_view configurationFilename)
Configures the given configuration from file configurationFilename.
Definition XMLTag.cpp:284
Tightly coupled to the parameters of Participant()
Definition XMLTag.hpp:21
No operation listener for tests.
Definition XMLTag.hpp:246
void xmlTagCallback(ConfigurationContext const &context, XMLTag &callingTag) override
Callback at begin of XML tag.
Definition XMLTag.hpp:247
void xmlEndTagCallback(ConfigurationContext const &context, XMLTag &callingTag) override
Callback at end of XML tag and at end of subtag.
Definition XMLTag.hpp:248
Callback interface for configuration classes using XMLTag.
Definition XMLTag.hpp:46
virtual void xmlTagCallback(ConfigurationContext const &context, XMLTag &callingTag)=0
Callback at begin of XML tag.
Listener & operator=(Listener &&)=delete
virtual void xmlEndTagCallback(ConfigurationContext const &context, XMLTag &callingTag)=0
Callback at end of XML tag and at end of subtag.