MyGUI 3.5.3
MyGUI_XmlDocument.h
Go to the documentation of this file.
1/*
2 * This source file is part of MyGUI. For the latest info, see http://mygui.info/
3 * Distributed under the MIT License
4 * (See accompanying file COPYING.MIT or copy at http://opensource.org/licenses/MIT)
5 */
6
7#ifndef MYGUI_XML_DOCUMENT_H_
8#define MYGUI_XML_DOCUMENT_H_
9
10#include "MyGUI_Prerequest.h"
11#include "MyGUI_UString.h"
12#include "MyGUI_Diagnostic.h"
13#include "MyGUI_DataStream.h"
15
16#include <string>
17#include <string_view>
18#include <iostream>
19#include <vector>
20#include <utility>
21#include <memory>
22#include <filesystem>
23
24namespace pugi
25{
26
27 class xml_node;
28 class xml_document;
29 struct xml_parse_result;
30
31}
32
33namespace MyGUI::xml
34{
35
36 using PairAttribute = std::pair<std::string, std::string>;
37 using VectorAttributes = std::vector<PairAttribute>;
38
39 class ElementPtr;
40 class Element;
41
42 class MYGUI_EXPORT Document : public MyGUI::MemberObsolete<Document>
43 {
44 public:
45 Document();
46
47 // Open a regular file using its native path
48 bool open(const std::filesystem::path& _filename);
49
50 // Open as regular file, filename in UTF-8
51 bool open(const std::string& _filename);
52
53 // Open as regular stream
54 bool open(std::istream& _stream);
55
56 bool open(const UString& _filename);
57
58 bool open(IDataStream* _stream);
59
60 bool save(const std::filesystem::path& _filename);
61
62 bool save(const std::string& _filename);
63
64 bool save(std::ostream& _stream);
65
66 bool save(const UString& _filename);
67
68 void clear();
69
70 std::string getLastError() const;
71
72 void clearLastError();
73
74 ElementPtr createDeclaration(std::string_view _version = "1.0", std::string_view _encoding = "UTF-8");
75 ElementPtr createRoot(std::string_view _name);
76
77 ElementPtr getRoot() const;
78
79 private:
80 std::shared_ptr<pugi::xml_document> mDoc;
81 std::shared_ptr<pugi::xml_parse_result> mResult;
82 std::string mLastError;
83 std::filesystem::path mLastErrorFile;
84 bool mLastErrorFromStream{false};
85 };
86
87 class MYGUI_EXPORT ElementEnumerator
88 {
89 public:
90 ElementEnumerator(std::shared_ptr<pugi::xml_node> parent);
91
92 public:
93 bool next();
94 bool next(std::string_view _name);
95
96 ElementPtr operator->() const;
97 ElementPtr current();
98
99 private:
100 std::vector<ElementPtr> mElements;
101 size_t mIndex{size_t(-1)};
102 };
103
104 class MYGUI_EXPORT ElementPtr
105 {
106 public:
107 ElementPtr() = default;
108 ElementPtr(std::nullptr_t)
109 {
110 }
111 ElementPtr(const pugi::xml_node& _node);
112
113 operator pugi::xml_node() const;
114
115 ElementPtr* operator->();
116 const ElementPtr* operator->() const;
117
118 friend bool operator==(const ElementPtr& lhs, std::nullptr_t)
119 {
120 return lhs.mNode == nullptr;
121 }
122 friend bool operator==(std::nullptr_t, const ElementPtr& rhs)
123 {
124 return nullptr == rhs.mNode;
125 }
126 friend bool operator!=(const ElementPtr& lhs, std::nullptr_t)
127 {
128 return lhs.mNode != nullptr;
129 }
130 friend bool operator!=(std::nullptr_t, const ElementPtr& rhs)
131 {
132 return nullptr != rhs.mNode;
133 }
134
135 ElementPtr& operator=(std::nullptr_t)
136 {
137 mNode.reset();
138 return *this;
139 }
140
141 ElementPtr createChild(std::string_view _name, std::string_view _content = {});
142 void removeChild(ElementPtr _child);
143
144 template<typename T, typename = std::enable_if_t<!std::is_convertible_v<T, std::string_view>>>
145 void addAttribute(std::string_view _key, const T& _value)
146 {
147 addAttribute(_key, MyGUI::utility::toString(_value));
148 }
149
150 void addAttribute(std::string_view _key, std::string_view _value);
151 void removeAttribute(std::string_view _key);
152 void setAttribute(std::string_view _key, std::string_view _value);
153
154 template<typename T, typename = std::enable_if_t<!std::is_convertible_v<T, std::string_view>>>
155 void setContent(const T& _content)
156 {
157 setContent(MyGUI::utility::toString(_content));
158 }
159
160 void setContent(std::string_view _content);
161
162 void clear();
163
164 bool findAttribute(std::string_view _name, std::string& _value);
165 std::string_view findAttribute(std::string_view _name);
166
167 std::string getName() const;
168 std::string getContent() const;
169
170 VectorAttributes getAttributes() const;
171
172 ElementPtr getParent() const;
173
174 ElementEnumerator getElementEnumerator();
175
176 const pugi::xml_node& node() const;
177
178 std::unique_ptr<Element> createCopy();
179
180 private:
181 std::shared_ptr<pugi::xml_node> mNode;
182 };
183
184 class MYGUI_EXPORT Element : public ElementPtr
185 {
186 friend class ElementPtr;
187
188 public:
189 Element(std::shared_ptr<pugi::xml_node> _node, std::shared_ptr<pugi::xml_document> _doc);
190
191 private:
192 std::shared_ptr<pugi::xml_document> mDocument;
193 };
194
195} // namespace MyGUI
196
197#endif // MYGUI_XML_DOCUMENT_H_
#define MYGUI_EXPORT
__attribute__((visibility("default"))) void trim(std std::string toString(T _value)
std::pair< std::string, std::string > PairAttribute
std::vector< PairAttribute > VectorAttributes