preCICE v3.1.2
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/assertion.hpp"
14#include "xml/ValueParser.hpp"
15
16namespace precice {
17namespace 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
31
33
35
38
40 {
41 return _doc;
42 }
43
45
46 template <class T>
48 {
49 static_assert(std::is_convertible<T, ATTRIBUTE_T>::value, "Type of initializer_list must be converible to ATTRIBUTE_T!");
50 return setOptions(std::vector<ATTRIBUTE_T>(options.begin(), options.end()));
51 }
52
54 {
55 return _options;
56 };
57
59
61 {
62 return _defaultValue;
63 };
64
65 bool hasDefaultValue() const
66 {
67 return _hasDefaultValue;
68 };
69
70 bool hasValidation() const
71 {
72 return _hasValidation;
73 };
74
76
77 const std::string &getName() const
78 {
79 return _name;
80 };
81
82 const ATTRIBUTE_T &getValue() const
83 {
84 return _value;
85 };
86
87 void setRead(bool read)
88 {
89 _read = read;
90 };
91
92 bool isRead() const
93 {
94 return _read;
95 };
96
97private:
98 logging::Logger _log{"xml::XMLAttribute"};
99
101
103
104 bool _read = false;
105
107
108 bool _hasDefaultValue = false;
109
111
112 bool _hasValidation = false;
113
115
117 template <typename VALUE_T>
118 typename std::enable_if<
121
123 template <typename VALUE_T>
124 typename std::enable_if<
127};
128
129template <typename ATTRIBUTE_T>
135
136template <typename ATTRIBUTE_T>
138{
139 const auto iter = std::unique(options.begin(), options.end());
141 _hasValidation = true;
142 return *this;
143}
144
145template <typename ATTRIBUTE_T>
153
154template <typename ATTRIBUTE_T>
156{
158 PRECICE_ASSERT(!_read, "Attribute \"" + _name + "\" has already been read.");
159
160 const auto position = aAttributes.find(getName());
161 if (position == aAttributes.end()) {
162 PRECICE_CHECK(_hasDefaultValue, "Attribute \"{}\" is required, but was not defined.", _name);
164 } else {
165 try {
166 readValueSpecific(position->second, _value);
167 } catch (const std::exception &e) {
168 PRECICE_ERROR(e.what());
169 }
170 if (_hasValidation) {
173 stream << "Invalid value \"" << _value << "\" of attribute \""
174 << getName() << "\": ";
175 // print first
176 auto first = _options.begin();
177 stream << "value must be \"" << *first << '"';
178 ++first;
179 // print the remaining with separator
180 for (; first != _options.end(); ++first) {
181 stream << " or value must be \"" << *first << '"';
182 }
183
184 PRECICE_ERROR(stream.str());
185 }
186 }
187 }
188 PRECICE_DEBUG("Read valid attribute \"{}\" value = {}", getName(), _value);
189}
190
191template <typename ATTRIBUTE_T>
192template <typename VALUE_T>
193typename std::enable_if<
201
202template <typename ATTRIBUTE_T>
203template <typename VALUE_T>
204typename std::enable_if<
208 const VALUE_T &setter)
209{
210 toSet = setter;
211}
212
223
230template <typename T>
235
236} // namespace xml
237} // namespace precice
#define PRECICE_ERROR(...)
Definition LogMacros.hpp:15
#define PRECICE_DEBUG(...)
Definition LogMacros.hpp:64
#define PRECICE_TRACE(...)
Definition LogMacros.hpp:95
#define PRECICE_CHECK(check,...)
Definition LogMacros.hpp:35
std::string name
#define PRECICE_ASSERT(...)
Definition assertion.hpp:87
T begin(T... args)
This class provides a lightweight logger.
Definition Logger.hpp:16
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 documentation)
Sets a documentation string for the attribute.
XMLAttribute & operator=(const XMLAttribute< ATTRIBUTE_T > &other)=default
T end(T... args)
T find(T... args)
XMLAttribute< std::string > makeXMLAttribute(std::string name, const char *defaultValue)
void readValueSpecific(const std::string &rawValue, double &value)
Main namespace of the precice library.
STL namespace.
T unique(T... args)