File indexing completed on 2025-01-18 09:29:47
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #ifndef BOOST_CHRONO_IO_DURATION_IO_HPP
0011 #define BOOST_CHRONO_IO_DURATION_IO_HPP
0012
0013 #include <boost/chrono/duration.hpp>
0014 #include <boost/ratio/ratio_io.hpp>
0015 #include <boost/chrono/io/duration_style.hpp>
0016 #include <boost/chrono/io/ios_base_state.hpp>
0017 #include <boost/chrono/io/duration_put.hpp>
0018 #include <boost/chrono/io/duration_get.hpp>
0019 #include <boost/chrono/io/utility/manip_base.hpp>
0020 #include <boost/core/no_exceptions_support.hpp>
0021 #include <boost/type_traits/is_integral.hpp>
0022 #include <boost/type_traits/is_floating_point.hpp>
0023 #include <locale>
0024 #include <iosfwd>
0025 #include <sstream>
0026
0027 namespace boost
0028 {
0029 namespace chrono
0030 {
0031
0032
0033
0034
0035
0036 class duration_fmt: public manip<duration_fmt>
0037 {
0038 duration_style style_;
0039 public:
0040
0041
0042
0043
0044 explicit duration_fmt(duration_style style)BOOST_NOEXCEPT
0045 : style_(style)
0046 {}
0047
0048
0049
0050
0051 void operator()(std::ios_base &ios) const
0052
0053 {
0054 set_duration_style(ios, style_);
0055 }
0056 };
0057
0058
0059
0060
0061
0062
0063 struct duration_style_io_saver
0064 {
0065
0066
0067 typedef std::ios_base state_type;
0068
0069 typedef duration_style aspect_type;
0070
0071
0072
0073
0074
0075
0076 explicit duration_style_io_saver(state_type &s) :
0077 s_save_(s), a_save_(get_duration_style(s))
0078 {
0079 }
0080
0081
0082
0083
0084
0085
0086 duration_style_io_saver(state_type &s, aspect_type new_value) :
0087 s_save_(s), a_save_(get_duration_style(s))
0088 {
0089 set_duration_style(s, new_value);
0090 }
0091
0092
0093
0094
0095
0096
0097 ~duration_style_io_saver()
0098 {
0099 this->restore();
0100 }
0101
0102
0103
0104
0105 void restore()
0106 {
0107 set_duration_style(s_save_, a_save_);
0108 }
0109
0110 private:
0111 duration_style_io_saver& operator=(duration_style_io_saver const& rhs) ;
0112
0113 state_type& s_save_;
0114 aspect_type a_save_;
0115 };
0116
0117 template <class Rep>
0118 struct duration_put_enabled
0119 : integral_constant<bool,
0120 is_integral<Rep>::value || is_floating_point<Rep>::value
0121 >
0122 {};
0123
0124
0125
0126
0127
0128
0129
0130
0131
0132 template <class CharT, class Traits, class Rep, class Period>
0133 typename boost::enable_if_c< ! duration_put_enabled<Rep>::value, std::basic_ostream<CharT, Traits>& >::type
0134 operator<<(std::basic_ostream<CharT, Traits>& os, const duration<Rep, Period>& d)
0135 {
0136 std::basic_ostringstream<CharT, Traits> ostr;
0137 ostr << d.count();
0138 duration<int, Period> dd(0);
0139 bool failed = false;
0140 BOOST_TRY
0141 {
0142 std::ios_base::iostate err = std::ios_base::goodbit;
0143 BOOST_TRY
0144 {
0145 typename std::basic_ostream<CharT, Traits>::sentry opfx(os);
0146 if (bool(opfx))
0147 {
0148 if (!std::has_facet<duration_put<CharT> >(os.getloc()))
0149 {
0150 if (duration_put<CharT> ().put(os, os, os.fill(), dd, ostr.str().c_str()) .failed())
0151 {
0152 err = std::ios_base::badbit;
0153 }
0154 }
0155 else if (std::use_facet<duration_put<CharT> >(os.getloc()) .put(os, os, os.fill(), dd, ostr.str().c_str()) .failed())
0156 {
0157 err = std::ios_base::badbit;
0158 }
0159 os.width(0);
0160 }
0161 }
0162 BOOST_CATCH(...)
0163 {
0164 bool flag = false;
0165 BOOST_TRY
0166 {
0167 os.setstate(std::ios_base::failbit);
0168 }
0169 BOOST_CATCH (const std::ios_base::failure& )
0170 {
0171 flag = true;
0172 }
0173 BOOST_CATCH_END
0174 if (flag) throw;
0175 }
0176 BOOST_CATCH_END
0177 if (err) os.setstate(err);
0178 return os;
0179 }
0180 BOOST_CATCH(...)
0181 {
0182 failed = true;
0183 }
0184 BOOST_CATCH_END
0185 if (failed) os.setstate(std::ios_base::failbit | std::ios_base::badbit);
0186 return os;
0187
0188 }
0189
0190 template <class CharT, class Traits, class Rep, class Period>
0191 typename boost::enable_if_c< duration_put_enabled<Rep>::value, std::basic_ostream<CharT, Traits>& >::type
0192 operator<<(std::basic_ostream<CharT, Traits>& os, const duration<Rep, Period>& d)
0193 {
0194 bool failed = false;
0195 BOOST_TRY
0196 {
0197 std::ios_base::iostate err = std::ios_base::goodbit;
0198 BOOST_TRY
0199 {
0200 typename std::basic_ostream<CharT, Traits>::sentry opfx(os);
0201 if (bool(opfx))
0202 {
0203 if (!std::has_facet<duration_put<CharT> >(os.getloc()))
0204 {
0205 if (duration_put<CharT> ().put(os, os, os.fill(), d) .failed())
0206 {
0207 err = std::ios_base::badbit;
0208 }
0209 }
0210 else if (std::use_facet<duration_put<CharT> >(os.getloc()) .put(os, os, os.fill(), d) .failed())
0211 {
0212 err = std::ios_base::badbit;
0213 }
0214 os.width(0);
0215 }
0216 }
0217 BOOST_CATCH(...)
0218 {
0219 bool flag = false;
0220 BOOST_TRY
0221 {
0222 os.setstate(std::ios_base::failbit);
0223 }
0224 BOOST_CATCH (const std::ios_base::failure& )
0225 {
0226 flag = true;
0227 }
0228 BOOST_CATCH_END
0229 if (flag) throw;
0230 }
0231 BOOST_CATCH_END
0232 if (err) os.setstate(err);
0233 return os;
0234 }
0235 BOOST_CATCH(...)
0236 {
0237 failed = true;
0238 }
0239 BOOST_CATCH_END
0240 if (failed) os.setstate(std::ios_base::failbit | std::ios_base::badbit);
0241 return os;
0242 }
0243
0244
0245
0246
0247
0248
0249
0250 template <class CharT, class Traits, class Rep, class Period>
0251 std::basic_istream<CharT, Traits>&
0252 operator>>(std::basic_istream<CharT, Traits>& is, duration<Rep, Period>& d)
0253 {
0254 std::ios_base::iostate err = std::ios_base::goodbit;
0255
0256 BOOST_TRY
0257 {
0258 typename std::basic_istream<CharT, Traits>::sentry ipfx(is);
0259 if (bool(ipfx))
0260 {
0261 if (!std::has_facet<duration_get<CharT> >(is.getloc()))
0262 {
0263 duration_get<CharT> ().get(is, std::istreambuf_iterator<CharT, Traits>(), is, err, d);
0264 }
0265 else
0266 {
0267 std::use_facet<duration_get<CharT> >(is.getloc()) .get(is, std::istreambuf_iterator<CharT, Traits>(), is,
0268 err, d);
0269 }
0270 }
0271 }
0272 BOOST_CATCH (...)
0273 {
0274 bool flag = false;
0275 BOOST_TRY
0276 {
0277 is.setstate(std::ios_base::failbit);
0278 }
0279 BOOST_CATCH (const std::ios_base::failure& )
0280 {
0281 flag = true;
0282 }
0283 BOOST_CATCH_END
0284 if (flag) { BOOST_RETHROW }
0285 }
0286 BOOST_CATCH_END
0287 if (err) is.setstate(err);
0288 return is;
0289 }
0290
0291 }
0292
0293 }
0294
0295 #endif