1 // (C) Copyright Howard Hinnant
2 // (C) Copyright 2011 Vicente J. Botet Escriba
3 // Use, modification and distribution are subject to the Boost Software License,
4 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt).
7 // This code was adapted by Vicente from Howard Hinnant's experimental work
8 // on chrono i/o to Boost
10 #ifndef BOOST_CHRONO_IO_DURATION_IO_HPP
11 #define BOOST_CHRONO_IO_DURATION_IO_HPP
13 #include <boost/chrono/duration.hpp>
14 #include <boost/ratio/ratio_io.hpp>
15 #include <boost/chrono/io/duration_style.hpp>
16 #include <boost/chrono/io/ios_base_state.hpp>
17 #include <boost/chrono/io/duration_put.hpp>
18 #include <boost/chrono/io/duration_get.hpp>
19 #include <boost/chrono/io/utility/manip_base.hpp>
20 #include <boost/detail/no_exceptions_support.hpp>
21 #include <boost/type_traits/is_integral.hpp>
22 #include <boost/type_traits/is_floating_point.hpp>
33 * duration parameterized manipulator.
36 class duration_fmt: public manip<duration_fmt>
38 duration_style style_;
42 * explicit manipulator constructor from a @c duration_style
44 explicit duration_fmt(duration_style style)BOOST_NOEXCEPT
49 * Change the duration_style ios state;
51 void operator()(std::ios_base &ios) const
54 set_duration_style(ios, style_);
59 * duration_style i/o saver.
61 * See Boost.IO i/o state savers for a motivating compression.
63 struct duration_style_io_saver
66 //! the type of the state to restore
67 typedef std::ios_base state_type;
68 //! the type of aspect to save
69 typedef duration_style aspect_type;
72 * Explicit construction from an i/o stream.
74 * Store a reference to the i/o stream and the value of the associated @c duration_style.
76 explicit duration_style_io_saver(state_type &s) :
77 s_save_(s), a_save_(get_duration_style(s))
82 * Construction from an i/o stream and a @c duration_style to restore.
84 * Stores a reference to the i/o stream and the value @c new_value @c duration_style to set.
86 duration_style_io_saver(state_type &s, aspect_type new_value) :
87 s_save_(s), a_save_(get_duration_style(s))
89 set_duration_style(s, new_value);
95 * Restores the i/o stream with the duration_style to be restored.
97 ~duration_style_io_saver()
103 * Restores the i/o stream with the duration_style to be restored.
107 set_duration_style(s_save_, a_save_);
111 duration_style_io_saver& operator=(duration_style_io_saver const& rhs) ;
118 struct duration_put_enabled
119 : integral_constant<bool,
120 is_integral<Rep>::value || is_floating_point<Rep>::value
126 * duration stream inserter
127 * @param os the output stream
128 * @param d to value to insert
132 template <class CharT, class Traits, class Rep, class Period>
133 typename boost::enable_if_c< ! duration_put_enabled<Rep>::value, std::basic_ostream<CharT, Traits>& >::type
134 operator<<(std::basic_ostream<CharT, Traits>& os, const duration<Rep, Period>& d)
136 std::basic_ostringstream<CharT, Traits> ostr;
138 duration<int, Period> dd(0);
142 std::ios_base::iostate err = std::ios_base::goodbit;
145 typename std::basic_ostream<CharT, Traits>::sentry opfx(os);
148 if (!std::has_facet<duration_put<CharT> >(os.getloc()))
150 if (duration_put<CharT> ().put(os, os, os.fill(), dd, ostr.str().c_str()) .failed())
152 err = std::ios_base::badbit;
155 else if (std::use_facet<duration_put<CharT> >(os.getloc()) .put(os, os, os.fill(), dd, ostr.str().c_str()) .failed())
157 err = std::ios_base::badbit;
167 os.setstate(std::ios_base::failbit);
169 BOOST_CATCH (std::ios_base::failure )
177 if (err) os.setstate(err);
185 if (failed) os.setstate(std::ios_base::failbit | std::ios_base::badbit);
190 template <class CharT, class Traits, class Rep, class Period>
191 typename boost::enable_if_c< duration_put_enabled<Rep>::value, std::basic_ostream<CharT, Traits>& >::type
192 operator<<(std::basic_ostream<CharT, Traits>& os, const duration<Rep, Period>& d)
197 std::ios_base::iostate err = std::ios_base::goodbit;
200 typename std::basic_ostream<CharT, Traits>::sentry opfx(os);
203 if (!std::has_facet<duration_put<CharT> >(os.getloc()))
205 if (duration_put<CharT> ().put(os, os, os.fill(), d) .failed())
207 err = std::ios_base::badbit;
210 else if (std::use_facet<duration_put<CharT> >(os.getloc()) .put(os, os, os.fill(), d) .failed())
212 err = std::ios_base::badbit;
222 os.setstate(std::ios_base::failbit);
224 BOOST_CATCH (std::ios_base::failure )
232 if (err) os.setstate(err);
240 if (failed) os.setstate(std::ios_base::failbit | std::ios_base::badbit);
246 * @param is the input stream
247 * @param d the duration
250 template <class CharT, class Traits, class Rep, class Period>
251 std::basic_istream<CharT, Traits>&
252 operator>>(std::basic_istream<CharT, Traits>& is, duration<Rep, Period>& d)
254 std::ios_base::iostate err = std::ios_base::goodbit;
258 typename std::basic_istream<CharT, Traits>::sentry ipfx(is);
261 if (!std::has_facet<duration_get<CharT> >(is.getloc()))
263 duration_get<CharT> ().get(is, std::istreambuf_iterator<CharT, Traits>(), is, err, d);
267 std::use_facet<duration_get<CharT> >(is.getloc()) .get(is, std::istreambuf_iterator<CharT, Traits>(), is,
277 is.setstate(std::ios_base::failbit);
279 BOOST_CATCH (std::ios_base::failure )
284 if (flag) { BOOST_RETHROW }
287 if (err) is.setstate(err);