ADUL
Collection of reusable C++ utilities
Loading...
Searching...
No Matches
XMLMap.hpp
Go to the documentation of this file.
1
2#ifndef ADUL_XMLMAP_HPP
3#define ADUL_XMLMAP_HPP
4
5#include "../extern/tinyxml2.hpp"
6#include "exceptions.hpp"
7#include "smallUtils.hpp"
8
9#include <unordered_map>
10
11namespace adul { namespace atm {
12
20template <typename keyT, typename valueT> class XMLMap {
21public:
22 std::unordered_map<keyT, valueT> map;
23
24 void saveMapToXMLElement(adul::xml::XMLElement *const pElement, adul::xml::XMLDocument *const xmlDoc) const {
25 adul::xml::XMLElement *pSubEl = nullptr;
26
27 for (auto cur = map.begin(); cur != map.end(); cur++) {
28 pSubEl = xmlDoc->NewElement("mapElement");
29
30 pSubEl->SetAttribute("key",
32 pSubEl->SetAttribute("value",
33 adul::utils::convert_string<std::string>(cur->second).c_str());
34
35 pElement->InsertEndChild(pSubEl);
36 }
37 }
38
39 void loadMapFromXMLElement(const adul::xml::XMLElement *const pElement) {
40 const adul::xml::XMLElement *pSubEl =
41 pElement->FirstChildElement("mapElement");
42
43 while (pSubEl != nullptr) {
44 const char* keyAttr = pSubEl->Attribute("key");
45 const char* valueAttr = pSubEl->Attribute("value");
46 if (!keyAttr || !valueAttr) {
47 throw adul::exceptions::Message("Missing key/value attribute in XML");
48 }
49
52 pSubEl = pSubEl->NextSiblingElement("mapElement");
53 }
54 }
55
56 void saveMapToXMLFile(const char *dir) {
57 adul::xml::XMLDocument document;
58 adul::xml::XMLElement *pRoot = document.NewElement("root");
59
60 saveMapToXMLElement(pRoot, &document);
61 document.InsertFirstChild(pRoot);
62
63 if (document.SaveFile(dir) != adul::xml::XMLError::XML_SUCCESS)
65 }
66
67 void loadMapFromXMLFile(const char *dir) {
68 adul::xml::XMLDocument document;
69
70 if (document.LoadFile(dir) != adul::xml::XMLError::XML_SUCCESS)
72
73 adul::xml::XMLElement *pRoot = document.FirstChildElement("root");
74 if (!pRoot) throw adul::exceptions::FailedReadingXMLFile("Missing root element");
75
77 }
78};
79
80}} // namespace adul
81#endif
this class is wrapper for STL container std::unordered_map
Definition XMLMap.hpp:20
void saveMapToXMLFile(const char *dir)
Definition XMLMap.hpp:56
void loadMapFromXMLFile(const char *dir)
Definition XMLMap.hpp:67
std::unordered_map< keyT, valueT > map
Definition XMLMap.hpp:22
void saveMapToXMLElement(adul::xml::XMLElement *const pElement, adul::xml::XMLDocument *const xmlDoc) const
Definition XMLMap.hpp:24
void loadMapFromXMLElement(const adul::xml::XMLElement *const pElement)
Definition XMLMap.hpp:39
represents failure with reading from XML file
Definition exceptions.hpp:58
represents failure with writing to XML file
Definition exceptions.hpp:71
modified std::exception class The only difference is that you define message by yourself when create...
Definition exceptions.hpp:27
T convert_string(const std::string &str)
converts std::string into other type
Definition smallUtils.hpp:23
std::string convert_string< std::string >(const std::string &str)
Definition smallUtils.hpp:33
project api's namespace
Definition atm.hpp:4