Medusa  1.1
Coordinate Free Mehless Method implementation
XML.hpp
Go to the documentation of this file.
1 #ifndef MEDUSA_BITS_IO_XML_HPP_
2 #define MEDUSA_BITS_IO_XML_HPP_
3 
4 #include "XML_fwd.hpp"
6 #include <sstream>
7 #include <iomanip>
8 
14 namespace mm {
15 
16 template <typename T>
17 T XML::get(const std::string& path) const {
18  std::vector<std::string> path_elements;
19  std::string attribute_name;
20  std::tie(path_elements, attribute_name) = splitPath(path);
21  char* value = getString(path_elements, attribute_name);
22  std::stringstream ss(value);
23  T result;
24  ss >> result;
25  return result;
26 }
27 
29 template <>
30 std::string XML::get<std::string>(const std::string& path) const;
31 
32 template <typename T>
33 void XML::set(const std::string& path, const T& value, bool overwrite) {
34  std::vector<std::string> path_elements;
35  std::string attribute_name;
36  std::tie(path_elements, attribute_name) = splitPath(path);
37  if (!overwrite) {
38  assert_msg(!exists(path), "Attribute on path '%s' already exists with value '%s'. "
39  "Set overwrite=true to overwrite its value.",
40  path, getString(path_elements, attribute_name));
41  }
42  std::stringstream ss;
43  ss << std::setprecision(16) << value;
44  setString(path_elements, attribute_name, ss.str());
45 }
46 
47 } // namespace mm
48 
49 #endif // MEDUSA_BITS_IO_XML_HPP_
mm
Root namespace for the whole library.
Definition: Gaussian.hpp:14
mm::XML::get
T get(const std::string &path) const
Reads a values from an attribute specified by path.
Definition: XML.hpp:17
assert_msg
#define assert_msg(cond,...)
Assert with better error reporting.
Definition: assert.hpp:75
mm::XML::getString
char * getString(const std::vector< std::string > &path, const std::string &attribute_name) const
Reads the contents of the attribute specified by path as a string.
Definition: XML.cpp:87
XML_fwd.hpp
mm::XML::set
void set(const std::string &path, const T &value, bool overwrite=false)
Saves a value to the attribute pointed to by path.
Definition: XML.hpp:33
assert.hpp
mm::XML::splitPath
static std::pair< std::vector< std::string >, std::string > splitPath(std::string path)
Splits dot separated path into elements path and attribute name.
Definition: XML.cpp:55
mm::XML::setString
void setString(const std::vector< std::string > &path, const std::string &attribute_name, const std::string &content)
Writes the contents of string content to the attribute specified by path.
Definition: XML.cpp:101
mm::XML::exists
bool exists(const std::string &path) const
Returns true if the an attribute specified by path exists and false otherwise.
Definition: XML.cpp:164