duration_put.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. /**
  8. * Duration formatting facet for output.
  9. */
  10. #ifndef BOOST_CHRONO_IO_DURATION_PUT_HPP
  11. #define BOOST_CHRONO_IO_DURATION_PUT_HPP
  12. #include <boost/chrono/config.hpp>
  13. #include <boost/chrono/io/duration_units.hpp>
  14. #include <boost/chrono/process_cpu_clocks.hpp>
  15. #include <boost/assert.hpp>
  16. #include <locale>
  17. namespace boost
  18. {
  19. namespace chrono
  20. {
  21. namespace detail
  22. {
  23. template <class T>
  24. struct propagate {
  25. typedef T type;
  26. };
  27. template <>
  28. struct propagate<boost::int_least32_t> {
  29. typedef boost::int_least64_t type;
  30. };
  31. }
  32. /**
  33. * @tparam ChatT a character type
  34. * @tparam OutputIterator a model of @c OutputIterator
  35. *
  36. * The @c duration_put facet provides facilities for formatted output of duration values.
  37. * The member function of @c duration_put take a duration and format it into character string representation.
  38. *
  39. */
  40. template <class CharT, class OutputIterator = std::ostreambuf_iterator<CharT> >
  41. class duration_put: public std::locale::facet
  42. {
  43. public:
  44. /**
  45. * Type of character the facet is instantiated on.
  46. */
  47. typedef CharT char_type;
  48. /**
  49. * Type of character string passed to member functions.
  50. */
  51. typedef std::basic_string<CharT> string_type;
  52. /**
  53. * Type of iterator used to write in the character buffer.
  54. */
  55. typedef OutputIterator iter_type;
  56. /**
  57. * Construct a duration_put facet.
  58. * @param refs
  59. * @Effects Construct a duration_put facet.
  60. * If the @c refs argument is @c 0 then destruction of the object is
  61. * delegated to the @c locale, or locales, containing it. This allows
  62. * the user to ignore lifetime management issues. On the other had,
  63. * if @c refs is @c 1 then the object must be explicitly deleted;
  64. * the @c locale will not do so. In this case, the object can be
  65. * maintained across the lifetime of multiple locales.
  66. */
  67. explicit duration_put(size_t refs = 0) :
  68. std::locale::facet(refs)
  69. {
  70. }
  71. /**
  72. *
  73. * @param s an output stream iterator
  74. * @param ios a reference to a ios_base
  75. * @param fill the character used as filler
  76. * @param d the duration
  77. * @param pattern begin of the formatting pattern
  78. * @param pat_end end of the formatting pattern
  79. *
  80. * @Effects Steps through the sequence from @c pattern to @c pat_end,
  81. * identifying characters that are part of a pattern sequence. Each character
  82. * that is not part of a pattern sequence is written to @c s immediately, and
  83. * each pattern sequence, as it is identified, results in a call to
  84. * @c put_value or @c put_unit;
  85. * thus, pattern elements and other characters are interleaved in the output
  86. * in the order in which they appear in the pattern. Pattern sequences are
  87. * identified by converting each character @c c to a @c char value as if by
  88. * @c ct.narrow(c,0), where @c ct is a reference to @c ctype<charT> obtained from
  89. * @c ios.getloc(). The first character of each sequence is equal to @c '%',
  90. * followed by a pattern specifier character @c spec, which can be @c 'v' for
  91. * the duration value or @c 'u' for the duration unit. .
  92. * For each valid pattern sequence identified, calls
  93. * <c>put_value(s, ios, fill, d)</c> or <c>put_unit(s, ios, fill, d)</c>.
  94. *
  95. * @Returns An iterator pointing immediately after the last character produced.
  96. */
  97. template <typename Rep, typename Period>
  98. iter_type put(iter_type s, std::ios_base& ios, char_type fill, duration<Rep, Period> const& d, const CharT* pattern,
  99. const CharT* pat_end, const char_type* val = 0) const
  100. {
  101. if (std::has_facet<duration_units<CharT> >(ios.getloc()))
  102. {
  103. duration_units<CharT> const&facet = std::use_facet<duration_units<CharT> >(
  104. ios.getloc());
  105. return put(facet, s, ios, fill, d, pattern, pat_end, val);
  106. }
  107. else
  108. {
  109. duration_units_default<CharT> facet;
  110. return put(facet, s, ios, fill, d, pattern, pat_end, val);
  111. }
  112. }
  113. template <typename Rep, typename Period>
  114. iter_type put(duration_units<CharT> const& units_facet, iter_type s, std::ios_base& ios, char_type fill,
  115. duration<Rep, Period> const& d, const CharT* pattern, const CharT* pat_end, const char_type* val = 0) const
  116. {
  117. const std::ctype<char_type>& ct = std::use_facet<std::ctype<char_type> >(ios.getloc());
  118. for (; pattern != pat_end; ++pattern)
  119. {
  120. if (ct.narrow(*pattern, 0) == '%')
  121. {
  122. if (++pattern == pat_end)
  123. {
  124. *s++ = pattern[-1];
  125. break;
  126. }
  127. char fmt = ct.narrow(*pattern, 0);
  128. switch (fmt)
  129. {
  130. case 'v':
  131. {
  132. s = put_value(s, ios, fill, d, val);
  133. break;
  134. }
  135. case 'u':
  136. {
  137. s = put_unit(units_facet, s, ios, fill, d);
  138. break;
  139. }
  140. default:
  141. BOOST_ASSERT(false && "Boost::Chrono internal error.");
  142. break;
  143. }
  144. }
  145. else
  146. *s++ = *pattern;
  147. }
  148. return s;
  149. }
  150. /**
  151. *
  152. * @param s an output stream iterator
  153. * @param ios a reference to a ios_base
  154. * @param fill the character used as filler
  155. * @param d the duration
  156. * @Effects imbue in @c ios the @c duration_units_default facet if not already present.
  157. * Retrieves Stores the duration pattern from the @c duration_unit facet in let say @c str. Last as if
  158. * @code
  159. * return put(s, ios, d, str.data(), str.data() + str.size());
  160. * @endcode
  161. * @Returns An iterator pointing immediately after the last character produced.
  162. */
  163. template <typename Rep, typename Period>
  164. iter_type put(iter_type s, std::ios_base& ios, char_type fill, duration<Rep, Period> const& d, const char_type* val = 0) const
  165. {
  166. if (std::has_facet<duration_units<CharT> >(ios.getloc()))
  167. {
  168. duration_units<CharT> const&facet = std::use_facet<duration_units<CharT> >(
  169. ios.getloc());
  170. std::basic_string<CharT> str = facet.get_pattern();
  171. return put(facet, s, ios, fill, d, str.data(), str.data() + str.size(), val);
  172. }
  173. else
  174. {
  175. duration_units_default<CharT> facet;
  176. std::basic_string<CharT> str = facet.get_pattern();
  177. return put(facet, s, ios, fill, d, str.data(), str.data() + str.size(), val);
  178. }
  179. }
  180. /**
  181. *
  182. * @param s an output stream iterator
  183. * @param ios a reference to a ios_base
  184. * @param fill the character used as filler
  185. * @param d the duration
  186. * @Effects As if s=std::use_facet<std::num_put<CharT, iter_type> >(ios.getloc()).put(s, ios, fill, static_cast<long int> (d.count())).
  187. * @Returns s, iterator pointing immediately after the last character produced.
  188. */
  189. template <typename Rep, typename Period>
  190. iter_type put_value(iter_type s, std::ios_base& ios, char_type fill, duration<Rep, Period> const& d, const char_type* val = 0) const
  191. {
  192. if (val)
  193. {
  194. while (*val) {
  195. *s = *val;
  196. s++; val++;
  197. }
  198. return s;
  199. }
  200. return std::use_facet<std::num_put<CharT, iter_type> >(ios.getloc()).put(s, ios, fill,
  201. static_cast<typename detail::propagate<Rep>::type> (d.count()));
  202. }
  203. template <typename Rep, typename Period>
  204. iter_type put_value(iter_type s, std::ios_base& ios, char_type fill, duration<process_times<Rep>, Period> const& d, const char_type* = 0) const
  205. {
  206. *s++ = CharT('{');
  207. s = put_value(s, ios, fill, process_real_cpu_clock::duration(d.count().real));
  208. *s++ = CharT(';');
  209. s = put_value(s, ios, fill, process_user_cpu_clock::duration(d.count().user));
  210. *s++ = CharT(';');
  211. s = put_value(s, ios, fill, process_system_cpu_clock::duration(d.count().system));
  212. *s++ = CharT('}');
  213. return s;
  214. }
  215. /**
  216. *
  217. * @param s an output stream iterator
  218. * @param ios a reference to a ios_base
  219. * @param fill the character used as filler
  220. * @param d the duration
  221. * @Effects Let facet be the duration_units<CharT> facet associated to ios. If the associated unit is named,
  222. * as if
  223. * @code
  224. string_type str = facet.get_unit(get_duration_style(ios), d);
  225. s=std::copy(str.begin(), str.end(), s);
  226. * @endcode
  227. * Otherwise, format the unit as "[Period::num/Period::den]" followed by the unit associated to [N/D] obtained using facet.get_n_d_unit(get_duration_style(ios), d)
  228. * @Returns s, iterator pointing immediately after the last character produced.
  229. */
  230. template <typename Rep, typename Period>
  231. iter_type put_unit(iter_type s, std::ios_base& ios, char_type fill, duration<Rep, Period> const& d) const
  232. {
  233. if (std::has_facet<duration_units<CharT> >(ios.getloc()))
  234. {
  235. duration_units<CharT> const&facet = std::use_facet<duration_units<CharT> >(
  236. ios.getloc());
  237. return put_unit(facet, s, ios, fill, d);
  238. }
  239. else
  240. {
  241. duration_units_default<CharT> facet;
  242. return put_unit(facet, s, ios, fill, d);
  243. }
  244. }
  245. template <typename Rep, typename Period>
  246. iter_type put_unit(duration_units<CharT> const& facet, iter_type s, std::ios_base& ios, char_type fill,
  247. duration<Rep, Period> const& d) const
  248. {
  249. if (facet.template is_named_unit<Period>()) {
  250. string_type str = facet.get_unit(get_duration_style(ios), d);
  251. s=std::copy(str.begin(), str.end(), s);
  252. } else {
  253. *s++ = CharT('[');
  254. std::use_facet<std::num_put<CharT, iter_type> >(ios.getloc()).put(s, ios, fill, Period::num);
  255. *s++ = CharT('/');
  256. std::use_facet<std::num_put<CharT, iter_type> >(ios.getloc()).put(s, ios, fill, Period::den);
  257. *s++ = CharT(']');
  258. string_type str = facet.get_n_d_unit(get_duration_style(ios), d);
  259. s=std::copy(str.begin(), str.end(), s);
  260. }
  261. return s;
  262. }
  263. template <typename Rep, typename Period>
  264. iter_type put_unit(duration_units<CharT> const& facet, iter_type s, std::ios_base& ios, char_type fill,
  265. duration<process_times<Rep>, Period> const& d) const
  266. {
  267. duration<Rep,Period> real(d.count().real);
  268. if (facet.template is_named_unit<Period>()) {
  269. string_type str = facet.get_unit(get_duration_style(ios), real);
  270. s=std::copy(str.begin(), str.end(), s);
  271. } else {
  272. *s++ = CharT('[');
  273. std::use_facet<std::num_put<CharT, iter_type> >(ios.getloc()).put(s, ios, fill, Period::num);
  274. *s++ = CharT('/');
  275. std::use_facet<std::num_put<CharT, iter_type> >(ios.getloc()).put(s, ios, fill, Period::den);
  276. *s++ = CharT(']');
  277. string_type str = facet.get_n_d_unit(get_duration_style(ios), real);
  278. s=std::copy(str.begin(), str.end(), s);
  279. }
  280. return s;
  281. }
  282. /**
  283. * Unique identifier for this type of facet.
  284. */
  285. static std::locale::id id;
  286. /**
  287. * @Effects Destroy the facet
  288. */
  289. ~duration_put()
  290. {
  291. }
  292. };
  293. template <class CharT, class OutputIterator>
  294. std::locale::id duration_put<CharT, OutputIterator>::id;
  295. } // chrono
  296. } // boost
  297. #endif // header