123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261 |
- #ifndef BOOST_CHRONO_IO_TIME_POINT_PUT_HPP
- #define BOOST_CHRONO_IO_TIME_POINT_PUT_HPP
- #include <boost/chrono/config.hpp>
- #include <boost/chrono/io/time_point_units.hpp>
- #include <boost/chrono/io/duration_put.hpp>
- #include <boost/assert.hpp>
- #include <locale>
- namespace boost
- {
- namespace chrono
- {
-
- template <class CharT, class OutputIterator = std::ostreambuf_iterator<CharT> >
- class time_point_put: public std::locale::facet
- {
- public:
-
- typedef CharT char_type;
-
- typedef std::basic_string<CharT> string_type;
-
- typedef OutputIterator iter_type;
-
- explicit time_point_put(size_t refs = 0) :
- std::locale::facet(refs)
- {
- }
-
- template <class Clock, class Duration>
- iter_type put(iter_type i, std::ios_base& ios, char_type fill, time_point<Clock, Duration> const& tp, const CharT* pattern,
- const CharT* pat_end) const
- {
- if (std::has_facet<time_point_units<CharT> >(ios.getloc()))
- {
- time_point_units<CharT> const &facet =
- std::use_facet<time_point_units<CharT> >(ios.getloc());
- return put(facet, i, ios, fill, tp, pattern, pat_end);
- }
- else
- {
- time_point_units_default<CharT> facet;
- return put(facet, i, ios, fill, tp, pattern, pat_end);
- }
- }
- template <class Clock, class Duration>
- iter_type put(time_point_units<CharT> const& units_facet, iter_type s, std::ios_base& ios, char_type fill,
- time_point<Clock, Duration> const& tp, const CharT* pattern, const CharT* pat_end) 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 'd':
- {
- s = put_duration(s, ios, fill, tp.time_since_epoch());
- break;
- }
- case 'e':
- {
- s = put_epoch<Clock> (units_facet, s, ios);
- break;
- }
- default:
- BOOST_ASSERT(false && "Boost::Chrono internal error.");
- break;
- }
- }
- else
- *s++ = *pattern;
- }
- return s;
- }
-
- template <class Clock, class Duration>
- iter_type put(iter_type i, std::ios_base& ios, char_type fill, time_point<Clock, Duration> const& tp) const
- {
- if (std::has_facet<time_point_units<CharT> >(ios.getloc()))
- {
- time_point_units<CharT> const &facet =
- std::use_facet<time_point_units<CharT> >(ios.getloc());
- std::basic_string<CharT> str = facet.get_pattern();
- return put(facet, i, ios, fill, tp, str.data(), str.data() + str.size());
- }
- else
- {
- time_point_units_default<CharT> facet;
- std::basic_string<CharT> str = facet.get_pattern();
- return put(facet, i, ios, fill, tp, str.data(), str.data() + str.size());
- }
- }
-
- template <typename Rep, typename Period>
- iter_type put_duration(iter_type i, std::ios_base& ios, char_type fill, duration<Rep, Period> const& d) const
- {
- if (std::has_facet<duration_put<CharT> >(ios.getloc()))
- {
- duration_put<CharT> const &facet = std::use_facet<duration_put<CharT> >(ios.getloc());
- return facet.put(i, ios, fill, d);
- }
- else
- {
- duration_put<CharT> facet;
- return facet.put(i, ios, fill, d);
- }
- }
-
- template <typename Clock>
- iter_type put_epoch(iter_type i, std::ios_base& os) const
- {
- if (std::has_facet<time_point_units<CharT> >(os.getloc()))
- {
- time_point_units<CharT> const &facet = std::use_facet<time_point_units<CharT> >(os.getloc());
- return put_epoch<Clock> (facet, i, os);
- }
- else
- {
- time_point_units_default<CharT> facet;
- return put_epoch<Clock> (facet, i, os);
- }
- }
- template <typename Clock>
- iter_type put_epoch(time_point_units<CharT> const& facet, iter_type s, std::ios_base&) const
- {
- string_type str = facet.template get_epoch<Clock>();
- s= std::copy(str.begin(), str.end(), s);
- return s;
- }
-
- static std::locale::id id;
-
- ~time_point_put()
- {
- }
- };
- template <class CharT, class OutputIterator>
- std::locale::id time_point_put<CharT, OutputIterator>::id;
- }
- }
- #endif
|