123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591 |
- #ifndef BOOST_CHRONO_IO_DURATION_GET_HPP
- #define BOOST_CHRONO_IO_DURATION_GET_HPP
- #include <boost/chrono/config.hpp>
- #include <string>
- #include <boost/type_traits/is_scalar.hpp>
- #include <boost/utility/enable_if.hpp>
- #include <boost/type_traits/is_signed.hpp>
- #include <boost/mpl/if.hpp>
- #include <boost/integer/common_factor_rt.hpp>
- #include <boost/chrono/detail/scan_keyword.hpp>
- #include <boost/chrono/detail/no_warning/signed_unsigned_cmp.hpp>
- #include <boost/chrono/process_cpu_clocks.hpp>
- #include <boost/assert.hpp>
- #include <locale>
- namespace boost
- {
- namespace chrono
- {
- namespace detail
- {
- template <class Rep, bool = is_scalar<Rep>::value>
- struct duration_io_intermediate
- {
- typedef Rep type;
- };
- template <class Rep>
- struct duration_io_intermediate<Rep, true>
- {
- typedef typename mpl::if_c<is_floating_point<Rep>::value, long double, typename mpl::if_c<
- is_signed<Rep>::value, long long, unsigned long long>::type>::type type;
- };
- template <class Rep>
- struct duration_io_intermediate<process_times<Rep>, false>
- {
- typedef process_times<typename duration_io_intermediate<Rep>::type> type;
- };
- template <typename intermediate_type>
- typename enable_if<is_integral<intermediate_type> , bool>::type reduce(intermediate_type& r,
- unsigned long long& den, std::ios_base::iostate& err)
- {
- typedef typename common_type<intermediate_type, unsigned long long>::type common_type_t;
-
- common_type_t t = integer::gcd<common_type_t>(common_type_t(r), common_type_t(den));
- r /= t;
- den /= t;
- if (den != 1)
- {
-
- err |= std::ios_base::failbit;
- return false;
- }
- return true;
- }
- template <typename intermediate_type>
- typename disable_if<is_integral<intermediate_type> , bool>::type reduce(intermediate_type&, unsigned long long&,
- std::ios_base::iostate&)
- {
- return true;
- }
- }
-
- template <class CharT, class InputIterator = std::istreambuf_iterator<CharT> >
- class duration_get: public std::locale::facet
- {
- public:
-
- typedef CharT char_type;
-
- typedef std::basic_string<CharT> string_type;
-
- typedef InputIterator iter_type;
-
- explicit duration_get(size_t refs = 0) :
- std::locale::facet(refs)
- {
- }
-
- template <typename Rep, typename Period>
- iter_type get(iter_type s, iter_type end, std::ios_base& ios, std::ios_base::iostate& err,
- duration<Rep, Period> &d, const char_type *pattern, const char_type *pat_end) const
- {
- if (std::has_facet<duration_units<CharT> >(ios.getloc()))
- {
- duration_units<CharT> const&facet = std::use_facet<duration_units<CharT> >(ios.getloc());
- return get(facet, s, end, ios, err, d, pattern, pat_end);
- }
- else
- {
- duration_units_default<CharT> facet;
- return get(facet, s, end, ios, err, d, pattern, pat_end);
- }
- }
- template <typename Rep, typename Period>
- iter_type get(duration_units<CharT> const&facet, iter_type s, iter_type end, std::ios_base& ios,
- std::ios_base::iostate& err, duration<Rep, Period> &d, const char_type *pattern, const char_type *pat_end) const
- {
- typedef typename detail::duration_io_intermediate<Rep>::type intermediate_type;
- intermediate_type r;
- rt_ratio rt;
- bool value_found = false, unit_found = false;
- const std::ctype<char_type>& ct = std::use_facet<std::ctype<char_type> >(ios.getloc());
- while (pattern != pat_end && err == std::ios_base::goodbit)
- {
- if (s == end)
- {
- err |= std::ios_base::eofbit;
- break;
- }
- if (ct.narrow(*pattern, 0) == '%')
- {
- if (++pattern == pat_end)
- {
- err |= std::ios_base::failbit;
- return s;
- }
- char cmd = ct.narrow(*pattern, 0);
- switch (cmd)
- {
- case 'v':
- {
- if (value_found)
- {
- err |= std::ios_base::failbit;
- return s;
- }
- value_found = true;
- s = get_value(s, end, ios, err, r);
- if (err & (std::ios_base::badbit | std::ios_base::failbit))
- {
- return s;
- }
- break;
- }
- case 'u':
- {
- if (unit_found)
- {
- err |= std::ios_base::failbit;
- return s;
- }
- unit_found = true;
- s = get_unit(facet, s, end, ios, err, rt);
- if (err & (std::ios_base::badbit | std::ios_base::failbit))
- {
- return s;
- }
- break;
- }
- default:
- BOOST_ASSERT(false && "Boost::Chrono internal error.");
- break;
- }
- ++pattern;
- }
- else if (ct.is(std::ctype_base::space, *pattern))
- {
- for (++pattern; pattern != pat_end && ct.is(std::ctype_base::space, *pattern); ++pattern)
- ;
- for (; s != end && ct.is(std::ctype_base::space, *s); ++s)
- ;
- }
- else if (ct.toupper(*s) == ct.toupper(*pattern))
- {
- ++s;
- ++pattern;
- }
- else
- {
- err |= std::ios_base::failbit;
- return s;
- }
- }
- unsigned long long num = rt.num;
- unsigned long long den = rt.den;
-
-
- unsigned long long gcd_n1_n2 = integer::gcd<unsigned long long>(num, Period::num);
- unsigned long long gcd_d1_d2 = integer::gcd<unsigned long long>(den, Period::den);
- num /= gcd_n1_n2;
- den /= gcd_d1_d2;
- unsigned long long n2 = Period::num / gcd_n1_n2;
- unsigned long long d2 = Period::den / gcd_d1_d2;
- if (num > (std::numeric_limits<unsigned long long>::max)() / d2 || den
- > (std::numeric_limits<unsigned long long>::max)() / n2)
- {
-
- err |= std::ios_base::failbit;
- return s;
- }
- num *= d2;
- den *= n2;
- typedef typename common_type<intermediate_type, unsigned long long>::type common_type_t;
-
- if (!detail::reduce(r, den, err)) return s;
- if (chrono::detail::gt(r, ( (duration_values<common_type_t>::max)() / num)))
- {
-
- err |= std::ios_base::failbit;
- return s;
- }
- common_type_t t = r * num;
- t /= den;
- if (t > duration_values<common_type_t>::zero())
- {
- if ( (duration_values<Rep>::max)() < Rep(t))
- {
-
- err |= std::ios_base::failbit;
- return s;
- }
- }
-
- d = duration<Rep, Period> (Rep(t));
- return s;
- }
-
- template <typename Rep, typename Period>
- iter_type get(iter_type s, iter_type end, std::ios_base& ios, std::ios_base::iostate& err,
- duration<Rep, Period> & d) const
- {
- if (std::has_facet<duration_units<CharT> >(ios.getloc()))
- {
- duration_units<CharT> const&facet = std::use_facet<duration_units<CharT> >(ios.getloc());
- std::basic_string<CharT> str = facet.get_pattern();
- return get(facet, s, end, ios, err, d, str.data(), str.data() + str.size());
- }
- else
- {
- duration_units_default<CharT> facet;
- std::basic_string<CharT> str = facet.get_pattern();
- return get(facet, s, end, ios, err, d, str.data(), str.data() + str.size());
- }
- }
-
- template <typename Rep>
- iter_type get_value(iter_type s, iter_type end, std::ios_base& ios, std::ios_base::iostate& err, Rep& r) const
- {
- return std::use_facet<std::num_get<CharT, iter_type> >(ios.getloc()).get(s, end, ios, err, r);
- }
- template <typename Rep>
- iter_type get_value(iter_type s, iter_type end, std::ios_base& ios, std::ios_base::iostate& err, process_times<Rep>& r) const
- {
- if (s == end) {
- err |= std::ios_base::eofbit;
- return s;
- } else if (*s != '{') {
- err |= std::ios_base::failbit;
- return s;
- }
- ++s;
- s = std::use_facet<std::num_get<CharT, iter_type> >(ios.getloc()).get(s, end, ios, err, r.real);
- if (s == end) {
- err |= std::ios_base::eofbit;
- return s;
- } else if (*s != ';') {
- err |= std::ios_base::failbit;
- return s;
- }
- ++s;
- s = std::use_facet<std::num_get<CharT, iter_type> >(ios.getloc()).get(s, end, ios, err, r.user);
- if (s == end) {
- err |= std::ios_base::eofbit;
- return s;
- } else if (*s != ';') {
- err |= std::ios_base::failbit;
- return s;
- }
- ++s;
- s = std::use_facet<std::num_get<CharT, iter_type> >(ios.getloc()).get(s, end, ios, err, r.system);
- if (s == end) {
- err |= std::ios_base::eofbit;
- return s;
- } else if (*s != '}') {
- err |= std::ios_base::failbit;
- return s;
- }
- return s;
- }
-
- iter_type get_unit(iter_type i, iter_type e, std::ios_base& is, std::ios_base::iostate& err, rt_ratio &rt) const
- {
- if (std::has_facet<duration_units<CharT> >(is.getloc()))
- {
- return get_unit(std::use_facet<duration_units<CharT> >(is.getloc()), i, e, is, err, rt);
- }
- else
- {
- duration_units_default<CharT> facet;
- return get_unit(facet, i, e, is, err, rt);
- }
- }
- iter_type get_unit(duration_units<CharT> const &facet, iter_type i, iter_type e, std::ios_base& is,
- std::ios_base::iostate& err, rt_ratio &rt) const
- {
- if (*i == '[')
- {
-
- ++i;
- i = std::use_facet<std::num_get<CharT, iter_type> >(is.getloc()).get(i, e, is, err, rt.num);
- if ( (err & std::ios_base::failbit) != 0)
- {
- return i;
- }
- if (i == e)
- {
- err |= std::ios_base::failbit;
- return i;
- }
- CharT x = *i++;
- if (x != '/')
- {
- err |= std::ios_base::failbit;
- return i;
- }
- i = std::use_facet<std::num_get<CharT, iter_type> >(is.getloc()).get(i, e, is, err, rt.den);
- if ( (err & std::ios_base::failbit) != 0)
- {
- return i;
- }
- if (i == e)
- {
- err |= std::ios_base::failbit;
- return i;
- }
- if (*i != ']')
- {
- err |= std::ios_base::failbit;
- return i;
- }
- ++i;
- if (i == e)
- {
- err |= std::ios_base::failbit;
- return i;
- }
-
- return do_get_n_d_valid_unit(facet, i, e, is, err);
- }
- else
- {
- return do_get_valid_unit(facet, i, e, is, err, rt);
- }
- }
-
- static std::locale::id id;
-
- ~duration_get()
- {
- }
- protected:
-
- iter_type do_get_n_d_valid_unit(duration_units<CharT> const &facet, iter_type i, iter_type e,
- std::ios_base&, std::ios_base::iostate& err) const
- {
-
- const string_type* units = facet.get_n_d_valid_units_start();
- const string_type* units_end = facet.get_n_d_valid_units_end();
- const string_type* k = chrono_detail::scan_keyword(i, e, units, units_end,
-
- err);
- if (err & (std::ios_base::badbit | std::ios_base::failbit))
- {
- return i;
- }
- if (!facet.match_n_d_valid_unit(k))
- {
- err |= std::ios_base::failbit;
- }
- return i;
- }
-
- iter_type do_get_valid_unit(duration_units<CharT> const &facet, iter_type i, iter_type e,
- std::ios_base&, std::ios_base::iostate& err, rt_ratio &rt) const
- {
-
- const string_type* units = facet.get_valid_units_start();
- const string_type* units_end = facet.get_valid_units_end();
- err = std::ios_base::goodbit;
- const string_type* k = chrono_detail::scan_keyword(i, e, units, units_end,
-
- err);
- if (err & (std::ios_base::badbit | std::ios_base::failbit))
- {
- return i;
- }
- if (!facet.match_valid_unit(k, rt))
- {
- err |= std::ios_base::failbit;
- }
- return i;
- }
- };
-
- template <class CharT, class InputIterator>
- std::locale::id duration_get<CharT, InputIterator>::id;
- }
- }
- #endif
|