Boost C++ Libraries

...one of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

This is the documentation for a snapshot of the develop branch, built from commit 8a68c0666b.
PrevUpHomeNext

Class template basic_ptree

boost::property_tree::basic_ptree

Synopsis

// In header: <boost/property_tree/ptree.hpp>

template<typename Key, typename Data, typename KeyCompare> 
class basic_ptree {
public:
  // types
  typedef basic_ptree< Key, Data, KeyCompare > self_type;  
  typedef Key                                  key_type;   
  typedef Data                                 data_type;  
  typedef KeyCompare                           key_compare;
  typedef std::pair< const Key, self_type >    value_type; 
  typedef std::size_t                          size_type;  
  typedef path_of< Key >::type                 path_type;  

  // public member functions
  basic_ptree();
  explicit basic_ptree(const data_type &);
  basic_ptree(const self_type &);
  ~basic_ptree();
  self_type & operator=(const self_type &);
  void swap(self_type &);
  size_type size() const;
  size_type max_size() const;
  bool empty() const;
  iterator begin();
  const_iterator begin() const;
  iterator end();
  const_iterator end() const;
  reverse_iterator rbegin();
  const_reverse_iterator rbegin() const;
  reverse_iterator rend();
  const_reverse_iterator rend() const;
  value_type & front();
  const value_type & front() const;
  value_type & back();
  const value_type & back() const;
  iterator insert(iterator, const value_type &);
  template<typename It> void insert(iterator, It, It);
  iterator erase(iterator);
  iterator erase(iterator, iterator);
  iterator push_front(const value_type &);
  iterator push_back(const value_type &);
  void pop_front();
  void pop_back();
  void reverse();
  template<typename Compare> void sort(Compare);
  void sort();
  bool operator==(const self_type &) const;
  bool operator!=(const self_type &) const;
  assoc_iterator ordered_begin();
  const_assoc_iterator ordered_begin() const;
  assoc_iterator not_found();
  const_assoc_iterator not_found() const;
  assoc_iterator find(const key_type &);
  const_assoc_iterator find(const key_type &) const;
  std::pair< assoc_iterator, assoc_iterator > equal_range(const key_type &);
  std::pair< const_assoc_iterator, const_assoc_iterator > 
  equal_range(const key_type &) const;
  size_type count(const key_type &) const;
  size_type erase(const key_type &);
  iterator to_iterator(assoc_iterator);
  const_iterator to_iterator(const_assoc_iterator) const;
  data_type & data();
  const data_type & data() const;
  void clear();
  self_type & get_child(const path_type &);
  const self_type & get_child(const path_type &) const;
  self_type & get_child(const path_type &, self_type &);
  const self_type & get_child(const path_type &, const self_type &) const;
  void get_child(const path_type &, const self_type &&) const = delete;
  optional< self_type & > get_child_optional(const path_type &);
  optional< const self_type & > get_child_optional(const path_type &) const;
  self_type & put_child(const path_type &, const self_type &);
  self_type & add_child(const path_type &, const self_type &);
  template<typename Type, typename Translator> 
    unspecified get_value(Translator) const;
  template<typename Type> Type get_value() const;
  template<typename Type, typename Translator> 
    Type get_value(const Type &, Translator) const;
  template<typename Ch, typename Translator> 
    unspecified get_value(const Ch *, Translator) const;
  template<typename Type> unspecified get_value(const Type &) const;
  template<typename Ch> unspecified get_value(const Ch *) const;
  template<typename Type, typename Translator> 
    optional< Type > get_value_optional(Translator) const;
  template<typename Type> optional< Type > get_value_optional() const;
  template<typename Type, typename Translator> 
    void put_value(const Type &, Translator);
  template<typename Type> void put_value(const Type &);
  template<typename Type, typename Translator> 
    unspecified get(const path_type &, Translator) const;
  template<typename Type> Type get(const path_type &) const;
  template<typename Type, typename Translator> 
    Type get(const path_type &, const Type &, Translator) const;
  template<typename Ch, typename Translator> 
    unspecified get(const path_type &, const Ch *, Translator) const;
  template<typename Type> 
    unspecified get(const path_type &, const Type &) const;
  template<typename Ch> unspecified get(const path_type &, const Ch *) const;
  template<typename Type, typename Translator> 
    optional< Type > get_optional(const path_type &, Translator) const;
  template<typename Type> 
    optional< Type > get_optional(const path_type &) const;
  template<typename Type, typename Translator> 
    self_type & put(const path_type &, const Type &, Translator);
  template<typename Type> self_type & put(const path_type &, const Type &);
  template<typename Type, typename Translator> 
    self_type & add(const path_type &, const Type &, Translator);
  template<typename Type> self_type & add(const path_type &, const Type &);

  // private member functions
  self_type * walk_path(path_type &) const;
  self_type & force_path(path_type &);
};

Description

Property tree main structure. A property tree is a hierarchical data structure which has one element of type Data in each node, as well as an ordered sequence of sub-nodes, which are additionally identified by a non-unique key of type Key.

Key equivalency is defined by KeyCompare, a predicate defining a strict weak ordering.

Property tree defines a Container-like interface to the (key-node) pairs of its direct sub-nodes. The iterators are bidirectional. The sequence of nodes is held in insertion order, not key order.

basic_ptree public types

  1. typedef basic_ptree< Key, Data, KeyCompare > self_type;

    Simpler way to refer to this basic_ptree<C,K,P,A> type. Note that this is private, and made public only for doxygen.

basic_ptree public member functions

  1. basic_ptree();

    Creates a node with no children and default-constructed data.

  2. explicit basic_ptree(const data_type & data);

    Creates a node with no children and a copy of the given data.

  3. basic_ptree(const self_type & rhs);
  4. ~basic_ptree();
  5. self_type & operator=(const self_type & rhs);

    Basic guarantee only.

  6. void swap(self_type & rhs);

    Swap with other tree. Only constant-time and nothrow if the data type's swap is.

  7. size_type size() const;

    The number of direct children of this node.

  8. size_type max_size() const;
  9. bool empty() const;

    Whether there are any direct children.

  10. iterator begin();
  11. const_iterator begin() const;
  12. iterator end();
  13. const_iterator end() const;
  14. reverse_iterator rbegin();
  15. const_reverse_iterator rbegin() const;
  16. reverse_iterator rend();
  17. const_reverse_iterator rend() const;
  18. value_type & front();
  19. const value_type & front() const;
  20. value_type & back();
  21. const value_type & back() const;
  22. iterator insert(iterator where, const value_type & value);

    Insert a copy of the given tree with its key just before the given position in this node. This operation invalidates no iterators.

    Returns:

    An iterator to the newly created child.

  23. template<typename It> void insert(iterator where, It first, It last);

    Range insert. Equivalent to:

    for(; first != last; ++first) insert(where, *first);
    

  24. iterator erase(iterator where);

    Erase the child pointed at by the iterator. This operation invalidates the given iterator, as well as its equivalent assoc_iterator.

    Returns:

    A valid iterator pointing to the element after the erased.

  25. iterator erase(iterator first, iterator last);

    Range erase. Equivalent to:

    while(first != last;) first = erase(first);
    

  26. iterator push_front(const value_type & value);

    Equivalent to insert(begin(), value).

  27. iterator push_back(const value_type & value);

    Equivalent to insert(end(), value).

  28. void pop_front();

    Equivalent to erase(begin()).

  29. void pop_back();

    Equivalent to erase(boost::prior(end())).

  30. void reverse();

    Reverses the order of direct children in the property tree.

  31. template<typename Compare> void sort(Compare comp);

    Sorts the direct children of this node according to the predicate. The predicate is passed the whole pair of key and child.

  32. void sort();

    Sorts the direct children of this node according to key order.

  33. bool operator==(const self_type & rhs) const;

    Two property trees are the same if they have the same data, the keys and order of their children are the same, and the children compare equal, recursively.

  34. bool operator!=(const self_type & rhs) const;
  35. assoc_iterator ordered_begin();

    Returns an iterator to the first child, in key order.

  36. const_assoc_iterator ordered_begin() const;

    Returns an iterator to the first child, in key order.

  37. assoc_iterator not_found();

    Returns the not-found iterator. Equivalent to end() in a real associative container.

  38. const_assoc_iterator not_found() const;

    Returns the not-found iterator. Equivalent to end() in a real associative container.

  39. assoc_iterator find(const key_type & key);

    Find a child with the given key, or not_found() if there is none. There is no guarantee about which child is returned if multiple have the same key.

  40. const_assoc_iterator find(const key_type & key) const;

    Find a child with the given key, or not_found() if there is none. There is no guarantee about which child is returned if multiple have the same key.

  41. std::pair< assoc_iterator, assoc_iterator > equal_range(const key_type & key);

    Find the range of children that have the given key.

  42. std::pair< const_assoc_iterator, const_assoc_iterator > 
    equal_range(const key_type & key) const;

    Find the range of children that have the given key.

  43. size_type count(const key_type & key) const;

    Count the number of direct children with the given key.

  44. size_type erase(const key_type & key);

    Erase all direct children with the given key and return the count.

  45. iterator to_iterator(assoc_iterator it);

    Get the iterator that points to the same element as the argument.

    [Note] Note

    A valid assoc_iterator range (a, b) does not imply that (to_iterator(a), to_iterator(b)) is a valid range.

  46. const_iterator to_iterator(const_assoc_iterator it) const;

    Get the iterator that points to the same element as the argument.

    [Note] Note

    A valid const_assoc_iterator range (a, b) does not imply that (to_iterator(a), to_iterator(b)) is a valid range.

  47. data_type & data();

    Reference to the actual data in this node.

  48. const data_type & data() const;

    Reference to the actual data in this node.

  49. void clear();

    Clear this tree completely, of both data and children.

  50. self_type & get_child(const path_type & path);

    Get the child at the given path, or throw ptree_bad_path.

    [Note] Note

    Depending on the path, the result at each level may not be completely deterministic, i.e. if the same key appears multiple times, which child is chosen is not specified. This can lead to the path not being resolved even though there is a descendant with this path. Example:

    a -> b -> c
      -> b
    

    The path "a.b.c" will succeed if the resolution of "b" chooses the first such node, but fail if it chooses the second.

  51. const self_type & get_child(const path_type & path) const;

    Get the child at the given path, or throw ptree_bad_path.

  52. self_type & get_child(const path_type & path, self_type & default_value);

    Get the child at the given path, or return default_value.

  53. const self_type & 
    get_child(const path_type & path, const self_type & default_value) const;

    Get the child at the given path, or return default_value.

  54. void get_child(const path_type & path, const self_type && default_value) const = delete;

    Prevents calls to get_child with temporary default values

  55. optional< self_type & > get_child_optional(const path_type & path);

    Get the child at the given path, or return boost::null.

  56. optional< const self_type & > get_child_optional(const path_type & path) const;

    Get the child at the given path, or return boost::null.

  57. self_type & put_child(const path_type & path, const self_type & value);

    Set the node at the given path to the given value. Create any missing parents. If the node at the path already exists, replace it.

    [Note] Note

    Because of the way paths work, it is not generally guaranteed that a node newly created can be accessed using the same path.

    [Note] Note

    If the path could refer to multiple nodes, it is unspecified which one gets replaced.

    Returns:

    A reference to the inserted subtree.

  58. self_type & add_child(const path_type & path, const self_type & value);

    Add the node at the given path. Create any missing parents. If there already is a node at the path, add another one with the same key.

    [Note] Note

    Because of the way paths work, it is not generally guaranteed that a node newly created can be accessed using the same path.

    Parameters:

    path

    Path to the child. The last fragment must not have an index.

    Returns:

    A reference to the inserted subtree.

  59. template<typename Type, typename Translator> 
      unspecified get_value(Translator tr) const;

    Take the value of this node and attempt to translate it to a Type object using the supplied translator.

    Throws:

    ptree_bad_data if the conversion fails.
  60. template<typename Type> Type get_value() const;

    Take the value of this node and attempt to translate it to a Type object using the default translator.

    Throws:

    ptree_bad_data if the conversion fails.
  61. template<typename Type, typename Translator> 
      Type get_value(const Type & default_value, Translator tr) const;

    Take the value of this node and attempt to translate it to a Type object using the supplied translator. Return default_value if this fails.

  62. template<typename Ch, typename Translator> 
      unspecified get_value(const Ch * default_value, Translator tr) const;

    Make get_value do the right thing for string literals.

  63. template<typename Type> 
      unspecified get_value(const Type & default_value) const;

    Take the value of this node and attempt to translate it to a Type object using the default translator. Return default_value if this fails.

  64. template<typename Ch> unspecified get_value(const Ch * default_value) const;

    Make get_value do the right thing for string literals.

  65. template<typename Type, typename Translator> 
      optional< Type > get_value_optional(Translator tr) const;

    Take the value of this node and attempt to translate it to a Type object using the supplied translator. Return boost::null if this fails.

  66. template<typename Type> optional< Type > get_value_optional() const;

    Take the value of this node and attempt to translate it to a Type object using the default translator. Return boost::null if this fails.

  67. template<typename Type, typename Translator> 
      void put_value(const Type & value, Translator tr);

    Replace the value at this node with the given value, translated to the tree's data type using the supplied translator.

    Throws:

    ptree_bad_data if the conversion fails.
  68. template<typename Type> void put_value(const Type & value);

    Replace the value at this node with the given value, translated to the tree's data type using the default translator.

    Throws:

    ptree_bad_data if the conversion fails.
  69. template<typename Type, typename Translator> 
      unspecified get(const path_type & path, Translator tr) const;

    Shorthand for get_child(path).get_value(tr).

  70. template<typename Type> Type get(const path_type & path) const;

    Shorthand for get_child(path).get_value<Type>().

  71. template<typename Type, typename Translator> 
      Type get(const path_type & path, const Type & default_value, Translator tr) const;

    Shorthand for get_child(path, empty_ptree()) .get_value(default_value, tr). That is, return the translated value if possible, and the default value if the node doesn't exist or conversion fails.

  72. template<typename Ch, typename Translator> 
      unspecified get(const path_type & path, const Ch * default_value, 
                      Translator tr) const;

    Make get do the right thing for string literals.

  73. template<typename Type> 
      unspecified get(const path_type & path, const Type & default_value) const;

    Shorthand for get_child(path, empty_ptree()) .get_value(default_value). That is, return the translated value if possible, and the default value if the node doesn't exist or conversion fails.

  74. template<typename Ch> 
      unspecified get(const path_type & path, const Ch * default_value) const;

    Make get do the right thing for string literals.

  75. template<typename Type, typename Translator> 
      optional< Type > get_optional(const path_type & path, Translator tr) const;

    Shorthand for:

    if(optional\<self_type&\> node = get_child_optional(path))
      return node->get_value_optional(tr);
    return boost::null;
    

    That is, return the value if it exists and can be converted, or nil.

  76. template<typename Type> 
      optional< Type > get_optional(const path_type & path) const;

    Shorthand for:

    if(optional\<const self_type&\> node = get_child_optional(path))
      return node->get_value_optional();
    return boost::null;
    

    That is, return the value if it exists and can be converted, or nil.

  77. template<typename Type, typename Translator> 
      self_type & put(const path_type & path, const Type & value, Translator tr);

    Set the value of the node at the given path to the supplied value, translated to the tree's data type. If the node doesn't exist, it is created, including all its missing parents.

    Returns:

    The node that had its value changed.

    Throws:

    ptree_bad_data if the conversion fails.
  78. template<typename Type> 
      self_type & put(const path_type & path, const Type & value);

    Set the value of the node at the given path to the supplied value, translated to the tree's data type. If the node doesn't exist, it is created, including all its missing parents.

    Returns:

    The node that had its value changed.

    Throws:

    ptree_bad_data if the conversion fails.
  79. template<typename Type, typename Translator> 
      self_type & add(const path_type & path, const Type & value, Translator tr);

    If the node identified by the path does not exist, create it, including all its missing parents. If the node already exists, add a sibling with the same key. Set the newly created node's value to the given paremeter, translated with the supplied translator.

    Parameters:

    path

    Path to the child. The last fragment must not have an index.

    value

    The value to add.

    tr

    The translator to use.

    Returns:

    The node that was added.

    Throws:

    ptree_bad_data if the conversion fails.
  80. template<typename Type> 
      self_type & add(const path_type & path, const Type & value);

    If the node identified by the path does not exist, create it, including all its missing parents. If the node already exists, add a sibling with the same key. Set the newly created node's value to the given paremeter, translated with the supplied translator.

    Parameters:

    path

    Path to the child. The last fragment must not have an index.

    value

    The value to add.

    Returns:

    The node that was added.

    Throws:

    ptree_bad_data if the conversion fails.

basic_ptree private member functions

  1. self_type * walk_path(path_type & p) const;
  2. self_type & force_path(path_type & p);

PrevUpHomeNext