xmlwrapp
Lightweight C++ XML parsing library
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 
50 namespace xml
51 {
52 
53 class node;
54 class const_nodes_view;
55 
56 namespace impl
57 {
58 
59 struct nipimpl;
60 class iter_advance_functor;
61 struct xpath_context_impl;
62 
63 } // namespace impl
64 
65 /**
66  This class implements a view of XML nodes. A @em view is a container-like
67  class that only allows access to a subset of xml::node's child nodes. The
68  exact content depends on how the view was obtained; typical uses are
69  e.g. a view of all element children or all elements with a given name.
70 
71  The nodes_view class implements the same container interface that
72  xml::node does: it has begin() and end() methods.
73 
74  @since 0.6.0
75 
76  @see xml::node::elements(), xml::node::elements(const char *)
77  */
78 class XMLWRAPP_API nodes_view
79 {
80 public:
81  /// Size type.
82  typedef std::size_t size_type;
83 
84  nodes_view() : data_begin_(0), advance_func_(0) {}
85  nodes_view(const nodes_view& other);
86  ~nodes_view();
87 
88  nodes_view& operator=(const nodes_view& other);
89 
90  class const_iterator;
91 
92  /**
93  The iterator provides a way to access nodes in the view
94  similar to a standard C++ container.
95 
96  As xml::node::iterator itself, this is only a forward iterator.
97  */
98  class iterator
99  {
100  public:
101  typedef node value_type;
102  typedef int difference_type;
103  typedef value_type* pointer;
104  typedef value_type& reference;
105  typedef std::forward_iterator_tag iterator_category;
106 
107  iterator() : pimpl_(0), advance_func_(0) {}
108  iterator(const iterator& other);
109  iterator& operator=(const iterator& other);
110  ~iterator();
111 
112  reference operator*() const;
113  pointer operator->() const;
114 
115  iterator& operator++();
116  iterator operator++(int);
117 
118  private:
119  explicit iterator(void *data, impl::iter_advance_functor *advance_func);
120  void* get_raw_node() const;
121  void swap(iterator& other);
122 
123  impl::nipimpl *pimpl_;
124  // function for advancing the iterator (note that it is "owned" by the
125  // parent view object, so we don't have to care about its reference
126  // count here)
127  impl::iter_advance_functor *advance_func_;
128 
129  friend class nodes_view;
130  friend class const_iterator;
131  friend bool XMLWRAPP_API operator==(const iterator& lhs, const iterator& rhs);
132  };
133 
134  /**
135  The const_iterator provides a way to access nodes in the view
136  similar to a standard C++ container. The nodes that are pointed to by
137  the iterator cannot be changed.
138 
139  As xml::node::const_iterator itself, this is only a forward iterator.
140  */
142  {
143  public:
144  typedef const node value_type;
145  typedef int difference_type;
146  typedef value_type* pointer;
147  typedef value_type& reference;
148  typedef std::forward_iterator_tag iterator_category;
149 
150  const_iterator() : pimpl_(0), advance_func_(0) {}
151  const_iterator(const const_iterator& other);
152  const_iterator(const iterator& other);
153  const_iterator& operator=(const const_iterator& other);
154  const_iterator& operator=(const iterator& other);
155  ~const_iterator();
156 
157  reference operator*() const;
158  pointer operator->() const;
159 
160  const_iterator& operator++();
161  const_iterator operator++(int);
162 
163  private:
164  explicit const_iterator(void *data, impl::iter_advance_functor *advance_func);
165  void* get_raw_node() const;
166  void swap(const_iterator& other);
167 
168  impl::nipimpl *pimpl_;
169  // function for advancing the iterator (note that it is "owned" by the
170  // parent view object, so we don't have to care about its reference
171  // count here)
172  impl::iter_advance_functor *advance_func_;
173 
174  friend class const_nodes_view;
175  friend class nodes_view;
176  friend bool XMLWRAPP_API operator==(const const_iterator& lhs, const const_iterator& rhs);
177  };
178 
179  /// Get an iterator that points to the beginning of this view's nodes.
180  iterator begin() { return iterator(data_begin_, advance_func_); }
181 
182  /// Get an iterator that points to the beginning of this view's nodes.
183  const_iterator begin() const { return const_iterator(data_begin_, advance_func_); }
184 
185  /// Get an iterator that points one past the last child for this view.
186  iterator end() { return iterator(); }
187 
188  /// Get an iterator that points one past the last child for this view.
189  const_iterator end() const { return const_iterator(); }
190 
191  /// Returns the number of nodes in this view.
192  size_type size() const;
193 
194  /// Is the view empty?
195  bool empty() const { return !data_begin_; }
196 
197  /**
198  Erase the node that is pointed to by the given iterator.
199 
200  The node and all its children will be removed from its parent node.
201  This will invalidate any iterators that point to the node to be erased,
202  or any pointers or references to that node (but not any other iterators).
203 
204  @param to_erase An iterator that points to the node to be erased.
205  @return An iterator that points to the next node in this view
206  after the one being erased or end() if there are none.
207 
208  @since 0.8.0
209  */
210  iterator erase(const iterator& to_erase);
211 
212  /**
213  Erase all nodes in the given range, from first to last.
214 
215  This will invalidate any iterators that point to the nodes to be
216  erased, or any pointers or references to those nodes.
217 
218  @param first The first node in the range to be removed.
219  @param last An iterator that points one past the last node to erase. Think xml::node::end().
220 
221  @return An iterator that points to the next node in this view
222  after the ones being erased or end() if there are none.
223 
224  @since 0.8.0
225  */
226  iterator erase(iterator first, const iterator& last);
227 
228 private:
229  explicit nodes_view(void *data_begin, impl::iter_advance_functor *advance_func)
230  : data_begin_(data_begin), advance_func_(advance_func) {}
231 
232  // begin iterator
233  void *data_begin_;
234  // function for advancing the iterator (owned by the view object)
235  impl::iter_advance_functor *advance_func_;
236 
237  friend class node;
238  friend struct impl::xpath_context_impl;
239  friend class const_nodes_view;
240 };
241 
242 
243 /**
244  This class implements a @em read-only view of XML nodes. The only
245  difference from xml::nodes_view is that it doesn't allow modifications of
246  the nodes, it is otherwise identical.
247 
248  @see nodes_view
249 
250  @since 0.6.0
251  */
252 class XMLWRAPP_API const_nodes_view
253 {
254 public:
255  /// Size type.
256  typedef std::size_t size_type;
257 
258  const_nodes_view() : data_begin_(0), advance_func_(0) {}
259  const_nodes_view(const const_nodes_view& other);
260  const_nodes_view(const nodes_view& other);
261  ~const_nodes_view();
262 
263  const_nodes_view& operator=(const const_nodes_view& other);
264  const_nodes_view& operator=(const nodes_view& other);
265 
268 
269  /// Get an iterator that points to the beginning of this view's nodes.
270  const_iterator begin() const
271  { return const_iterator(data_begin_, advance_func_); }
272 
273  /// Get an iterator that points one past the last child for this view.
274  const_iterator end() const { return const_iterator(); }
275 
276  /// Returns the number of nodes in this view.
277  size_type size() const;
278 
279  /// Is the view empty?
280  bool empty() const { return !data_begin_; }
281 
282 private:
283  explicit const_nodes_view(void *data_begin, impl::iter_advance_functor *advance_func)
284  : data_begin_(data_begin), advance_func_(advance_func) {}
285 
286  // begin iterator
287  void *data_begin_;
288  // function for advancing the iterator (owned by the view object)
289  impl::iter_advance_functor *advance_func_;
290 
291  friend class node;
292  friend struct impl::xpath_context_impl;
293 };
294 
295 // Comparison operators for xml::[const_]nodes_view iterators
296 
297 inline bool XMLWRAPP_API operator==(const nodes_view::iterator& lhs,
298  const nodes_view::iterator& rhs)
299  { return lhs.get_raw_node() == rhs.get_raw_node(); }
300 inline bool XMLWRAPP_API operator!=(const nodes_view::iterator& lhs,
301  const nodes_view::iterator& rhs)
302  { return !(lhs == rhs); }
303 
304 inline bool XMLWRAPP_API operator==(const nodes_view::const_iterator& lhs,
305  const nodes_view::const_iterator& rhs)
306  { return lhs.get_raw_node() == rhs.get_raw_node(); }
307 inline bool XMLWRAPP_API operator!=(const nodes_view::const_iterator& lhs,
308  const nodes_view::const_iterator& rhs)
309  { return !(lhs == rhs); }
310 
311 } // end xml namespace
312 
313 #endif // _xmlwrapp_nodes_view_h_
This file contains the definition of the xml::init class.
const_iterator begin() const
Get an iterator that points to the beginning of this view&#39;s nodes.
Definition: nodes_view.h:183
const_iterator begin() const
Get an iterator that points to the beginning of this view&#39;s nodes.
Definition: nodes_view.h:270
const_iterator end() const
Get an iterator that points one past the last child for this view.
Definition: nodes_view.h:189
iterator begin()
Get an iterator that points to the beginning of this view&#39;s nodes.
Definition: nodes_view.h:180
This class implements a read-only view of XML nodes.
Definition: nodes_view.h:252
std::size_t size_type
Size type.
Definition: nodes_view.h:256
XML library namespace.
Definition: attributes.h:51
bool empty() const
Is the view empty?
Definition: nodes_view.h:280
std::size_t size_type
Size type.
Definition: nodes_view.h:82
The xml::node class is used to hold information about one XML node.
Definition: node.h:88
The iterator provides a way to access nodes in the view similar to a standard C++ container...
Definition: nodes_view.h:98
This class implements a view of XML nodes.
Definition: nodes_view.h:78
const_iterator end() const
Get an iterator that points one past the last child for this view.
Definition: nodes_view.h:274
The const_iterator provides a way to access nodes in the view similar to a standard C++ container...
Definition: nodes_view.h:141
iterator end()
Get an iterator that points one past the last child for this view.
Definition: nodes_view.h:186
bool empty() const
Is the view empty?
Definition: nodes_view.h:195