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 9 #ifndef BOOST_CHRONO_IO_IOS_BASE_STATE_HPP 10 #define BOOST_CHRONO_IO_IOS_BASE_STATE_HPP 11 12 #include <boost/chrono/config.hpp> 13 #include <locale> 14 #include <boost/chrono/io/duration_style.hpp> 15 #include <boost/chrono/io/timezone.hpp> 16 #include <boost/chrono/io/utility/ios_base_state_ptr.hpp> 17 18 namespace boost 19 { 20 namespace chrono 21 { 22 23 class fmt_masks : public ios_flags<fmt_masks> 24 { 25 typedef ios_flags<fmt_masks> base_type; 26 fmt_masks& operator=(fmt_masks const& rhs) ; 27 28 public: fmt_masks(std::ios_base & ios)29 fmt_masks(std::ios_base& ios): base_type(ios) {} 30 enum type 31 { 32 uses_symbol = 1 << 0, 33 uses_local = 1 << 1 34 }; 35 get_duration_style()36 inline duration_style get_duration_style() 37 { 38 return (flags() & uses_symbol) ? duration_style::symbol : duration_style::prefix; 39 } set_duration_style(duration_style style)40 inline void set_duration_style(duration_style style) 41 { 42 if (style == duration_style::symbol) 43 setf(uses_symbol); 44 else 45 unsetf(uses_symbol); 46 } 47 get_timezone()48 inline timezone get_timezone() 49 { 50 return (flags() & uses_local) ? timezone::local : timezone::utc; 51 } set_timezone(timezone tz)52 inline void set_timezone(timezone tz) 53 { 54 if (tz == timezone::local) 55 setf(uses_local); 56 else 57 unsetf(uses_local); 58 } 59 }; 60 namespace detail 61 { 62 namespace /**/ { 63 xalloc_key_initializer<fmt_masks > fmt_masks_xalloc_key_initializer; 64 } // namespace 65 } // namespace detail 66 get_duration_style(std::ios_base & ios)67 inline duration_style get_duration_style(std::ios_base & ios) 68 { 69 return fmt_masks(ios).get_duration_style(); 70 } set_duration_style(std::ios_base & ios,duration_style style)71 inline void set_duration_style(std::ios_base& ios, duration_style style) 72 { 73 fmt_masks(ios).set_duration_style(style); 74 } symbol_format(std::ios_base & ios)75 inline std::ios_base& symbol_format(std::ios_base& ios) 76 { 77 fmt_masks(ios).setf(fmt_masks::uses_symbol); 78 return ios; 79 } name_format(std::ios_base & ios)80 inline std::ios_base& name_format(std::ios_base& ios) 81 { 82 fmt_masks(ios).unsetf(fmt_masks::uses_symbol); 83 return ios; 84 } 85 get_timezone(std::ios_base & ios)86 inline timezone get_timezone(std::ios_base & ios) 87 { 88 return fmt_masks(ios).get_timezone(); 89 } set_timezone(std::ios_base & ios,timezone tz)90 inline void set_timezone(std::ios_base& ios, timezone tz) 91 { 92 fmt_masks(ios).set_timezone(tz); 93 } local_timezone(std::ios_base & ios)94 inline std::ios_base& local_timezone(std::ios_base& ios) 95 { 96 fmt_masks(ios).setf(fmt_masks::uses_local); 97 return ios; 98 } 99 utc_timezone(std::ios_base & ios)100 inline std::ios_base& utc_timezone(std::ios_base& ios) 101 { 102 fmt_masks(ios).unsetf(fmt_masks::uses_local); 103 return ios; 104 } 105 106 namespace detail 107 { 108 109 template<typename CharT> 110 struct ios_base_data_aux 111 { 112 std::basic_string<CharT> time_fmt; 113 std::basic_string<CharT> duration_fmt; 114 public: 115 ios_base_data_auxboost::chrono::detail::ios_base_data_aux116 ios_base_data_aux() 117 //: 118 // time_fmt(""), 119 // duration_fmt("") 120 { 121 } 122 }; 123 template<typename CharT> 124 struct ios_base_data {}; 125 namespace /**/ { 126 xalloc_key_initializer<detail::ios_base_data<char> > ios_base_data_aux_xalloc_key_initializer; 127 xalloc_key_initializer<detail::ios_base_data<wchar_t> > wios_base_data_aux_xalloc_key_initializer; 128 #if BOOST_CHRONO_HAS_UNICODE_SUPPORT 129 xalloc_key_initializer<detail::ios_base_data<char16_t> > u16ios_base_data_aux_xalloc_key_initializer; 130 xalloc_key_initializer<detail::ios_base_data<char32_t> > u32ios_base_data_aux_xalloc_key_initializer; 131 #endif 132 } // namespace 133 } // namespace detail 134 135 template<typename CharT> get_time_fmt(std::ios_base & ios)136 inline std::basic_string<CharT> get_time_fmt(std::ios_base & ios) 137 { 138 ios_state_not_null_ptr<detail::ios_base_data<CharT>, detail::ios_base_data_aux<CharT> > ptr(ios); 139 return ptr->time_fmt; 140 } 141 template<typename CharT> set_time_fmt(std::ios_base & ios,std::basic_string<CharT> const & fmt)142 inline void set_time_fmt(std::ios_base& ios, std::basic_string< 143 CharT> const& fmt) 144 { 145 ios_state_not_null_ptr<detail::ios_base_data<CharT>, detail::ios_base_data_aux<CharT> > ptr(ios); 146 ptr->time_fmt = fmt; 147 } 148 149 } // chrono 150 } // boost 151 152 #endif // header 153