-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathxmldocument.cpp
More file actions
89 lines (74 loc) · 2.66 KB
/
Copy pathxmldocument.cpp
File metadata and controls
89 lines (74 loc) · 2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include "common.hpp"
#include "pugixml.hpp"
#include "qsharedpointer.h"
#include "common/private/xmldocument_p.h"
namespace shelllet {
namespace common {
using namespace pugi;
template <typename F, typename... TArgs>
decltype(auto) __invoke__(const XmlDocumentPrivate* p, F fuc, TArgs&& ... args) {
if (std::holds_alternative<SharedPointer<pugi::xml_document>>(p->node)) {
return (*std::get<1>(p->node).*fuc)(std::forward<TArgs>(args)...);
}
throw std::logic_error("The method or operation is not implemented.");
//return (*std::get<0>(p->node).*fuc)(std::forward<TArgs>(args)...);
}
std::ostream& operator<<(std::ostream& out, const XmlDocument& doc)
{
__invoke__(doc.d_func(), static_cast<void(pugi::xml_document::*)(std::basic_ostream<char, std::char_traits<char> >&
, const char_t *
, unsigned int
, pugi::xml_encoding encoding
, unsigned int
) const>(&pugi::xml_document::print), out, "\t", pugi::format_default, pugi::encoding_auto, 0);
return out;
}
}
}
shelllet::common::XmlDocument::XmlDocument(Object* parent/* = nullptr*/)
:XmlDocument(*new XmlDocumentPrivate, parent)
{
}
shelllet::common::XmlNode shelllet::common::XmlDocument::documentElement() const
{
Q_D(const XmlDocument);
auto node = __invoke__(d, &pugi::xml_document::document_element);
return XmlNode::from(&node);
}
void shelllet::common::XmlDocument::addDeclaration()
{
Q_D(XmlDocument);
pugi::xml_node decl = __invoke__(d, static_cast<pugi::xml_node(pugi::xml_document::*)(pugi::xml_node_type)>(&pugi::xml_document::append_child), pugi::node_declaration);
decl.append_attribute("version") = "1.0";
decl.append_attribute("encoding") = "utf-8";
decl.append_attribute("standalone") = "yes";
}
void shelllet::common::XmlDocument::loadFile(const Path& path)
{
Q_D(XmlDocument);
pugi::xml_parse_result result = __invoke__(d, static_cast<pugi::xml_parse_result(pugi::xml_document::*)(const char*, unsigned int, pugi::xml_encoding)>(&pugi::xml_document::load_file)
, path.toString().toUtf8()
, pugi::parse_default
, pugi::encoding_auto
);
if (!result)
{
LOG_WARNING(PROJECT_NAME) << "# error description: " << result.description() << "\n"
<< "error offset: " << result.offset << " (error at " << result.offset << ".\n\n";
}
}
void shelllet::common::XmlDocument::print(std::ostream& stream)
{
Q_D(XmlDocument);
stream << *this;
}
shelllet::common::XmlDocument::XmlDocument(const Path& p, Object* parent /*= nullptr*/)
: XmlDocument(*new XmlDocumentPrivate, parent)
{
loadFile(p);
}
shelllet::common::XmlDocument::XmlDocument(XmlDocumentPrivate& d, Object* parent/* = nullptr*/)
: XmlNode(d, parent)
{
d.node = std::make_shared<pugi::xml_document>();
}