123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255 |
- #ifndef BOOST_PROPERTY_TREE_STRING_PATH_HPP_INCLUDED
- #define BOOST_PROPERTY_TREE_STRING_PATH_HPP_INCLUDED
- #include <boost/property_tree/ptree_fwd.hpp>
- #include <boost/property_tree/id_translator.hpp>
- #include <boost/property_tree/exceptions.hpp>
- #include <boost/property_tree/detail/ptree_utils.hpp>
- #include <boost/static_assert.hpp>
- #include <boost/assert.hpp>
- #include <boost/type_traits/is_same.hpp>
- #include <boost/optional/optional.hpp>
- #include <boost/throw_exception.hpp>
- #include <algorithm>
- #include <string>
- #include <iterator>
- namespace boost { namespace property_tree
- {
- namespace detail
- {
- template <typename Sequence, typename Iterator>
- void append_and_preserve_iter(Sequence &s, const Sequence &r,
- Iterator &, std::forward_iterator_tag)
- {
-
-
- s.insert(s.end(), r.begin(), r.end());
- }
- template <typename Sequence, typename Iterator>
- void append_and_preserve_iter(Sequence &s, const Sequence &r,
- Iterator &it,
- std::random_access_iterator_tag)
- {
-
- typename std::iterator_traits<Iterator>::difference_type idx =
- it - s.begin();
- s.insert(s.end(), r.begin(), r.end());
- it = s.begin() + idx;
- }
- template <typename Sequence>
- inline std::string dump_sequence(const Sequence &)
- {
- return "<undumpable sequence>";
- }
- inline std::string dump_sequence(const std::string &s)
- {
- return s;
- }
- #ifndef BOOST_NO_STD_WSTRING
- inline std::string dump_sequence(const std::wstring &s)
- {
- return narrow<std::string>(s.c_str());
- }
- #endif
- }
-
-
-
-
-
-
-
-
-
-
-
-
- template <typename String, typename Translator>
- class string_path
- {
- BOOST_STATIC_ASSERT((is_same<String,
- typename Translator::internal_type>::value));
- public:
- typedef typename Translator::external_type key_type;
- typedef typename String::value_type char_type;
-
- explicit string_path(char_type separator = char_type('.'));
-
-
-
-
-
-
- string_path(const String &value, char_type separator = char_type('.'),
- Translator tr = Translator());
-
-
-
-
-
-
-
-
- string_path(const char_type *value,
- char_type separator = char_type('.'),
- Translator tr = Translator());
-
- string_path(const string_path &o);
- string_path& operator =(const string_path &o);
-
- key_type reduce();
-
- bool empty() const;
-
- bool single() const;
-
- char_type separator() const { return m_separator; }
- std::string dump() const {
- return detail::dump_sequence(m_value);
- }
-
- friend string_path operator /(string_path p1, const string_path &p2)
- {
- p1 /= p2;
- return p1;
- }
-
-
- string_path& operator /=(const string_path &o) {
-
-
-
- BOOST_ASSERT((m_separator == o.m_separator
- || o.empty()
- || o.single())
- && "Incompatible paths.");
- if(!o.empty()) {
- String sub;
- if(!this->empty()) {
- sub.push_back(m_separator);
- }
- sub.insert(sub.end(), o.cstart(), o.m_value.end());
- detail::append_and_preserve_iter(m_value, sub, m_start,
- typename std::iterator_traits<s_iter>::iterator_category());
- }
- return *this;
- }
- private:
- typedef typename String::iterator s_iter;
- typedef typename String::const_iterator s_c_iter;
- String m_value;
- char_type m_separator;
- Translator m_tr;
- s_iter m_start;
- s_c_iter cstart() const { return m_start; }
- };
- template <typename String, typename Translator> inline
- string_path<String, Translator>::string_path(char_type separator)
- : m_separator(separator), m_start(m_value.begin())
- {}
- template <typename String, typename Translator> inline
- string_path<String, Translator>::string_path(const String &value,
- char_type separator,
- Translator tr)
- : m_value(value), m_separator(separator),
- m_tr(tr), m_start(m_value.begin())
- {}
- template <typename String, typename Translator> inline
- string_path<String, Translator>::string_path(const char_type *value,
- char_type separator,
- Translator tr)
- : m_value(value), m_separator(separator),
- m_tr(tr), m_start(m_value.begin())
- {}
- template <typename String, typename Translator> inline
- string_path<String, Translator>::string_path(const string_path &o)
- : m_value(o.m_value), m_separator(o.m_separator),
- m_tr(o.m_tr), m_start(m_value.begin())
- {
- std::advance(m_start, std::distance(o.m_value.begin(), o.cstart()));
- }
- template <typename String, typename Translator> inline
- string_path<String, Translator>&
- string_path<String, Translator>::operator =(const string_path &o)
- {
- m_value = o.m_value;
- m_separator = o.m_separator;
- m_tr = o.m_tr;
- m_start = m_value.begin();
- std::advance(m_start, std::distance(o.m_value.begin(), o.cstart()));
- return *this;
- }
- template <typename String, typename Translator>
- typename Translator::external_type string_path<String, Translator>::reduce()
- {
- BOOST_ASSERT(!empty() && "Reducing empty path");
- s_iter next_sep = std::find(m_start, m_value.end(), m_separator);
- String part(m_start, next_sep);
- m_start = next_sep;
- if(!empty()) {
-
- ++m_start;
- }
- if(optional<key_type> key = m_tr.get_value(part)) {
- return *key;
- }
- BOOST_PROPERTY_TREE_THROW(ptree_bad_path("Path syntax error", *this));
- }
- template <typename String, typename Translator> inline
- bool string_path<String, Translator>::empty() const
- {
- return m_start == m_value.end();
- }
- template <typename String, typename Translator> inline
- bool string_path<String, Translator>::single() const
- {
- return std::find(static_cast<s_c_iter>(m_start),
- m_value.end(), m_separator)
- == m_value.end();
- }
-
-
- template <typename Ch, typename Traits, typename Alloc>
- struct path_of< std::basic_string<Ch, Traits, Alloc> >
- {
- typedef std::basic_string<Ch, Traits, Alloc> _string;
- typedef string_path< _string, id_translator<_string> > type;
- };
- }}
- #endif
|