stream_translator.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_STREAM_TRANSLATOR_HPP_INCLUDED
12 #define BOOST_PROPERTY_TREE_STREAM_TRANSLATOR_HPP_INCLUDED
13 
15 
16 #include <boost/optional.hpp>
21 #include <sstream>
22 #include <string>
23 #include <locale>
24 #include <limits>
25 
26 namespace boost { namespace property_tree
27 {
28 
29  template <typename Ch, typename Traits, typename E, typename Enabler = void>
31  {
32  static void insert(std::basic_ostream<Ch, Traits>& s, const E& e) {
33  s << e;
34  }
35  static void extract(std::basic_istream<Ch, Traits>& s, E& e) {
36  s >> e;
37  if(!s.eof()) {
38  s >> std::ws;
39  }
40  }
41  };
42 
43  // No whitespace skipping for single characters.
44  template <typename Ch, typename Traits>
45  struct customize_stream<Ch, Traits, Ch, void>
46  {
47  static void insert(std::basic_ostream<Ch, Traits>& s, Ch e) {
48  s << e;
49  }
50  static void extract(std::basic_istream<Ch, Traits>& s, Ch& e) {
51  s.unsetf(std::ios_base::skipws);
52  s >> e;
53  }
54  };
55 
56  // Ugly workaround for numeric_traits that don't have members when not
57  // specialized, e.g. MSVC.
58  namespace detail
59  {
60  template <bool is_specialized>
62  {
63  template <typename T>
64  struct test
65  {
67  };
68  };
69  template <>
70  struct is_inexact_impl<true>
71  {
72  template <typename T>
73  struct test
74  {
75  typedef boost::integral_constant<bool,
76  !std::numeric_limits<T>::is_exact> type;
77  };
78  };
79 
80  template <typename F>
81  struct is_inexact
82  {
83  typedef typename boost::decay<F>::type decayed;
84  typedef typename is_inexact_impl<
85  std::numeric_limits<decayed>::is_specialized
86  >::BOOST_NESTED_TEMPLATE test<decayed>::type type;
87  static const bool value = type::value;
88  };
89  }
90 
91  template <typename Ch, typename Traits, typename F>
92  struct customize_stream<Ch, Traits, F,
93  typename boost::enable_if< detail::is_inexact<F> >::type
94  >
95  {
96  static void insert(std::basic_ostream<Ch, Traits>& s, const F& e) {
97 #ifndef BOOST_NO_CXX11_NUMERIC_LIMITS
98  s.precision(std::numeric_limits<F>::max_digits10);
99 #else
100  s.precision(std::numeric_limits<F>::digits10 + 2);
101 #endif
102  s << e;
103  }
104  static void extract(std::basic_istream<Ch, Traits>& s, F& e) {
105  s >> e;
106  if(!s.eof()) {
107  s >> std::ws;
108  }
109  }
110  };
111 
112  template <typename Ch, typename Traits>
113  struct customize_stream<Ch, Traits, bool, void>
114  {
115  static void insert(std::basic_ostream<Ch, Traits>& s, bool e) {
116  s.setf(std::ios_base::boolalpha);
117  s << e;
118  }
119  static void extract(std::basic_istream<Ch, Traits>& s, bool& e) {
120  s >> e;
121  if(s.fail()) {
122  // Try again in word form.
123  s.clear();
124  s.setf(std::ios_base::boolalpha);
125  s >> e;
126  }
127  if(!s.eof()) {
128  s >> std::ws;
129  }
130  }
131  };
132 
133  template <typename Ch, typename Traits>
134  struct customize_stream<Ch, Traits, signed char, void>
135  {
136  static void insert(std::basic_ostream<Ch, Traits>& s, signed char e) {
137  s << (int)e;
138  }
139  static void extract(std::basic_istream<Ch, Traits>& s, signed char& e) {
140  int i;
141  s >> i;
142  // out of range?
143  if(i > (std::numeric_limits<signed char>::max)() ||
144  i < (std::numeric_limits<signed char>::min)())
145  {
146  s.clear(); // guarantees eof to be unset
147  e = 0;
148  s.setstate(std::ios_base::badbit);
149  return;
150  }
151  e = (signed char)i;
152  if(!s.eof()) {
153  s >> std::ws;
154  }
155  }
156  };
157 
158  template <typename Ch, typename Traits>
159  struct customize_stream<Ch, Traits, unsigned char, void>
160  {
161  static void insert(std::basic_ostream<Ch, Traits>& s, unsigned char e) {
162  s << (unsigned)e;
163  }
164  static void extract(std::basic_istream<Ch,Traits>& s, unsigned char& e){
165  unsigned i;
166  s >> i;
167  // out of range?
168  if(i > (std::numeric_limits<unsigned char>::max)()) {
169  s.clear(); // guarantees eof to be unset
170  e = 0;
171  s.setstate(std::ios_base::badbit);
172  return;
173  }
174  e = (unsigned char)i;
175  if(!s.eof()) {
176  s >> std::ws;
177  }
178  }
179  };
180 
182  template <typename Ch, typename Traits, typename Alloc, typename E>
184  {
186  public:
187  typedef std::basic_string<Ch, Traits, Alloc> internal_type;
188  typedef E external_type;
189 
190  explicit stream_translator(std::locale loc = std::locale())
191  : m_loc(loc)
192  {}
193 
195  std::basic_istringstream<Ch, Traits, Alloc> iss(v);
196  iss.imbue(m_loc);
197  E e;
198  customized::extract(iss, e);
199  if(iss.fail() || iss.bad() || iss.get() != Traits::eof()) {
200  return boost::optional<E>();
201  }
202  return e;
203  }
205  std::basic_ostringstream<Ch, Traits, Alloc> oss;
206  oss.imbue(m_loc);
207  customized::insert(oss, v);
208  if(oss) {
209  return oss.str();
210  }
212  }
213 
214  private:
215  std::locale m_loc;
216  };
217 
218  // This is the default translator when basic_string is the internal type.
219  // Unless the external type is also basic_string, in which case
220  // id_translator takes over.
221  template <typename Ch, typename Traits, typename Alloc, typename E>
222  struct translator_between<std::basic_string<Ch, Traits, Alloc>, E>
223  {
225  };
226 
227 }}
228 
229 #endif
optional_io.hpp
boost::property_tree::stream_translator::stream_translator
stream_translator(std::locale loc=std::locale())
Definition: stream_translator.hpp:190
boost::property_tree::stream_translator::m_loc
std::locale m_loc
Definition: stream_translator.hpp:215
boost::property_tree::customize_stream< Ch, Traits, F, typename boost::enable_if< detail::is_inexact< F > >::type >::extract
static void extract(std::basic_istream< Ch, Traits > &s, F &e)
Definition: stream_translator.hpp:104
s
XmlRpcServer s
integral_constant.hpp
boost::type
Definition: type.hpp:14
boost::property_tree::customize_stream< Ch, Traits, bool, void >::insert
static void insert(std::basic_ostream< Ch, Traits > &s, bool e)
Definition: stream_translator.hpp:115
boost::property_tree::customize_stream< Ch, Traits, unsigned char, void >::insert
static void insert(std::basic_ostream< Ch, Traits > &s, unsigned char e)
Definition: stream_translator.hpp:161
boost
BOOST_MOVE_USE_STANDARD_LIBRARY_MOVE.
boost::property_tree::customize_stream< Ch, Traits, bool, void >::extract
static void extract(std::basic_istream< Ch, Traits > &s, bool &e)
Definition: stream_translator.hpp:119
boost::property_tree::translator_between
Definition: ptree_fwd.hpp:80
boost::property_tree::customize_stream
Definition: stream_translator.hpp:30
boost::property_tree::detail::is_inexact
Definition: stream_translator.hpp:81
test
boost::property_tree::stream_translator::put_value
boost::optional< internal_type > put_value(const E &v)
Definition: stream_translator.hpp:204
boost::property_tree::customize_stream< Ch, Traits, Ch, void >::extract
static void extract(std::basic_istream< Ch, Traits > &s, Ch &e)
Definition: stream_translator.hpp:50
boost::property_tree::customize_stream::extract
static void extract(std::basic_istream< Ch, Traits > &s, E &e)
Definition: stream_translator.hpp:35
boost::property_tree::detail::is_inexact::decayed
boost::decay< F >::type decayed
Definition: stream_translator.hpp:83
enable_if.hpp
boost::property_tree::stream_translator::customized
customize_stream< Ch, Traits, E > customized
Definition: stream_translator.hpp:185
boost::property_tree::customize_stream< Ch, Traits, unsigned char, void >::extract
static void extract(std::basic_istream< Ch, Traits > &s, unsigned char &e)
Definition: stream_translator.hpp:164
boost::enable_if
Definition: core/enable_if.hpp:41
boost::property_tree::translator_between< std::basic_string< Ch, Traits, Alloc >, E >::type
stream_translator< Ch, Traits, Alloc, E > type
Definition: stream_translator.hpp:224
boost::property_tree::customize_stream< Ch, Traits, Ch, void >::insert
static void insert(std::basic_ostream< Ch, Traits > &s, Ch e)
Definition: stream_translator.hpp:47
BOOST_NESTED_TEMPLATE
#define BOOST_NESTED_TEMPLATE
Definition: suffix.hpp:437
boost::property_tree::detail::is_inexact_impl
Definition: stream_translator.hpp:61
boost::property_tree::stream_translator::external_type
E external_type
Definition: stream_translator.hpp:188
boost::property_tree::stream_translator::internal_type
std::basic_string< Ch, Traits, Alloc > internal_type
Definition: stream_translator.hpp:187
boost::property_tree::customize_stream::insert
static void insert(std::basic_ostream< Ch, Traits > &s, const E &e)
Definition: stream_translator.hpp:32
boost::iterators::i
D const & i
Definition: iterator_facade.hpp:956
decay.hpp
boost::property_tree::stream_translator::get_value
boost::optional< E > get_value(const internal_type &v)
Definition: stream_translator.hpp:194
boost::property_tree::detail::is_inexact_impl::test::type
boost::false_type type
Definition: stream_translator.hpp:66
Ch
#define Ch(x, y, z)
Definition: SHA256.cpp:47
std
boost::property_tree::customize_stream< Ch, Traits, signed char, void >::insert
static void insert(std::basic_ostream< Ch, Traits > &s, signed char e)
Definition: stream_translator.hpp:136
boost::decay::type
boost::detail::decay_imp< Ty, boost::is_array< Ty >::value, boost::is_function< Ty >::value >::type type
Definition: decay.hpp:37
boost::property_tree::detail::is_inexact::value
static const bool value
Definition: stream_translator.hpp:87
boost::property_tree::customize_stream< Ch, Traits, signed char, void >::extract
static void extract(std::basic_istream< Ch, Traits > &s, signed char &e)
Definition: stream_translator.hpp:139
boost::property_tree::detail::is_inexact_impl< true >::test::type
boost::integral_constant< bool, !std::numeric_limits< T >::is_exact > type
Definition: stream_translator.hpp:76
ptree_fwd.hpp
boost::integral_constant< bool, false >
boost::optional
Definition: old_optional_implementation.hpp:646
boost::property_tree::detail::is_inexact::type
is_inexact_impl< std::numeric_limits< decayed >::is_specialized >::BOOST_NESTED_TEMPLATE test< decayed >::type type
Definition: stream_translator.hpp:86
boost::property_tree::customize_stream< Ch, Traits, F, typename boost::enable_if< detail::is_inexact< F > >::type >::insert
static void insert(std::basic_ostream< Ch, Traits > &s, const F &e)
Definition: stream_translator.hpp:96
optional.hpp
boost::property_tree::stream_translator
Implementation of Translator that uses the stream overloads.
Definition: stream_translator.hpp:183


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