xmlwrapp
Lightweight C++ XML parsing library
Loading...
Searching...
No Matches
nodes_view.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2009 Vaclav Slavik <vslavik@gmail.com>
3 * All Rights Reserved
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
14 * distribution.
15 * 3. Neither the name of the Author nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR
23 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33/**
34 @file
35
36 This file contains the definition of the xml::nodes_view and
37 xml::const_nodes_view classes.
38 */
39
40#ifndef _xmlwrapp_nodes_view_h_
41#define _xmlwrapp_nodes_view_h_
42
43// xmlwrapp includes
44#include "xmlwrapp/init.h"
45#include "xmlwrapp/export.h"
46
47// standard includes
48#include <iterator>
49#include <memory>
50
51XMLWRAPP_MSVC_SUPPRESS_DLL_MEMBER_WARN
52
53namespace xml
54{
55
56class node;
57class const_nodes_view;
58
59namespace impl
60{
61
62struct nipimpl;
63class iter_advance_functor;
64struct xpath_context_impl;
65
66} // namespace impl
67
68/**
69 This class implements a view of XML nodes. A @em view is a container-like
70 class that only allows access to a subset of xml::node's child nodes. The
71 exact content depends on how the view was obtained; typical uses are
72 e.g. a view of all element children or all elements with a given name.
73
74 The nodes_view class implements the same container interface that
75 xml::node does: it has begin() and end() methods.
76
77 @since 0.6.0
78
79 @see xml::node::elements(), xml::node::elements(const char *)
80 */
81class XMLWRAPP_API nodes_view
82{
83public:
84 /// Size type.
85 using size_type = std::size_t;
86
87 nodes_view() : data_begin_(nullptr), advance_func_(nullptr) {}
88 nodes_view(const nodes_view& other);
90
91 nodes_view& operator=(const nodes_view& other);
92
93 class const_iterator;
94
95 /**
96 The iterator provides a way to access nodes in the view
97 similar to a standard C++ container.
98
99 As xml::node::iterator itself, this is only a forward iterator.
100 */
101 class XMLWRAPP_API iterator
102 {
103 public:
104 using value_type = node;
105 using difference_type = int;
106 using pointer = value_type *;
107 using reference = value_type &;
108 using iterator_category = std::forward_iterator_tag;
109
110 iterator();
111 iterator(const iterator& other);
112 iterator& operator=(const iterator& other);
113 ~iterator();
114
115 reference operator*() const;
116 pointer operator->() const;
117
118 iterator& operator++();
119 iterator operator++(int);
120
121 private:
122 explicit iterator(void *data, impl::iter_advance_functor *advance_func);
123 void* get_raw_node() const;
124 void swap(iterator& other);
125
126 std::unique_ptr<impl::nipimpl> pimpl_;
127 // function for advancing the iterator (note that it is "owned" by the
128 // parent view object, so we don't have to care about its reference
129 // count here)
130 impl::iter_advance_functor *advance_func_ = nullptr;
131
132 friend class nodes_view;
133 friend class const_iterator;
134 friend bool XMLWRAPP_API operator==(const iterator& lhs, const iterator& rhs);
135 };
136
137 /**
138 The const_iterator provides a way to access nodes in the view
139 similar to a standard C++ container. The nodes that are pointed to by
140 the iterator cannot be changed.
141
142 As xml::node::const_iterator itself, this is only a forward iterator.
143 */
144 class XMLWRAPP_API const_iterator
145 {
146 public:
147 using value_type = const node;
148 using difference_type = int;
149 using pointer = value_type *;
150 using reference = value_type &;
151 using iterator_category = std::forward_iterator_tag;
152
154 const_iterator(const const_iterator& other);
155 const_iterator(const iterator& other);
156 const_iterator& operator=(const const_iterator& other);
157 const_iterator& operator=(const iterator& other);
159
160 reference operator*() const;
161 pointer operator->() const;
162
163 const_iterator& operator++();
164 const_iterator operator++(int);
165
166 private:
167 explicit const_iterator(void *data, impl::iter_advance_functor *advance_func);
168 void* get_raw_node() const;
169 void swap(const_iterator& other);
170
171 std::unique_ptr<impl::nipimpl> pimpl_;
172 // function for advancing the iterator (note that it is "owned" by the
173 // parent view object, so we don't have to care about its reference
174 // count here)
175 impl::iter_advance_functor *advance_func_ = nullptr;
176
177 friend class const_nodes_view;
178 friend class nodes_view;
179 friend bool XMLWRAPP_API operator==(const const_iterator& lhs, const const_iterator& rhs);
180 };
181
182 /// Get an iterator that points to the beginning of this view's nodes.
183 iterator begin() { return iterator(data_begin_, advance_func_); }
184
185 /// Get an iterator that points to the beginning of this view's nodes.
186 const_iterator begin() const { return const_iterator(data_begin_, advance_func_); }
187
188 /// Get an iterator that points one past the last child for this view.
189 iterator end() { return iterator(); }
190
191 /// Get an iterator that points one past the last child for this view.
192 const_iterator end() const { return const_iterator(); }
193
194 /// Returns the number of nodes in this view.
196
197 /// Is the view empty?
198 bool empty() const { return !data_begin_; }
199
200 /**
201 Erase the node that is pointed to by the given iterator.
202
203 The node and all its children will be removed from its parent node.
204 This will invalidate any iterators that point to the node to be erased,
205 or any pointers or references to that node (but not any other iterators).
206
207 @param to_erase An iterator that points to the node to be erased.
208 @return An iterator that points to the next node in this view
209 after the one being erased or end() if there are none.
210
211 @since 0.8.0
212 */
213 iterator erase(const iterator& to_erase);
214
215 /**
216 Erase all nodes in the given range, from first to last.
217
218 This will invalidate any iterators that point to the nodes to be
219 erased, or any pointers or references to those nodes.
220
221 @param first The first node in the range to be removed.
222 @param last An iterator that points one past the last node to erase. Think xml::node::end().
223
224 @return An iterator that points to the next node in this view
225 after the ones being erased or end() if there are none.
226
227 @since 0.8.0
228 */
229 iterator erase(iterator first, const iterator& last);
230
231private:
232 explicit nodes_view(void *data_begin, impl::iter_advance_functor *advance_func)
233 : data_begin_(data_begin), advance_func_(advance_func) {}
234
235 // begin iterator
236 void *data_begin_;
237 // function for advancing the iterator (owned by the view object)
238 impl::iter_advance_functor *advance_func_;
239
240 friend class node;
241 friend struct impl::xpath_context_impl;
242 friend class const_nodes_view;
243};
244
245
246/**
247 This class implements a @em read-only view of XML nodes. The only
248 difference from xml::nodes_view is that it doesn't allow modifications of
249 the nodes, it is otherwise identical.
250
251 @see nodes_view
252
253 @since 0.6.0
254 */
255class XMLWRAPP_API const_nodes_view
256{
257public:
258 /// Size type.
259 using size_type = std::size_t;
260
261 const_nodes_view() : data_begin_(nullptr), advance_func_(nullptr) {}
263 const_nodes_view(const nodes_view& other);
265
266 const_nodes_view& operator=(const const_nodes_view& other);
267 const_nodes_view& operator=(const nodes_view& other);
268
269 using iterator = nodes_view::const_iterator;
270 using const_iterator = nodes_view::const_iterator;
271
272 /// Get an iterator that points to the beginning of this view's nodes.
274 { return const_iterator(data_begin_, advance_func_); }
275
276 /// Get an iterator that points one past the last child for this view.
277 const_iterator end() const { return const_iterator(); }
278
279 /// Returns the number of nodes in this view.
281
282 /// Is the view empty?
283 bool empty() const { return !data_begin_; }
284
285private:
286 explicit const_nodes_view(void *data_begin, impl::iter_advance_functor *advance_func)
287 : data_begin_(data_begin), advance_func_(advance_func) {}
288
289 // begin iterator
290 void *data_begin_;
291 // function for advancing the iterator (owned by the view object)
292 impl::iter_advance_functor *advance_func_;
293
294 friend class node;
295 friend struct impl::xpath_context_impl;
296};
297
298// Comparison operators for xml::[const_]nodes_view iterators
299
300inline bool operator==(const nodes_view::iterator& lhs, const nodes_view::iterator& rhs)
301 { return lhs.get_raw_node() == rhs.get_raw_node(); }
302inline bool operator!=(const nodes_view::iterator& lhs, const nodes_view::iterator& rhs)
303 { return !(lhs == rhs); }
304
305inline bool operator==(const nodes_view::const_iterator& lhs, const nodes_view::const_iterator& rhs)
306 { return lhs.get_raw_node() == rhs.get_raw_node(); }
307inline bool operator!=(const nodes_view::const_iterator& lhs, const nodes_view::const_iterator& rhs)
308 { return !(lhs == rhs); }
309
310} // end xml namespace
311
312XMLWRAPP_MSVC_RESTORE_DLL_MEMBER_WARN
313
314#endif // _xmlwrapp_nodes_view_h_
This class implements a read-only view of XML nodes.
Definition nodes_view.h:256
bool empty() const
Is the view empty?
Definition nodes_view.h:283
const_iterator end() const
Get an iterator that points one past the last child for this view.
Definition nodes_view.h:277
std::size_t size_type
Size type.
Definition nodes_view.h:259
const_iterator begin() const
Get an iterator that points to the beginning of this view's nodes.
Definition nodes_view.h:273
size_type size() const
Returns the number of nodes in this view.
The xml::node class is used to hold information about one XML node.
Definition node.h:92
The const_iterator provides a way to access nodes in the view similar to a standard C++ container.
Definition nodes_view.h:145
The iterator provides a way to access nodes in the view similar to a standard C++ container.
Definition nodes_view.h:102
This class implements a view of XML nodes.
Definition nodes_view.h:82
bool empty() const
Is the view empty?
Definition nodes_view.h:198
iterator erase(const iterator &to_erase)
Erase the node that is pointed to by the given iterator.
iterator erase(iterator first, const iterator &last)
Erase all nodes in the given range, from first to last.
size_type size() const
Returns the number of nodes in this view.
iterator end()
Get an iterator that points one past the last child for this view.
Definition nodes_view.h:189
const_iterator begin() const
Get an iterator that points to the beginning of this view's nodes.
Definition nodes_view.h:186
std::size_t size_type
Size type.
Definition nodes_view.h:85
const_iterator end() const
Get an iterator that points one past the last child for this view.
Definition nodes_view.h:192
iterator begin()
Get an iterator that points to the beginning of this view's nodes.
Definition nodes_view.h:183
XML library namespace.
Definition attributes.h:55
This file contains the definition of the xml::init class.