-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontext.hpp
183 lines (141 loc) · 4.61 KB
/
context.hpp
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// Copyright Morten Bendiksen 2014 - 2016.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#ifndef MEDIASEQUENCER_PLUGIN_UTIL_XPATH_CONTEXT_HPP
#define MEDIASEQUENCER_PLUGIN_UTIL_XPATH_CONTEXT_HPP
#include <boost/range/adaptor/filtered.hpp>
#include <boost/range/adaptor/transformed.hpp>
#include <boost/range/any_range.hpp>
#include "singleton_iterator.hpp"
#include <deque>
#include "scopedmap.hpp"
namespace mediasequencer { namespace plugin { namespace util { namespace xpath {
using namespace boost::adaptors;
// a context object holds a node (i.e. pugi::xml_node)
// and uses an Adaptor to access it
template <typename Adaptor, typename NodeType = typename Adaptor::node_type>
class _context
{
public:
typedef singleton_iterator<_context<Adaptor> > iterator;
typedef singleton_iterator<_context<Adaptor> > const_iterator;
typedef NodeType node_type;
typedef Adaptor adaptor;
typedef typename Adaptor::attribute_range AttributeRange;
typedef typename AttributeRange::iterator AttributeIterator;
private:
NodeType node;
std::deque<scopedmap<std::string, std::string> > maps;
void build_map(NodeType const& n) {
if (Adaptor::is_null(n)) {
maps.push_back(scopedmap<std::string, std::string>());
return;
}
build_map(Adaptor::parent(n));
push_map(n);
}
void push_map(NodeType const& n) {
assert(!Adaptor::is_null(n));
auto namespacePairs = Adaptor::namespace_declarations(n);
maps.push_back(maps.back().add(namespacePairs.begin(), namespacePairs.end()));
}
public:
_context& operator=(_context const& other) {
maps = other.maps;
node = other.node;
return *this;
}
_context& operator=(_context&& other) {
maps = std::move(other.maps);
node = std::move(other.node);
return *this;
}
singleton_iterator<_context> begin() const {
if (Adaptor::is_null(node)) {
return singleton_iterator<_context>();
}else {
return singleton_iterator<_context>(*this);
}
}
singleton_iterator<_context> end() const {
return singleton_iterator<_context>();
}
explicit _context(NodeType const& n): node(n) {
build_map(n);
}
_context(_context const& other): node(other.node), maps(other.maps) {
}
_context(_context && other)
: node(std::move(other.node)),
maps(std::move(other.maps)) {
}
_context() : node(Adaptor::null()) {}
NodeType const& get_node() const {
return node;
}
std::string to_text() const {
if (Adaptor::is_null(node)) {
return "";
} else {
return Adaptor::to_text(node);
}
}
std::string text() const {
return Adaptor::text(node);
}
bool operator==(_context const& other) const {
return node == other.node;
}
void first_child() {
node = Adaptor::first_child(node);
if (node)
push_map(node);
}
void next_sibling() {
maps.pop_back();
node = Adaptor::next_sibling(node);
if (node)
push_map(node);
}
void parent() {
maps.pop_back();
node = Adaptor::parent(node);
}
bool has_children() const {
return Adaptor::has_children(node);
}
bool has_next_sibling() const {
return Adaptor::has_next_sibling(node);
}
bool is_root() const {
return Adaptor::is_root(node);
}
scopedmap<std::string, std::string> const& ns() const {
return maps.back();
}
AttributeRange attributes() const {
return Adaptor::attributes(node);
}
std::string attribute(std::string const& name) const{
return Adaptor::attribute(node, name);
}
bool is_null() const {
return Adaptor::is_null(node);
}
std::string name() const {
return node.name();
}
};
/// Erases the type of the actual range. This enables conversion
/// from any result range from any query to this type which hides
/// the actual type. This is useful for example as function
/// parameter types, instead of using a template function everywhere.
template <typename T>
class range : public boost::any_range<T, boost::forward_traversal_tag, T&, boost::use_default> {
public:
template<typename ActualRange>
range(ActualRange const& other) : boost::any_range<T, boost::forward_traversal_tag, T&, boost::use_default>(other) { }
};
}}}}
#endif // MEDIASEQUENCER_PLUGIN_UTIL_XPATH_CONTEXT_HPP