string_path.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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.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. /// Append a second path to this one.
  119. /// @pre o's separator is the same as this one's, or o has no separators
  120. string_path& operator /=(const string_path &o) {
  121. // If it's single, there's no separator. This allows to do
  122. // p /= "piece";
  123. // even for non-default separators.
  124. BOOST_ASSERT((m_separator == o.m_separator
  125. || o.empty()
  126. || o.single())
  127. && "Incompatible paths.");
  128. if(!o.empty()) {
  129. String sub;
  130. if(!this->empty()) {
  131. sub.push_back(m_separator);
  132. }
  133. sub.insert(sub.end(), o.cstart(), o.m_value.end());
  134. detail::append_and_preserve_iter(m_value, sub, m_start,
  135. typename std::iterator_traits<s_iter>::iterator_category());
  136. }
  137. return *this;
  138. }
  139. private:
  140. typedef typename String::iterator s_iter;
  141. typedef typename String::const_iterator s_c_iter;
  142. String m_value;
  143. char_type m_separator;
  144. Translator m_tr;
  145. s_iter m_start;
  146. s_c_iter cstart() const { return m_start; }
  147. };
  148. template <typename String, typename Translator> inline
  149. string_path<String, Translator>::string_path(char_type separator)
  150. : m_separator(separator), m_start(m_value.begin())
  151. {}
  152. template <typename String, typename Translator> inline
  153. string_path<String, Translator>::string_path(const String &value,
  154. char_type separator,
  155. Translator tr)
  156. : m_value(value), m_separator(separator),
  157. m_tr(tr), m_start(m_value.begin())
  158. {}
  159. template <typename String, typename Translator> inline
  160. string_path<String, Translator>::string_path(const char_type *value,
  161. char_type separator,
  162. Translator tr)
  163. : m_value(value), m_separator(separator),
  164. m_tr(tr), m_start(m_value.begin())
  165. {}
  166. template <typename String, typename Translator> inline
  167. string_path<String, Translator>::string_path(const string_path &o)
  168. : m_value(o.m_value), m_separator(o.m_separator),
  169. m_tr(o.m_tr), m_start(m_value.begin())
  170. {
  171. std::advance(m_start, std::distance(o.m_value.begin(), o.cstart()));
  172. }
  173. template <typename String, typename Translator> inline
  174. string_path<String, Translator>&
  175. string_path<String, Translator>::operator =(const string_path &o)
  176. {
  177. m_value = o.m_value;
  178. m_separator = o.m_separator;
  179. m_tr = o.m_tr;
  180. m_start = m_value.begin();
  181. std::advance(m_start, std::distance(o.m_value.begin(), o.cstart()));
  182. return *this;
  183. }
  184. template <typename String, typename Translator>
  185. typename Translator::external_type string_path<String, Translator>::reduce()
  186. {
  187. BOOST_ASSERT(!empty() && "Reducing empty path");
  188. s_iter next_sep = std::find(m_start, m_value.end(), m_separator);
  189. String part(m_start, next_sep);
  190. m_start = next_sep;
  191. if(!empty()) {
  192. // Unless we're at the end, skip the separator we found.
  193. ++m_start;
  194. }
  195. if(optional<key_type> key = m_tr.get_value(part)) {
  196. return *key;
  197. }
  198. BOOST_PROPERTY_TREE_THROW(ptree_bad_path("Path syntax error", *this));
  199. }
  200. template <typename String, typename Translator> inline
  201. bool string_path<String, Translator>::empty() const
  202. {
  203. return m_start == m_value.end();
  204. }
  205. template <typename String, typename Translator> inline
  206. bool string_path<String, Translator>::single() const
  207. {
  208. return std::find(static_cast<s_c_iter>(m_start),
  209. m_value.end(), m_separator)
  210. == m_value.end();
  211. }
  212. // By default, this is the path for strings. You can override this by
  213. // specializing path_of for a more specific form of std::basic_string.
  214. template <typename Ch, typename Traits, typename Alloc>
  215. struct path_of< std::basic_string<Ch, Traits, Alloc> >
  216. {
  217. typedef std::basic_string<Ch, Traits, Alloc> _string;
  218. typedef string_path< _string, id_translator<_string> > type;
  219. };
  220. template <typename String, typename Translator> inline
  221. string_path<String, Translator> operator /(
  222. string_path<String, Translator> p1,
  223. const string_path<String, Translator> &p2)
  224. {
  225. p1 /= p2;
  226. return p1;
  227. }
  228. // These shouldn't be necessary, but GCC won't find the one above.
  229. template <typename String, typename Translator> inline
  230. string_path<String, Translator> operator /(
  231. string_path<String, Translator> p1,
  232. const typename String::value_type *p2)
  233. {
  234. p1 /= p2;
  235. return p1;
  236. }
  237. template <typename String, typename Translator> inline
  238. string_path<String, Translator> operator /(
  239. const typename String::value_type *p1,
  240. const string_path<String, Translator> &p2)
  241. {
  242. string_path<String, Translator> t(p1);
  243. t /= p2;
  244. return t;
  245. }
  246. }}
  247. #endif