string_path.hpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. // ----------------------------------------------------------------------------
  2. // Copyright (C) 2009 Sebastian Redl
  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. // ----------------------------------------------------------------------------
  10. #ifndef BOOST_PROPERTY_TREE_STRING_PATH_HPP_INCLUDED
  11. #define BOOST_PROPERTY_TREE_STRING_PATH_HPP_INCLUDED
  12. #include <boost/property_tree/ptree_fwd.hpp>
  13. #include <boost/property_tree/id_translator.hpp>
  14. #include <boost/property_tree/exceptions.hpp>
  15. #include <boost/property_tree/detail/ptree_utils.hpp>
  16. #include <boost/static_assert.hpp>
  17. #include <boost/assert.hpp>
  18. #include <boost/type_traits/is_same.hpp>
  19. #include <boost/optional/optional.hpp>
  20. #include <boost/throw_exception.hpp>
  21. #include <algorithm>
  22. #include <string>
  23. #include <iterator>
  24. namespace boost { namespace property_tree
  25. {
  26. namespace detail
  27. {
  28. template <typename Sequence, typename Iterator>
  29. void append_and_preserve_iter(Sequence &s, const Sequence &r,
  30. Iterator &, std::forward_iterator_tag)
  31. {
  32. // Here we boldly assume that anything that is not random-access
  33. // preserves validity. This is valid for the STL sequences.
  34. s.insert(s.end(), r.begin(), r.end());
  35. }
  36. template <typename Sequence, typename Iterator>
  37. void append_and_preserve_iter(Sequence &s, const Sequence &r,
  38. Iterator &it,
  39. std::random_access_iterator_tag)
  40. {
  41. // Convert the iterator to an index, and later back.
  42. typename std::iterator_traits<Iterator>::difference_type idx =
  43. it - s.begin();
  44. s.insert(s.end(), r.begin(), r.end());
  45. it = s.begin() + idx;
  46. }
  47. template <typename Sequence>
  48. inline std::string dump_sequence(const Sequence &)
  49. {
  50. return "<undumpable sequence>";
  51. }
  52. inline std::string dump_sequence(const std::string &s)
  53. {
  54. return s;
  55. }
  56. #ifndef BOOST_NO_STD_WSTRING
  57. inline std::string dump_sequence(const std::wstring &s)
  58. {
  59. return narrow<std::string>(s.c_str());
  60. }
  61. #endif
  62. }
  63. /// Default path class. A path is a sequence of values. Groups of values
  64. /// are separated by the separator value, which defaults to '.' cast to
  65. /// the sequence's value type. The group of values is then passed to the
  66. /// translator to get a key.
  67. ///
  68. /// If instantiated with std::string and id_translator\<std::string\>,
  69. /// it accepts paths of the form "one.two.three.four".
  70. ///
  71. /// @tparam String Any Sequence. If the sequence does not support random-
  72. /// access iteration, concatenation of paths assumes that
  73. /// insertions at the end preserve iterator validity.
  74. /// @tparam Translator A translator with internal_type == String.
  75. template <typename String, typename Translator>
  76. class string_path
  77. {
  78. BOOST_STATIC_ASSERT((is_same<String,
  79. typename Translator::internal_type>::value));
  80. public:
  81. typedef typename Translator::external_type key_type;
  82. typedef typename String::value_type char_type;
  83. /// Create an empty path.
  84. explicit string_path(char_type separator = char_type('.'));
  85. /// Create a path by parsing the given string.
  86. /// @param value A sequence, possibly with separators, that describes
  87. /// the path, e.g. "one.two.three".
  88. /// @param separator The separator used in parsing. Defaults to '.'.
  89. /// @param tr The translator used by this path to convert the individual
  90. /// parts to keys.
  91. string_path(const String &value, char_type separator = char_type('.'),
  92. Translator tr = Translator());
  93. /// Create a path by parsing the given string.
  94. /// @param value A zero-terminated array of values. Only use if zero-
  95. /// termination makes sense for your type, and your
  96. /// sequence supports construction from it. Intended for
  97. /// string literals.
  98. /// @param separator The separator used in parsing. Defaults to '.'.
  99. /// @param tr The translator used by this path to convert the individual
  100. /// parts to keys.
  101. string_path(const char_type *value,
  102. char_type separator = char_type('.'),
  103. Translator tr = Translator());
  104. // Default copying doesn't do the right thing with the iterator
  105. string_path(const string_path &o);
  106. string_path& operator =(const string_path &o);
  107. /// Take a single element off the path at the front and return it.
  108. key_type reduce();
  109. /// Test if the path is empty.
  110. bool empty() const;
  111. /// Test if the path contains a single element, i.e. no separators.
  112. bool single() const;
  113. /// Get the separator used by this path.
  114. char_type separator() const { return m_separator; }
  115. std::string dump() const {
  116. return detail::dump_sequence(m_value);
  117. }
  118. /// Concatenates two path components
  119. friend string_path operator /(string_path p1, const string_path &p2)
  120. {
  121. p1 /= p2;
  122. return p1;
  123. }
  124. /// Append a second path to this one.
  125. /// @pre o's separator is the same as this one's, or o has no separators
  126. string_path& operator /=(const string_path &o) {
  127. // If it's single, there's no separator. This allows to do
  128. // p /= "piece";
  129. // even for non-default separators.
  130. BOOST_ASSERT((m_separator == o.m_separator
  131. || o.empty()
  132. || o.single())
  133. && "Incompatible paths.");
  134. if(!o.empty()) {
  135. String sub;
  136. if(!this->empty()) {
  137. sub.push_back(m_separator);
  138. }
  139. sub.insert(sub.end(), o.cstart(), o.m_value.end());
  140. detail::append_and_preserve_iter(m_value, sub, m_start,
  141. typename std::iterator_traits<s_iter>::iterator_category());
  142. }
  143. return *this;
  144. }
  145. private:
  146. typedef typename String::iterator s_iter;
  147. typedef typename String::const_iterator s_c_iter;
  148. String m_value;
  149. char_type m_separator;
  150. Translator m_tr;
  151. s_iter m_start;
  152. s_c_iter cstart() const { return m_start; }
  153. };
  154. template <typename String, typename Translator> inline
  155. string_path<String, Translator>::string_path(char_type separator)
  156. : m_separator(separator), m_start(m_value.begin())
  157. {}
  158. template <typename String, typename Translator> inline
  159. string_path<String, Translator>::string_path(const String &value,
  160. char_type separator,
  161. Translator tr)
  162. : m_value(value), m_separator(separator),
  163. m_tr(tr), m_start(m_value.begin())
  164. {}
  165. template <typename String, typename Translator> inline
  166. string_path<String, Translator>::string_path(const char_type *value,
  167. char_type separator,
  168. Translator tr)
  169. : m_value(value), m_separator(separator),
  170. m_tr(tr), m_start(m_value.begin())
  171. {}
  172. template <typename String, typename Translator> inline
  173. string_path<String, Translator>::string_path(const string_path &o)
  174. : m_value(o.m_value), m_separator(o.m_separator),
  175. m_tr(o.m_tr), m_start(m_value.begin())
  176. {
  177. std::advance(m_start, std::distance(o.m_value.begin(), o.cstart()));
  178. }
  179. template <typename String, typename Translator> inline
  180. string_path<String, Translator>&
  181. string_path<String, Translator>::operator =(const string_path &o)
  182. {
  183. m_value = o.m_value;
  184. m_separator = o.m_separator;
  185. m_tr = o.m_tr;
  186. m_start = m_value.begin();
  187. std::advance(m_start, std::distance(o.m_value.begin(), o.cstart()));
  188. return *this;
  189. }
  190. template <typename String, typename Translator>
  191. typename Translator::external_type string_path<String, Translator>::reduce()
  192. {
  193. BOOST_ASSERT(!empty() && "Reducing empty path");
  194. s_iter next_sep = std::find(m_start, m_value.end(), m_separator);
  195. String part(m_start, next_sep);
  196. m_start = next_sep;
  197. if(!empty()) {
  198. // Unless we're at the end, skip the separator we found.
  199. ++m_start;
  200. }
  201. if(optional<key_type> key = m_tr.get_value(part)) {
  202. return *key;
  203. }
  204. BOOST_PROPERTY_TREE_THROW(ptree_bad_path("Path syntax error", *this));
  205. }
  206. template <typename String, typename Translator> inline
  207. bool string_path<String, Translator>::empty() const
  208. {
  209. return m_start == m_value.end();
  210. }
  211. template <typename String, typename Translator> inline
  212. bool string_path<String, Translator>::single() const
  213. {
  214. return std::find(static_cast<s_c_iter>(m_start),
  215. m_value.end(), m_separator)
  216. == m_value.end();
  217. }
  218. // By default, this is the path for strings. You can override this by
  219. // specializing path_of for a more specific form of std::basic_string.
  220. template <typename Ch, typename Traits, typename Alloc>
  221. struct path_of< std::basic_string<Ch, Traits, Alloc> >
  222. {
  223. typedef std::basic_string<Ch, Traits, Alloc> _string;
  224. typedef string_path< _string, id_translator<_string> > type;
  225. };
  226. }}
  227. #endif