preCICE v3.2.0
Loading...
Searching...
No Matches
XMLAttribute.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <algorithm>
4#include <exception>
5#include <initializer_list>
6#include <map>
7#include <sstream>
8#include <string>
9#include <type_traits>
10#include <utility>
11#include <vector>
12#include "logging/Logger.hpp"
13#include "utils/String.hpp"
14#include "utils/assertion.hpp"
15#include "xml/ValueParser.hpp"
16
17namespace precice::xml {
18
19template <typename ATTRIBUTE_T>
21 static_assert(std::is_default_constructible<ATTRIBUTE_T>::value, "The value type of XMLAttributes need to be default-constructible.");
22
23public:
24 XMLAttribute() = delete;
25
27 : _name(std::move(name))
28 {
30 };
31
32 XMLAttribute(std::string name, ATTRIBUTE_T defaultValue)
33 : _name(std::move(name)), _hasDefaultValue(true), _defaultValue(std::move(defaultValue))
34 {
36 };
37
38 XMLAttribute(const XMLAttribute<ATTRIBUTE_T> &other) = default;
39
41
44
46 {
47 return _doc;
48 }
49
51
52 template <class T>
54 {
55 static_assert(std::is_convertible<T, ATTRIBUTE_T>::value, "Type of initializer_list must be converible to ATTRIBUTE_T!");
56 return setOptions(std::vector<ATTRIBUTE_T>(options.begin(), options.end()));
57 }
58
60 {
61 return _options;
62 };
63
64 XMLAttribute &setDefaultValue(const ATTRIBUTE_T &defaultValue);
65
66 const ATTRIBUTE_T &getDefaultValue() const
67 {
68 return _defaultValue;
69 };
70
71 bool hasDefaultValue() const
72 {
73 return _hasDefaultValue;
74 };
75
76 bool hasValidation() const
77 {
78 return _hasValidation;
79 };
80
81 void readValue(const std::map<std::string, std::string> &aAttributes);
82
83 const std::string &getName() const
84 {
85 return _name;
86 };
87
88 const ATTRIBUTE_T &getValue() const
89 {
90 return _value;
91 };
92
93 void setRead(bool read)
94 {
95 _read = read;
96 };
97
98 bool isRead() const
99 {
100 return _read;
101 };
102
103private:
104 logging::Logger _log{"xml::XMLAttribute"};
105
107
109
110 bool _read = false;
111
112 ATTRIBUTE_T _value{};
113
114 bool _hasDefaultValue = false;
115
116 ATTRIBUTE_T _defaultValue{};
117
118 bool _hasValidation = false;
119
121
123 template <typename VALUE_T>
124 typename std::enable_if<
126 set(ATTRIBUTE_T &toSet, const VALUE_T &setter);
127
129 template <typename VALUE_T>
130 typename std::enable_if<
132 set(ATTRIBUTE_T &toSet, const VALUE_T &setter);
133};
134
135template <typename ATTRIBUTE_T>
137{
138 _doc = documentation;
139 return *this;
140}
141
142template <typename ATTRIBUTE_T>
144{
145 const auto iter = std::unique(options.begin(), options.end());
146 _options = std::vector<ATTRIBUTE_T>(options.begin(), iter);
147 _hasValidation = true;
148 return *this;
149}
150
151template <typename ATTRIBUTE_T>
153{
154 PRECICE_TRACE(defaultValue);
155 _hasDefaultValue = true;
156 set(_defaultValue, defaultValue);
157 return *this;
158}
159
160template <typename ATTRIBUTE_T>
162{
164 PRECICE_ASSERT(!_read, "Attribute \"" + _name + "\" has already been read.");
165
166 const auto position = aAttributes.find(getName());
167 if (position == aAttributes.end()) {
168 PRECICE_CHECK(_hasDefaultValue, "Attribute \"{}\" is required, but was not defined.", _name);
170 } else {
171 try {
172 readValueSpecific(position->second, _value);
173 } catch (const std::exception &e) {
174 PRECICE_ERROR(e.what());
175 }
176 if (_hasValidation) {
177 if (std::find(_options.begin(), _options.end(), _value) == _options.end()) {
178 std::ostringstream stream;
179 stream << "Invalid value \"" << _value << "\" of attribute \""
180 << getName() << "\": ";
181 // print first
182 auto first = _options.begin();
183 stream << "value must be \"" << *first << '"';
184 ++first;
185 // print the remaining with separator
186 for (; first != _options.end(); ++first) {
187 stream << " or value must be \"" << *first << '"';
188 }
189
190 PRECICE_ERROR(stream.str());
191 }
192 }
193 }
194 PRECICE_DEBUG("Read valid attribute \"{}\" value = {}", getName(), _value);
195}
196
197template <typename ATTRIBUTE_T>
198template <typename VALUE_T>
199typename std::enable_if<
202 ATTRIBUTE_T &toSet,
203 const VALUE_T &setter)
204{
205 toSet = setter;
206}
207
208template <typename ATTRIBUTE_T>
209template <typename VALUE_T>
210typename std::enable_if<
213 ATTRIBUTE_T &toSet,
214 const VALUE_T &setter)
215{
216 toSet = setter;
217}
218
225inline XMLAttribute<std::string> makeXMLAttribute(std::string name, const char *defaultValue)
226{
227 return XMLAttribute<std::string>(std::move(name), defaultValue);
228}
229
236template <typename T>
238{
239 return XMLAttribute<T>(std::move(name), std::move(defaultValue));
240}
241
242} // namespace precice::xml
#define PRECICE_ERROR(...)
Definition LogMacros.hpp:16
#define PRECICE_DEBUG(...)
Definition LogMacros.hpp:61
#define PRECICE_TRACE(...)
Definition LogMacros.hpp:92
#define PRECICE_CHECK(check,...)
Definition LogMacros.hpp:32
#define PRECICE_ASSERT(...)
Definition assertion.hpp:85
T begin(T... args)
This class provides a lightweight logger.
Definition Logger.hpp:17
std::enable_if< std::is_same< VALUE_T, ATTRIBUTE_T >::value &&notstd::is_same< VALUE_T, Eigen::VectorXd >::value, void >::type set(ATTRIBUTE_T &toSet, const VALUE_T &setter)
Sets non Eigen::VectorXd type values.
XMLAttribute & setOptions(std::initializer_list< T > &&options)
const ATTRIBUTE_T & getDefaultValue() const
const std::string & getUserDocumentation() const
void readValue(const std::map< std::string, std::string > &aAttributes)
const ATTRIBUTE_T & getValue() const
XMLAttribute(std::string name)
XMLAttribute & setOptions(std::vector< ATTRIBUTE_T > options)
const std::vector< ATTRIBUTE_T > & getOptions() const
XMLAttribute(const XMLAttribute< ATTRIBUTE_T > &other)=default
XMLAttribute & setDefaultValue(const ATTRIBUTE_T &defaultValue)
const std::string & getName() const
std::vector< ATTRIBUTE_T > _options
XMLAttribute(std::string name, ATTRIBUTE_T defaultValue)
XMLAttribute & setDocumentation(std::string_view documentation)
Sets a documentation string for the attribute.
XMLAttribute & operator=(const XMLAttribute< ATTRIBUTE_T > &other)=default
T end(T... args)
T find(T... args)
bool isKebabStyle(std::string_view sv)
Definition String.cpp:103
contains the XML configuration parser.
XMLAttribute< std::string > makeXMLAttribute(std::string name, const char *defaultValue)
std::string getName(const XMLTag::Attribute &attribute)
Returns the name of an Attribute.
Definition XMLTag.cpp:273
void readValueSpecific(const std::string &rawValue, double &value)
STL namespace.
T str(T... args)
T unique(T... args)
T what(T... args)