time_point_units.hpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. #ifndef BOOST_CHRONO_IO_TIME_POINT_UNITS_HPP
  8. #define BOOST_CHRONO_IO_TIME_POINT_UNITS_HPP
  9. #include <boost/chrono/config.hpp>
  10. #include <boost/chrono/process_cpu_clocks.hpp>
  11. #include <boost/chrono/system_clocks.hpp>
  12. #include <boost/chrono/thread_clock.hpp>
  13. #include <boost/chrono/io/ios_base_state.hpp>
  14. #include <string>
  15. #include <iostream>
  16. #include <ios>
  17. #include <locale>
  18. #include <algorithm>
  19. namespace boost
  20. {
  21. namespace chrono
  22. {
  23. /**
  24. * @c time_point_units facet gives useful information about the time_point pattern,
  25. * the text associated to a time_point's epoch,
  26. */
  27. template <typename CharT=char>
  28. class time_point_units: public std::locale::facet
  29. {
  30. public:
  31. /**
  32. * Type of character the facet is instantiated on.
  33. */
  34. typedef CharT char_type;
  35. /**
  36. * Type of character string used by member functions.
  37. */
  38. typedef std::basic_string<char_type> string_type;
  39. /**
  40. * Unique identifier for this type of facet.
  41. */
  42. static std::locale::id id;
  43. /**
  44. * Construct a @c time_point_units facet.
  45. * @param refs
  46. * @Effects Construct a @c time_point_units facet.
  47. * If the @c refs argument is @c 0 then destruction of the object is
  48. * delegated to the @c locale, or locales, containing it. This allows
  49. * the user to ignore lifetime management issues. On the other had,
  50. * if @c refs is @c 1 then the object must be explicitly deleted;
  51. * the @c locale will not do so. In this case, the object can be
  52. * maintained across the lifetime of multiple locales.
  53. */
  54. explicit time_point_units(size_t refs = 0) :
  55. std::locale::facet(refs)
  56. {
  57. }
  58. /**
  59. * @return the pattern to be used by default.
  60. */
  61. virtual string_type get_pattern() const =0;
  62. /**
  63. * @return the epoch associated to the clock @c Clock calling @c do_get_epoch(Clock())
  64. */
  65. template <typename Clock>
  66. string_type get_epoch() const
  67. {
  68. return do_get_epoch(Clock());
  69. }
  70. protected:
  71. /**
  72. * Destroy the facet.
  73. */
  74. virtual ~time_point_units() {}
  75. /**
  76. *
  77. * @param c a dummy instance of @c system_clock.
  78. * @return The epoch string associated to the @c system_clock.
  79. */
  80. virtual string_type do_get_epoch(system_clock) const=0;
  81. /**
  82. *
  83. * @param c a dummy instance of @c steady_clock.
  84. * @return The epoch string associated to the @c steady_clock.
  85. */
  86. virtual string_type do_get_epoch(steady_clock) const=0;
  87. #if defined(BOOST_CHRONO_HAS_PROCESS_CLOCKS)
  88. /**
  89. *
  90. * @param c a dummy instance of @c process_real_cpu_clock.
  91. * @return The epoch string associated to the @c process_real_cpu_clock.
  92. */
  93. virtual string_type do_get_epoch(process_real_cpu_clock) const=0;
  94. /**
  95. *
  96. * @param c a dummy instance of @c process_user_cpu_clock.
  97. * @return The epoch string associated to the @c process_user_cpu_clock.
  98. */
  99. virtual string_type do_get_epoch(process_user_cpu_clock) const=0;
  100. /**
  101. *
  102. * @param c a dummy instance of @c process_system_cpu_clock.
  103. * @return The epoch string associated to the @c process_system_cpu_clock.
  104. */
  105. virtual string_type do_get_epoch(process_system_cpu_clock) const=0;
  106. /**
  107. *
  108. * @param c a dummy instance of @c process_cpu_clock.
  109. * @return The epoch string associated to the @c process_cpu_clock.
  110. */
  111. virtual string_type do_get_epoch(process_cpu_clock) const=0;
  112. #endif
  113. #if defined(BOOST_CHRONO_HAS_THREAD_CLOCK)
  114. /**
  115. *
  116. * @param c a dummy instance of @c thread_clock.
  117. * @return The epoch string associated to the @c thread_clock.
  118. */
  119. virtual string_type do_get_epoch(thread_clock) const=0;
  120. #endif
  121. };
  122. template <typename CharT>
  123. std::locale::id time_point_units<CharT>::id;
  124. // This class is used to define the strings for the default English
  125. template <typename CharT=char>
  126. class time_point_units_default: public time_point_units<CharT>
  127. {
  128. public:
  129. /**
  130. * Type of character the facet is instantiated on.
  131. */
  132. typedef CharT char_type;
  133. /**
  134. * Type of character string returned by member functions.
  135. */
  136. typedef std::basic_string<char_type> string_type;
  137. explicit time_point_units_default(size_t refs = 0) :
  138. time_point_units<CharT> (refs)
  139. {
  140. }
  141. ~time_point_units_default() {}
  142. /**
  143. * @return the default pattern "%d%e".
  144. */
  145. string_type get_pattern() const
  146. {
  147. static const CharT t[] =
  148. { '%', 'd', '%', 'e' };
  149. static const string_type pattern(t, t + sizeof (t) / sizeof (t[0]));
  150. return pattern;
  151. }
  152. protected:
  153. /**
  154. * @param c a dummy instance of @c system_clock.
  155. * @return The epoch string returned by @c clock_string<system_clock,CharT>::since().
  156. */
  157. string_type do_get_epoch(system_clock ) const
  158. {
  159. return clock_string<system_clock,CharT>::since();
  160. }
  161. /**
  162. * @param c a dummy instance of @c steady_clock.
  163. * @return The epoch string returned by @c clock_string<steady_clock,CharT>::since().
  164. */
  165. string_type do_get_epoch(steady_clock ) const
  166. {
  167. return clock_string<steady_clock,CharT>::since();
  168. }
  169. #if defined(BOOST_CHRONO_HAS_PROCESS_CLOCKS)
  170. /**
  171. * @param c a dummy instance of @c process_real_cpu_clock.
  172. * @return The epoch string returned by @c clock_string<process_real_cpu_clock,CharT>::since().
  173. */
  174. string_type do_get_epoch(process_real_cpu_clock ) const
  175. {
  176. return clock_string<process_real_cpu_clock,CharT>::since();
  177. }
  178. /**
  179. * @param c a dummy instance of @c process_user_cpu_clock.
  180. * @return The epoch string returned by @c clock_string<process_user_cpu_clock,CharT>::since().
  181. */
  182. string_type do_get_epoch(process_user_cpu_clock ) const
  183. {
  184. return clock_string<process_user_cpu_clock,CharT>::since();
  185. }
  186. /**
  187. * @param c a dummy instance of @c process_system_cpu_clock.
  188. * @return The epoch string returned by @c clock_string<process_system_cpu_clock,CharT>::since().
  189. */
  190. string_type do_get_epoch(process_system_cpu_clock ) const
  191. {
  192. return clock_string<process_system_cpu_clock,CharT>::since();
  193. }
  194. /**
  195. * @param c a dummy instance of @c process_cpu_clock.
  196. * @return The epoch string returned by @c clock_string<process_cpu_clock,CharT>::since().
  197. */
  198. string_type do_get_epoch(process_cpu_clock ) const
  199. {
  200. return clock_string<process_cpu_clock,CharT>::since();
  201. }
  202. #endif
  203. #if defined(BOOST_CHRONO_HAS_THREAD_CLOCK)
  204. /**
  205. * @param c a dummy instance of @c thread_clock.
  206. * @return The epoch string returned by @c clock_string<thread_clock,CharT>::since().
  207. */
  208. string_type do_get_epoch(thread_clock ) const
  209. {
  210. return clock_string<thread_clock,CharT>::since();
  211. }
  212. #endif
  213. };
  214. } // chrono
  215. } // boost
  216. #endif // header