xmlwrapp
Lightweight C++ XML parsing library
Loading...
Searching...
No Matches
attributes.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2001-2003 Peter J Jones (pjones@pmade.org)
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::attributes class.
37 */
38
39#ifndef _xmlwrapp_attributes_h_
40#define _xmlwrapp_attributes_h_
41
42// xmlwrapp includes
43#include "xmlwrapp/init.h"
44#include "xmlwrapp/export.h"
45
46// standard includes
47#include <cstddef>
48#include <iosfwd>
49#include <memory>
50#include <string>
51
52XMLWRAPP_MSVC_SUPPRESS_DLL_MEMBER_WARN
53
54namespace xml
55{
56
57// forward declarations
58class node;
59
60namespace impl
61{
62class ait_impl;
63struct node_impl;
64}
65
66/**
67 The xml::attributes class is used to access all the attributes of one
68 xml::node. You can add, find and erase attributes by name, and for some
69 member functions, use the provided iterator classes.
70
71 The iterator classes allow you to access one XML attribute. This is done
72 using the xml::attributes::attr class interface.
73 */
74class XMLWRAPP_API attributes
75{
76public:
77 /// size type
78 using size_type = std::size_t;
79
80 /**
81 Create a new xml::attributes object with no attributes.
82 */
84
85 /**
86 Copy construct a xml::attributes object.
87
88 @param other The xml::attributes object to copy from.
89 */
90 attributes(const attributes& other);
91
92 /**
93 Copy the given xml::attributes object into this one.
94
95 @param other The xml::attributes object to copy from.
96 @return *this.
97 */
99
100 /**
101 Swap this xml::attributes object with another one.
102
103 @param other The other xml::attributes object to swap with.
104 */
105 void swap(attributes& other);
106
107 ~attributes();
108
109 // forward declarations
110 class const_iterator;
111
112 /**
113 The xml::attributes::attr class is used to hold information about one
114 attribute.
115 */
116 class XMLWRAPP_API attr
117 {
118 public:
119 /**
120 Get the name of this attribute.
121
122 @return The name for this attribute.
123 */
124 const char *get_name() const;
125
126 /**
127 Get the value of this attribute.
128
129 @return The value for this attribute.
130 */
131 const char* get_value() const;
132
133 private:
134 void *node_{nullptr};
135 void *prop_{nullptr};
136
137 std::string name_;
138 mutable std::string value_;
139
140 attr() = default;
141 attr(const attr& other) = default;
142 attr& operator=(const attr& other) = default;
143 void swap(attr& other);
144
145 void set_data(void *node, void *prop);
146 void set_data(const char *name, const char *value, bool);
147
148 friend class impl::ait_impl;
149 };
150
151 /**
152 Iterator class for accessing attribute pairs.
153 */
154 class XMLWRAPP_API iterator
155 {
156 public:
157 using value_type = attr;
158 using difference_type = std::ptrdiff_t;
159 using pointer = value_type *;
160 using reference = value_type &;
161 using iterator_category = std::forward_iterator_tag;
162
163 iterator();
164 iterator(const iterator& other);
165 iterator& operator=(const iterator& other);
166 ~iterator();
167
168 reference operator*() const;
169 pointer operator->() const;
170
171 /// prefix increment
173
174 /// postfix increment (avoid if possible for better performance)
176
177 friend bool XMLWRAPP_API operator==(const iterator& lhs, const iterator& rhs);
178 friend bool XMLWRAPP_API operator!=(const iterator& lhs, const iterator& rhs);
179
180 private:
181 std::unique_ptr<impl::ait_impl> pimpl_;
182
183 iterator(void *node, void *prop);
184 iterator(const char *name, const char *value, bool);
185 void swap(iterator& other);
186 void* get_raw_attr();
187
188 friend class attributes;
189 friend class const_iterator;
190 };
191
192 /**
193 Const Iterator class for accessing attribute pairs.
194 */
195 class XMLWRAPP_API const_iterator
196 {
197 public:
198 using value_type = const attr;
199 using difference_type = std::ptrdiff_t;
200 using pointer = value_type *;
201 using reference = value_type &;
202 using iterator_category = std::forward_iterator_tag;
203
205 const_iterator(const const_iterator& other);
206 const_iterator(const iterator& other);
207 const_iterator& operator=(const const_iterator& other);
209
210 reference operator*() const;
211 pointer operator->() const;
212
213 /// prefix increment
215
216 /// postfix increment (avoid if possible better for performance)
217 const_iterator operator++ (int);
218
219 friend bool XMLWRAPP_API operator== (const const_iterator &lhs, const const_iterator &rhs);
220 friend bool XMLWRAPP_API operator!= (const const_iterator &lhs, const const_iterator &rhs);
221
222 private:
223 std::unique_ptr<impl::ait_impl> pimpl_;
224
225 const_iterator(void *node, void *prop);
226 const_iterator(const char *name, const char *value, bool);
227 void swap(const_iterator &other);
228 void* get_raw_attr();
229
230 friend class attributes;
231 };
232
233 /**
234 Get an iterator that points to the first attribute.
235
236 @return An iterator that points to the first attribute.
237 @return An iterator equal to end() if there are no attributes.
238 @see xml::attributes::iterator
239 @see xml::attributes::attr
240 */
242
243 /**
244 Get a const_iterator that points to the first attribute.
245
246 @return A const_iterator that points to the first attribute.
247 @return A const_iterator equal to end() if there are no attributes.
248 @see xml::attributes::const_iterator
249 @see xml::attributes::attr
250 */
252
253 /**
254 Get an iterator that points one past the the last attribute.
255
256 @return An "end" iterator.
257 */
259
260 /**
261 Get a const_iterator that points one past the last attribute.
262
263 @return An "end" const_iterator.
264 */
266
267 /**
268 Add an attribute to the attributes list. If there is another
269 attribute with the same name, it will be replaced with this one.
270
271 @param name The name of the attribute to add.
272 @param value The value of the attribute to add.
273 */
274 void insert(const char *name, const char *value);
275
276 /**
277 Find the attribute with the given name. If the attribute is not found
278 on the current node, the DTD will be searched for a default value.
279 This is, of course, if there was a DTD parsed with the XML document.
280
281 @param name The name of the attribute to find.
282 @return An iterator that points to the attribute with the given name.
283 @return If the attribute was not found, find will return end().
284 @see xml::attributes::iterator
285 @see xml::attributes::attr
286 */
287 iterator find(const char *name);
288
289 /**
290 Find the attribute with the given name. If the attribute is not found
291 on the current node, the DTD will be searched for a default value.
292 This is, of course, if there was a DTD parsed with the XML document.
293
294 @param name The name of the attribute to find.
295 @return A const_iterator that points to the attribute with the given name.
296 @return If the attribute was not found, find will return end().
297 @see xml::attributes::const_iterator
298 @see xml::attributes::attr
299 */
300 const_iterator find(const char *name) const;
301
302 /**
303 Erase the attribute that is pointed to by the given iterator. This
304 will invalidate any iterators for this attribute, as well as any
305 pointers or references to it.
306
307 @param to_erase An iterator that points to the attribute to erased.
308 @return An iterator that points to the attribute after the one to be erased.
309 @see xml::attributes::iterator
310 @see xml::attributes::attr
311 */
313
314 /**
315 Erase the attribute with the given name. This will invalidate any
316 iterators that are pointing to that attribute, as well as any
317 pointers or references to that attribute.
318
319 @param name The name of the attribute to erase.
320 */
321 void erase(const char *name);
322
323 /**
324 Find out if there are any attributes in this xml::attributes object.
325
326 @return True if there are no attributes.
327 @return False if there is at least one attribute.
328 */
329 bool empty() const;
330
331 /**
332 Find out how many attributes there are in this xml::attributes
333 object.
334
335 @return The number of attributes in this xml::attributes object.
336 */
338
339private:
340 struct pimpl;
341 std::unique_ptr<pimpl> pimpl_;
342
343 // private ctor to create uninitialized instance
344 explicit attributes (int);
345
346 void set_data (void *node);
347 void* get_data();
348 friend struct impl::node_impl;
349 friend class node;
350};
351
352} // namespace xml
353
354XMLWRAPP_MSVC_RESTORE_DLL_MEMBER_WARN
355
356#endif // _xmlwrapp_attributes_h_
The xml::attributes::attr class is used to hold information about one attribute.
Definition attributes.h:117
const char * get_name() const
Get the name of this attribute.
const char * get_value() const
Get the value of this attribute.
Const Iterator class for accessing attribute pairs.
Definition attributes.h:196
const_iterator & operator++()
prefix increment
Iterator class for accessing attribute pairs.
Definition attributes.h:155
iterator operator++(int)
postfix increment (avoid if possible for better performance)
iterator & operator++()
prefix increment
The xml::attributes class is used to access all the attributes of one xml::node.
Definition attributes.h:75
size_type size() const
Find out how many attributes there are in this xml::attributes object.
iterator begin()
Get an iterator that points to the first attribute.
attributes(const attributes &other)
Copy construct a xml::attributes object.
void swap(attributes &other)
Swap this xml::attributes object with another one.
iterator erase(iterator to_erase)
Erase the attribute that is pointed to by the given iterator.
const_iterator find(const char *name) const
Find the attribute with the given name.
const_iterator begin() const
Get a const_iterator that points to the first attribute.
iterator find(const char *name)
Find the attribute with the given name.
void erase(const char *name)
Erase the attribute with the given name.
iterator end()
Get an iterator that points one past the the last attribute.
attributes & operator=(const attributes &other)
Copy the given xml::attributes object into this one.
const_iterator end() const
Get a const_iterator that points one past the last attribute.
attributes()
Create a new xml::attributes object with no attributes.
bool empty() const
Find out if there are any attributes in this xml::attributes object.
std::size_t size_type
size type
Definition attributes.h:78
void insert(const char *name, const char *value)
Add an attribute to the attributes list.
The xml::node class is used to hold information about one XML node.
Definition node.h:92
XML library namespace.
Definition attributes.h:55
This file contains the definition of the xml::init class.