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