ios_base_state.hpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // (C) Copyright 2011 Vicente J. Botet Escriba
  2. // Use, modification and distribution are subject to the Boost Software License,
  3. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt).
  5. //
  6. // This code was adapted by Vicente from Howard Hinnant's experimental work
  7. // on chrono i/o to Boost
  8. #ifndef BOOST_CHRONO_IO_IOS_BASE_STATE_HPP
  9. #define BOOST_CHRONO_IO_IOS_BASE_STATE_HPP
  10. #include <boost/chrono/config.hpp>
  11. #include <locale>
  12. #include <boost/chrono/io/duration_style.hpp>
  13. #include <boost/chrono/io/timezone.hpp>
  14. #include <boost/chrono/io/utility/ios_base_state_ptr.hpp>
  15. namespace boost
  16. {
  17. namespace chrono
  18. {
  19. class fmt_masks : public ios_flags<fmt_masks>
  20. {
  21. typedef ios_flags<fmt_masks> base_type;
  22. fmt_masks& operator=(fmt_masks const& rhs) ;
  23. public:
  24. fmt_masks(std::ios_base& ios): base_type(ios) {}
  25. enum type
  26. {
  27. uses_symbol = 1 << 0,
  28. uses_local = 1 << 1
  29. };
  30. inline duration_style get_duration_style()
  31. {
  32. return (flags() & uses_symbol) ? duration_style::symbol : duration_style::prefix;
  33. }
  34. inline void set_duration_style(duration_style style)
  35. {
  36. if (style == duration_style::symbol)
  37. setf(uses_symbol);
  38. else
  39. unsetf(uses_symbol);
  40. }
  41. inline timezone get_timezone()
  42. {
  43. return (flags() & uses_local) ? timezone::local : timezone::utc;
  44. }
  45. inline void set_timezone(timezone tz)
  46. {
  47. if (tz == timezone::local)
  48. setf(uses_local);
  49. else
  50. unsetf(uses_local);
  51. }
  52. };
  53. namespace detail
  54. {
  55. namespace /**/ {
  56. xalloc_key_initializer<fmt_masks > fmt_masks_xalloc_key_initializer;
  57. } // namespace
  58. } // namespace detail
  59. inline duration_style get_duration_style(std::ios_base & ios)
  60. {
  61. return fmt_masks(ios).get_duration_style();
  62. }
  63. inline void set_duration_style(std::ios_base& ios, duration_style style)
  64. {
  65. fmt_masks(ios).set_duration_style(style);
  66. }
  67. inline std::ios_base& symbol_format(std::ios_base& ios)
  68. {
  69. fmt_masks(ios).setf(fmt_masks::uses_symbol);
  70. return ios;
  71. }
  72. inline std::ios_base& name_format(std::ios_base& ios)
  73. {
  74. fmt_masks(ios).unsetf(fmt_masks::uses_symbol);
  75. return ios;
  76. }
  77. inline timezone get_timezone(std::ios_base & ios)
  78. {
  79. return fmt_masks(ios).get_timezone();
  80. }
  81. inline void set_timezone(std::ios_base& ios, timezone tz)
  82. {
  83. fmt_masks(ios).set_timezone(tz);
  84. }
  85. inline std::ios_base& local_timezone(std::ios_base& ios)
  86. {
  87. fmt_masks(ios).setf(fmt_masks::uses_local);
  88. return ios;
  89. }
  90. inline std::ios_base& utc_timezone(std::ios_base& ios)
  91. {
  92. fmt_masks(ios).unsetf(fmt_masks::uses_local);
  93. return ios;
  94. }
  95. namespace detail
  96. {
  97. template<typename CharT>
  98. struct ios_base_data_aux
  99. {
  100. std::basic_string<CharT> time_fmt;
  101. std::basic_string<CharT> duration_fmt;
  102. public:
  103. ios_base_data_aux()
  104. //:
  105. // time_fmt(""),
  106. // duration_fmt("")
  107. {
  108. }
  109. };
  110. template<typename CharT>
  111. struct ios_base_data {};
  112. namespace /**/ {
  113. xalloc_key_initializer<detail::ios_base_data<char> > ios_base_data_aux_xalloc_key_initializer;
  114. xalloc_key_initializer<detail::ios_base_data<wchar_t> > wios_base_data_aux_xalloc_key_initializer;
  115. #if BOOST_CHRONO_HAS_UNICODE_SUPPORT
  116. xalloc_key_initializer<detail::ios_base_data<char16_t> > u16ios_base_data_aux_xalloc_key_initializer;
  117. xalloc_key_initializer<detail::ios_base_data<char32_t> > u32ios_base_data_aux_xalloc_key_initializer;
  118. #endif
  119. } // namespace
  120. } // namespace detail
  121. template<typename CharT>
  122. inline std::basic_string<CharT> get_time_fmt(std::ios_base & ios)
  123. {
  124. ios_state_not_null_ptr<detail::ios_base_data<CharT>, detail::ios_base_data_aux<CharT> > ptr(ios);
  125. return ptr->time_fmt;
  126. }
  127. template<typename CharT>
  128. inline void set_time_fmt(std::ios_base& ios, std::basic_string<
  129. CharT> const& fmt)
  130. {
  131. ios_state_not_null_ptr<detail::ios_base_data<CharT>, detail::ios_base_data_aux<CharT> > ptr(ios);
  132. ptr->time_fmt = fmt;
  133. }
  134. } // chrono
  135. } // boost
  136. #endif // header