fix doc example typo
[boost.git] / boost / property_tree / ptree.hpp
blob1e2956cb005936c139c723330c2fc91ddb9ef939
1 // ----------------------------------------------------------------------------
2 // Copyright (C) 2002-2006 Marcin Kalicinski
3 //
4 // Distributed under the Boost Software License, Version 1.0.
5 // (See accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
7 //
8 // For more information, see www.boost.org
9 // ----------------------------------------------------------------------------
11 /// This header contains definition of basic_ptree class template and supporting definitions.
13 #ifndef BOOST_PROPERTY_TREE_PTREE_HPP_INCLUDED
14 #define BOOST_PROPERTY_TREE_PTREE_HPP_INCLUDED
16 #include <boost/property_tree/ptree_fwd.hpp> // Must be the first include, because of config.hpp
18 #include <boost/assert.hpp>
19 #include <boost/optional.hpp>
20 #include <boost/static_assert.hpp>
21 #include <boost/type_traits/is_pointer.hpp>
22 #include <boost/type_traits/is_same.hpp>
23 #include <boost/type_traits/remove_const.hpp>
24 #include <boost/type_traits/remove_pointer.hpp>
25 #include <boost/any.hpp>
26 #include <boost/throw_exception.hpp>
28 #ifdef BOOST_PROPERTY_TREE_DEBUG
29 # include <boost/detail/lightweight_mutex.hpp> // For syncing debug instances counter
30 #endif
32 #include <functional> // for std::less
33 #include <limits>
34 #include <list>
35 #include <sstream>
36 #include <stdexcept>
37 #include <utility> // for std::pair
38 #include <vector>
39 #include <cstdlib>
41 #if !defined(BOOST_PROPERTY_TREE_DOXYGEN_INVOKED)
42 // Throwing macro to avoid no return warnings portably
43 # define BOOST_PROPERTY_TREE_THROW(e) { throw_exception(e); std::exit(1); }
44 #endif
46 namespace boost { namespace property_tree
49 /**
50 * Class template implementing property tree.
52 * A property tree can have data associated with it
53 * along with a sequence of @c (key,basic_ptree) children.
54 * Iterators provide bidirectional iterative access into children sequence.
56 * @tparam C Key comparison type
57 * @tparam K Key type
58 * @tparam P Path type
59 * @tparam D Data type
60 * @tparam X Translator type to use for converting values to and from std::string
62 template<class C, class K, class P, class D, class X>
63 class basic_ptree
65 #if defined(BOOST_PROPERTY_TREE_DOXYGEN_INVOKED)
66 public:
67 #endif
68 // Internal types
69 /**
70 * Simpler way to refer to this basic_ptree<C,K,P,D,X> type.
71 * Do not use in client code; exposed only for documentation purposes.
73 typedef basic_ptree<C, K, P, D, X> self_type;
75 public:
76 // Basic types
77 typedef C key_compare;
78 typedef K key_type;
79 typedef P path_type;
80 typedef D data_type;
81 typedef X translator_type;
83 /**
84 * Property tree stores a sequence of values of this type.
86 * The first element is the key and the second is the child property tree
87 * stored at this key.
89 typedef std::pair<key_type, self_type> value_type;
91 private:
92 // Internal types
93 typedef std::list<value_type> container_type;
95 public:
96 // Container-related types
97 typedef typename container_type::size_type size_type;
98 typedef typename container_type::iterator iterator;
99 typedef typename container_type::const_iterator const_iterator;
100 typedef typename container_type::reverse_iterator reverse_iterator;
101 typedef typename container_type::const_reverse_iterator const_reverse_iterator;
103 public:
104 ///////////////////////////////////////////////////////////////////////////
105 // Construction & destruction
107 /** Creates an empty property tree. */
108 basic_ptree();
111 * Creates a property_tree with the given data.
112 * @param data Data to be assigned to the tree's data field.
114 explicit basic_ptree(const data_type &data);
117 * Copy constructor from another property_tree.
118 * @param rhs The property tree to be copied.
120 basic_ptree(const self_type &rhs);
122 /** Destroys the property tree including recusively destoying all children. */
123 ~basic_ptree();
125 ///////////////////////////////////////////////////////////////////////////
126 // Iterator access
129 * Access to the start of the direct children sequence.
130 * @return iterator pointing to the first element of the direct children sequence.
132 iterator begin();
135 * Const access to the start of the direct children sequence.
136 * @return const_iterator pointing to the first element of the direct children sequence.
138 const_iterator begin() const;
141 * Access to the end of the direct children sequence.
142 * @return iterator pointing just past the end of the direct children sequence.
144 iterator end();
147 * Const access to the end of the direct children sequence.
148 * @return const_iterator pointing just past the end of the direct children sequence.
150 const_iterator end() const;
153 * Access to the start of the reversed direct children sequence.
154 * @return reverse_iterator pointing to first element of the reversed direct children sequence.
156 reverse_iterator rbegin();
159 * Const access to the start of the reversed direct children sequence.
160 * @return const_reverse_iterator pointing to first element of the reversed direct children sequence.
162 const_reverse_iterator rbegin() const;
165 * Access to the end of the reverse direct children sequence.
166 * @return reverse_iterator pointing just past the end of the reversed direct children sequence.
168 reverse_iterator rend();
171 * Const access to the end of the reverse direct children sequence.
172 * @return const_reverse_iterator pointing just past the end of the reversed direct children sequence.
174 const_reverse_iterator rend() const;
176 ///////////////////////////////////////////////////////////////////////////
177 // Data access
180 * The size fo the direct children sequnce.
181 * @return Number of direct children of the property tree.
183 size_type size() const;
185 size_type max_size() const;
188 * Determine whether the children sequence is empty.
189 * @note empty() should be prefered over <tt>size() == 0</tt>
190 * @retval true There are no direct children.
191 * @retval false There is at least one direct child.
193 bool empty() const;
196 * Retruns a reference to the data of a property tree.
197 * @return Reference to the stored data which can be used to modify the data.
199 data_type &data();
202 * Returns a const reference to the data of a property tree.
203 * @return Const reference to the stored data.
205 const data_type &data() const;
208 * Returns a reference to the first element in the direct children sequence.
209 * @pre !(this->empty())
210 * @return Reference to the first element in the direct children sequence.
212 value_type &front();
215 * Returns a const reference to the first element in the direct children sequence.
216 * @pre !(this->empty())
217 * @return Const reference to the first element in the direct children sequence.
219 const value_type &front() const;
222 * Returns a reference to the last element in the direct children sequence.
223 * @pre !(this->empty())
224 * @return Reference to the last element in the direct children sequence.
226 value_type &back();
229 * Returns a const reference to the last element in the direct children sequence.
230 * @pre !(this->empty())
231 * @return Const reference to the last element in the direct children sequence.
233 const value_type &back() const;
235 ///////////////////////////////////////////////////////////////////////////
236 // Operators
239 * Replaces current contents of this property tree with another's contents.
240 * @param rhs The property tree to assign to this property tree.
242 self_type &operator =(const self_type &rhs);
245 * Check for equality of property trees.
246 * @retval true If both property trees contain the same data values and equivalent
247 * children sequences, recusively.
248 * @retval false Otherwise.
250 bool operator ==(const self_type &rhs) const;
253 * Check for inequality of property trees.
254 * @return !(*this == rhs)
256 bool operator !=(const self_type &rhs) const;
258 ///////////////////////////////////////////////////////////////////////////
259 // Container operations
262 * Finds direct child stored at specified key.
264 * If there is more than one child with the same key, the first one is
265 * returned. Time complexity is <tt>O(log n)</tt>. Keys equivalence is
266 * tested with a predicate supplied as basic_ptree template parameter.
267 * If childe is not found, returns end(). Both const and non-const
268 * versions are provided. To find non-direct children use get_child
269 * function.
271 * @param key The key to search in the direct children sequence.
272 * @return iterator pointing to the found sequence element, or end()
273 * if no such element exists.
275 iterator find(const key_type &key);
278 * Finds direct child stored at specified key.
280 * If there is more than one child with the same key, the first one is
281 * returned. Time complexity is <tt>O(log n)</tt>. Keys equivalence is
282 * tested with a predicate supplied as basic_ptree template parameter.
283 * If child is not found, returns end(). Both const and non-const
284 * versions are provided. To find non-direct children use get_child
285 * function.
287 * @param key The key to search in the direct children sequence.
288 * @return const_iterator pointing to the found sequence element,
289 * or end() if no such element exists.
291 const_iterator find(const key_type &key) const;
294 * Count the number of direct children with the given key.
296 * Keys equivalence is tested with a predicate supplied as basic_ptree
297 * template parameter.
298 * @param key Key to count.
299 * @return Number of direct children with given key.
301 size_type count(const key_type &key) const;
304 * Recursively deletes all children of the property tree and clears the
305 * data from the property tree.
307 void clear();
310 * Inserts a new direct child into the property tree.
312 * @param where Iterator pointing at the position where new element will be
313 * inserted. Passing begin() will insert at the front of the
314 * list. Passing end() will insert at the back. Any other
315 * valid iterator will insert in the appropriate place in the
316 * middle.
317 * @param value value to be inserted.
318 * @return iterator pointing to newly inserted element of the sequence.
320 iterator insert(iterator where, const value_type &value);
323 * Inserts a range of direct children into the property tree.
325 * Time complexity is <tt>O(m log n)</tt>, where @c m is number of inserted
326 * children, @c n is number of existing children.
328 * @param where Iterator pointing at the position where new elements will be
329 * inserted. Passing begin() will insert at the front of the
330 * list. Passing end() will insert at the back. Any other
331 * valid iterator will insert in the appropriate place in the
332 * middle.
333 * @param first Iterator designating the first element of range to be
334 * inserted.
335 * @param last Iterator referring to just past the end of the range to be
336 * inserted.
338 template<class It> void insert(iterator where, It first, It last);
341 * Erases a direct child from the property tree.
343 * @param where Iterator pointing at the child to be erased from the property tree.
344 * @return Iterator pointing to the element after the erased element of the sequence,
345 * or end() if the element erased was the last in the sequence.
347 iterator erase(iterator where);
350 * Erases direct children from the property tree matching the given key.
352 * @param key Key designating child or children to erase.
353 * @return Number of children that were erased.
355 size_type erase(const key_type &key);
358 * Erases a range of direct children from the property tree.
360 * @param first Iterator designating the first element of range to be
361 * erased.
362 * @param last Iterator referring to just past the end of the range to be
363 * erased.
365 template<class It> iterator erase(It first, It last);
368 * Inserts a new direct child at the front of the sequence.
370 * Equivalent to insert(begin(), value).
371 * @param value Value to be inserted.
372 * @return Iterator pointing to newly inserted element of the sequence.
374 iterator push_front(const value_type &value);
377 * Inserts a new direct child at the back of the sequence.
379 * Equivalent to insert(end(), value)
380 * @param value Value to be inserted.
381 * @return Iterator pointing to newly inserted element of the sequence.
383 iterator push_back(const value_type &value);
386 * Erases the first direct child in the sequence.
388 * Equivalent to erase(begin()).
389 * @pre !(this->empty())
391 void pop_front();
394 * Erases the last direct child in the sequence.
396 * Equivalent to erase(boost::prior(end())).
397 * @pre !(this->empty())
399 void pop_back();
402 * Swaps contents of this property tree with the contents of another.
404 * Time complexity is <tt>O(1)</tt>.
405 * @param rhs Property tree with which to swap.
407 void swap(self_type &rhs);
409 /** Reverses the order of direct children in the property tree. */
410 void reverse();
413 * Sorts direct children of the property tree in ascending order.
414 * @param tr The binary predicate used to sort child values of type @c #value_type.
415 * @post For each adjacent child of the sequence, @c v1 followed by @c v2,
416 * @c tr(v1,v2) evaluates to true.
418 template<class SortTr> void sort(SortTr tr);
420 ///////////////////////////////////////////////////////////////////////////
421 // ptree operations
424 * Get a reference to the child property tree at the given path.
426 * Traverses the tree using the given path and retrieves a child property tree
427 * stored there. This function will retrieve indirect children if the path contains
428 * at least one separator.
429 * @param path A sequence of keys with zero or more separator characters.
430 * Can indicate indirect children if path contains at least one separator
431 * character.
432 * @throw ptree_bad_path if child property tree cannot be located.
433 * @return A reference to the child property tree at the given path relative to this
434 * property tree.
436 self_type &get_child(const path_type &path);
439 * Get a const reference to the child property tree at the given path.
441 * Traverses the tree using the given path and retrieves a child property tree
442 * stored there. This function will retrieve indirect children if the path contains
443 * at least one separator.
444 * @param path A sequence of keys with zero or more separator characters.
445 * Can indicate indirect children if path contains at least one separator
446 * character.
447 * @throw ptree_bad_path if child property tree cannot be located.
448 * @return A const reference to the child property tree at the given path relative to this
449 * property tree.
451 const self_type &get_child(const path_type &path) const;
454 * Get a reference to the child property tree at the given path or a default if none found.
456 * Traverses the tree using the given path and retrieves a child property tree
457 * stored there. This function will retrieve indirect children if the path contains
458 * at least one separator. If child isn't found then the @c default_value will be returned.
459 * @param path A sequence of keys with zero or more separator characters.
460 * Can indicate indirect children if path contains at least one separator
461 * character.
462 * @param default_value The value to be returned if no child is found at @c path.
463 * @return A reference to the child property tree at the given path relative to this
464 * property tree or the @c default_value if that child isn't found
466 self_type &get_child(const path_type &path, self_type &default_value);
469 * Get a const reference to the child property tree at the given path or a default if none found.
471 * Traverses the tree using the given path and retrieves a child property tree
472 * stored there. This function will retrieve indirect children if the path contains
473 * at least one separator. If child isn't found then the @c default_value will be returned.
474 * @note One use of default value is to return a reference to empty property tree if the
475 * required one is not found. In many cases, the subsequent code using the return
476 * value can be then made simpler. @see boost::property_tree::empty_ptree.
477 * @param path A sequence of keys with zero or more separator characters.
478 * Can indicate indirect children if path contains at least one separator
479 * character.
480 * @param default_value The value to be returned if no child is found at @c path.
481 * @return A const reference to the child property tree at the given path relative to this
482 * property tree or the @c default_value if that child isn't found
484 const self_type &get_child(const path_type &path, const self_type &default_value) const;
487 * Get a reference to the child property tree at the given path if it exists.
489 * Traverses the tree using the given path and retrieves a child property tree
490 * stored there. This function will retrieve indirect children if the path contains
491 * at least one separator.
492 * @param path A sequence of keys with zero or more separator characters.
493 * Can indicate indirect children if path contains at least one separator
494 * character.
495 * @return If the child is found, the function returns boost::optional initialized with
496 * a reference to it. Otherwise it returns uninitialized boost::optional.
498 optional<self_type &> get_child_optional(const path_type &path);
501 * Get a const reference to the child property tree at the given path if it exists.
503 * Traverses the tree using the given path and retrieves a child property tree
504 * stored there. This function will retrieve indirect children if the path contains
505 * at least one separator.
506 * @param path A sequence of keys with zero or more separator characters.
507 * Can indicate indirect children if path contains at least one separator
508 * character.
509 * @return If the child is found, the function returns boost::optional initialized with
510 * a const reference to it. Otherwise it returns uninitialized boost::optional.
512 optional<const self_type &> get_child_optional(const path_type &path) const;
515 * Traverses the tree using given path, and inserts a new property tree or replaces
516 * existing one. If any of the intermediate keys specified by path does not exist,
517 * it is inserted, with empty data and no children, at the back of existing sequence.
519 * For example, if @c path is "key1.key2.key3", the function will find a child designated
520 * by "key1.key2" path. This child will be checked for presence of "key3" subkey. If it
521 * exists, it will be replaced with the one specified by value parameter. If it does not
522 * exist, "key3" will be added at the back of existing sequence (if any). If either
523 * "key1" or "key1.key2" do not exist, the function will insert them as well.
525 * This function is a complement to @c #get_child. If @c put_child(path,value) was called,
526 * @c get_child(path) will return a reference to element inserted/replaced by put_child.
528 * @param path Location to place value. A sequence of keys with zero or more separator
529 * characters.
530 * @param value Property tree to be inserted as a child of this property tree.
531 * @param do_not_replace If set to true, causes function to always insert a new key,
532 * even if there already exists key with the same name.
533 * @return Reference to the inserted property tree.
535 self_type &put_child(const path_type &path, const self_type &value, bool do_not_replace = false);
538 * Extracts value stored in property tree data and translates it to Type using
539 * the given translator.
540 * @throw ptree_bad_data If data cannot be translated to an instance of @c Type
541 * using the given translator_type.
542 * @param x Translator to use to extract and convert the contained #data_type to @c Type
543 * using <tt>bool translator_type::get_value(self_type const&, Type&)</tt>.
544 * @return The extracted value as an instance of @c Type.
546 template<class Type> Type get_value(const translator_type &x = translator_type()) const;
549 * Extracts value stored in property tree data and translates it to Type using the
550 * given translator. If extraction does not succeed then return the given default value.
551 * @param default_value The value to be returned if the the given translator cannot
552 * extract the data as an instance of @c Type.
553 * @param x Translator to use to extract and convert the contained #data_type to @c Type
554 * using <tt>bool translator_type::get_value(self_type const&, Type&)</tt>.
555 * @return The extracted value as an instance of @c Type if extraction succeeds.
556 * Otherwise it returns the @c default_value.
558 template<class Type> Type get_value(const Type &default_value, const translator_type &x = translator_type()) const;
561 * Extracts value stored in property tree data and translates it to @c std::basic_string<CharType>
562 * using the given translator. If extraction does not succeed then return the given default string.
563 * @param default_value The string to be returned if the the given translator cannot
564 * extract the data as an instance of @c Type.
565 * @param x Translator to use to extract and convert the contained #data_type to @c std::basic_string<CharType>
566 * using <tt>bool translator_type::get_value(self_type const&, std::basic_string<CharType>&)</tt>.
567 * @return The extracted value as an instance of @c std::basic_string<CharType> if extraction succeeds.
568 * Otherwise it returns the @c default_value.
570 template<class CharType> std::basic_string<CharType> get_value(const CharType *default_value, const translator_type &x = translator_type()) const;
573 * Extracts value stored in property tree data and translates it to Type using the
574 * given translator.
575 * @param default_value The value to be returned if the the given translator cannot
576 * extract the data as an instance of @c Type.
577 * @param x Translator to use to extract and convert the contained #data_type to @c Type
578 * using <tt>bool translator_type::get_value(self_type const&, Type&)</tt>.
579 * @return The extracted value as an instance of @c Type if extraction succeeds.
580 * Otherwise it returns an uninitialized @c boost::optional<Type>.
582 template<class Type> optional<Type> get_value_optional(const translator_type &x = translator_type()) const;
585 * Get the property tree value at the given path.
587 * Traverses the tree using the given path and retrieves the value stored there.
588 * This function will retrieve values of indirect children if the path contains at least
589 * one separator. The value will be converted to an instance of @c Type using the
590 * given translator.
591 * @param path A sequence of keys with zero or more separator characters.
592 * Can indicate indirect children if path contains at least one separator
593 * character.
594 * @param x Translator to use to extract and convert the contained #data_type to @c Type
595 * using <tt>bool translator_type::get_value(self_type const&, Type&)</tt>.
596 * @throw ptree_bad_path if child property tree cannot be located using the given path.
597 * @return The child property tree's value at the given path relative to this
598 * property tree.
600 template<class Type> Type get(const path_type &path, const translator_type &x = translator_type()) const;
603 * Get the property tree value at the given path or a default if none found.
605 * Traverses the tree using the given path and retrieves the value stored there
606 * This function will retrieve values of indirect children if the path contains at least
607 * one separator. The value will be converted to an instance of @c Type using the
608 * given translator. If child isn't found then the @c default_value will be returned.
609 * @param path A sequence of keys with zero or more separator characters.
610 * Can indicate indirect children if path contains at least one separator
611 * character.
612 * @param default_value The value to be returned if no child is found at @c path
613 * or translation fails.
614 * @param x Translator to use to extract and convert the contained #data_type to @c Type
615 * using <tt>bool translator_type::get_value(self_type const&, Type&)</tt>.
616 * @return The child property tree's value at the given path relative to this
617 * property tree or the @c default_value if that child is not found.
619 template<class Type> Type get(const path_type &path, const Type &default_value, const translator_type &x = translator_type()) const;
622 * Get the property tree value as @c std::basic_string<CharType> at the given path
623 * or a default if none found.
625 * Traverses the tree using the given path and retrieves the value stored there
626 * This function will retrieve values of indirect children if the path contains at least
627 * one separator. The value will be converted to an instance of
628 * @c std::basic_string<CharType> using the given translator. If child isn't
629 * found then the @c default_value will be returned.
630 * @param path A sequence of keys with zero or more separator characters.
631 * Can indicate indirect children if path contains at least one separator
632 * character.
633 * @param default_value The string to be returned if no child is found at @c path
634 * or translation fails.
635 * @param x Translator to use to extract and convert the contained #data_type to @c std::basic_string<CharType>
636 * using <tt>bool translator_type::get_value(self_type const&, std::basic_string<CharType>&)</tt>.
637 * @return The child property tree's value as @c std::basic_string<CharType> at
638 * the given path relative to this property tree or the @c default_value
639 * if that child is not found or translation fails.
641 template<class CharType> std::basic_string<CharType> get(const path_type &path, const CharType *default_value, const translator_type &x = translator_type()) const;
644 * Get the property tree value at the given path or an uninitialized
645 * @c boost::optional<Type> if none found.
647 * Traverses the tree using the given path and retrieves the value stored there
648 * This function will retrieve values of indirect children if the path contains at least
649 * one separator. The value will be converted to an instance of @c Type using the
650 * given translator. If child isn't found then an unitialized @c boost::optional<Type>
651 * will be returned.
652 * @param path A sequence of keys with zero or more separator characters.
653 * Can indicate indirect children if path contains at least one separator
654 * character.
655 * @param x Translator to use to extract and convert the contained #data_type to @c Type
656 * using <tt>bool translator_type::get_value(self_type const&, Type&)</tt>.
657 * @return The child property tree's value at the given path relative to this
658 * property tree or an unitialized @c boost::optional<Type> if that child is not
659 * found or translation fails.
661 template<class Type> optional<Type> get_optional(const path_type &path, const translator_type &x = translator_type()) const;
664 * Store the given value as the data of this property tree.
666 * Translates @c value from @c Type to @c #data_type using the given translator, and stores the
667 * result as the data value of this property tree.
668 * @throw ptree_bad_data If the given value cannot be translated to @c #data_type.
669 * @param value The parameter to store as the data of this property tree.
670 * @param x Translator to use to convert @c value to an instance of @c #data_type
671 * using <tt>bool translator_type::put_value(self_type&,Type const&)</tt>.
673 template<class Type> void put_value(const Type &value, const translator_type &x = translator_type());
676 * Traverses the tree using given path, and inserts a new value or replaces existing one.
677 * If any of the intermediate keys specified by path does not exist, it is inserted,
678 * with empty data and no children, at the back of existing sequence.
680 * For example, if @c path is "key1.key2.key3", the function will find a child designated
681 * by "key1.key2" path. This child will be checked for presence of "key3" subkey. If it
682 * exists, it will be replaced with the one specified by value parameter. If it does not
683 * exist, "key3" will be added at the back of existing sequence (if any). If either
684 * "key1" or "key1.key2" do not exist, the function will insert them as well.
686 * This function is a complement to @c #get. If @c put(path,value) was called,
687 * @c get(path) will return the value inserted by @c #put.
689 * @param path Location to place value. A sequence of keys with zero or more separator
690 * characters.
691 * @param value value to be inserted as the data of a child of this property tree.
692 * @param do_not_replace If set to true, causes function to always insert a new key,
693 * even if there already exists key with the same name.
694 * @param x Translator to use to convert @c value to an instance of @c #data_type
695 * using <tt>bool translator_type::put_value(self_type&,Type const&)</tt>.
696 * @return Reference to the property tree where the value was inserted. It is either a
697 * newly inserted property tree or an existing one if it was there prior to
698 * this call.
700 template<class Type> self_type &put(const path_type &path, const Type &value, bool do_not_replace = false, const translator_type &x = translator_type());
702 private:
704 data_type m_data;
705 container_type m_container;
707 ////////////////////////////////////////////////////////////////////////////
708 // Debugging
710 #ifdef BOOST_PROPERTY_TREE_DEBUG
711 private:
712 static boost::detail::lightweight_mutex debug_mutex; // Mutex for syncing instances counter
713 static size_type debug_instances_count; // Total number of instances of this ptree class
714 public:
715 static size_type debug_get_instances_count();
716 #endif
720 ///////////////////////////////////////////////////////////////////////////
721 // basic_path class template
723 /** Class template used to represent a path containing a sequence of Key instances. */
724 template<class Key>
725 class basic_path
727 #if defined(BOOST_PROPERTY_TREE_DOXYGEN_INVOKED)
728 public:
729 #endif
731 * Simpler way to refer to the Key::value_type type.
732 * Do not use in client code; exposed only for documentation purposes.
734 typedef typename Key::value_type char_type;
736 public:
738 ///////////////////////////////////////////////////////////////////////
739 // Construction & destruction
741 /** Constructs an empty basic_path<Key> instance */
742 basic_path();
745 * Constructs an path using the given path using the given separator to split it.
746 * @param path The path to which to initialize the constructed instance.
747 * @param separator The separator to use to split the @c path parameter.
749 basic_path(const Key &path, char_type separator = char_type('.'));
752 * Constructs an path using the given path using the given separator to split it.
753 * @param path The path to which to initialize the constructed instance. This
754 * path instance must be terminated with char_type('\\0');
755 * @param separator The separator to use to split the @c path parameter.
757 basic_path(const char_type *path, char_type separator = char_type('.'));
759 ///////////////////////////////////////////////////////////////////////
760 // Path manipulation
763 * Append the given path to this instance.
764 * @param rhs The path to append.
765 * @return A reference to this path instance after appending @c rhs.
767 basic_path<Key> &operator /=(const basic_path<Key> &rhs);
770 * Convert this path instance to a @c std::string representation.
771 * @return A string representation of this path.
773 std::string to_string() const;
775 ///////////////////////////////////////////////////////////////////////
776 // Operations
779 * Extract the child subtree of the given property tree at the location indicated
780 * by this path instance.
781 * @param root The property tree from which to extract the child subtree.
782 * @return Pointer to the child subtree of the input property tree indicated by the
783 * location given by this path instance. If no child exists at the indicated
784 * location then NULL is returned.
786 template<class C, class D, class X>
787 basic_ptree<C, Key, basic_path<Key>, D, X> *get_child(basic_ptree<C, Key, basic_path<Key>, D, X> &root) const;
790 * Extract the child subtree of the given property tree at the location indicated
791 * by this path instance.
792 * @param root The property tree from which to extract the child subtree.
793 * @return Pointer to the constant child subtree of the input property tree indicated by the
794 * location given by this path instance. If no child exists at the indicated
795 * location then NULL is returned.
797 template<class C, class D, class X>
798 const basic_ptree<C, Key, basic_path<Key>, D, X> *get_child(const basic_ptree<C, Key, basic_path<Key>, D, X> &root) const;
801 * Insert or replace in the given property tree at the location indicated by this path
802 * instance the second given property tree as a child.
803 * @param root The property tree in which to insert or replace the child.
804 * @param child The property tree to insert within the tree given by that @c root parameter.
805 * @param do_not_replace If set to true, causes function to always insert a new key,
806 * even if there already exists key with the same name. Otherwise
807 * @return Pointer to the child property tree inserted at the given location
808 * location given by this path instance. If this path is empty then return NULL.
810 template<class C, class D, class X>
811 basic_ptree<C, Key, basic_path<Key>, D, X> *put_child(basic_ptree<C, Key, basic_path<Key>, D, X> &root,
812 const basic_ptree<C, Key, basic_path<Key>, D, X> &child,
813 bool do_not_replace) const;
815 private:
817 std::vector<Key> m_path;
819 ///////////////////////////////////////////////////////////////////////
820 // Internal
822 template<class RanIt> void parse(RanIt begin, RanIt end, char_type separator);
826 ///////////////////////////////////////////////////////////////////////////
827 // translator class
829 class translator
832 public:
833 /** Default construct a translator instance. */
834 translator();
837 * Construct a translator instance setting the internal locale state using
838 * the given input locale.
839 * @param loc The locale to use in this instance.
841 translator(const std::locale &loc);
844 * Extract data value from the given property tree and convert it to an
845 * instance of type @c T.
846 * @param pt Property tree from which to retrieve the data value.
847 * @param[out] value The variable in which to store the retrieved data value.
848 * @return @c true If the data value was sucessfully converted and retrieved.
849 * Otherwise return @c false.
851 template<class Ptree, class T> bool get_value(const Ptree &pt, T &value) const;
854 * Insert the given value as the data member in the given property tree after
855 * converting it to instance of type @c Ptree::data_type.
856 * @param pt Property tree in which to insert the given value as data.
857 * @param value The value to store as data in the given property tree.
858 * @return @c true If the data value was sucessfully converted and retrieved.
859 * Otherwise return @c false.
861 template<class Ptree, class T> bool put_value(Ptree &pt, const T &value) const;
863 private:
865 std::locale m_locale;
869 ///////////////////////////////////////////////////////////////////////////
870 // exceptions
872 /// Base class for all property tree errors. Derives from @c std::runtime_error.
873 /// Call member function @c what to get human readable message associated with the error.
874 class ptree_error: public std::runtime_error
876 public:
877 /// Instantiate a ptree_error instance with the given message.
878 /// @param what The message to associate with this error.
879 ptree_error(const std::string &what);
881 ~ptree_error() throw();
885 /// Error indicating that translation from given value to the property tree data_type
886 /// (or vice versa) failed. Derives from ptree_error.
887 class ptree_bad_data: public ptree_error
889 public:
890 /// Instantiate a ptree_bad_data instance with the given message and data.
891 /// @param what The message to associate with this error.
892 /// @param data The value associated with this error that was the source of the
893 /// translation failure.
894 template<class T> ptree_bad_data(const std::string &what, const T &data);
896 ~ptree_bad_data() throw();
898 /// Retrieve the data associated with this error. This is the source value that
899 /// failed to be translated.
900 template<class T> T data();
901 private:
902 boost::any m_data;
906 /// Error indicating that specified path does not exist. Derives from ptree_error.
907 class ptree_bad_path: public ptree_error
909 public:
910 /// Instantiate a ptree_bad_path with the given message and path data.
911 /// @param what The message to associate with this error.
912 /// @param path The path that could not be found in the property_tree.
913 template<class T> ptree_bad_path(const std::string &what, const T &path);
915 ~ptree_bad_path() throw();
917 /// Retrieve the invalid path.
918 template<class T> T path();
919 private:
920 boost::any m_path;
925 // Include implementations
926 #include <boost/property_tree/detail/ptree_implementation.hpp>
927 // FIXME: There's a very nasty order dependency between exceptions_impl and
928 // the other two headers on compilers that do really compliant ADL, like
929 // GCC 4.3.
930 #include <boost/property_tree/detail/path_implementation.hpp>
931 #include <boost/property_tree/detail/translator_implementation.hpp>
932 #include <boost/property_tree/detail/exceptions_implementation.hpp>
934 #endif