// ---------------------------------------------------------------------------- // Copyright (C) 2002-2006 Marcin Kalicinski // Copyright (C) 2009 Sebastian Redl // // Distributed under the Boost Software License, Version 1.0. // (See accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) // // For more information, see www.boost.org // ---------------------------------------------------------------------------- #ifndef BOOST_PROPERTY_TREE_DETAIL_PTREE_IMPLEMENTATION_HPP_INCLUDED #define BOOST_PROPERTY_TREE_DETAIL_PTREE_IMPLEMENTATION_HPP_INCLUDED #include #include #include #include #include #include #include #if (defined(BOOST_MSVC) && \ (_MSC_FULL_VER >= 160000000 && _MSC_FULL_VER < 170000000)) || \ (defined(BOOST_INTEL_WIN) && \ defined(BOOST_DINKUMWARE_STDLIB)) #define BOOST_PROPERTY_TREE_PAIR_BUG #endif namespace boost { namespace property_tree { template struct basic_ptree::subs { struct by_name {}; // The actual child container. #if defined(BOOST_PROPERTY_TREE_PAIR_BUG) // MSVC 10 has moved std::pair's members to a base // class. Unfortunately this does break the interface. BOOST_STATIC_CONSTANT(unsigned, first_offset = offsetof(value_type, first)); #endif typedef multi_index_container, multi_index::ordered_non_unique, #if defined(BOOST_PROPERTY_TREE_PAIR_BUG) multi_index::member_offset, #else multi_index::member, #endif key_compare > > > base_container; // The by-name lookup index. typedef typename base_container::template index::type by_name_index; // Access functions for getting to the children of a tree. static base_container& ch(self_type *s) { return *static_cast(s->m_children); } static const base_container& ch(const self_type *s) { return *static_cast(s->m_children); } static by_name_index& assoc(self_type *s) { return ch(s).BOOST_NESTED_TEMPLATE get(); } static const by_name_index& assoc(const self_type *s) { return ch(s).BOOST_NESTED_TEMPLATE get(); } }; template class basic_ptree::iterator : public boost::iterator_adaptor< iterator, typename subs::base_container::iterator, value_type> { friend class boost::iterator_core_access; typedef boost::iterator_adaptor< iterator, typename subs::base_container::iterator, value_type> baset; public: typedef typename baset::reference reference; iterator() {} explicit iterator(typename iterator::base_type b) : iterator::iterator_adaptor_(b) {} reference dereference() const { // multi_index doesn't allow modification of its values, because // indexes could sort by anything, and modification screws that up. // However, we only sort by the key, and it's protected against // modification in the value_type, so this const_cast is safe. return const_cast(*this->base_reference()); } }; template class basic_ptree::const_iterator : public boost::iterator_adaptor< const_iterator, typename subs::base_container::const_iterator> { public: const_iterator() {} explicit const_iterator(typename const_iterator::base_type b) : const_iterator::iterator_adaptor_(b) {} const_iterator(iterator b) : const_iterator::iterator_adaptor_(b.base()) {} }; template class basic_ptree::reverse_iterator : public boost::reverse_iterator { public: reverse_iterator() {} explicit reverse_iterator(iterator b) : boost::reverse_iterator(b) {} }; template class basic_ptree::const_reverse_iterator : public boost::reverse_iterator { public: const_reverse_iterator() {} explicit const_reverse_iterator(const_iterator b) : boost::reverse_iterator(b) {} const_reverse_iterator( typename basic_ptree::reverse_iterator b) : boost::reverse_iterator(b) {} }; template class basic_ptree::assoc_iterator : public boost::iterator_adaptor { friend class boost::iterator_core_access; typedef boost::iterator_adaptor baset; public: typedef typename baset::reference reference; assoc_iterator() {} explicit assoc_iterator(typename assoc_iterator::base_type b) : assoc_iterator::iterator_adaptor_(b) {} reference dereference() const { return const_cast(*this->base_reference()); } }; template class basic_ptree::const_assoc_iterator : public boost::iterator_adaptor { public: const_assoc_iterator() {} explicit const_assoc_iterator( typename const_assoc_iterator::base_type b) : const_assoc_iterator::iterator_adaptor_(b) {} const_assoc_iterator(assoc_iterator b) : const_assoc_iterator::iterator_adaptor_(b.base()) {} }; // Big five // Perhaps the children collection could be created on-demand only, to // reduce heap traffic. But that's a lot more work to implement. template inline basic_ptree::basic_ptree() : m_children(new typename subs::base_container) { } template inline basic_ptree::basic_ptree(const data_type &d) : m_data(d), m_children(new typename subs::base_container) { } template inline basic_ptree::basic_ptree(const basic_ptree &rhs) : m_data(rhs.m_data), m_children(new typename subs::base_container(subs::ch(&rhs))) { } template basic_ptree & basic_ptree::operator =(const basic_ptree &rhs) { self_type(rhs).swap(*this); return *this; } template basic_ptree::~basic_ptree() { delete &subs::ch(this); } template inline void basic_ptree::swap(basic_ptree &rhs) { boost::core::invoke_swap(m_data, rhs.m_data); // Void pointers, no ADL necessary std::swap(m_children, rhs.m_children); } // Container view template inline typename basic_ptree::size_type basic_ptree::size() const { return subs::ch(this).size(); } template inline typename basic_ptree::size_type basic_ptree::max_size() const { return subs::ch(this).max_size(); } template inline bool basic_ptree::empty() const { return subs::ch(this).empty(); } template inline typename basic_ptree::iterator basic_ptree::begin() { return iterator(subs::ch(this).begin()); } template inline typename basic_ptree::const_iterator basic_ptree::begin() const { return const_iterator(subs::ch(this).begin()); } template inline typename basic_ptree::iterator basic_ptree::end() { return iterator(subs::ch(this).end()); } template inline typename basic_ptree::const_iterator basic_ptree::end() const { return const_iterator(subs::ch(this).end()); } template inline typename basic_ptree::reverse_iterator basic_ptree::rbegin() { return reverse_iterator(this->end()); } template inline typename basic_ptree::const_reverse_iterator basic_ptree::rbegin() const { return const_reverse_iterator(this->end()); } template inline typename basic_ptree::reverse_iterator basic_ptree::rend() { return reverse_iterator(this->begin()); } template inline typename basic_ptree::const_reverse_iterator basic_ptree::rend() const { return const_reverse_iterator(this->begin()); } template inline typename basic_ptree::value_type & basic_ptree::front() { return const_cast(subs::ch(this).front()); } template inline const typename basic_ptree::value_type & basic_ptree::front() const { return subs::ch(this).front(); } template inline typename basic_ptree::value_type & basic_ptree::back() { return const_cast(subs::ch(this).back()); } template inline const typename basic_ptree::value_type & basic_ptree::back() const { return subs::ch(this).back(); } template inline typename basic_ptree::iterator basic_ptree::insert(iterator where, const value_type &value) { return iterator(subs::ch(this).insert(where.base(), value).first); } template template inline void basic_ptree::insert(iterator where, It first, It last) { subs::ch(this).insert(where.base(), first, last); } template inline typename basic_ptree::iterator basic_ptree::erase(iterator where) { return iterator(subs::ch(this).erase(where.base())); } template inline typename basic_ptree::iterator basic_ptree::erase(iterator first, iterator last) { return iterator(subs::ch(this).erase(first.base(), last.base())); } template inline typename basic_ptree::iterator basic_ptree::push_front(const value_type &value) { return iterator(subs::ch(this).push_front(value).first); } template inline typename basic_ptree::iterator basic_ptree::push_back(const value_type &value) { return iterator(subs::ch(this).push_back(value).first); } template inline void basic_ptree::pop_front() { subs::ch(this).pop_front(); } template inline void basic_ptree::pop_back() { subs::ch(this).pop_back(); } template inline void basic_ptree::reverse() { subs::ch(this).reverse(); } namespace impl { struct by_first { template bool operator ()(const P& lhs, const P& rhs) const { return lhs.first < rhs.first; } }; template struct equal_pred { template bool operator ()(const P& lhs, const P& rhs) const { C c; return !c(lhs.first, rhs.first) && !c(rhs.first, lhs.first) && lhs.second == rhs.second; } }; template bool equal_children(const MI& ch1, const MI& ch2) { // Assumes ch1.size() == ch2.size() return std::equal(ch1.begin(), ch1.end(), ch2.begin(), equal_pred()); } } template inline void basic_ptree::sort() { sort(impl::by_first()); } template template inline void basic_ptree::sort(Compare comp) { subs::ch(this).sort(comp); } // Equality template inline bool basic_ptree::operator ==( const basic_ptree &rhs) const { // The size test is cheap, so add it as an optimization return size() == rhs.size() && data() == rhs.data() && impl::equal_children(subs::ch(this), subs::ch(&rhs)); } template inline bool basic_ptree::operator !=( const basic_ptree &rhs) const { return !(*this == rhs); } // Associative view template inline typename basic_ptree::assoc_iterator basic_ptree::ordered_begin() { return assoc_iterator(subs::assoc(this).begin()); } template inline typename basic_ptree::const_assoc_iterator basic_ptree::ordered_begin() const { return const_assoc_iterator(subs::assoc(this).begin()); } template inline typename basic_ptree::assoc_iterator basic_ptree::not_found() { return assoc_iterator(subs::assoc(this).end()); } template inline typename basic_ptree::const_assoc_iterator basic_ptree::not_found() const { return const_assoc_iterator(subs::assoc(this).end()); } template inline typename basic_ptree::assoc_iterator basic_ptree::find(const key_type &key) { return assoc_iterator(subs::assoc(this).find(key)); } template inline typename basic_ptree::const_assoc_iterator basic_ptree::find(const key_type &key) const { return const_assoc_iterator(subs::assoc(this).find(key)); } template inline std::pair< typename basic_ptree::assoc_iterator, typename basic_ptree::assoc_iterator > basic_ptree::equal_range(const key_type &key) { std::pair r( subs::assoc(this).equal_range(key)); return std::pair( assoc_iterator(r.first), assoc_iterator(r.second)); } template inline std::pair< typename basic_ptree::const_assoc_iterator, typename basic_ptree::const_assoc_iterator > basic_ptree::equal_range(const key_type &key) const { std::pair r( subs::assoc(this).equal_range(key)); return std::pair( const_assoc_iterator(r.first), const_assoc_iterator(r.second)); } template inline typename basic_ptree::size_type basic_ptree::count(const key_type &key) const { return subs::assoc(this).count(key); } template inline typename basic_ptree::size_type basic_ptree::erase(const key_type &key) { return subs::assoc(this).erase(key); } template inline typename basic_ptree::iterator basic_ptree::to_iterator(assoc_iterator ai) { return iterator(subs::ch(this). BOOST_NESTED_TEMPLATE project<0>(ai.base())); } template inline typename basic_ptree::const_iterator basic_ptree::to_iterator(const_assoc_iterator ai) const { return const_iterator(subs::ch(this). BOOST_NESTED_TEMPLATE project<0>(ai.base())); } // Property tree view template inline typename basic_ptree::data_type & basic_ptree::data() { return m_data; } template inline const typename basic_ptree::data_type & basic_ptree::data() const { return m_data; } template inline void basic_ptree::clear() { m_data = data_type(); subs::ch(this).clear(); } template basic_ptree & basic_ptree::get_child(const path_type &path) { path_type p(path); self_type *n = walk_path(p); if (!n) { BOOST_PROPERTY_TREE_THROW(ptree_bad_path("No such node", path)); } return *n; } template inline const basic_ptree & basic_ptree::get_child(const path_type &path) const { return const_cast(this)->get_child(path); } template inline basic_ptree & basic_ptree::get_child(const path_type &path, self_type &default_value) { path_type p(path); self_type *n = walk_path(p); return n ? *n : default_value; } template inline const basic_ptree & basic_ptree::get_child(const path_type &path, const self_type &default_value) const { return const_cast(this)->get_child(path, const_cast(default_value)); } template optional &> basic_ptree::get_child_optional(const path_type &path) { path_type p(path); self_type *n = walk_path(p); if (!n) { return optional(); } return *n; } template optional &> basic_ptree::get_child_optional(const path_type &path) const { path_type p(path); self_type *n = walk_path(p); if (!n) { return optional(); } return *n; } template basic_ptree & basic_ptree::put_child(const path_type &path, const self_type &value) { path_type p(path); self_type &parent = force_path(p); // Got the parent. Now get the correct child. key_type fragment = p.reduce(); assoc_iterator el = parent.find(fragment); // If the new child exists, replace it. if(el != parent.not_found()) { return el->second = value; } else { return parent.push_back(value_type(fragment, value))->second; } } template basic_ptree & basic_ptree::add_child(const path_type &path, const self_type &value) { path_type p(path); self_type &parent = force_path(p); // Got the parent. key_type fragment = p.reduce(); return parent.push_back(value_type(fragment, value))->second; } template template typename boost::enable_if, Type>::type basic_ptree::get_value(Translator tr) const { if(boost::optional o = get_value_optional(tr)) { return *o; } BOOST_PROPERTY_TREE_THROW(ptree_bad_data( std::string("conversion of data to type \"") + boost::core::type_name() + "\" failed", data())); } template template inline Type basic_ptree::get_value() const { return get_value( typename translator_between::type()); } template template inline Type basic_ptree::get_value(const Type &default_value, Translator tr) const { return get_value_optional(tr).get_value_or(default_value); } template template typename boost::enable_if< detail::is_character, std::basic_string >::type basic_ptree::get_value(const Ch *default_value, Translator tr)const { return get_value, Translator>(default_value, tr); } template template inline typename boost::disable_if, Type>::type basic_ptree::get_value(const Type &default_value) const { return get_value(default_value, typename translator_between::type()); } template template typename boost::enable_if< detail::is_character, std::basic_string >::type basic_ptree::get_value(const Ch *default_value) const { return get_value< std::basic_string >(default_value); } template template inline optional basic_ptree::get_value_optional( Translator tr) const { return tr.get_value(data()); } template template inline optional basic_ptree::get_value_optional() const { return get_value_optional( typename translator_between::type()); } template template inline typename boost::enable_if, Type>::type basic_ptree::get(const path_type &path, Translator tr) const { return get_child(path).BOOST_NESTED_TEMPLATE get_value(tr); } template template inline Type basic_ptree::get(const path_type &path) const { return get_child(path).BOOST_NESTED_TEMPLATE get_value(); } template template inline Type basic_ptree::get(const path_type &path, const Type &default_value, Translator tr) const { return get_optional(path, tr).get_value_or(default_value); } template template typename boost::enable_if< detail::is_character, std::basic_string >::type basic_ptree::get( const path_type &path, const Ch *default_value, Translator tr) const { return get, Translator>(path, default_value, tr); } template template inline typename boost::disable_if, Type>::type basic_ptree::get(const path_type &path, const Type &default_value) const { return get_optional(path).get_value_or(default_value); } template template typename boost::enable_if< detail::is_character, std::basic_string >::type basic_ptree::get( const path_type &path, const Ch *default_value) const { return get< std::basic_string >(path, default_value); } template template optional basic_ptree::get_optional(const path_type &path, Translator tr) const { if (optional child = get_child_optional(path)) return child.get(). BOOST_NESTED_TEMPLATE get_value_optional(tr); else return optional(); } template template optional basic_ptree::get_optional( const path_type &path) const { if (optional child = get_child_optional(path)) return child.get().BOOST_NESTED_TEMPLATE get_value_optional(); else return optional(); } template template void basic_ptree::put_value(const Type &value, Translator tr) { if(optional o = tr.put_value(value)) { data() = *o; } else { BOOST_PROPERTY_TREE_THROW(ptree_bad_data( std::string("conversion of type \"") + boost::core::type_name() + "\" to data failed", boost::any())); } } template template inline void basic_ptree::put_value(const Type &value) { put_value(value, typename translator_between::type()); } template template basic_ptree & basic_ptree::put( const path_type &path, const Type &value, Translator tr) { if(optional child = get_child_optional(path)) { child.get().put_value(value, tr); return *child; } else { self_type &child2 = put_child(path, self_type()); child2.put_value(value, tr); return child2; } } template template inline basic_ptree & basic_ptree::put( const path_type &path, const Type &value) { return put(path, value, typename translator_between::type()); } template template inline basic_ptree & basic_ptree::add( const path_type &path, const Type &value, Translator tr) { self_type &child = add_child(path, self_type()); child.put_value(value, tr); return child; } template template inline basic_ptree & basic_ptree::add( const path_type &path, const Type &value) { return add(path, value, typename translator_between::type()); } template basic_ptree * basic_ptree::walk_path(path_type &p) const { if(p.empty()) { // I'm the child we're looking for. return const_cast(this); } // Recurse down the tree to find the path. key_type fragment = p.reduce(); const_assoc_iterator el = find(fragment); if(el == not_found()) { // No such child. return 0; } // Not done yet, recurse. return el->second.walk_path(p); } template basic_ptree & basic_ptree::force_path(path_type &p) { BOOST_ASSERT(!p.empty() && "Empty path not allowed for put_child."); if(p.single()) { // I'm the parent we're looking for. return *this; } key_type fragment = p.reduce(); assoc_iterator el = find(fragment); // If we've found an existing child, go down that path. Else // create a new one. self_type& child = el == not_found() ? push_back(value_type(fragment, self_type()))->second : el->second; return child.force_path(p); } // Free functions template inline void swap(basic_ptree &pt1, basic_ptree &pt2) { pt1.swap(pt2); } } } #if defined(BOOST_PROPERTY_TREE_PAIR_BUG) #undef BOOST_PROPERTY_TREE_PAIR_BUG #endif #endif