GEOSX
xmlWrapper.hpp
Go to the documentation of this file.
1 /*
2  * ------------------------------------------------------------------------------------------------------------
3  * SPDX-License-Identifier: LGPL-2.1-only
4  *
5  * Copyright (c) 2018-2020 Lawrence Livermore National Security LLC
6  * Copyright (c) 2018-2020 The Board of Trustees of the Leland Stanford Junior University
7  * Copyright (c) 2018-2020 Total, S.A
8  * Copyright (c) 2019- GEOSX Contributors
9  * All rights reserved
10  *
11  * See top level LICENSE, COPYRIGHT, CONTRIBUTORS, NOTICE, and ACKNOWLEDGEMENTS files for details.
12  * ------------------------------------------------------------------------------------------------------------
13  */
14 
19 #ifndef GEOSX_DATAREPOSITORY_XMLWRAPPER_HPP_
20 #define GEOSX_DATAREPOSITORY_XMLWRAPPER_HPP_
21 
22 // Source includes
23 #include "common/DataTypes.hpp"
25 #include "rajaInterface/GEOS_RAJA_Interface.hpp"
26 #include "LvArray/src/output.hpp"
27 #include "LvArray/src/input.hpp"
28 
29 // TPL includes
30 #include <pugixml.hpp>
31 
32 // System includes
33 #include <algorithm>
34 #include <sstream>
35 
36 namespace geosx
37 {
38 
39 
50 {
51 public:
52 
54  using xmlDocument = pugi::xml_document;
55 
57  using xmlResult = pugi::xml_parse_result;
58 
60  using xmlNode = pugi::xml_node;
61 
63  using xmlAttribute = pugi::xml_attribute;
64 
66  using xmlTypes = pugi::xml_node_type;
67 
73  static constexpr auto filePathString = "filePath";
74 
78 
81  xmlWrapper() = delete;
82 
84  ~xmlWrapper() = delete;
85 
87  xmlWrapper( xmlWrapper const & ) = delete;
88 
90  xmlWrapper( xmlWrapper && ) = delete;
91 
93 
102  static void addIncludedXML( xmlNode & targetNode );
103 
111 
120  template< typename T >
121  static std::enable_if_t< traits::CanStreamInto< std::istringstream, T > >
122  StringToInputVariable( T & target, string const & value )
123  {
124  std::istringstream ss( value );
125  ss>>target;
126  }
127 
133  static void StringToInputVariable( R1Tensor & target, string const & value );
134 
144  template< typename T, int NDIM, typename PERMUTATION >
145  static std::enable_if_t< traits::CanStreamInto< std::istringstream, T > >
146  StringToInputVariable( Array< T, NDIM, PERMUTATION > & array, string const & value )
147  { LvArray::input::stringToArray( array, value ); }
148 
150 
153  IS_VALID_EXPRESSION( canParseVariable, T, StringToInputVariable( std::declval< T & >(), std::string() ) );
154 
158 
170  template< typename T, typename T_DEF = T >
171  static std::enable_if_t< canParseVariable< T >, bool >
173  string const & name,
174  xmlNode const & targetNode,
175  T_DEF const & defVal )
176  {
177  pugi::xml_attribute xmlatt = targetNode.attribute( name.c_str() );
178  if( !xmlatt.empty() )
179  {
180  // parse the string/attribute into a value
181  StringToInputVariable( rval, xmlatt.value() );
182  }
183  else
184  {
185  // set the value to the default value
186  equate( rval, defVal );
187  }
188  return true;
189  }
190 
200  template< typename T >
201  static std::enable_if_t< canParseVariable< T >, bool >
203  string const & name,
204  xmlNode const & targetNode,
205  bool const required )
206  {
207  pugi::xml_attribute xmlatt = targetNode.attribute( name.c_str() );
208 
209  bool const success = !(xmlatt.empty() && required);
210 
211  if( success )
212  {
213  // parse the string/attribute into a value
214  StringToInputVariable( rval, xmlatt.value() );
215  }
216  return success;
217  }
218 
227  template< typename T >
228  static std::enable_if_t< !dataRepository::DefaultValue< T >::has_default_value, bool >
230  string const & name,
231  xmlNode const & targetNode,
233  { return ReadAttributeAsType( rval, name, targetNode, false ); }
234 
244  template< typename T >
245  static typename std::enable_if_t< dataRepository::DefaultValue< T >::has_default_value, bool >
247  string const & name,
248  xmlNode const & targetNode,
249  dataRepository::DefaultValue< T > const & defVal )
250  { return ReadAttributeAsType( rval, name, targetNode, defVal.value ); }
251 
256  template< typename T, typename U >
257  static std::enable_if_t< !canParseVariable< T >, bool >
258  ReadAttributeAsType( T &, string const &, xmlNode const &, U const & )
259  {
260  GEOSX_ERROR( "Cannot parse the given type " << LvArray::system::demangleType< T >() );
261  return false;
262  }
263 
265 
266 private:
267 
274  template< typename T >
275  static void equate( T & lhs, T const & rhs )
276  { lhs = rhs; }
277 
286  template< typename T, int NDIM, typename PERM >
287  static void equate( Array< T, NDIM, PERM > const & lhs, T const & rhs )
288  { lhs.template setValues< serialPolicy >( rhs ); }
289 
290 };
291 
292 } /* namespace geosx */
293 
294 #endif /*GEOSX_DATAREPOSITORY_XMLWRAPPER_HPP_ */
static std::enable_if_t< traits::CanStreamInto< std::istringstream, T > > StringToInputVariable(Array< T, NDIM, PERMUTATION > &array, string const &value)
Parse a string and fill an Array with the value(s) in the string.
Definition: xmlWrapper.hpp:146
pugi::xml_document xmlDocument
Alias for the type of xml document.
Definition: xmlWrapper.hpp:54
Contains functions for outputting array objects.
IS_VALID_EXPRESSION(canParseVariable, T, StringToInputVariable(std::declval< T & >(), std::string()))
pugi::xml_node xmlNode
Alias for the type of an xml node.
Definition: xmlWrapper.hpp:60
static std::enable_if_t< !canParseVariable< T >, bool > ReadAttributeAsType(T &, string const &, xmlNode const &, U const &)
Stub that for unreadable types that errors out.
Definition: xmlWrapper.hpp:258
static std::enable_if_t< canParseVariable< T >, bool > ReadAttributeAsType(T &rval, string const &name, xmlNode const &targetNode, T_DEF const &defVal)
Extract attribute in an xml tree, and translate its value into a typed variable.
Definition: xmlWrapper.hpp:172
~xmlWrapper()=delete
Deleted default default destructor.
static void stringToArray(Array< T, NDIM, PERMUTATION, INDEX_TYPE, BUFFER_TYPE > &array, std::string valueString)
This function reads the contents of a string into an Array.
Definition: input.hpp:121
static constexpr auto filePathString
constexpr variable to hold name for inserting the file path into the xml file.
Definition: xmlWrapper.hpp:73
Contains functions for creating objects from strings.
static std::enable_if_t< traits::CanStreamInto< std::istringstream, T > > StringToInputVariable(T &target, string const &value)
Parse a string and fill a variable with the value(s) in the string.
Definition: xmlWrapper.hpp:122
static std::enable_if_t< !dataRepository::DefaultValue< T >::has_default_value, bool > ReadAttributeAsType(T &rval, string const &name, xmlNode const &targetNode, dataRepository::DefaultValue< T > const &)
Extract attribute in an xml tree, and translate its value into a typed variable.
Definition: xmlWrapper.hpp:229
internal::Helper< T > DefaultValue
A templated alias to hold default values.
#define GEOSX_ERROR(msg)
Raise a hard error and terminate the program.
Definition: Logger.hpp:110
pugi::xml_parse_result xmlResult
Alias for the type of the result from an xml parse attempt.
Definition: xmlWrapper.hpp:57
static std::enable_if_t< canParseVariable< T >, bool > ReadAttributeAsType(T &rval, string const &name, xmlNode const &targetNode, bool const required)
Extract attribute in an xml tree, and translate its value into a typed variable.
Definition: xmlWrapper.hpp:202
static std::enable_if_t< dataRepository::DefaultValue< T >::has_default_value, bool > ReadAttributeAsType(T &rval, string const &name, xmlNode const &targetNode, dataRepository::DefaultValue< T > const &defVal)
Extract attribute in an xml tree, and translate its value into a typed variable.
Definition: xmlWrapper.hpp:246
std::string string
String type.
Definition: DataTypes.hpp:131
xmlWrapper()=delete
Deleted default constructor.
pugi::xml_attribute xmlAttribute
Alias for the type of an xml attribute.
Definition: xmlWrapper.hpp:63
This class provides a fixed dimensional resizeable array interface in addition to an interface simila...
Definition: Array.hpp:55
static void addIncludedXML(xmlNode &targetNode)
Function to add xml nodes from included files.
pugi::xml_node_type xmlTypes
Alias for the type variant of an xml node.
Definition: xmlWrapper.hpp:66