1 // (C) Copyright Howard Hinnant
2 // (C) Copyright 2011 Vicente J. Botet Escriba
3 // Use, modification and distribution are subject to the Boost Software License,
4 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt).
8 #ifndef BOOST_CHRONO_IO_TIME_POINT_GET_HPP
9 #define BOOST_CHRONO_IO_TIME_POINT_GET_HPP
11 #include <boost/chrono/config.hpp>
12 #include <boost/chrono/detail/scan_keyword.hpp>
13 #include <boost/chrono/io/time_point_units.hpp>
14 #include <boost/chrono/io/duration_get.hpp>
15 #include <boost/assert.hpp>
20 * Duration formatting facet for input.
27 template <class CharT, class InputIterator = std::istreambuf_iterator<CharT> >
28 class time_point_get: public std::locale::facet
32 * Type of character the facet is instantiated on.
34 typedef CharT char_type;
36 * Type of iterator used to scan the character buffer.
38 typedef InputIterator iter_type;
41 * Construct a @c time_point_get facet.
43 * @Effects Construct a @c time_point_get facet.
44 * If the @c refs argument is @c 0 then destruction of the object is
45 * delegated to the @c locale, or locales, containing it. This allows
46 * the user to ignore lifetime management issues. On the other had,
47 * if @c refs is @c 1 then the object must be explicitly deleted;
48 * the @c locale will not do so. In this case, the object can be
49 * maintained across the lifetime of multiple locales.
52 explicit time_point_get(size_t refs = 0) :
53 std::locale::facet(refs)
58 * @param s start input stream iterator
59 * @param end end input stream iterator
60 * @param ios a reference to a ios_base
61 * @param err the ios_base state
62 * @param d the duration
63 * @param pattern begin of the formatting pattern
64 * @param pat_end end of the formatting pattern
66 * Requires: [pattern,pat_end) shall be a valid range.
68 * Effects: The function starts by evaluating err = std::ios_base::goodbit.
69 * It then enters a loop, reading zero or more characters from s at
70 * each iteration. Unless otherwise specified below, the loop
71 * terminates when the first of the following conditions holds:
72 * - The expression pattern == pat_end evaluates to true.
73 * - The expression err == std::ios_base::goodbit evaluates to false.
74 * - The expression s == end evaluates to true, in which case the
75 * function evaluates err = std::ios_base::eofbit | std::ios_base::failbit.
76 * - The next element of pattern is equal to '%', followed by a conversion
77 * specifier character, the functions @c get_duration or @c get_epoch are called depending on
78 * whether the format is @c 'd' or @c 'e'.
79 * If the number of elements in the range [pattern,pat_end) is not
80 * sufficient to unambiguously determine whether the conversion
81 * specification is complete and valid, the function evaluates
82 * err = std::ios_base::failbit. Otherwise, the function evaluates
83 * s = do_get(s, end, ios, err, d). If err == std::ios_base::goodbit holds after
84 * the evaluation of the expression, the function increments pattern to
85 * point just past the end of the conversion specification and continues
87 * - The expression isspace(*pattern, ios.getloc()) evaluates to true, in
88 * which case the function first increments pattern until
89 * pattern == pat_end || !isspace(*pattern, ios.getloc()) evaluates to true,
90 * then advances s until s == end || !isspace(*s, ios.getloc()) is true,
91 * and finally resumes looping.
92 * - The next character read from s matches the element pointed to by
93 * pattern in a case-insensitive comparison, in which case the function
94 * evaluates ++pattern, ++s and continues looping. Otherwise, the function
95 * evaluates err = std::ios_base::failbit.
100 template <class Clock, class Duration>
101 iter_type get(iter_type i, iter_type e, std::ios_base& is, std::ios_base::iostate& err,
102 time_point<Clock, Duration> &tp, const char_type *pattern, const char_type *pat_end) const
104 if (std::has_facet<time_point_units<CharT> >(is.getloc()))
106 time_point_units<CharT> const &facet = std::use_facet<time_point_units<CharT> >(is.getloc());
107 return get(facet, i, e, is, err, tp, pattern, pat_end);
111 time_point_units_default<CharT> facet;
112 return get(facet, i, e, is, err, tp, pattern, pat_end);
116 template <class Clock, class Duration>
117 iter_type get(time_point_units<CharT> const &facet, iter_type s, iter_type end, std::ios_base& ios,
118 std::ios_base::iostate& err, time_point<Clock, Duration> &tp, const char_type *pattern,
119 const char_type *pat_end) const
123 bool duration_found = false, epoch_found = false;
125 const std::ctype<char_type>& ct = std::use_facet<std::ctype<char_type> >(ios.getloc());
126 err = std::ios_base::goodbit;
127 while (pattern != pat_end && err == std::ios_base::goodbit)
131 err |= std::ios_base::eofbit;
134 if (ct.narrow(*pattern, 0) == '%')
136 if (++pattern == pat_end)
138 err |= std::ios_base::failbit;
141 char cmd = ct.narrow(*pattern, 0);
148 err |= std::ios_base::failbit;
151 duration_found = true;
152 s = get_duration(s, end, ios, err, d);
153 if (err & (std::ios_base::badbit | std::ios_base::failbit))
163 err |= std::ios_base::failbit;
167 s = get_epoch<Clock> (facet, s, end, ios, err);
168 if (err & (std::ios_base::badbit | std::ios_base::failbit))
175 BOOST_ASSERT(false && "Boost::Chrono internal error.");
181 else if (ct.is(std::ctype_base::space, *pattern))
183 for (++pattern; pattern != pat_end && ct.is(std::ctype_base::space, *pattern); ++pattern)
185 for (; s != end && ct.is(std::ctype_base::space, *s); ++s)
188 else if (ct.toupper(*s) == ct.toupper(*pattern))
195 err |= std::ios_base::failbit;
199 // Success! Store it.
200 tp = time_point<Clock, Duration> (d);
206 * @param s an input stream iterator
207 * @param ios a reference to a ios_base
208 * @param d the duration
209 * Stores the duration pattern from the @c duration_unit facet in let say @c str. Last as if
211 * return get(s, end, ios, err, ios, d, str.data(), str.data() + str.size());
213 * @Returns An iterator pointing just beyond the last character that can be determined to be part of a valid name
215 template <class Clock, class Duration>
216 iter_type get(iter_type i, iter_type e, std::ios_base& is, std::ios_base::iostate& err,
217 time_point<Clock, Duration> &tp) const
219 if (std::has_facet<time_point_units<CharT> >(is.getloc()))
221 time_point_units<CharT> const &facet = std::use_facet<time_point_units<CharT> >(is.getloc());
222 std::basic_string<CharT> str = facet.get_pattern();
223 return get(facet, i, e, is, err, tp, str.data(), str.data() + str.size());
227 time_point_units_default<CharT> facet;
228 std::basic_string<CharT> str = facet.get_pattern();
229 return get(facet, i, e, is, err, tp, str.data(), str.data() + str.size());
236 * return facet.get(s, end, ios, err, d);
238 * where @c facet is either the @c duration_get facet associated to the @c ios or an instance of the default @c duration_get facet.
240 * @Returns An iterator pointing just beyond the last character that can be determined to be part of a valid duration.
242 template <typename Rep, typename Period>
243 iter_type get_duration(iter_type i, iter_type e, std::ios_base& is, std::ios_base::iostate& err,
244 duration<Rep, Period>& d) const
246 if (std::has_facet<duration_get<CharT> >(is.getloc()))
248 duration_get<CharT> const &facet = std::use_facet<duration_get<CharT> >(is.getloc());
249 return get_duration(facet, i, e, is, err, d);
253 duration_get<CharT> facet;
254 return get_duration(facet, i, e, is, err, d);
258 template <typename Rep, typename Period>
259 iter_type get_duration(duration_get<CharT> const& facet, iter_type s, iter_type end, std::ios_base& ios,
260 std::ios_base::iostate& err, duration<Rep, Period>& d) const
262 return facet.get(s, end, ios, err, d);
267 * @Effects Let @c facet be the @c time_point_units facet associated to @c is or a new instance of the default @c time_point_units_default facet.
268 * Let @c epoch be the epoch string associated to the Clock using this facet.
269 * Scans @c i to match @c epoch or @c e is reached.
271 * If not match before the @c e is reached @c std::ios_base::failbit is set in @c err.
272 * If @c e is reached @c std::ios_base::failbit is set in @c err.
274 * @Returns An iterator pointing just beyond the last character that can be determined to be part of a valid epoch.
276 template <class Clock>
277 iter_type get_epoch(iter_type i, iter_type e, std::ios_base& is, std::ios_base::iostate& err) const
279 if (std::has_facet<time_point_units<CharT> >(is.getloc()))
281 time_point_units<CharT> const &facet = std::use_facet<time_point_units<CharT> >(is.getloc());
282 return get_epoch<Clock>(facet, i, e, is, err);
286 time_point_units_default<CharT> facet;
287 return get_epoch<Clock>(facet, i, e, is, err);
291 template <class Clock>
292 iter_type get_epoch(time_point_units<CharT> const &facet, iter_type i, iter_type e, std::ios_base&,
293 std::ios_base::iostate& err) const
295 const std::basic_string<CharT> epoch = facet.template get_epoch<Clock> ();
296 std::ptrdiff_t k = chrono_detail::scan_keyword(i, e, &epoch, &epoch + 1,
297 //~ std::use_facet<std::ctype<CharT> >(ios.getloc()),
301 err |= std::ios_base::failbit;
308 * Unique identifier for this type of facet.
310 static std::locale::id id;
313 * @Effects Destroy the facet
321 * Unique identifier for this type of facet.
323 template <class CharT, class InputIterator>
324 std::locale::id time_point_get<CharT, InputIterator>::id;