time_point_put.hpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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_TIME_POINT_PUT_HPP
  11. #define BOOST_CHRONO_IO_TIME_POINT_PUT_HPP
  12. #include <boost/chrono/config.hpp>
  13. #include <boost/chrono/io/time_point_units.hpp>
  14. #include <boost/chrono/io/duration_put.hpp>
  15. #include <boost/assert.hpp>
  16. #include <locale>
  17. namespace boost
  18. {
  19. namespace chrono
  20. {
  21. /**
  22. * @tparam ChatT a character type
  23. * @tparam OutputIterator a model of @c OutputIterator
  24. *
  25. * The @c time_point_put facet provides facilities for formatted output of @c time_point values.
  26. * The member function of @c time_point_put take a @c time_point and format it into character string representation.
  27. *
  28. */
  29. template <class CharT, class OutputIterator = std::ostreambuf_iterator<CharT> >
  30. class time_point_put: public std::locale::facet
  31. {
  32. public:
  33. /**
  34. * Type of character the facet is instantiated on.
  35. */
  36. typedef CharT char_type;
  37. /**
  38. * Type of character string passed to member functions.
  39. */
  40. typedef std::basic_string<CharT> string_type;
  41. /**
  42. * Type of iterator used to write in the character buffer.
  43. */
  44. typedef OutputIterator iter_type;
  45. /**
  46. * Construct a time_point_put facet.
  47. * @param refs
  48. * @Effects Construct a time_point_put facet.
  49. * If the @c refs argument is @c 0 then destruction of the object is
  50. * delegated to the @c locale, or locales, containing it. This allows
  51. * the user to ignore lifetime management issues. On the other had,
  52. * if @c refs is @c 1 then the object must be explicitly deleted;
  53. * the @c locale will not do so. In this case, the object can be
  54. * maintained across the lifetime of multiple locales.
  55. */
  56. explicit time_point_put(size_t refs = 0) :
  57. std::locale::facet(refs)
  58. {
  59. }
  60. /**
  61. * @param i an output stream iterator
  62. * @param ios a reference to a ios_base
  63. * @param fill the character used as filler
  64. * @param tp the @c time_point
  65. * @param pattern begin of the formatting pattern
  66. * @param pat_end end of the formatting pattern
  67. *
  68. * @Effects Steps through the sequence from @c pattern to @c pat_end,
  69. * identifying characters that are part of a pattern sequence. Each character
  70. * that is not part of a pattern sequence is written to @c s immediately, and
  71. * each pattern sequence, as it is identified, results in a call to
  72. * @c put_duration or @c put_epoch;
  73. * thus, pattern elements and other characters are interleaved in the output
  74. * in the order in which they appear in the pattern. Pattern sequences are
  75. * identified by converting each character @c c to a @c char value as if by
  76. * @c ct.narrow(c,0), where @c ct is a reference to @c ctype<charT> obtained from
  77. * @c ios.getloc(). The first character of each sequence is equal to @c '%',
  78. * followed by a pattern specifier character @c spec, which can be @c 'd' for
  79. * the duration value or @c 'e' for the epoch.
  80. * For each valid pattern sequence identified, calls
  81. * <c>put_duration(s, ios, fill, tp.time_since_epoch())</c> or <c>put_epoch(s, ios)</c>.
  82. *
  83. * @Returns An iterator pointing immediately after the last character produced.
  84. */
  85. template <class Clock, class Duration>
  86. iter_type put(iter_type i, std::ios_base& ios, char_type fill, time_point<Clock, Duration> const& tp, const CharT* pattern,
  87. const CharT* pat_end) const
  88. {
  89. if (std::has_facet<time_point_units<CharT> >(ios.getloc()))
  90. {
  91. time_point_units<CharT> const &facet =
  92. std::use_facet<time_point_units<CharT> >(ios.getloc());
  93. return put(facet, i, ios, fill, tp, pattern, pat_end);
  94. }
  95. else
  96. {
  97. time_point_units_default<CharT> facet;
  98. return put(facet, i, ios, fill, tp, pattern, pat_end);
  99. }
  100. }
  101. template <class Clock, class Duration>
  102. iter_type put(time_point_units<CharT> const& units_facet, iter_type s, std::ios_base& ios, char_type fill,
  103. time_point<Clock, Duration> const& tp, const CharT* pattern, const CharT* pat_end) const
  104. {
  105. const std::ctype<char_type>& ct = std::use_facet<std::ctype<char_type> >(ios.getloc());
  106. for (; pattern != pat_end; ++pattern)
  107. {
  108. if (ct.narrow(*pattern, 0) == '%')
  109. {
  110. if (++pattern == pat_end)
  111. {
  112. *s++ = pattern[-1];
  113. break;
  114. }
  115. char fmt = ct.narrow(*pattern, 0);
  116. switch (fmt)
  117. {
  118. case 'd':
  119. {
  120. s = put_duration(s, ios, fill, tp.time_since_epoch());
  121. break;
  122. }
  123. case 'e':
  124. {
  125. s = put_epoch<Clock> (units_facet, s, ios);
  126. break;
  127. }
  128. default:
  129. BOOST_ASSERT(false && "Boost::Chrono internal error.");
  130. break;
  131. }
  132. }
  133. else
  134. *s++ = *pattern;
  135. }
  136. return s;
  137. }
  138. /**
  139. * @param i an output stream iterator
  140. * @param ios a reference to a ios_base
  141. * @param fill the character used as filler
  142. * @param tp the @c time_point
  143. * @param pattern begin of the formatting pattern
  144. * @param pat_end end of the formatting pattern
  145. *
  146. * @Effects Stores the time_point pattern from the @c time_point_unit facet in let say @c str. Last as if
  147. * @code
  148. * return put(s, ios, dill, tp, str.data(), str.data() + str.size());
  149. * @endcode
  150. * @Returns An iterator pointing immediately after the last character produced.
  151. */
  152. template <class Clock, class Duration>
  153. iter_type put(iter_type i, std::ios_base& ios, char_type fill, time_point<Clock, Duration> const& tp) const
  154. {
  155. if (std::has_facet<time_point_units<CharT> >(ios.getloc()))
  156. {
  157. time_point_units<CharT> const &facet =
  158. std::use_facet<time_point_units<CharT> >(ios.getloc());
  159. std::basic_string<CharT> str = facet.get_pattern();
  160. return put(facet, i, ios, fill, tp, str.data(), str.data() + str.size());
  161. }
  162. else
  163. {
  164. time_point_units_default<CharT> facet;
  165. std::basic_string<CharT> str = facet.get_pattern();
  166. return put(facet, i, ios, fill, tp, str.data(), str.data() + str.size());
  167. }
  168. }
  169. /**
  170. * @param i an output stream iterator
  171. * @param ios a reference to a ios_base
  172. * @param fill the character used as filler
  173. * @param d the @c duration
  174. * @Effects As if <c>facet.put(s, ios, fill, d)</c> where facet is the @c duration_put<CharT> facet associated
  175. * to the @c ios or a new instance of @c duration_put<CharT>.
  176. * @Returns An iterator pointing immediately after the last character produced.
  177. */
  178. template <typename Rep, typename Period>
  179. iter_type put_duration(iter_type i, std::ios_base& ios, char_type fill, duration<Rep, Period> const& d) const
  180. {
  181. if (std::has_facet<duration_put<CharT> >(ios.getloc()))
  182. {
  183. duration_put<CharT> const &facet = std::use_facet<duration_put<CharT> >(ios.getloc());
  184. return facet.put(i, ios, fill, d);
  185. }
  186. else
  187. {
  188. duration_put<CharT> facet;
  189. return facet.put(i, ios, fill, d);
  190. }
  191. }
  192. /**
  193. *
  194. * @param i an output stream iterator
  195. * @param ios a reference to a ios_base
  196. * @Effects As if
  197. * @code
  198. * string_type str = facet.template get_epoch<Clock>();
  199. * s=std::copy(str.begin(), str.end(), s);
  200. * @endcode
  201. * where facet is the @c time_point_units<CharT> facet associated
  202. * to the @c ios or a new instance of @c time_point_units_default<CharT>.
  203. * @Returns s, iterator pointing immediately after the last character produced.
  204. */
  205. template <typename Clock>
  206. iter_type put_epoch(iter_type i, std::ios_base& os) const
  207. {
  208. if (std::has_facet<time_point_units<CharT> >(os.getloc()))
  209. {
  210. time_point_units<CharT> const &facet = std::use_facet<time_point_units<CharT> >(os.getloc());
  211. return put_epoch<Clock> (facet, i, os);
  212. }
  213. else
  214. {
  215. time_point_units_default<CharT> facet;
  216. return put_epoch<Clock> (facet, i, os);
  217. }
  218. }
  219. template <typename Clock>
  220. iter_type put_epoch(time_point_units<CharT> const& facet, iter_type s, std::ios_base&) const
  221. {
  222. string_type str = facet.template get_epoch<Clock>();
  223. s= std::copy(str.begin(), str.end(), s);
  224. return s;
  225. }
  226. /**
  227. * Unique identifier for this type of facet.
  228. */
  229. static std::locale::id id;
  230. /**
  231. * @Effects Destroy the facet
  232. */
  233. ~time_point_put()
  234. {
  235. }
  236. };
  237. template <class CharT, class OutputIterator>
  238. std::locale::id time_point_put<CharT, OutputIterator>::id;
  239. } // chrono
  240. } // boost
  241. #endif // header