123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317 |
- #ifndef BOOST_CHRONO_IO_DURATION_PUT_HPP
- #define BOOST_CHRONO_IO_DURATION_PUT_HPP
- #include <boost/chrono/config.hpp>
- #include <boost/chrono/io/duration_units.hpp>
- #include <boost/chrono/process_cpu_clocks.hpp>
- #include <boost/assert.hpp>
- #include <locale>
- namespace boost
- {
- namespace chrono
- {
- namespace detail
- {
- template <class T>
- struct propagate {
- typedef T type;
- };
- template <>
- struct propagate<boost::int_least32_t> {
- typedef boost::int_least64_t type;
- };
- }
-
- template <class CharT, class OutputIterator = std::ostreambuf_iterator<CharT> >
- class duration_put: public std::locale::facet
- {
- public:
-
- typedef CharT char_type;
-
- typedef std::basic_string<CharT> string_type;
-
- typedef OutputIterator iter_type;
-
- explicit duration_put(size_t refs = 0) :
- std::locale::facet(refs)
- {
- }
-
- template <typename Rep, typename Period>
- iter_type put(iter_type s, std::ios_base& ios, char_type fill, duration<Rep, Period> const& d, const CharT* pattern,
- const CharT* pat_end, const char_type* val = 0) const
- {
- if (std::has_facet<duration_units<CharT> >(ios.getloc()))
- {
- duration_units<CharT> const&facet = std::use_facet<duration_units<CharT> >(
- ios.getloc());
- return put(facet, s, ios, fill, d, pattern, pat_end, val);
- }
- else
- {
- duration_units_default<CharT> facet;
- return put(facet, s, ios, fill, d, pattern, pat_end, val);
- }
- }
- template <typename Rep, typename Period>
- iter_type put(duration_units<CharT> const& units_facet, iter_type s, std::ios_base& ios, char_type fill,
- duration<Rep, Period> const& d, const CharT* pattern, const CharT* pat_end, const char_type* val = 0) const
- {
- const std::ctype<char_type>& ct = std::use_facet<std::ctype<char_type> >(ios.getloc());
- for (; pattern != pat_end; ++pattern)
- {
- if (ct.narrow(*pattern, 0) == '%')
- {
- if (++pattern == pat_end)
- {
- *s++ = pattern[-1];
- break;
- }
- char fmt = ct.narrow(*pattern, 0);
- switch (fmt)
- {
- case 'v':
- {
- s = put_value(s, ios, fill, d, val);
- break;
- }
- case 'u':
- {
- s = put_unit(units_facet, s, ios, fill, d);
- break;
- }
- default:
- BOOST_ASSERT(false && "Boost::Chrono internal error.");
- break;
- }
- }
- else
- *s++ = *pattern;
- }
- return s;
- }
-
- template <typename Rep, typename Period>
- iter_type put(iter_type s, std::ios_base& ios, char_type fill, duration<Rep, Period> const& d, const char_type* val = 0) 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 put(facet, s, ios, fill, d, str.data(), str.data() + str.size(), val);
- }
- else
- {
- duration_units_default<CharT> facet;
- std::basic_string<CharT> str = facet.get_pattern();
- return put(facet, s, ios, fill, d, str.data(), str.data() + str.size(), val);
- }
- }
-
- template <typename Rep, typename Period>
- iter_type put_value(iter_type s, std::ios_base& ios, char_type fill, duration<Rep, Period> const& d, const char_type* val = 0) const
- {
- if (val)
- {
- while (*val) {
- *s = *val;
- s++; val++;
- }
- return s;
- }
- return std::use_facet<std::num_put<CharT, iter_type> >(ios.getloc()).put(s, ios, fill,
- static_cast<typename detail::propagate<Rep>::type> (d.count()));
- }
- template <typename Rep, typename Period>
- iter_type put_value(iter_type s, std::ios_base& ios, char_type fill, duration<process_times<Rep>, Period> const& d, const char_type* = 0) const
- {
- *s++ = CharT('{');
- s = put_value(s, ios, fill, process_real_cpu_clock::duration(d.count().real));
- *s++ = CharT(';');
- s = put_value(s, ios, fill, process_user_cpu_clock::duration(d.count().user));
- *s++ = CharT(';');
- s = put_value(s, ios, fill, process_system_cpu_clock::duration(d.count().system));
- *s++ = CharT('}');
- return s;
- }
-
- template <typename Rep, typename Period>
- iter_type put_unit(iter_type s, std::ios_base& ios, char_type fill, duration<Rep, Period> const& d) const
- {
- if (std::has_facet<duration_units<CharT> >(ios.getloc()))
- {
- duration_units<CharT> const&facet = std::use_facet<duration_units<CharT> >(
- ios.getloc());
- return put_unit(facet, s, ios, fill, d);
- }
- else
- {
- duration_units_default<CharT> facet;
- return put_unit(facet, s, ios, fill, d);
- }
- }
- template <typename Rep, typename Period>
- iter_type put_unit(duration_units<CharT> const& facet, iter_type s, std::ios_base& ios, char_type fill,
- duration<Rep, Period> const& d) const
- {
- if (facet.template is_named_unit<Period>()) {
- string_type str = facet.get_unit(get_duration_style(ios), d);
- s=std::copy(str.begin(), str.end(), s);
- } else {
- *s++ = CharT('[');
- std::use_facet<std::num_put<CharT, iter_type> >(ios.getloc()).put(s, ios, fill, Period::num);
- *s++ = CharT('/');
- std::use_facet<std::num_put<CharT, iter_type> >(ios.getloc()).put(s, ios, fill, Period::den);
- *s++ = CharT(']');
- string_type str = facet.get_n_d_unit(get_duration_style(ios), d);
- s=std::copy(str.begin(), str.end(), s);
- }
- return s;
- }
- template <typename Rep, typename Period>
- iter_type put_unit(duration_units<CharT> const& facet, iter_type s, std::ios_base& ios, char_type fill,
- duration<process_times<Rep>, Period> const& d) const
- {
- duration<Rep,Period> real(d.count().real);
- if (facet.template is_named_unit<Period>()) {
- string_type str = facet.get_unit(get_duration_style(ios), real);
- s=std::copy(str.begin(), str.end(), s);
- } else {
- *s++ = CharT('[');
- std::use_facet<std::num_put<CharT, iter_type> >(ios.getloc()).put(s, ios, fill, Period::num);
- *s++ = CharT('/');
- std::use_facet<std::num_put<CharT, iter_type> >(ios.getloc()).put(s, ios, fill, Period::den);
- *s++ = CharT(']');
- string_type str = facet.get_n_d_unit(get_duration_style(ios), real);
- s=std::copy(str.begin(), str.end(), s);
- }
- return s;
- }
-
- static std::locale::id id;
-
- ~duration_put()
- {
- }
- };
- template <class CharT, class OutputIterator>
- std::locale::id duration_put<CharT, OutputIterator>::id;
- }
- }
- #endif
|