/Src/Dependencies/Boost/boost/property_tree/string_path.hpp

http://hadesmem.googlecode.com/ · C++ Header · 275 lines · 193 code · 30 blank · 52 comment · 11 complexity · 2125134d600b52308d2ac52584fdf4b5 MD5 · raw file

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