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 #ifndef BOOST_CHRONO_IO_DURATION_GET_HPP
|
Chris@16
|
9 #define BOOST_CHRONO_IO_DURATION_GET_HPP
|
Chris@16
|
10
|
Chris@16
|
11 #include <boost/chrono/config.hpp>
|
Chris@16
|
12 #include <string>
|
Chris@16
|
13 #include <boost/type_traits/is_scalar.hpp>
|
Chris@16
|
14 #include <boost/utility/enable_if.hpp>
|
Chris@16
|
15 #include <boost/type_traits/is_signed.hpp>
|
Chris@16
|
16 #include <boost/mpl/if.hpp>
|
Chris@101
|
17 #include <boost/integer/common_factor_rt.hpp>
|
Chris@16
|
18 #include <boost/chrono/detail/scan_keyword.hpp>
|
Chris@16
|
19 #include <boost/chrono/detail/no_warning/signed_unsigned_cmp.hpp>
|
Chris@101
|
20 #include <boost/chrono/process_cpu_clocks.hpp>
|
Chris@16
|
21
|
Chris@16
|
22 #include <boost/assert.hpp>
|
Chris@16
|
23 #include <locale>
|
Chris@16
|
24
|
Chris@16
|
25 /**
|
Chris@16
|
26 * Duration formatting facet for input.
|
Chris@16
|
27 */
|
Chris@16
|
28 namespace boost
|
Chris@16
|
29 {
|
Chris@16
|
30 namespace chrono
|
Chris@16
|
31 {
|
Chris@16
|
32
|
Chris@16
|
33 namespace detail
|
Chris@16
|
34 {
|
Chris@16
|
35 template <class Rep, bool = is_scalar<Rep>::value>
|
Chris@16
|
36 struct duration_io_intermediate
|
Chris@16
|
37 {
|
Chris@16
|
38 typedef Rep type;
|
Chris@16
|
39 };
|
Chris@16
|
40
|
Chris@16
|
41 template <class Rep>
|
Chris@16
|
42 struct duration_io_intermediate<Rep, true>
|
Chris@16
|
43 {
|
Chris@16
|
44 typedef typename mpl::if_c<is_floating_point<Rep>::value, long double, typename mpl::if_c<
|
Chris@16
|
45 is_signed<Rep>::value, long long, unsigned long long>::type>::type type;
|
Chris@16
|
46 };
|
Chris@16
|
47
|
Chris@101
|
48 template <class Rep>
|
Chris@101
|
49 struct duration_io_intermediate<process_times<Rep>, false>
|
Chris@101
|
50 {
|
Chris@101
|
51 typedef process_times<typename duration_io_intermediate<Rep>::type> type;
|
Chris@101
|
52 };
|
Chris@101
|
53
|
Chris@16
|
54 template <typename intermediate_type>
|
Chris@16
|
55 typename enable_if<is_integral<intermediate_type> , bool>::type reduce(intermediate_type& r,
|
Chris@16
|
56 unsigned long long& den, std::ios_base::iostate& err)
|
Chris@16
|
57 {
|
Chris@16
|
58 typedef typename common_type<intermediate_type, unsigned long long>::type common_type_t;
|
Chris@16
|
59
|
Chris@16
|
60 // Reduce r * num / den
|
Chris@101
|
61 common_type_t t = integer::gcd<common_type_t>(common_type_t(r), common_type_t(den));
|
Chris@16
|
62 r /= t;
|
Chris@16
|
63 den /= t;
|
Chris@16
|
64 if (den != 1)
|
Chris@16
|
65 {
|
Chris@16
|
66 // Conversion to Period is integral and not exact
|
Chris@16
|
67 err |= std::ios_base::failbit;
|
Chris@16
|
68 return false;
|
Chris@16
|
69 }
|
Chris@16
|
70 return true;
|
Chris@16
|
71 }
|
Chris@16
|
72 template <typename intermediate_type>
|
Chris@16
|
73 typename disable_if<is_integral<intermediate_type> , bool>::type reduce(intermediate_type&, unsigned long long&,
|
Chris@16
|
74 std::ios_base::iostate&)
|
Chris@16
|
75 {
|
Chris@16
|
76 return true;
|
Chris@16
|
77 }
|
Chris@16
|
78
|
Chris@16
|
79 }
|
Chris@16
|
80
|
Chris@16
|
81 /**
|
Chris@16
|
82 * @c duration_get is used to parse a character sequence, extracting
|
Chris@16
|
83 * components of a duration into a class duration.
|
Chris@16
|
84 * Each get member parses a format as produced by a corresponding format specifier to time_put<>::put.
|
Chris@16
|
85 * If the sequence being parsed matches the correct format, the
|
Chris@16
|
86 * corresponding member of the class duration argument are set to the
|
Chris@16
|
87 * value used to produce the sequence;
|
Chris@16
|
88 * otherwise either an error is reported or unspecified values are assigned.
|
Chris@16
|
89 * In other words, user confirmation is required for reliable parsing of
|
Chris@16
|
90 * user-entered durations, but machine-generated formats can be parsed
|
Chris@16
|
91 * reliably. This allows parsers to be aggressive about interpreting user
|
Chris@16
|
92 * variations on standard formats.
|
Chris@16
|
93 *
|
Chris@16
|
94 * If the end iterator is reached during parsing of the get() member
|
Chris@16
|
95 * function, the member sets std::ios_base::eofbit in err.
|
Chris@16
|
96 */
|
Chris@16
|
97 template <class CharT, class InputIterator = std::istreambuf_iterator<CharT> >
|
Chris@16
|
98 class duration_get: public std::locale::facet
|
Chris@16
|
99 {
|
Chris@16
|
100 public:
|
Chris@16
|
101 /**
|
Chris@16
|
102 * Type of character the facet is instantiated on.
|
Chris@16
|
103 */
|
Chris@16
|
104 typedef CharT char_type;
|
Chris@16
|
105 /**
|
Chris@16
|
106 * Type of character string passed to member functions.
|
Chris@16
|
107 */
|
Chris@16
|
108 typedef std::basic_string<CharT> string_type;
|
Chris@16
|
109 /**
|
Chris@16
|
110 * Type of iterator used to scan the character buffer.
|
Chris@16
|
111 */
|
Chris@16
|
112 typedef InputIterator iter_type;
|
Chris@16
|
113
|
Chris@16
|
114 /**
|
Chris@16
|
115 * Construct a @c duration_get facet.
|
Chris@16
|
116 * @param refs
|
Chris@16
|
117 * @Effects Construct a @c duration_get facet.
|
Chris@16
|
118 * If the @c refs argument is @c 0 then destruction of the object is
|
Chris@16
|
119 * delegated to the @c locale, or locales, containing it. This allows
|
Chris@16
|
120 * the user to ignore lifetime management issues. On the other had,
|
Chris@16
|
121 * if @c refs is @c 1 then the object must be explicitly deleted;
|
Chris@16
|
122 * the @c locale will not do so. In this case, the object can be
|
Chris@16
|
123 * maintained across the lifetime of multiple locales.
|
Chris@16
|
124 */
|
Chris@16
|
125
|
Chris@16
|
126 explicit duration_get(size_t refs = 0) :
|
Chris@16
|
127 std::locale::facet(refs)
|
Chris@16
|
128 {
|
Chris@16
|
129 }
|
Chris@16
|
130
|
Chris@16
|
131 /**
|
Chris@16
|
132 * @param s start input stream iterator
|
Chris@16
|
133 * @param end end input stream iterator
|
Chris@16
|
134 * @param ios a reference to a ios_base
|
Chris@16
|
135 * @param err the ios_base state
|
Chris@16
|
136 * @param d the duration
|
Chris@16
|
137 * @param pattern begin of the formatting pattern
|
Chris@16
|
138 * @param pat_end end of the formatting pattern
|
Chris@16
|
139 *
|
Chris@16
|
140 * Requires: [pattern,pat_end) shall be a valid range.
|
Chris@16
|
141 *
|
Chris@16
|
142 * Effects: The function starts by evaluating err = std::ios_base::goodbit.
|
Chris@16
|
143 * It then enters a loop, reading zero or more characters from s at
|
Chris@16
|
144 * each iteration. Unless otherwise specified below, the loop
|
Chris@16
|
145 * terminates when the first of the following conditions holds:
|
Chris@16
|
146 * - The expression pattern == pat_end evaluates to true.
|
Chris@16
|
147 * - The expression err == std::ios_base::goodbit evaluates to false.
|
Chris@16
|
148 * - The expression s == end evaluates to true, in which case the
|
Chris@16
|
149 * function evaluates err = std::ios_base::eofbit | std::ios_base::failbit.
|
Chris@16
|
150 * - The next element of pattern is equal to '%', followed by a conversion
|
Chris@16
|
151 * specifier character, format.
|
Chris@16
|
152 * If the number of elements in the range [pattern,pat_end) is not
|
Chris@16
|
153 * sufficient to unambiguously determine whether the conversion
|
Chris@16
|
154 * specification is complete and valid, the function evaluates
|
Chris@16
|
155 * err = std::ios_base::failbit. Otherwise, the function evaluates
|
Chris@16
|
156 * s = get_value(s, end, ios, err, r) when the conversion specification is 'v' and
|
Chris@16
|
157 * s = get_value(s, end, ios, err, rt) when the conversion specification is 'u'.
|
Chris@16
|
158 * If err == std::ios_base::goodbit holds after
|
Chris@16
|
159 * the evaluation of the expression, the function increments pattern to
|
Chris@16
|
160 * point just past the end of the conversion specification and continues
|
Chris@16
|
161 * looping.
|
Chris@16
|
162 * - The expression isspace(*pattern, ios.getloc()) evaluates to true, in
|
Chris@16
|
163 * which case the function first increments pattern until
|
Chris@16
|
164 * pattern == pat_end || !isspace(*pattern, ios.getloc()) evaluates to true,
|
Chris@16
|
165 * then advances s until s == end || !isspace(*s, ios.getloc()) is true,
|
Chris@16
|
166 * and finally resumes looping.
|
Chris@16
|
167 * - The next character read from s matches the element pointed to by
|
Chris@16
|
168 * pattern in a case-insensitive comparison, in which case the function
|
Chris@16
|
169 * evaluates ++pattern, ++s and continues looping. Otherwise, the function
|
Chris@16
|
170 * evaluates err = std::ios_base::failbit.
|
Chris@16
|
171 *
|
Chris@16
|
172 * Once r and rt are retrieved,
|
Chris@16
|
173 * Returns: s
|
Chris@16
|
174 */
|
Chris@16
|
175 template <typename Rep, typename Period>
|
Chris@16
|
176 iter_type get(iter_type s, iter_type end, std::ios_base& ios, std::ios_base::iostate& err,
|
Chris@16
|
177 duration<Rep, Period> &d, const char_type *pattern, const char_type *pat_end) const
|
Chris@16
|
178 {
|
Chris@16
|
179 if (std::has_facet<duration_units<CharT> >(ios.getloc()))
|
Chris@16
|
180 {
|
Chris@16
|
181 duration_units<CharT> const&facet = std::use_facet<duration_units<CharT> >(ios.getloc());
|
Chris@16
|
182 return get(facet, s, end, ios, err, d, pattern, pat_end);
|
Chris@16
|
183 }
|
Chris@16
|
184 else
|
Chris@16
|
185 {
|
Chris@16
|
186 duration_units_default<CharT> facet;
|
Chris@16
|
187 return get(facet, s, end, ios, err, d, pattern, pat_end);
|
Chris@16
|
188 }
|
Chris@16
|
189 }
|
Chris@16
|
190
|
Chris@16
|
191 template <typename Rep, typename Period>
|
Chris@16
|
192 iter_type get(duration_units<CharT> const&facet, iter_type s, iter_type end, std::ios_base& ios,
|
Chris@16
|
193 std::ios_base::iostate& err, duration<Rep, Period> &d, const char_type *pattern, const char_type *pat_end) const
|
Chris@16
|
194 {
|
Chris@16
|
195
|
Chris@16
|
196 typedef typename detail::duration_io_intermediate<Rep>::type intermediate_type;
|
Chris@16
|
197 intermediate_type r;
|
Chris@16
|
198 rt_ratio rt;
|
Chris@16
|
199 bool value_found = false, unit_found = false;
|
Chris@16
|
200
|
Chris@16
|
201 const std::ctype<char_type>& ct = std::use_facet<std::ctype<char_type> >(ios.getloc());
|
Chris@16
|
202 while (pattern != pat_end && err == std::ios_base::goodbit)
|
Chris@16
|
203 {
|
Chris@16
|
204 if (s == end)
|
Chris@16
|
205 {
|
Chris@16
|
206 err |= std::ios_base::eofbit;
|
Chris@16
|
207 break;
|
Chris@16
|
208 }
|
Chris@16
|
209 if (ct.narrow(*pattern, 0) == '%')
|
Chris@16
|
210 {
|
Chris@16
|
211 if (++pattern == pat_end)
|
Chris@16
|
212 {
|
Chris@16
|
213 err |= std::ios_base::failbit;
|
Chris@16
|
214 return s;
|
Chris@16
|
215 }
|
Chris@16
|
216 char cmd = ct.narrow(*pattern, 0);
|
Chris@16
|
217 switch (cmd)
|
Chris@16
|
218 {
|
Chris@16
|
219 case 'v':
|
Chris@16
|
220 {
|
Chris@16
|
221 if (value_found)
|
Chris@16
|
222 {
|
Chris@16
|
223 err |= std::ios_base::failbit;
|
Chris@16
|
224 return s;
|
Chris@16
|
225 }
|
Chris@16
|
226 value_found = true;
|
Chris@16
|
227 s = get_value(s, end, ios, err, r);
|
Chris@16
|
228 if (err & (std::ios_base::badbit | std::ios_base::failbit))
|
Chris@16
|
229 {
|
Chris@16
|
230 return s;
|
Chris@16
|
231 }
|
Chris@16
|
232 break;
|
Chris@16
|
233 }
|
Chris@16
|
234 case 'u':
|
Chris@16
|
235 {
|
Chris@16
|
236 if (unit_found)
|
Chris@16
|
237 {
|
Chris@16
|
238 err |= std::ios_base::failbit;
|
Chris@16
|
239 return s;
|
Chris@16
|
240 }
|
Chris@16
|
241 unit_found = true;
|
Chris@16
|
242 s = get_unit(facet, s, end, ios, err, rt);
|
Chris@16
|
243 if (err & (std::ios_base::badbit | std::ios_base::failbit))
|
Chris@16
|
244 {
|
Chris@16
|
245 return s;
|
Chris@16
|
246 }
|
Chris@16
|
247 break;
|
Chris@16
|
248 }
|
Chris@16
|
249 default:
|
Chris@16
|
250 BOOST_ASSERT(false && "Boost::Chrono internal error.");
|
Chris@16
|
251 break;
|
Chris@16
|
252 }
|
Chris@16
|
253
|
Chris@16
|
254 ++pattern;
|
Chris@16
|
255 }
|
Chris@16
|
256 else if (ct.is(std::ctype_base::space, *pattern))
|
Chris@16
|
257 {
|
Chris@16
|
258 for (++pattern; pattern != pat_end && ct.is(std::ctype_base::space, *pattern); ++pattern)
|
Chris@16
|
259 ;
|
Chris@16
|
260 for (; s != end && ct.is(std::ctype_base::space, *s); ++s)
|
Chris@16
|
261 ;
|
Chris@16
|
262 }
|
Chris@16
|
263 else if (ct.toupper(*s) == ct.toupper(*pattern))
|
Chris@16
|
264 {
|
Chris@16
|
265 ++s;
|
Chris@16
|
266 ++pattern;
|
Chris@16
|
267 }
|
Chris@16
|
268 else
|
Chris@16
|
269 {
|
Chris@16
|
270 err |= std::ios_base::failbit;
|
Chris@16
|
271 return s;
|
Chris@16
|
272 }
|
Chris@16
|
273
|
Chris@16
|
274 }
|
Chris@16
|
275
|
Chris@16
|
276 unsigned long long num = rt.num;
|
Chris@16
|
277 unsigned long long den = rt.den;
|
Chris@16
|
278
|
Chris@16
|
279 // r should be multiplied by (num/den) / Period
|
Chris@16
|
280 // Reduce (num/den) / Period to lowest terms
|
Chris@101
|
281 unsigned long long gcd_n1_n2 = integer::gcd<unsigned long long>(num, Period::num);
|
Chris@101
|
282 unsigned long long gcd_d1_d2 = integer::gcd<unsigned long long>(den, Period::den);
|
Chris@16
|
283 num /= gcd_n1_n2;
|
Chris@16
|
284 den /= gcd_d1_d2;
|
Chris@16
|
285 unsigned long long n2 = Period::num / gcd_n1_n2;
|
Chris@16
|
286 unsigned long long d2 = Period::den / gcd_d1_d2;
|
Chris@16
|
287 if (num > (std::numeric_limits<unsigned long long>::max)() / d2 || den
|
Chris@16
|
288 > (std::numeric_limits<unsigned long long>::max)() / n2)
|
Chris@16
|
289 {
|
Chris@16
|
290 // (num/den) / Period overflows
|
Chris@16
|
291 err |= std::ios_base::failbit;
|
Chris@16
|
292 return s;
|
Chris@16
|
293 }
|
Chris@16
|
294 num *= d2;
|
Chris@16
|
295 den *= n2;
|
Chris@16
|
296
|
Chris@16
|
297 typedef typename common_type<intermediate_type, unsigned long long>::type common_type_t;
|
Chris@16
|
298
|
Chris@16
|
299 // num / den is now factor to multiply by r
|
Chris@16
|
300 if (!detail::reduce(r, den, err)) return s;
|
Chris@16
|
301
|
Chris@16
|
302 if (chrono::detail::gt(r, ( (duration_values<common_type_t>::max)() / num)))
|
Chris@16
|
303 {
|
Chris@16
|
304 // Conversion to Period overflowed
|
Chris@16
|
305 err |= std::ios_base::failbit;
|
Chris@16
|
306 return s;
|
Chris@16
|
307 }
|
Chris@16
|
308 common_type_t t = r * num;
|
Chris@16
|
309 t /= den;
|
Chris@101
|
310 if (t > duration_values<common_type_t>::zero())
|
Chris@16
|
311 {
|
Chris@16
|
312 Rep pt = t;
|
Chris@16
|
313 if ( (duration_values<Rep>::max)() < pt)
|
Chris@16
|
314 {
|
Chris@16
|
315 // Conversion to Period overflowed
|
Chris@16
|
316 err |= std::ios_base::failbit;
|
Chris@16
|
317 return s;
|
Chris@16
|
318 }
|
Chris@16
|
319 }
|
Chris@16
|
320 // Success! Store it.
|
Chris@16
|
321 r = Rep(t);
|
Chris@16
|
322 d = duration<Rep, Period> (r);
|
Chris@16
|
323
|
Chris@16
|
324 return s;
|
Chris@16
|
325 }
|
Chris@16
|
326
|
Chris@16
|
327 /**
|
Chris@16
|
328 *
|
Chris@16
|
329 * @param s start input stream iterator
|
Chris@16
|
330 * @param end end input stream iterator
|
Chris@16
|
331 * @param ios a reference to a ios_base
|
Chris@16
|
332 * @param err the ios_base state
|
Chris@16
|
333 * @param d the duration
|
Chris@16
|
334 * Stores the duration pattern from the @c duration_unit facet in let say @c str. Last as if
|
Chris@16
|
335 * @code
|
Chris@16
|
336 * return get(s, end, ios, err, ios, d, str.data(), str.data() + str.size());
|
Chris@16
|
337 * @codeend
|
Chris@16
|
338 * @Returns An iterator pointing just beyond the last character that can be determined to be part of a valid name
|
Chris@16
|
339 */
|
Chris@16
|
340 template <typename Rep, typename Period>
|
Chris@16
|
341 iter_type get(iter_type s, iter_type end, std::ios_base& ios, std::ios_base::iostate& err,
|
Chris@16
|
342 duration<Rep, Period> & d) const
|
Chris@16
|
343 {
|
Chris@16
|
344 if (std::has_facet<duration_units<CharT> >(ios.getloc()))
|
Chris@16
|
345 {
|
Chris@16
|
346 duration_units<CharT> const&facet = std::use_facet<duration_units<CharT> >(ios.getloc());
|
Chris@16
|
347 std::basic_string<CharT> str = facet.get_pattern();
|
Chris@16
|
348 return get(facet, s, end, ios, err, d, str.data(), str.data() + str.size());
|
Chris@16
|
349 }
|
Chris@16
|
350 else
|
Chris@16
|
351 {
|
Chris@16
|
352 duration_units_default<CharT> facet;
|
Chris@16
|
353 std::basic_string<CharT> str = facet.get_pattern();
|
Chris@16
|
354 return get(facet, s, end, ios, err, d, str.data(), str.data() + str.size());
|
Chris@16
|
355 }
|
Chris@16
|
356 }
|
Chris@16
|
357
|
Chris@16
|
358 /**
|
Chris@16
|
359 *
|
Chris@16
|
360 * @param s start input stream iterator
|
Chris@16
|
361 * @param end end input stream iterator
|
Chris@16
|
362 * @param ios a reference to a ios_base
|
Chris@16
|
363 * @param err the ios_base state
|
Chris@16
|
364 * @param r a reference to the duration representation.
|
Chris@16
|
365 * @Effects As if
|
Chris@16
|
366 * @code
|
Chris@16
|
367 * return std::use_facet<std::num_get<cahr_type, iter_type> >(ios.getloc()).get(s, end, ios, err, r);
|
Chris@16
|
368 * @endcode
|
Chris@16
|
369 *
|
Chris@16
|
370 * @Returns An iterator pointing just beyond the last character that can be determined to be part of a valid name
|
Chris@16
|
371 */
|
Chris@16
|
372 template <typename Rep>
|
Chris@16
|
373 iter_type get_value(iter_type s, iter_type end, std::ios_base& ios, std::ios_base::iostate& err, Rep& r) const
|
Chris@16
|
374 {
|
Chris@16
|
375 return std::use_facet<std::num_get<CharT, iter_type> >(ios.getloc()).get(s, end, ios, err, r);
|
Chris@16
|
376 }
|
Chris@101
|
377 template <typename Rep>
|
Chris@101
|
378 iter_type get_value(iter_type s, iter_type end, std::ios_base& ios, std::ios_base::iostate& err, process_times<Rep>& r) const
|
Chris@101
|
379 {
|
Chris@101
|
380 if (s == end) {
|
Chris@101
|
381 err |= std::ios_base::eofbit;
|
Chris@101
|
382 return s;
|
Chris@101
|
383 } else if (*s != '{') { // mandatory '{'
|
Chris@101
|
384 err |= std::ios_base::failbit;
|
Chris@101
|
385 return s;
|
Chris@101
|
386 }
|
Chris@101
|
387 ++s;
|
Chris@101
|
388 s = std::use_facet<std::num_get<CharT, iter_type> >(ios.getloc()).get(s, end, ios, err, r.real);
|
Chris@101
|
389 if (s == end) {
|
Chris@101
|
390 err |= std::ios_base::eofbit;
|
Chris@101
|
391 return s;
|
Chris@101
|
392 } else if (*s != ';') { // mandatory ';'
|
Chris@101
|
393 err |= std::ios_base::failbit;
|
Chris@101
|
394 return s;
|
Chris@101
|
395 }
|
Chris@101
|
396 ++s;
|
Chris@101
|
397 s = std::use_facet<std::num_get<CharT, iter_type> >(ios.getloc()).get(s, end, ios, err, r.user);
|
Chris@101
|
398 if (s == end) {
|
Chris@101
|
399 err |= std::ios_base::eofbit;
|
Chris@101
|
400 return s;
|
Chris@101
|
401 } else if (*s != ';') { // mandatory ';'
|
Chris@101
|
402 err |= std::ios_base::failbit;
|
Chris@101
|
403 return s;
|
Chris@101
|
404 }
|
Chris@101
|
405 ++s;
|
Chris@101
|
406 s = std::use_facet<std::num_get<CharT, iter_type> >(ios.getloc()).get(s, end, ios, err, r.system);
|
Chris@101
|
407 if (s == end) {
|
Chris@101
|
408 err |= std::ios_base::eofbit;
|
Chris@101
|
409 return s;
|
Chris@101
|
410 } else if (*s != '}') { // mandatory '}'
|
Chris@101
|
411 err |= std::ios_base::failbit;
|
Chris@101
|
412 return s;
|
Chris@101
|
413 }
|
Chris@101
|
414 return s;
|
Chris@101
|
415 }
|
Chris@16
|
416
|
Chris@16
|
417 /**
|
Chris@16
|
418 *
|
Chris@16
|
419 * @param s start input stream iterator
|
Chris@16
|
420 * @param e end input stream iterator
|
Chris@16
|
421 * @param ios a reference to a ios_base
|
Chris@16
|
422 * @param err the ios_base state
|
Chris@16
|
423 * @param rt a reference to the duration run-time ratio.
|
Chris@16
|
424 * @Returns An iterator pointing just beyond the last character that can be determined to be part of a valid name
|
Chris@16
|
425 */
|
Chris@16
|
426 iter_type get_unit(iter_type i, iter_type e, std::ios_base& is, std::ios_base::iostate& err, rt_ratio &rt) const
|
Chris@16
|
427 {
|
Chris@16
|
428 if (std::has_facet<duration_units<CharT> >(is.getloc()))
|
Chris@16
|
429 {
|
Chris@16
|
430 return get_unit(std::use_facet<duration_units<CharT> >(is.getloc()), i, e, is, err, rt);
|
Chris@16
|
431 }
|
Chris@16
|
432 else
|
Chris@16
|
433 {
|
Chris@16
|
434 duration_units_default<CharT> facet;
|
Chris@16
|
435 return get_unit(facet, i, e, is, err, rt);
|
Chris@16
|
436 }
|
Chris@16
|
437 }
|
Chris@16
|
438
|
Chris@16
|
439
|
Chris@16
|
440 iter_type get_unit(duration_units<CharT> const &facet, iter_type i, iter_type e, std::ios_base& is,
|
Chris@16
|
441 std::ios_base::iostate& err, rt_ratio &rt) const
|
Chris@16
|
442 {
|
Chris@16
|
443
|
Chris@16
|
444 if (*i == '[')
|
Chris@16
|
445 {
|
Chris@16
|
446 // parse [N/D]s or [N/D]second or [N/D]seconds format
|
Chris@16
|
447 ++i;
|
Chris@16
|
448 i = std::use_facet<std::num_get<CharT, iter_type> >(is.getloc()).get(i, e, is, err, rt.num);
|
Chris@16
|
449 if ( (err & std::ios_base::failbit) != 0)
|
Chris@16
|
450 {
|
Chris@16
|
451 return i;
|
Chris@16
|
452 }
|
Chris@16
|
453
|
Chris@16
|
454 if (i == e)
|
Chris@16
|
455 {
|
Chris@16
|
456 err |= std::ios_base::failbit;
|
Chris@16
|
457 return i;
|
Chris@16
|
458 }
|
Chris@16
|
459 CharT x = *i++;
|
Chris@16
|
460 if (x != '/')
|
Chris@16
|
461 {
|
Chris@16
|
462 err |= std::ios_base::failbit;
|
Chris@16
|
463 return i;
|
Chris@16
|
464 }
|
Chris@16
|
465 i = std::use_facet<std::num_get<CharT, iter_type> >(is.getloc()).get(i, e, is, err, rt.den);
|
Chris@16
|
466 if ( (err & std::ios_base::failbit) != 0)
|
Chris@16
|
467 {
|
Chris@16
|
468 return i;
|
Chris@16
|
469 }
|
Chris@16
|
470 if (i == e)
|
Chris@16
|
471 {
|
Chris@16
|
472 err |= std::ios_base::failbit;
|
Chris@16
|
473 return i;
|
Chris@16
|
474 }
|
Chris@16
|
475 if (*i != ']')
|
Chris@16
|
476 {
|
Chris@16
|
477 err |= std::ios_base::failbit;
|
Chris@16
|
478 return i;
|
Chris@16
|
479 }
|
Chris@16
|
480 ++i;
|
Chris@16
|
481 if (i == e)
|
Chris@16
|
482 {
|
Chris@16
|
483 err |= std::ios_base::failbit;
|
Chris@16
|
484 return i;
|
Chris@16
|
485 }
|
Chris@16
|
486 // parse s or second or seconds
|
Chris@16
|
487 return do_get_n_d_valid_unit(facet, i, e, is, err);
|
Chris@16
|
488 }
|
Chris@16
|
489 else
|
Chris@16
|
490 {
|
Chris@16
|
491 return do_get_valid_unit(facet, i, e, is, err, rt);
|
Chris@16
|
492 }
|
Chris@16
|
493 }
|
Chris@16
|
494
|
Chris@16
|
495 /**
|
Chris@16
|
496 * Unique identifier for this type of facet.
|
Chris@16
|
497 */
|
Chris@16
|
498 static std::locale::id id;
|
Chris@16
|
499
|
Chris@16
|
500 /**
|
Chris@16
|
501 * @Effects Destroy the facet
|
Chris@16
|
502 */
|
Chris@16
|
503 ~duration_get()
|
Chris@16
|
504 {
|
Chris@16
|
505 }
|
Chris@16
|
506
|
Chris@16
|
507 protected:
|
Chris@16
|
508
|
Chris@16
|
509 /**
|
Chris@16
|
510 * Extracts the run-time ratio associated to the duration when it is given in prefix form.
|
Chris@16
|
511 *
|
Chris@16
|
512 * This is an extension point of this facet so that we can take in account other periods that can have a useful
|
Chris@16
|
513 * translation in other contexts, as e.g. days and weeks.
|
Chris@16
|
514 *
|
Chris@16
|
515 * @param facet the duration_units facet
|
Chris@16
|
516 * @param i start input stream iterator.
|
Chris@16
|
517 * @param e end input stream iterator.
|
Chris@16
|
518 * @param ios a reference to a ios_base.
|
Chris@16
|
519 * @param err the ios_base state.
|
Chris@16
|
520 * @return @c s
|
Chris@16
|
521 */
|
Chris@16
|
522 iter_type do_get_n_d_valid_unit(duration_units<CharT> const &facet, iter_type i, iter_type e,
|
Chris@16
|
523 std::ios_base&, std::ios_base::iostate& err) const
|
Chris@16
|
524 {
|
Chris@16
|
525 // parse SI name, short or long
|
Chris@16
|
526
|
Chris@16
|
527 const string_type* units = facet.get_n_d_valid_units_start();
|
Chris@16
|
528 const string_type* units_end = facet.get_n_d_valid_units_end();
|
Chris@16
|
529
|
Chris@16
|
530 const string_type* k = chrono_detail::scan_keyword(i, e, units, units_end,
|
Chris@16
|
531 //~ std::use_facet<std::ctype<CharT> >(loc),
|
Chris@16
|
532 err);
|
Chris@16
|
533 if (err & (std::ios_base::badbit | std::ios_base::failbit))
|
Chris@16
|
534 {
|
Chris@16
|
535 return i;
|
Chris@16
|
536 }
|
Chris@16
|
537 if (!facet.match_n_d_valid_unit(k))
|
Chris@16
|
538 {
|
Chris@16
|
539 err |= std::ios_base::failbit;
|
Chris@16
|
540 }
|
Chris@16
|
541 return i;
|
Chris@16
|
542 }
|
Chris@16
|
543
|
Chris@16
|
544 /**
|
Chris@16
|
545 * Extracts the run-time ratio associated to the duration when it is given in prefix form.
|
Chris@16
|
546 *
|
Chris@16
|
547 * This is an extension point of this facet so that we can take in account other periods that can have a useful
|
Chris@16
|
548 * translation in other contexts, as e.g. days and weeks.
|
Chris@16
|
549 *
|
Chris@16
|
550 * @param facet the duration_units facet
|
Chris@16
|
551 * @param i start input stream iterator.
|
Chris@16
|
552 * @param e end input stream iterator.
|
Chris@16
|
553 * @param ios a reference to a ios_base.
|
Chris@16
|
554 * @param err the ios_base state.
|
Chris@16
|
555 * @param rt a reference to the duration run-time ratio.
|
Chris@16
|
556 * @Effects
|
Chris@16
|
557 * @Returns An iterator pointing just beyond the last character that can be determined to be part of a valid name.
|
Chris@16
|
558 */
|
Chris@16
|
559 iter_type do_get_valid_unit(duration_units<CharT> const &facet, iter_type i, iter_type e,
|
Chris@16
|
560 std::ios_base&, std::ios_base::iostate& err, rt_ratio &rt) const
|
Chris@16
|
561 {
|
Chris@16
|
562 // parse SI name, short or long
|
Chris@16
|
563
|
Chris@16
|
564 const string_type* units = facet.get_valid_units_start();
|
Chris@16
|
565 const string_type* units_end = facet.get_valid_units_end();
|
Chris@16
|
566
|
Chris@16
|
567 err = std::ios_base::goodbit;
|
Chris@16
|
568 const string_type* k = chrono_detail::scan_keyword(i, e, units, units_end,
|
Chris@16
|
569 //~ std::use_facet<std::ctype<CharT> >(loc),
|
Chris@16
|
570 err);
|
Chris@16
|
571 if (err & (std::ios_base::badbit | std::ios_base::failbit))
|
Chris@16
|
572 {
|
Chris@16
|
573 return i;
|
Chris@16
|
574 }
|
Chris@16
|
575 if (!facet.match_valid_unit(k, rt))
|
Chris@16
|
576 {
|
Chris@16
|
577 err |= std::ios_base::failbit;
|
Chris@16
|
578 }
|
Chris@16
|
579 return i;
|
Chris@16
|
580 }
|
Chris@16
|
581 };
|
Chris@16
|
582
|
Chris@16
|
583 /**
|
Chris@16
|
584 * Unique identifier for this type of facet.
|
Chris@16
|
585 */
|
Chris@16
|
586 template <class CharT, class InputIterator>
|
Chris@16
|
587 std::locale::id duration_get<CharT, InputIterator>::id;
|
Chris@16
|
588
|
Chris@16
|
589 } // chrono
|
Chris@16
|
590 }
|
Chris@16
|
591 // boost
|
Chris@16
|
592
|
Chris@16
|
593 #endif // header
|