OpenLB 1.8.1
Loading...
Searching...
No Matches
olb::XMLreader Class Reference

#include <xmlReader.h>

+ Collaboration diagram for olb::XMLreader:

Public Member Functions

 XMLreader (tinyxml2::XMLNode *pParent, OutputChannel outputChannel=OutputChannel::ERRCHANNEL)
 Constructs a new XMLreader from another XMLreader.
 
 XMLreader (const std::string &fName, OutputChannel outputChannel=OutputChannel::ERRCHANNEL)
 Constructs a new XMLreader from a XML file fName.
 
 ~XMLreader ()
 
template<typename T >
bool read (T &value, bool verboseOn=true, bool exitIfMissing=false) const
 Read a value from the xml file.
 
template<typename T , unsigned DIM>
bool read (util::ADf< T, DIM > &value, bool verboseOn=true, bool exitIfMissing=false) const
 
template<typename T >
bool read (std::vector< T > &value, bool verboseOn=true, bool exitIfMissing=false) const
 
template<typename T >
get (bool verboseOn=true, bool exitIfMissing=false) const
 
template<typename ParameterType >
bool readOrWarn (std::string name_parameter_1, std::string name_parameter_2, std::string name_parameter_3, ParameterType &var, bool defaultAvailable=true, bool exitIfMissing=false, bool showWarning=true) const
 This wrapper function reads the given parameter from the "type_parameter" and "name_parameter_1" or "name_parameter_2" tag and prints a warning, if the parameter can not be read.
 
template<typename ParameterType >
bool readOrWarn (std::string name_parameter_1, std::string name_parameter_2, std::string name_parameter_3, std::string name_parameter_4, ParameterType &var, bool defaultAvailable=true, bool exitIfMissing=false, bool showWarning=true) const
 
XMLreader const & operator[] (std::string name) const
 
std::vector< XMLreader * >::const_iterator begin () const
 Returns an iterator.begin() of the child XMLreader This means an iterator to the next level on an XML tree.
 
std::vector< XMLreader * >::const_iterator end () const
 Returns an iterator.end() of the child XMLreader This means an iterator to the next level on an XML tree.
 
void setWarningsOn (bool warnings) const
 switch warnings on/off
 
std::string getName () const
 return the name of the element
 
std::string getText () const
 return the text of the element
 
std::string getAttribute (const std::string &aName) const
 
template<>
bool read (bool &value, bool verboseOn, bool exitIfMissing) const
 
template<>
bool read (int &value, bool verboseOn, bool exitIfMissing) const
 
template<>
bool read (double &value, bool verboseOn, bool exitIfMissing) const
 
template<>
bool read (long double &value, bool verboseOn, bool exitIfMissing) const
 
template<>
bool read (float &value, bool verboseOn, bool exitIfMissing) const
 

Public Attributes

XMLreaderOutput _output
 handling all the output for the XMLreader
 

Protected Attributes

std::map< std::string, std::string > _attributes
 
std::vector< XMLreader * > _children
 

Friends

class XMLreaderOutput
 

Detailed Description

Definition at line 53 of file xmlReader.h.

Constructor & Destructor Documentation

◆ XMLreader() [1/2]

olb::XMLreader::XMLreader ( tinyxml2::XMLNode * pParent,
OutputChannel outputChannel = OutputChannel::ERRCHANNEL )

Constructs a new XMLreader from another XMLreader.

Parameters
pParentThe new root node for the XMLreader

Definition at line 249 of file xmlReader.h.

249 : _output(outputChannel)
250{
251 _outputChannel = outputChannel;
252 _warningsOn = true;
253
254 mainProcessorIni(pParent);
255}
XMLreaderOutput _output
handling all the output for the XMLreader
Definition xmlReader.h:109

◆ XMLreader() [2/2]

olb::XMLreader::XMLreader ( const std::string & fName,
OutputChannel outputChannel = OutputChannel::ERRCHANNEL )

Constructs a new XMLreader from a XML file fName.

Definition at line 257 of file xmlReader.h.

258 : _output(outputChannel), _outputChannel(outputChannel), _warningsOn(true)
259{
260 tinyxml2::XMLDocument doc(true, tinyxml2::COLLAPSE_WHITESPACE); // Create an instance of TinyXML2's XMLDocument
261 bool loadOK = (doc.LoadFile(fName.c_str()) == tinyxml2::XML_SUCCESS); // Load the XML file and check for success
262 _output.loadFile(loadOK, fName); // Call the output's loadFile method
263 mainProcessorIni(&doc);
264}
void loadFile(bool loadOK, std::string fName) const

References _output, and olb::XMLreaderOutput::loadFile().

+ Here is the call graph for this function:

◆ ~XMLreader()

olb::XMLreader::~XMLreader ( )

Definition at line 266 of file xmlReader.h.

267{
268 for (unsigned int iNode=0; iNode<_children.size(); ++iNode) {
269 delete _children[iNode];
270 }
271}
std::vector< XMLreader * > _children
Definition xmlReader.h:124

References _children.

Member Function Documentation

◆ begin()

std::vector< XMLreader * >::const_iterator olb::XMLreader::begin ( ) const

Returns an iterator.begin() of the child XMLreader This means an iterator to the next level on an XML tree.

Definition at line 322 of file xmlReader.h.

323{
324 return _children.begin();
325}

References _children, and begin().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ end()

std::vector< XMLreader * >::const_iterator olb::XMLreader::end ( ) const

Returns an iterator.end() of the child XMLreader This means an iterator to the next level on an XML tree.

Definition at line 327 of file xmlReader.h.

328{
329 return _children.end();
330}

References _children.

+ Here is the caller graph for this function:

◆ get()

template<typename T >
T olb::XMLreader::get ( bool verboseOn = true,
bool exitIfMissing = false ) const

Definition at line 169 of file xmlReader.h.

170{
171 std::stringstream valueStr(_text);
172 T tmp = T();
173 if (!(valueStr >> tmp)) {
174// if ( verboseOn ) {
175// clout << "Error: cannot read value from XML element " << _name << std::endl;
176// }
177 _output.printWarning(_name, typeid(T).name(), "", verboseOn, exitIfMissing);
178 }
179 return tmp;
180}
void printWarning(std::string name, std::string typeName, std::string value, bool verboseOn, bool exitIfMissing) const
print warning if verbose mode is on and exit, if exItMissing is true
std::string name()
Returns name of FIELD for human consumption.
Definition fields.h:49

References _output, and olb::XMLreaderOutput::printWarning().

+ Here is the call graph for this function:

◆ getAttribute()

std::string olb::XMLreader::getAttribute ( const std::string & aName) const
Returns
the value of attribute

Definition at line 471 of file xmlReader.h.

472{
473 std::map<std::string, std::string>::const_iterator it = _attributes.find(aName);
474 if ( it == _attributes.end()) {
475 return "Attribute not found.";
476 }
477 return it->second;
478}
std::map< std::string, std::string > _attributes
Definition xmlReader.h:123

References _attributes.

+ Here is the caller graph for this function:

◆ getName()

std::string olb::XMLreader::getName ( ) const

return the name of the element

Definition at line 332 of file xmlReader.h.

333{
334 return _name;
335}
+ Here is the caller graph for this function:

◆ getText()

std::string olb::XMLreader::getText ( ) const

return the text of the element

Definition at line 337 of file xmlReader.h.

338{
339 return _text;
340}

◆ operator[]()

XMLreader const & olb::XMLreader::operator[] ( std::string name) const
Returns
a Subtree placed at name
Parameters
nameThe name from which to take the subtree

Definition at line 311 of file xmlReader.h.

312{
313 for (unsigned int iNode=0; iNode<_children.size(); ++iNode) {
314 if (_children[iNode]->_name == fName) {
315 return *_children[iNode];
316 }
317 }
318 _output.readValue(_warningsOn, _name, fName);
319 return _notFound;
320}
void readValue(bool warningsOn, std::string name, std::string fName) const

References _children, _output, and olb::XMLreaderOutput::readValue().

+ Here is the call graph for this function:

◆ read() [1/8]

template<>
bool olb::XMLreader::read ( bool & value,
bool verboseOn,
bool exitIfMissing ) const

Definition at line 352 of file xmlReader.h.

353{
354 std::stringstream valueStr(_text);
355 std::string word;
356 valueStr >> word;
357 // Transform to lower-case, so that "true" and "false" are case-insensitive.
358 std::transform(word.begin(), word.end(), word.begin(), ::tolower);
359 if (!word.compare("true") || (word=="1")) {
360 value = true;
361 return true;
362 }
363 else if (!word.compare("false") || (word=="0")) {
364 value=false;
365 return true;
366 }
367 else {
368 if ( verboseOn ) {
369 std::stringstream ss;
370 ss << ( value ? "true" : "false" );
371 _output.printWarning(_name, "bool", ss.str(), verboseOn, exitIfMissing);
372 }
373 }
374 return false;
375}
typename std::integral_constant< TYPE, VALUE >::type value
Identity type to wrap non-type template arguments.
Definition meta.h:96

References _output, and olb::XMLreaderOutput::printWarning().

+ Here is the call graph for this function:

◆ read() [2/8]

template<>
bool olb::XMLreader::read ( double & value,
bool verboseOn,
bool exitIfMissing ) const

Definition at line 411 of file xmlReader.h.

412{
413 std::stringstream valueStr(_text);
414 double tmp = double();
415 if (!(valueStr >> tmp)) {
416 _output.printWarning(_name, "double", std::to_string(value), verboseOn, exitIfMissing);
417 return false;
418 }
419 value = tmp;
420 return true;
421}

References _output, and olb::XMLreaderOutput::printWarning().

+ Here is the call graph for this function:

◆ read() [3/8]

template<>
bool olb::XMLreader::read ( float & value,
bool verboseOn,
bool exitIfMissing ) const

Definition at line 438 of file xmlReader.h.

439{
440 std::stringstream valueStr(_text);
441 float tmp = float();
442 if (!(valueStr >> tmp)) {
443 std::stringstream ss;
444 ss << value;
445 _output.printWarning(_name, "float", ss.str(), verboseOn, exitIfMissing);
446 return false;
447 }
448 value = tmp;
449 return true;
450}

References _output, and olb::XMLreaderOutput::printWarning().

+ Here is the call graph for this function:

◆ read() [4/8]

template<>
bool olb::XMLreader::read ( int & value,
bool verboseOn,
bool exitIfMissing ) const

Definition at line 379 of file xmlReader.h.

380{
381 std::stringstream valueStr(_text);
382 int tmp = int();
383 if (!(valueStr >> tmp)) {
384 std::stringstream ss;
385 ss << value;
386 _output.printWarning(_name, "int", ss.str(), verboseOn, exitIfMissing);
387 return false;
388 }
389 value = tmp;
390 return true;
391}

References _output, and olb::XMLreaderOutput::printWarning().

+ Here is the call graph for this function:

◆ read() [5/8]

template<>
bool olb::XMLreader::read ( long double & value,
bool verboseOn,
bool exitIfMissing ) const

Definition at line 425 of file xmlReader.h.

426{
427 std::stringstream valueStr(_text);
428 std::string tmp {};
429 if (!(valueStr >> tmp)) {
430 _output.printWarning(_name, "long double", std::to_string(value), verboseOn, exitIfMissing);
431 return false;
432 }
433 value = std::stold(tmp);
434 return true;
435}

References _output, and olb::XMLreaderOutput::printWarning().

+ Here is the call graph for this function:

◆ read() [6/8]

template<typename T >
bool olb::XMLreader::read ( std::vector< T > & value,
bool verboseOn = true,
bool exitIfMissing = false ) const

Definition at line 147 of file xmlReader.h.

148{
149 std::stringstream multiValueStr(_text);
150 std::string word;
151 std::vector<T> tmp(values);
152 while (multiValueStr>>word) {
153 std::stringstream valueStr(word);
154 T value;
155 if (!(valueStr >> value)) {
156// if ( verboseOn ) {
157// clout << std::string("Error: cannot read value array from XML element ") << _name << std::endl;
158// }
159 _output.printWarning(_name, "std::vector", "", verboseOn, exitIfMissing);
160 return false;
161 }
162 tmp.push_back(value);
163 }
164 values.swap(tmp);
165 return true;
166}

References _output, and olb::XMLreaderOutput::printWarning().

+ Here is the call graph for this function:

◆ read() [7/8]

template<typename T >
bool olb::XMLreader::read ( T & value,
bool verboseOn = true,
bool exitIfMissing = false ) const

Read a value from the xml file.

Parameters
referenceto return the value
Returns
returns the value
+ Here is the caller graph for this function:

◆ read() [8/8]

template<typename T , unsigned DIM>
bool olb::XMLreader::read ( util::ADf< T, DIM > & value,
bool verboseOn = true,
bool exitIfMissing = false ) const

Definition at line 131 of file xmlReader.h.

132{
133 std::stringstream valueStr(_text);
134 T tmp = T();
135 if (!(valueStr >> tmp)) {
136 //if ( _verboseOn ) {
137 //clout << std::string("Error: cannot read value from XML element ") << _name << std::endl;
138 //}
139 _output.printWarning(_name, "ADf vector", "", verboseOn, exitIfMissing);
140 return false;
141 }
142 value = util::ADf<T,DIM>(tmp);
143 return true;
144}

References _output, and olb::XMLreaderOutput::printWarning().

+ Here is the call graph for this function:

◆ readOrWarn() [1/2]

template<typename ParameterType >
bool olb::XMLreader::readOrWarn ( std::string name_parameter_1,
std::string name_parameter_2,
std::string name_parameter_3,
ParameterType & var,
bool defaultAvailable = true,
bool exitIfMissing = false,
bool showWarning = true ) const

This wrapper function reads the given parameter from the "type_parameter" and "name_parameter_1" or "name_parameter_2" tag and prints a warning, if the parameter can not be read.

The warning contains the default value, if available. Will exit(1) if exitIfMissing == true. The warning is not displayed, if showWarning == false.

Definition at line 183 of file xmlReader.h.

186{
187 // deactivate default warnings and show default values instead
188 setWarningsOn(false);
189 if (name_parameter_3 == "") {
190 if (!(*this)[name_parameter_1][name_parameter_2].read<ParameterType>(var, false)) {
191 _output.parameterReading({name_parameter_1, name_parameter_2}, var, defaultAvailable, exitIfMissing, showWarning);
192 return false;
193 }
194 return true;
195 }
196 else{
197 if (!(*this)[name_parameter_1][name_parameter_2][name_parameter_3].read<ParameterType>(var, false)) {
198 _output.parameterReading({name_parameter_1, name_parameter_2, name_parameter_3}, var, defaultAvailable, exitIfMissing, showWarning);
199 return false;
200 }
201 return true;
202 }
203 // turn default warnings on again
204 setWarningsOn(true);
205}
void parameterReading(std::vector< std::string > parameters, ParameterType &var, bool defaultAvailable, bool exitIfMissing, bool showWarning) const
void setWarningsOn(bool warnings) const
switch warnings on/off
Definition xmlReader.h:342
bool read(T &value, bool verboseOn=true, bool exitIfMissing=false) const
Read a value from the xml file.

References _output, olb::XMLreaderOutput::parameterReading(), read(), and setWarningsOn().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ readOrWarn() [2/2]

template<typename ParameterType >
bool olb::XMLreader::readOrWarn ( std::string name_parameter_1,
std::string name_parameter_2,
std::string name_parameter_3,
std::string name_parameter_4,
ParameterType & var,
bool defaultAvailable = true,
bool exitIfMissing = false,
bool showWarning = true ) const
Returns
a Subtree placed at name
Parameters
nameThe name from which to take the subtree

Definition at line 208 of file xmlReader.h.

211{
212 // deactivate default warnings and show default values instead
213 setWarningsOn(false);
214 if (name_parameter_3 == "") {
215 if (!(*this)[name_parameter_1][name_parameter_2].read<ParameterType>(var, false)) {
216 _output.parameterReading({name_parameter_1, name_parameter_2}, var, defaultAvailable, exitIfMissing, showWarning);
217 return false;
218 }
219 return true;
220 }
221 else if(name_parameter_4 == ""){
222 if (!(*this)[name_parameter_1][name_parameter_2][name_parameter_3].read<ParameterType>(var, false)) {
223 _output.parameterReading({name_parameter_1, name_parameter_2, name_parameter_3}, var, defaultAvailable, exitIfMissing, showWarning);
224 return false;
225 }
226 return true;
227 }
228 else {
229 if (!(*this)[name_parameter_1][name_parameter_2][name_parameter_3][name_parameter_4].read<ParameterType>(var, false)) {
230 _output.parameterReading({name_parameter_1, name_parameter_2,name_parameter_3,name_parameter_4},
231 var, defaultAvailable, exitIfMissing, showWarning);
232 return false;
233 }
234 return true;
235 }
236 // turn default warnings on again
237 setWarningsOn(true);
238}

References _output, olb::XMLreaderOutput::parameterReading(), read(), and setWarningsOn().

+ Here is the call graph for this function:

◆ setWarningsOn()

void olb::XMLreader::setWarningsOn ( bool warnings) const

switch warnings on/off

Definition at line 342 of file xmlReader.h.

343{
344 _warningsOn = warnings;
345 for (unsigned int iNode=0; iNode<_children.size(); ++iNode) {
346 _children[iNode]->setWarningsOn(warnings);
347 }
348}

References _children.

+ Here is the caller graph for this function:

Friends And Related Symbol Documentation

◆ XMLreaderOutput

friend class XMLreaderOutput
friend

Definition at line 55 of file xmlReader.h.

Member Data Documentation

◆ _attributes

std::map<std::string, std::string> olb::XMLreader::_attributes
protected

Definition at line 123 of file xmlReader.h.

◆ _children

std::vector<XMLreader*> olb::XMLreader::_children
protected

Definition at line 124 of file xmlReader.h.

◆ _output

XMLreaderOutput olb::XMLreader::_output

handling all the output for the XMLreader

Definition at line 109 of file xmlReader.h.


The documentation for this class was generated from the following file: