1 // ----------------------------------------------------------------------------
2 // Copyright (C) 2009 Sebastian Redl
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)
8 // For more information, see www.boost.org
9 // ----------------------------------------------------------------------------
11 #ifndef BOOST_PROPERTY_TREE_STREAM_TRANSLATOR_HPP_INCLUDED
12 #define BOOST_PROPERTY_TREE_STREAM_TRANSLATOR_HPP_INCLUDED
14 #include <boost/property_tree/ptree_fwd.hpp>
16 #include <boost/optional.hpp>
17 #include <boost/optional/optional_io.hpp>
18 #include <boost/utility/enable_if.hpp>
19 #include <boost/type_traits/decay.hpp>
20 #include <boost/type_traits/integral_constant.hpp>
26 namespace boost { namespace property_tree
29 template <typename Ch, typename Traits, typename E, typename Enabler = void>
30 struct customize_stream
32 static void insert(std::basic_ostream<Ch, Traits>& s, const E& e) {
35 static void extract(std::basic_istream<Ch, Traits>& s, E& e) {
43 // No whitespace skipping for single characters.
44 template <typename Ch, typename Traits>
45 struct customize_stream<Ch, Traits, Ch, void>
47 static void insert(std::basic_ostream<Ch, Traits>& s, Ch e) {
50 static void extract(std::basic_istream<Ch, Traits>& s, Ch& e) {
51 s.unsetf(std::ios_base::skipws);
56 // Ugly workaround for numeric_traits that don't have members when not
57 // specialized, e.g. MSVC.
60 template <bool is_specialized>
61 struct is_inexact_impl
66 typedef boost::false_type type;
70 struct is_inexact_impl<true>
75 typedef boost::integral_constant<bool,
76 !std::numeric_limits<T>::is_exact> type;
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;
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
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);
100 s.precision(std::numeric_limits<F>::digits10 + 2);
104 static void extract(std::basic_istream<Ch, Traits>& s, F& e) {
112 template <typename Ch, typename Traits>
113 struct customize_stream<Ch, Traits, bool, void>
115 static void insert(std::basic_ostream<Ch, Traits>& s, bool e) {
116 s.setf(std::ios_base::boolalpha);
119 static void extract(std::basic_istream<Ch, Traits>& s, bool& e) {
122 // Try again in word form.
124 s.setf(std::ios_base::boolalpha);
133 template <typename Ch, typename Traits>
134 struct customize_stream<Ch, Traits, signed char, void>
136 static void insert(std::basic_ostream<Ch, Traits>& s, signed char e) {
139 static void extract(std::basic_istream<Ch, Traits>& s, signed char& e) {
143 if(i > (std::numeric_limits<signed char>::max)() ||
144 i < (std::numeric_limits<signed char>::min)())
146 s.clear(); // guarantees eof to be unset
148 s.setstate(std::ios_base::badbit);
158 template <typename Ch, typename Traits>
159 struct customize_stream<Ch, Traits, unsigned char, void>
161 static void insert(std::basic_ostream<Ch, Traits>& s, unsigned char e) {
164 static void extract(std::basic_istream<Ch,Traits>& s, unsigned char& e){
168 if(i > (std::numeric_limits<unsigned char>::max)()) {
169 s.clear(); // guarantees eof to be unset
171 s.setstate(std::ios_base::badbit);
174 e = (unsigned char)i;
181 /// Implementation of Translator that uses the stream overloads.
182 template <typename Ch, typename Traits, typename Alloc, typename E>
183 class stream_translator
185 typedef customize_stream<Ch, Traits, E> customized;
187 typedef std::basic_string<Ch, Traits, Alloc> internal_type;
188 typedef E external_type;
190 explicit stream_translator(std::locale loc = std::locale())
194 boost::optional<E> get_value(const internal_type &v) {
195 std::basic_istringstream<Ch, Traits, Alloc> iss(v);
198 customized::extract(iss, e);
199 if(iss.fail() || iss.bad() || iss.get() != Traits::eof()) {
200 return boost::optional<E>();
204 boost::optional<internal_type> put_value(const E &v) {
205 std::basic_ostringstream<Ch, Traits, Alloc> oss;
207 customized::insert(oss, v);
211 return boost::optional<internal_type>();
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>
224 typedef stream_translator<Ch, Traits, Alloc, E> type;