duration_io.hpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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).
  6. //
  7. // This code was adapted by Vicente from Howard Hinnant's experimental work
  8. // on chrono i/o to Boost
  9. #ifndef BOOST_CHRONO_IO_DURATION_IO_HPP
  10. #define BOOST_CHRONO_IO_DURATION_IO_HPP
  11. #include <boost/chrono/duration.hpp>
  12. #include <boost/ratio/ratio_io.hpp>
  13. #include <boost/chrono/io/duration_style.hpp>
  14. #include <boost/chrono/io/ios_base_state.hpp>
  15. #include <boost/chrono/io/duration_put.hpp>
  16. #include <boost/chrono/io/duration_get.hpp>
  17. #include <boost/chrono/io/utility/manip_base.hpp>
  18. #include <boost/core/no_exceptions_support.hpp>
  19. #include <boost/type_traits/is_integral.hpp>
  20. #include <boost/type_traits/is_floating_point.hpp>
  21. #include <locale>
  22. #include <iosfwd>
  23. #include <sstream>
  24. namespace boost
  25. {
  26. namespace chrono
  27. {
  28. /**
  29. * duration parameterized manipulator.
  30. */
  31. class duration_fmt: public manip<duration_fmt>
  32. {
  33. duration_style style_;
  34. public:
  35. /**
  36. * explicit manipulator constructor from a @c duration_style
  37. */
  38. explicit duration_fmt(duration_style style)BOOST_NOEXCEPT
  39. : style_(style)
  40. {}
  41. /**
  42. * Change the duration_style ios state;
  43. */
  44. void operator()(std::ios_base &ios) const
  45. {
  46. set_duration_style(ios, style_);
  47. }
  48. };
  49. /**
  50. * duration_style i/o saver.
  51. *
  52. * See Boost.IO i/o state savers for a motivating compression.
  53. */
  54. struct duration_style_io_saver
  55. {
  56. //! the type of the state to restore
  57. typedef std::ios_base state_type;
  58. //! the type of aspect to save
  59. typedef duration_style aspect_type;
  60. /**
  61. * Explicit construction from an i/o stream.
  62. *
  63. * Store a reference to the i/o stream and the value of the associated @c duration_style.
  64. */
  65. explicit duration_style_io_saver(state_type &s) :
  66. s_save_(s), a_save_(get_duration_style(s))
  67. {
  68. }
  69. /**
  70. * Construction from an i/o stream and a @c duration_style to restore.
  71. *
  72. * Stores a reference to the i/o stream and the value @c new_value @c duration_style to set.
  73. */
  74. duration_style_io_saver(state_type &s, aspect_type new_value) :
  75. s_save_(s), a_save_(get_duration_style(s))
  76. {
  77. set_duration_style(s, new_value);
  78. }
  79. /**
  80. * Destructor.
  81. *
  82. * Restores the i/o stream with the duration_style to be restored.
  83. */
  84. ~duration_style_io_saver()
  85. {
  86. this->restore();
  87. }
  88. /**
  89. * Restores the i/o stream with the duration_style to be restored.
  90. */
  91. void restore()
  92. {
  93. set_duration_style(s_save_, a_save_);
  94. }
  95. private:
  96. duration_style_io_saver& operator=(duration_style_io_saver const& rhs) ;
  97. state_type& s_save_;
  98. aspect_type a_save_;
  99. };
  100. template <class Rep>
  101. struct duration_put_enabled
  102. : integral_constant<bool,
  103. is_integral<Rep>::value || is_floating_point<Rep>::value
  104. >
  105. {};
  106. /**
  107. * duration stream inserter
  108. * @param os the output stream
  109. * @param d to value to insert
  110. * @return @c os
  111. */
  112. template <class CharT, class Traits, class Rep, class Period>
  113. typename boost::enable_if_c< ! duration_put_enabled<Rep>::value, std::basic_ostream<CharT, Traits>& >::type
  114. operator<<(std::basic_ostream<CharT, Traits>& os, const duration<Rep, Period>& d)
  115. {
  116. std::basic_ostringstream<CharT, Traits> ostr;
  117. ostr << d.count();
  118. duration<int, Period> dd(0);
  119. bool failed = false;
  120. BOOST_TRY
  121. {
  122. std::ios_base::iostate err = std::ios_base::goodbit;
  123. BOOST_TRY
  124. {
  125. typename std::basic_ostream<CharT, Traits>::sentry opfx(os);
  126. if (bool(opfx))
  127. {
  128. if (!std::has_facet<duration_put<CharT> >(os.getloc()))
  129. {
  130. if (duration_put<CharT> ().put(os, os, os.fill(), dd, ostr.str().c_str()) .failed())
  131. {
  132. err = std::ios_base::badbit;
  133. }
  134. }
  135. else if (std::use_facet<duration_put<CharT> >(os.getloc()) .put(os, os, os.fill(), dd, ostr.str().c_str()) .failed())
  136. {
  137. err = std::ios_base::badbit;
  138. }
  139. os.width(0);
  140. }
  141. }
  142. BOOST_CATCH(...)
  143. {
  144. bool flag = false;
  145. BOOST_TRY
  146. {
  147. os.setstate(std::ios_base::failbit);
  148. }
  149. BOOST_CATCH (const std::ios_base::failure& )
  150. {
  151. flag = true;
  152. }
  153. BOOST_CATCH_END
  154. if (flag) throw;
  155. }
  156. BOOST_CATCH_END
  157. if (err) os.setstate(err);
  158. return os;
  159. }
  160. BOOST_CATCH(...)
  161. {
  162. failed = true;
  163. }
  164. BOOST_CATCH_END
  165. if (failed) os.setstate(std::ios_base::failbit | std::ios_base::badbit);
  166. return os;
  167. }
  168. template <class CharT, class Traits, class Rep, class Period>
  169. typename boost::enable_if_c< duration_put_enabled<Rep>::value, std::basic_ostream<CharT, Traits>& >::type
  170. operator<<(std::basic_ostream<CharT, Traits>& os, const duration<Rep, Period>& d)
  171. {
  172. bool failed = false;
  173. BOOST_TRY
  174. {
  175. std::ios_base::iostate err = std::ios_base::goodbit;
  176. BOOST_TRY
  177. {
  178. typename std::basic_ostream<CharT, Traits>::sentry opfx(os);
  179. if (bool(opfx))
  180. {
  181. if (!std::has_facet<duration_put<CharT> >(os.getloc()))
  182. {
  183. if (duration_put<CharT> ().put(os, os, os.fill(), d) .failed())
  184. {
  185. err = std::ios_base::badbit;
  186. }
  187. }
  188. else if (std::use_facet<duration_put<CharT> >(os.getloc()) .put(os, os, os.fill(), d) .failed())
  189. {
  190. err = std::ios_base::badbit;
  191. }
  192. os.width(0);
  193. }
  194. }
  195. BOOST_CATCH(...)
  196. {
  197. bool flag = false;
  198. BOOST_TRY
  199. {
  200. os.setstate(std::ios_base::failbit);
  201. }
  202. BOOST_CATCH (const std::ios_base::failure& )
  203. {
  204. flag = true;
  205. }
  206. BOOST_CATCH_END
  207. if (flag) throw;
  208. }
  209. BOOST_CATCH_END
  210. if (err) os.setstate(err);
  211. return os;
  212. }
  213. BOOST_CATCH(...)
  214. {
  215. failed = true;
  216. }
  217. BOOST_CATCH_END
  218. if (failed) os.setstate(std::ios_base::failbit | std::ios_base::badbit);
  219. return os;
  220. }
  221. /**
  222. *
  223. * @param is the input stream
  224. * @param d the duration
  225. * @return @c is
  226. */
  227. template <class CharT, class Traits, class Rep, class Period>
  228. std::basic_istream<CharT, Traits>&
  229. operator>>(std::basic_istream<CharT, Traits>& is, duration<Rep, Period>& d)
  230. {
  231. std::ios_base::iostate err = std::ios_base::goodbit;
  232. BOOST_TRY
  233. {
  234. typename std::basic_istream<CharT, Traits>::sentry ipfx(is);
  235. if (bool(ipfx))
  236. {
  237. if (!std::has_facet<duration_get<CharT> >(is.getloc()))
  238. {
  239. duration_get<CharT> ().get(is, std::istreambuf_iterator<CharT, Traits>(), is, err, d);
  240. }
  241. else
  242. {
  243. std::use_facet<duration_get<CharT> >(is.getloc()) .get(is, std::istreambuf_iterator<CharT, Traits>(), is,
  244. err, d);
  245. }
  246. }
  247. }
  248. BOOST_CATCH (...)
  249. {
  250. bool flag = false;
  251. BOOST_TRY
  252. {
  253. is.setstate(std::ios_base::failbit);
  254. }
  255. BOOST_CATCH (const std::ios_base::failure& )
  256. {
  257. flag = true;
  258. }
  259. BOOST_CATCH_END
  260. if (flag) { BOOST_RETHROW }
  261. }
  262. BOOST_CATCH_END
  263. if (err) is.setstate(err);
  264. return is;
  265. }
  266. } // chrono
  267. }
  268. #endif // header