1 // ----------------------------------------------------------------------------
2 // Copyright (C) 2002-2006 Marcin Kalicinski
3 // Copyright (C) 2009 Sebastian Redl
5 // Distributed under the Boost Software License, Version 1.0.
6 // (See accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
9 // For more information, see www.boost.org
10 // ----------------------------------------------------------------------------
12 #ifndef BOOST_PROPERTY_TREE_PTREE_HPP_INCLUDED
13 #define BOOST_PROPERTY_TREE_PTREE_HPP_INCLUDED
15 #include <boost/property_tree/ptree_fwd.hpp>
16 #include <boost/property_tree/string_path.hpp>
17 #include <boost/property_tree/stream_translator.hpp>
18 #include <boost/property_tree/exceptions.hpp>
19 #include <boost/property_tree/detail/ptree_utils.hpp>
21 #include <boost/multi_index_container.hpp>
22 #include <boost/multi_index/indexed_by.hpp>
23 #include <boost/multi_index/sequenced_index.hpp>
24 #include <boost/multi_index/ordered_index.hpp>
25 #include <boost/multi_index/member.hpp>
26 #include <boost/utility/enable_if.hpp>
27 #include <boost/throw_exception.hpp>
28 #include <boost/optional.hpp>
29 #include <utility> // for std::pair
31 namespace boost { namespace property_tree
35 * Property tree main structure. A property tree is a hierarchical data
36 * structure which has one element of type @p Data in each node, as well
37 * as an ordered sequence of sub-nodes, which are additionally identified
38 * by a non-unique key of type @p Key.
40 * Key equivalency is defined by @p KeyCompare, a predicate defining a
41 * strict weak ordering.
43 * Property tree defines a Container-like interface to the (key-node) pairs
44 * of its direct sub-nodes. The iterators are bidirectional. The sequence
45 * of nodes is held in insertion order, not key order.
47 template<class Key, class Data, class KeyCompare>
50 #if defined(BOOST_PROPERTY_TREE_DOXYGEN_INVOKED)
55 * Simpler way to refer to this basic_ptree\<C,K,P,A\> type.
56 * Note that this is private, and made public only for doxygen.
58 typedef basic_ptree<Key, Data, KeyCompare> self_type;
63 typedef Data data_type;
64 typedef KeyCompare key_compare;
66 // Container view types
67 typedef std::pair<const Key, self_type> value_type;
68 typedef std::size_t size_type;
70 // The problem with the iterators is that I can't make them complete
71 // until the container is complete. Sucks. Especially for the reverses.
74 class reverse_iterator;
75 class const_reverse_iterator;
77 // Associative view types
79 class const_assoc_iterator;
81 // Property tree view types
82 typedef typename path_of<Key>::type path_type;
87 /** Creates a node with no children and default-constructed data. */
89 /** Creates a node with no children and a copy of the given data. */
90 explicit basic_ptree(const data_type &data);
91 basic_ptree(const self_type &rhs);
93 /** Basic guarantee only. */
94 self_type &operator =(const self_type &rhs);
96 /** Swap with other tree. Only constant-time and nothrow if the
97 * data type's swap is.
99 void swap(self_type &rhs);
101 // Container view functions
103 /** The number of direct children of this node. */
104 size_type size() const;
105 size_type max_size() const;
106 /** Whether there are any direct children. */
110 const_iterator begin() const;
112 const_iterator end() const;
113 reverse_iterator rbegin();
114 const_reverse_iterator rbegin() const;
115 reverse_iterator rend();
116 const_reverse_iterator rend() const;
119 const value_type &front() const;
121 const value_type &back() const;
123 /** Insert a copy of the given tree with its key just before the given
124 * position in this node. This operation invalidates no iterators.
125 * @return An iterator to the newly created child.
127 iterator insert(iterator where, const value_type &value);
129 /** Range insert. Equivalent to:
131 * for(; first != last; ++first) insert(where, *first);
134 template<class It> void insert(iterator where, It first, It last);
136 /** Erase the child pointed at by the iterator. This operation
137 * invalidates the given iterator, as well as its equivalent
139 * @return A valid iterator pointing to the element after the erased.
141 iterator erase(iterator where);
143 /** Range erase. Equivalent to:
145 * while(first != last;) first = erase(first);
148 iterator erase(iterator first, iterator last);
150 /** Equivalent to insert(begin(), value). */
151 iterator push_front(const value_type &value);
153 /** Equivalent to insert(end(), value). */
154 iterator push_back(const value_type &value);
156 /** Equivalent to erase(begin()). */
159 /** Equivalent to erase(boost::prior(end())). */
162 /** Reverses the order of direct children in the property tree. */
165 /** Sorts the direct children of this node according to the predicate.
166 * The predicate is passed the whole pair of key and child.
168 template<class Compare> void sort(Compare comp);
170 /** Sorts the direct children of this node according to key order. */
175 /** Two property trees are the same if they have the same data, the keys
176 * and order of their children are the same, and the children compare
177 * equal, recursively.
179 bool operator ==(const self_type &rhs) const;
180 bool operator !=(const self_type &rhs) const;
184 /** Returns an iterator to the first child, in key order. */
185 assoc_iterator ordered_begin();
186 /** Returns an iterator to the first child, in key order. */
187 const_assoc_iterator ordered_begin() const;
189 /** Returns the not-found iterator. Equivalent to end() in a real
190 * associative container.
192 assoc_iterator not_found();
193 /** Returns the not-found iterator. Equivalent to end() in a real
194 * associative container.
196 const_assoc_iterator not_found() const;
198 /** Find a child with the given key, or not_found() if there is none.
199 * There is no guarantee about which child is returned if multiple have
202 assoc_iterator find(const key_type &key);
204 /** Find a child with the given key, or not_found() if there is none.
205 * There is no guarantee about which child is returned if multiple have
208 const_assoc_iterator find(const key_type &key) const;
210 /** Find the range of children that have the given key. */
211 std::pair<assoc_iterator, assoc_iterator>
212 equal_range(const key_type &key);
214 /** Find the range of children that have the given key. */
215 std::pair<const_assoc_iterator, const_assoc_iterator>
216 equal_range(const key_type &key) const;
218 /** Count the number of direct children with the given key. */
219 size_type count(const key_type &key) const;
221 /** Erase all direct children with the given key and return the count.
223 size_type erase(const key_type &key);
225 /** Get the iterator that points to the same element as the argument.
226 * @note A valid assoc_iterator range (a, b) does not imply that
227 * (to_iterator(a), to_iterator(b)) is a valid range.
229 iterator to_iterator(assoc_iterator it);
231 /** Get the iterator that points to the same element as the argument.
232 * @note A valid const_assoc_iterator range (a, b) does not imply that
233 * (to_iterator(a), to_iterator(b)) is a valid range.
235 const_iterator to_iterator(const_assoc_iterator it) const;
237 // Property tree view
239 /** Reference to the actual data in this node. */
242 /** Reference to the actual data in this node. */
243 const data_type &data() const;
245 /** Clear this tree completely, of both data and children. */
248 /** Get the child at the given path, or throw @c ptree_bad_path.
249 * @note Depending on the path, the result at each level may not be
250 * completely deterministic, i.e. if the same key appears multiple
251 * times, which child is chosen is not specified. This can lead
252 * to the path not being resolved even though there is a
253 * descendant with this path. Example:
258 * The path "a.b.c" will succeed if the resolution of "b" chooses
259 * the first such node, but fail if it chooses the second.
261 self_type &get_child(const path_type &path);
263 /** Get the child at the given path, or throw @c ptree_bad_path. */
264 const self_type &get_child(const path_type &path) const;
266 /** Get the child at the given path, or return @p default_value. */
267 self_type &get_child(const path_type &path, self_type &default_value);
269 /** Get the child at the given path, or return @p default_value. */
270 const self_type &get_child(const path_type &path,
271 const self_type &default_value) const;
273 /** Get the child at the given path, or return boost::null. */
274 optional<self_type &> get_child_optional(const path_type &path);
276 /** Get the child at the given path, or return boost::null. */
277 optional<const self_type &>
278 get_child_optional(const path_type &path) const;
280 /** Set the node at the given path to the given value. Create any
281 * missing parents. If the node at the path already exists, replace it.
282 * @return A reference to the inserted subtree.
283 * @note Because of the way paths work, it is not generally guaranteed
284 * that a node newly created can be accessed using the same path.
285 * @note If the path could refer to multiple nodes, it is unspecified
286 * which one gets replaced.
288 self_type &put_child(const path_type &path, const self_type &value);
290 /** Add the node at the given path. Create any missing parents. If there
291 * already is a node at the path, add another one with the same key.
292 * @param path Path to the child. The last fragment must not have an
294 * @return A reference to the inserted subtree.
295 * @note Because of the way paths work, it is not generally guaranteed
296 * that a node newly created can be accessed using the same path.
298 self_type &add_child(const path_type &path, const self_type &value);
300 /** Take the value of this node and attempt to translate it to a
301 * @c Type object using the supplied translator.
302 * @throw ptree_bad_data if the conversion fails.
304 template<class Type, class Translator>
305 typename boost::enable_if<detail::is_translator<Translator>, Type>::type
306 get_value(Translator tr) const;
308 /** Take the value of this node and attempt to translate it to a
309 * @c Type object using the default translator.
310 * @throw ptree_bad_data if the conversion fails.
313 Type get_value() const;
315 /** Take the value of this node and attempt to translate it to a
316 * @c Type object using the supplied translator. Return @p default_value
319 template<class Type, class Translator>
320 Type get_value(const Type &default_value, Translator tr) const;
322 /** Make get_value do the right thing for string literals. */
323 template <class Ch, class Translator>
324 typename boost::enable_if<
325 detail::is_character<Ch>,
326 std::basic_string<Ch>
328 get_value(const Ch *default_value, Translator tr) const;
330 /** Take the value of this node and attempt to translate it to a
331 * @c Type object using the default translator. Return @p default_value
335 typename boost::disable_if<detail::is_translator<Type>, Type>::type
336 get_value(const Type &default_value) const;
338 /** Make get_value do the right thing for string literals. */
340 typename boost::enable_if<
341 detail::is_character<Ch>,
342 std::basic_string<Ch>
344 get_value(const Ch *default_value) const;
346 /** Take the value of this node and attempt to translate it to a
347 * @c Type object using the supplied translator. Return boost::null if
350 template<class Type, class Translator>
351 optional<Type> get_value_optional(Translator tr) const;
353 /** Take the value of this node and attempt to translate it to a
354 * @c Type object using the default translator. Return boost::null if
358 optional<Type> get_value_optional() const;
360 /** Replace the value at this node with the given value, translated
361 * to the tree's data type using the supplied translator.
362 * @throw ptree_bad_data if the conversion fails.
364 template<class Type, class Translator>
365 void put_value(const Type &value, Translator tr);
367 /** Replace the value at this node with the given value, translated
368 * to the tree's data type using the default translator.
369 * @throw ptree_bad_data if the conversion fails.
372 void put_value(const Type &value);
374 /** Shorthand for get_child(path).get_value(tr). */
375 template<class Type, class Translator>
376 typename boost::enable_if<detail::is_translator<Translator>, Type>::type
377 get(const path_type &path, Translator tr) const;
379 /** Shorthand for get_child(path).get_value\<Type\>(). */
381 Type get(const path_type &path) const;
383 /** Shorthand for get_child(path, empty_ptree())
384 * .get_value(default_value, tr).
385 * That is, return the translated value if possible, and the default
386 * value if the node doesn't exist or conversion fails.
388 template<class Type, class Translator>
389 Type get(const path_type &path,
390 const Type &default_value,
391 Translator tr) const;
393 /** Make get do the right thing for string literals. */
394 template <class Ch, class Translator>
395 typename boost::enable_if<
396 detail::is_character<Ch>,
397 std::basic_string<Ch>
399 get(const path_type &path, const Ch *default_value, Translator tr)const;
401 /** Shorthand for get_child(path, empty_ptree())
402 * .get_value(default_value).
403 * That is, return the translated value if possible, and the default
404 * value if the node doesn't exist or conversion fails.
407 typename boost::disable_if<detail::is_translator<Type>, Type>::type
408 get(const path_type &path, const Type &default_value) const;
410 /** Make get do the right thing for string literals. */
412 typename boost::enable_if<
413 detail::is_character<Ch>,
414 std::basic_string<Ch>
416 get(const path_type &path, const Ch *default_value) const;
420 * if(optional\<self_type&\> node = get_child_optional(path))
421 * return node->get_value_optional(tr);
422 * return boost::null;
424 * That is, return the value if it exists and can be converted, or nil.
426 template<class Type, class Translator>
427 optional<Type> get_optional(const path_type &path, Translator tr) const;
431 * if(optional\<const self_type&\> node = get_child_optional(path))
432 * return node->get_value_optional();
433 * return boost::null;
435 * That is, return the value if it exists and can be converted, or nil.
438 optional<Type> get_optional(const path_type &path) const;
440 /** Set the value of the node at the given path to the supplied value,
441 * translated to the tree's data type. If the node doesn't exist, it is
442 * created, including all its missing parents.
443 * @return The node that had its value changed.
444 * @throw ptree_bad_data if the conversion fails.
446 template<class Type, class Translator>
447 self_type &put(const path_type &path, const Type &value, Translator tr);
449 /** Set the value of the node at the given path to the supplied value,
450 * translated to the tree's data type. If the node doesn't exist, it is
451 * created, including all its missing parents.
452 * @return The node that had its value changed.
453 * @throw ptree_bad_data if the conversion fails.
456 self_type &put(const path_type &path, const Type &value);
458 /** If the node identified by the path does not exist, create it,
459 * including all its missing parents.
460 * If the node already exists, add a sibling with the same key.
461 * Set the newly created node's value to the given paremeter,
462 * translated with the supplied translator.
463 * @param path Path to the child. The last fragment must not have an
465 * @param value The value to add.
466 * @param tr The translator to use.
467 * @return The node that was added.
468 * @throw ptree_bad_data if the conversion fails.
470 template<class Type, class Translator>
471 self_type &add(const path_type &path,
475 /** If the node identified by the path does not exist, create it,
476 * including all its missing parents.
477 * If the node already exists, add a sibling with the same key.
478 * Set the newly created node's value to the given paremeter,
479 * translated with the supplied translator.
480 * @param path Path to the child. The last fragment must not have an
482 * @param value The value to add.
483 * @return The node that was added.
484 * @throw ptree_bad_data if the conversion fails.
487 self_type &add(const path_type &path, const Type &value);
490 // Hold the data of this node
492 // Hold the children - this is a void* because we can't complete the
493 // container type within the class.
496 // Getter tree-walk. Not const-safe! Gets the node the path refers to,
497 // or null. Destroys p's value.
498 self_type* walk_path(path_type& p) const;
500 // Modifer tree-walk. Gets the parent of the node referred to by the
501 // path, creating nodes as necessary. p is the path to the remaining
503 self_type& force_path(path_type& p);
505 // This struct contains typedefs for the concrete types.
508 friend class iterator;
509 friend class const_iterator;
510 friend class reverse_iterator;
511 friend class const_reverse_iterator;
516 #include <boost/property_tree/detail/ptree_implementation.hpp>