time_point_units.hpp 8.0 KB

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