Medusa  1.1
Coordinate Free Mehless Method implementation
XML_fwd.hpp
Go to the documentation of this file.
1 #ifndef MEDUSA_BITS_IO_XML_FWD_HPP_
2 #define MEDUSA_BITS_IO_XML_FWD_HPP_
3 
11 #include <medusa/Config.hpp>
12 #include <string>
13 #include <vector>
14 #include <iosfwd>
15 #include <rapidxml/rapidxml.hpp>
16 
17 namespace mm {
18 
40 class XML {
41  std::vector<char> file_contents;
42  rapidxml::xml_document<char> doc;
43 
44  public:
46  XML() = default;
52  XML(const XML&);
54  explicit XML(const std::string& filename);
59  void load(const std::string& filename);
60 
61  private:
67  void loadFileHelper(const std::string& file);
76  char* getString(const std::vector<std::string>& path, const std::string& attribute_name) const;
78  void setString(const std::vector<std::string>& path, const std::string& attribute_name,
79  const std::string& content);
87  void getAllRecursive(const rapidxml::xml_node<>* node, std::string path,
88  std::vector<std::pair<std::string, std::string>>& all_attr) const;
89 
94  static std::pair<std::vector<std::string>, std::string> splitPath(std::string path);
95 
97  static std::string join(const std::vector<std::string>& path);
98 
99  public:
101  bool exists(const std::string& path) const;
102 
111  template <typename T>
112  T get(const std::string& path) const;
113 
126  template <typename T>
127  void set(const std::string& path, const T& value, bool overwrite = false);
128 
130  std::vector<std::pair<std::string, std::string>> getAll() const;
131 
133  rapidxml::xml_document<char>& documentRoot();
135  const rapidxml::xml_document<char>& documentRoot() const;
136 
138  friend std::ostream& operator<<(std::ostream& os, const XML& xml);
139 };
140 
141 
142 } // namespace mm
143 
144 #endif // MEDUSA_BITS_IO_XML_FWD_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
mm::XML::loadFromStoredContents
void loadFromStoredContents()
Loads the XML document from a stored null-terminated file_contents.
Definition: XML.cpp:28
mm::XML::getAll
std::vector< std::pair< std::string, std::string > > getAll() const
Returns all pairs (path, attribute_value).
Definition: XML.cpp:148
mm::XML::XML
XML()=default
Creates an XML reader linked to no document.
mm::XML::loadFileHelper
void loadFileHelper(const std::string &file)
Function for opening files, called by XML::XML(const std::string&) and XML::load().
Definition: XML.cpp:16
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
Config.hpp
mm::XML::getAllRecursive
void getAllRecursive(const rapidxml::xml_node<> *node, std::string path, std::vector< std::pair< std::string, std::string >> &all_attr) const
Fills the all_attr array with all pairs (path, value) pairs that are descendants of node node at path...
Definition: XML.cpp:154
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
mm::XML::documentRoot
rapidxml::xml_document< char > & documentRoot()
Access to underlying XML root element from RapidXml library.
Definition: XML.cpp:45
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
Class for reading and storing values to XML files.
Definition: XML_fwd.hpp:40
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::file_contents
std::vector< char > file_contents
Whole XML file contents.
Definition: XML_fwd.hpp:41
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
mm::XML::doc
rapidxml::xml_document< char > doc
XML document root.
Definition: XML_fwd.hpp:42
mm::XML::operator<<
friend std::ostream & operator<<(std::ostream &os, const XML &xml)
Prints the contents of currently loaded XML document.
Definition: XML.cpp:49
mm::XML::join
static std::string join(const std::vector< std::string > &path)
Join a path into dot separated string.
Definition: XML.cpp:77
mm::XML::load
void load(const std::string &filename)
Loads XML document from a file given by filename.
Definition: XML.cpp:44