1 // ----------------------------------------------------------------------------
2 // Copyright (C) 2009 Sebastian Redl
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)
8 // For more information, see www.boost.org
9 // ----------------------------------------------------------------------------
11 #ifndef BOOST_PROPERTY_TREE_STRING_PATH_HPP_INCLUDED
12 #define BOOST_PROPERTY_TREE_STRING_PATH_HPP_INCLUDED
14 #include <boost/property_tree/ptree_fwd.hpp>
15 #include <boost/property_tree/id_translator.hpp>
16 #include <boost/property_tree/exceptions.hpp>
17 #include <boost/property_tree/detail/ptree_utils.hpp>
19 #include <boost/static_assert.hpp>
20 #include <boost/assert.hpp>
21 #include <boost/type_traits/is_same.hpp>
22 #include <boost/optional.hpp>
23 #include <boost/throw_exception.hpp>
28 namespace boost { namespace property_tree
32 template <typename Sequence, typename Iterator>
33 void append_and_preserve_iter(Sequence &s, const Sequence &r,
34 Iterator &, std::forward_iterator_tag)
36 // Here we boldly assume that anything that is not random-access
37 // preserves validity. This is valid for the STL sequences.
38 s.insert(s.end(), r.begin(), r.end());
40 template <typename Sequence, typename Iterator>
41 void append_and_preserve_iter(Sequence &s, const Sequence &r,
43 std::random_access_iterator_tag)
45 // Convert the iterator to an index, and later back.
46 typename std::iterator_traits<Iterator>::difference_type idx =
48 s.insert(s.end(), r.begin(), r.end());
52 template <typename Sequence>
53 inline std::string dump_sequence(const Sequence &)
55 return "<undumpable sequence>";
57 inline std::string dump_sequence(const std::string &s)
61 #ifndef BOOST_NO_STD_WSTRING
62 inline std::string dump_sequence(const std::wstring &s)
64 return narrow<std::string>(s.c_str());
69 /// Default path class. A path is a sequence of values. Groups of values
70 /// are separated by the separator value, which defaults to '.' cast to
71 /// the sequence's value type. The group of values is then passed to the
72 /// translator to get a key.
74 /// If instantiated with std::string and id_translator\<std::string\>,
75 /// it accepts paths of the form "one.two.three.four".
77 /// @tparam String Any Sequence. If the sequence does not support random-
78 /// access iteration, concatenation of paths assumes that
79 /// insertions at the end preserve iterator validity.
80 /// @tparam Translator A translator with internal_type == String.
81 template <typename String, typename Translator>
84 BOOST_STATIC_ASSERT((is_same<String,
85 typename Translator::internal_type>::value));
87 typedef typename Translator::external_type key_type;
88 typedef typename String::value_type char_type;
90 /// Create an empty path.
91 explicit string_path(char_type separator = char_type('.'));
92 /// Create a path by parsing the given string.
93 /// @param value A sequence, possibly with separators, that describes
94 /// the path, e.g. "one.two.three".
95 /// @param separator The separator used in parsing. Defaults to '.'.
96 /// @param tr The translator used by this path to convert the individual
98 string_path(const String &value, char_type separator = char_type('.'),
99 Translator tr = Translator());
100 /// Create a path by parsing the given string.
101 /// @param value A zero-terminated array of values. Only use if zero-
102 /// termination makes sense for your type, and your
103 /// sequence supports construction from it. Intended for
105 /// @param separator The separator used in parsing. Defaults to '.'.
106 /// @param tr The translator used by this path to convert the individual
108 string_path(const char_type *value,
109 char_type separator = char_type('.'),
110 Translator tr = Translator());
112 // Default copying doesn't do the right thing with the iterator
113 string_path(const string_path &o);
114 string_path& operator =(const string_path &o);
116 /// Take a single element off the path at the front and return it.
119 /// Test if the path is empty.
122 /// Test if the path contains a single element, i.e. no separators.
125 /// Get the separator used by this path.
126 char_type separator() const { return m_separator; }
128 std::string dump() const {
129 return detail::dump_sequence(m_value);
132 /// Append a second path to this one.
133 /// @pre o's separator is the same as this one's, or o has no separators
134 string_path& operator /=(const string_path &o) {
135 // If it's single, there's no separator. This allows to do
137 // even for non-default separators.
138 BOOST_ASSERT((m_separator == o.m_separator
141 && "Incompatible paths.");
145 sub.push_back(m_separator);
147 sub.insert(sub.end(), o.cstart(), o.m_value.end());
148 detail::append_and_preserve_iter(m_value, sub, m_start,
149 typename std::iterator_traits<s_iter>::iterator_category());
155 typedef typename String::iterator s_iter;
156 typedef typename String::const_iterator s_c_iter;
158 char_type m_separator;
161 s_c_iter cstart() const { return m_start; }
164 template <typename String, typename Translator> inline
165 string_path<String, Translator>::string_path(char_type separator)
166 : m_separator(separator), m_start(m_value.begin())
169 template <typename String, typename Translator> inline
170 string_path<String, Translator>::string_path(const String &value,
173 : m_value(value), m_separator(separator),
174 m_tr(tr), m_start(m_value.begin())
177 template <typename String, typename Translator> inline
178 string_path<String, Translator>::string_path(const char_type *value,
181 : m_value(value), m_separator(separator),
182 m_tr(tr), m_start(m_value.begin())
185 template <typename String, typename Translator> inline
186 string_path<String, Translator>::string_path(const string_path &o)
187 : m_value(o.m_value), m_separator(o.m_separator),
188 m_tr(o.m_tr), m_start(m_value.begin())
190 std::advance(m_start, std::distance(o.m_value.begin(), o.cstart()));
193 template <typename String, typename Translator> inline
194 string_path<String, Translator>&
195 string_path<String, Translator>::operator =(const string_path &o)
198 m_separator = o.m_separator;
200 m_start = m_value.begin();
201 std::advance(m_start, std::distance(o.m_value.begin(), o.cstart()));
205 template <typename String, typename Translator>
206 typename Translator::external_type string_path<String, Translator>::reduce()
208 BOOST_ASSERT(!empty() && "Reducing empty path");
210 s_iter next_sep = std::find(m_start, m_value.end(), m_separator);
211 String part(m_start, next_sep);
214 // Unless we're at the end, skip the separator we found.
218 if(optional<key_type> key = m_tr.get_value(part)) {
221 BOOST_PROPERTY_TREE_THROW(ptree_bad_path("Path syntax error", *this));
224 template <typename String, typename Translator> inline
225 bool string_path<String, Translator>::empty() const
227 return m_start == m_value.end();
230 template <typename String, typename Translator> inline
231 bool string_path<String, Translator>::single() const
233 return std::find(static_cast<s_c_iter>(m_start),
234 m_value.end(), m_separator)
238 // By default, this is the path for strings. You can override this by
239 // specializing path_of for a more specific form of std::basic_string.
240 template <typename Ch, typename Traits, typename Alloc>
241 struct path_of< std::basic_string<Ch, Traits, Alloc> >
243 typedef std::basic_string<Ch, Traits, Alloc> _string;
244 typedef string_path< _string, id_translator<_string> > type;
247 template <typename String, typename Translator> inline
248 string_path<String, Translator> operator /(
249 string_path<String, Translator> p1,
250 const string_path<String, Translator> &p2)
256 // These shouldn't be necessary, but GCC won't find the one above.
257 template <typename String, typename Translator> inline
258 string_path<String, Translator> operator /(
259 string_path<String, Translator> p1,
260 const typename String::value_type *p2)
266 template <typename String, typename Translator> inline
267 string_path<String, Translator> operator /(
268 const typename String::value_type *p1,
269 const string_path<String, Translator> &p2)
271 string_path<String, Translator> t(p1);