string_path.hpp
Go to the documentation of this 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 
11 #ifndef BOOST_PROPERTY_TREE_STRING_PATH_HPP_INCLUDED
12 #define BOOST_PROPERTY_TREE_STRING_PATH_HPP_INCLUDED
13 
18 
19 #include <boost/static_assert.hpp>
20 #include <boost/assert.hpp>
22 #include <boost/optional.hpp>
24 #include <algorithm>
25 #include <string>
26 #include <iterator>
27 
28 namespace boost { namespace property_tree
29 {
30  namespace detail
31  {
32  template <typename Sequence, typename Iterator>
33  void append_and_preserve_iter(Sequence &s, const Sequence &r,
34  Iterator &, std::forward_iterator_tag)
35  {
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());
39  }
40  template <typename Sequence, typename Iterator>
41  void append_and_preserve_iter(Sequence &s, const Sequence &r,
42  Iterator &it,
43  std::random_access_iterator_tag)
44  {
45  // Convert the iterator to an index, and later back.
46  typename std::iterator_traits<Iterator>::difference_type idx =
47  it - s.begin();
48  s.insert(s.end(), r.begin(), r.end());
49  it = s.begin() + idx;
50  }
51 
52  template <typename Sequence>
53  inline std::string dump_sequence(const Sequence &)
54  {
55  return "<undumpable sequence>";
56  }
57  inline std::string dump_sequence(const std::string &s)
58  {
59  return s;
60  }
61 #ifndef BOOST_NO_STD_WSTRING
62  inline std::string dump_sequence(const std::wstring &s)
63  {
64  return narrow<std::string>(s.c_str());
65  }
66 #endif
67  }
68 
81  template <typename String, typename Translator>
82  class string_path
83  {
85  typename Translator::internal_type>::value));
86  public:
87  typedef typename Translator::external_type key_type;
88  typedef typename String::value_type char_type;
89 
91  explicit string_path(char_type separator = char_type('.'));
98  string_path(const String &value, char_type separator = char_type('.'),
99  Translator tr = Translator());
108  string_path(const char_type *value,
110  Translator tr = Translator());
111 
112  // Default copying doesn't do the right thing with the iterator
113  string_path(const string_path &o);
115 
117  key_type reduce();
118 
120  bool empty() const;
121 
123  bool single() const;
124 
126  char_type separator() const { return m_separator; }
127 
128  std::string dump() const {
130  }
131 
135  // If it's single, there's no separator. This allows to do
136  // p /= "piece";
137  // even for non-default separators.
138  BOOST_ASSERT((m_separator == o.m_separator
139  || o.empty()
140  || o.single())
141  && "Incompatible paths.");
142  if(!o.empty()) {
143  String sub;
144  if(!this->empty()) {
145  sub.push_back(m_separator);
146  }
147  sub.insert(sub.end(), o.cstart(), o.m_value.end());
149  typename std::iterator_traits<s_iter>::iterator_category());
150  }
151  return *this;
152  }
153 
154  private:
155  typedef typename String::iterator s_iter;
156  typedef typename String::const_iterator s_c_iter;
157  String m_value;
159  Translator m_tr;
161  s_c_iter cstart() const { return m_start; }
162  };
163 
164  template <typename String, typename Translator> inline
166  : m_separator(separator), m_start(m_value.begin())
167  {}
168 
169  template <typename String, typename Translator> inline
171  char_type separator,
172  Translator tr)
173  : m_value(value), m_separator(separator),
174  m_tr(tr), m_start(m_value.begin())
175  {}
176 
177  template <typename String, typename Translator> inline
179  char_type separator,
180  Translator tr)
181  : m_value(value), m_separator(separator),
182  m_tr(tr), m_start(m_value.begin())
183  {}
184 
185  template <typename String, typename Translator> inline
187  : m_value(o.m_value), m_separator(o.m_separator),
188  m_tr(o.m_tr), m_start(m_value.begin())
189  {
190  std::advance(m_start, std::distance(o.m_value.begin(), o.cstart()));
191  }
192 
193  template <typename String, typename Translator> inline
196  {
197  m_value = o.m_value;
198  m_separator = o.m_separator;
199  m_tr = o.m_tr;
200  m_start = m_value.begin();
201  std::advance(m_start, std::distance(o.m_value.begin(), o.cstart()));
202  return *this;
203  }
204 
205  template <typename String, typename Translator>
206  typename Translator::external_type string_path<String, Translator>::reduce()
207  {
208  BOOST_ASSERT(!empty() && "Reducing empty path");
209 
210  s_iter next_sep = std::find(m_start, m_value.end(), m_separator);
211  String part(m_start, next_sep);
212  m_start = next_sep;
213  if(!empty()) {
214  // Unless we're at the end, skip the separator we found.
215  ++m_start;
216  }
217 
218  if(optional<key_type> key = m_tr.get_value(part)) {
219  return *key;
220  }
221  BOOST_PROPERTY_TREE_THROW(ptree_bad_path("Path syntax error", *this));
222  }
223 
224  template <typename String, typename Translator> inline
226  {
227  return m_start == m_value.end();
228  }
229 
230  template <typename String, typename Translator> inline
232  {
233  return std::find(static_cast<s_c_iter>(m_start),
234  m_value.end(), m_separator)
235  == m_value.end();
236  }
237 
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> >
242  {
243  typedef std::basic_string<Ch, Traits, Alloc> _string;
245  };
246 
247  template <typename String, typename Translator> inline
251  {
252  p1 /= p2;
253  return p1;
254  }
255 
256  // These shouldn't be necessary, but GCC won't find the one above.
257  template <typename String, typename Translator> inline
260  const typename String::value_type *p2)
261  {
262  p1 /= p2;
263  return p1;
264  }
265 
266  template <typename String, typename Translator> inline
268  const typename String::value_type *p1,
270  {
272  t /= p2;
273  return t;
274  }
275 
276 }}
277 
278 #endif
boost::property_tree::string_path::operator/=
string_path & operator/=(const string_path &o)
Definition: string_path.hpp:134
boost::property_tree::path_of< std::basic_string< Ch, Traits, Alloc > >::_string
std::basic_string< Ch, Traits, Alloc > _string
Definition: string_path.hpp:243
exceptions.hpp
boost::property_tree::path_of
Definition: ptree_fwd.hpp:73
boost::property_tree::string_path::string_path
string_path(char_type separator=char_type('.'))
Create an empty path.
Definition: string_path.hpp:165
boost::property_tree::string_path::m_separator
char_type m_separator
Definition: string_path.hpp:158
s
XmlRpcServer s
static_assert.hpp
boost::property_tree::path_of< std::basic_string< Ch, Traits, Alloc > >::type
string_path< _string, id_translator< _string > > type
Definition: string_path.hpp:244
ptree_utils.hpp
boost::property_tree::string_path::single
bool single() const
Test if the path contains a single element, i.e. no separators.
Definition: string_path.hpp:231
boost::property_tree::string_path::m_value
String m_value
Definition: string_path.hpp:157
boost::property_tree::operator/
string_path< String, Translator > operator/(string_path< String, Translator > p1, const string_path< String, Translator > &p2)
Definition: string_path.hpp:248
boost::property_tree::detail::dump_sequence
std::string dump_sequence(const Sequence &)
Definition: string_path.hpp:53
boost
BOOST_MOVE_USE_STANDARD_LIBRARY_MOVE.
boost::property_tree::string_path::s_c_iter
String::const_iterator s_c_iter
Definition: string_path.hpp:156
boost::is_same
Definition: type_traits/is_same.hpp:29
boost::foreach_detail_::begin
auto_any< BOOST_DEDUCED_TYPENAME foreach_iterator< T, C >::type > begin(auto_any_t col, type2type< T, C > *, boost::mpl::true_ *)
Definition: foreach.hpp:660
boost::property_tree::string_path::dump
std::string dump() const
Definition: string_path.hpp:128
boost::property_tree::string_path::cstart
s_c_iter cstart() const
Definition: string_path.hpp:161
boost::property_tree::string_path::m_start
s_iter m_start
Definition: string_path.hpp:160
BOOST_ASSERT
#define BOOST_ASSERT(expr)
Definition: assert.hpp:60
BOOST_PROPERTY_TREE_THROW
#define BOOST_PROPERTY_TREE_THROW(e)
Definition: ptree_fwd.hpp:140
boost::property_tree::string_path::BOOST_STATIC_ASSERT
BOOST_STATIC_ASSERT((is_same< String, typename Translator::internal_type >::value))
boost::property_tree::string_path::operator=
string_path & operator=(const string_path &o)
Definition: string_path.hpp:195
boost::property_tree::string_path::reduce
key_type reduce()
Take a single element off the path at the front and return it.
Definition: string_path.hpp:206
is_same.hpp
assert.hpp
boost::property_tree::string_path
Definition: ptree_fwd.hpp:36
boost::property_tree::string_path::s_iter
String::iterator s_iter
Definition: string_path.hpp:155
boost::property_tree::string_path::empty
bool empty() const
Test if the path is empty.
Definition: string_path.hpp:225
std
boost::property_tree::string_path::separator
char_type separator() const
Get the separator used by this path.
Definition: string_path.hpp:126
boost::property_tree::string_path::key_type
Translator::external_type key_type
Definition: string_path.hpp:87
id_translator.hpp
boost::property_tree::string_path::m_tr
Translator m_tr
Definition: string_path.hpp:159
boost::property_tree::detail::append_and_preserve_iter
void append_and_preserve_iter(Sequence &s, const Sequence &r, Iterator &, std::forward_iterator_tag)
Definition: string_path.hpp:33
throw_exception.hpp
ptree_fwd.hpp
boost::property_tree::ptree_bad_path
Definition: exceptions.hpp:64
boost::optional
Definition: old_optional_implementation.hpp:646
boost::property_tree::string_path::char_type
String::value_type char_type
Definition: string_path.hpp:88
optional.hpp


sick_visionary_ros
Author(s): SICK AG TechSupport 3D Snapshot
autogenerated on Thu Feb 8 2024 03:48:41