Chris@16: // (C) Copyright Howard Hinnant Chris@16: // (C) Copyright 2011 Vicente J. Botet Escriba Chris@16: // Use, modification and distribution are subject to the Boost Software License, Chris@16: // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at Chris@16: // http://www.boost.org/LICENSE_1_0.txt). Chris@16: // Chris@16: Chris@16: /** Chris@16: * Duration formatting facet for output. Chris@16: */ Chris@16: #ifndef BOOST_CHRONO_IO_TIME_POINT_PUT_HPP Chris@16: #define BOOST_CHRONO_IO_TIME_POINT_PUT_HPP Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: namespace boost Chris@16: { Chris@16: namespace chrono Chris@16: { Chris@16: Chris@16: /** Chris@16: * @tparam ChatT a character type Chris@16: * @tparam OutputIterator a model of @c OutputIterator Chris@16: * Chris@16: * The @c time_point_put facet provides facilities for formatted output of @c time_point values. Chris@16: * The member function of @c time_point_put take a @c time_point and format it into character string representation. Chris@16: * Chris@16: */ Chris@16: template > Chris@16: class time_point_put: public std::locale::facet Chris@16: { Chris@16: public: Chris@16: /** Chris@16: * Type of character the facet is instantiated on. Chris@16: */ Chris@16: typedef CharT char_type; Chris@16: /** Chris@16: * Type of character string passed to member functions. Chris@16: */ Chris@16: typedef std::basic_string string_type; Chris@16: /** Chris@16: * Type of iterator used to write in the character buffer. Chris@16: */ Chris@16: typedef OutputIterator iter_type; Chris@16: Chris@16: /** Chris@16: * Construct a time_point_put facet. Chris@16: * @param refs Chris@16: * @Effects Construct a time_point_put facet. Chris@16: * If the @c refs argument is @c 0 then destruction of the object is Chris@16: * delegated to the @c locale, or locales, containing it. This allows Chris@16: * the user to ignore lifetime management issues. On the other had, Chris@16: * if @c refs is @c 1 then the object must be explicitly deleted; Chris@16: * the @c locale will not do so. In this case, the object can be Chris@16: * maintained across the lifetime of multiple locales. Chris@16: */ Chris@16: explicit time_point_put(size_t refs = 0) : Chris@16: std::locale::facet(refs) Chris@16: { Chris@16: } Chris@16: Chris@16: /** Chris@16: * @param i an output stream iterator Chris@16: * @param ios a reference to a ios_base Chris@16: * @param fill the character used as filler Chris@16: * @param tp the @c time_point Chris@16: * @param pattern begin of the formatting pattern Chris@16: * @param pat_end end of the formatting pattern Chris@16: * Chris@16: * @Effects Steps through the sequence from @c pattern to @c pat_end, Chris@16: * identifying characters that are part of a pattern sequence. Each character Chris@16: * that is not part of a pattern sequence is written to @c s immediately, and Chris@16: * each pattern sequence, as it is identified, results in a call to Chris@16: * @c put_duration or @c put_epoch; Chris@16: * thus, pattern elements and other characters are interleaved in the output Chris@16: * in the order in which they appear in the pattern. Pattern sequences are Chris@16: * identified by converting each character @c c to a @c char value as if by Chris@16: * @c ct.narrow(c,0), where @c ct is a reference to @c ctype obtained from Chris@16: * @c ios.getloc(). The first character of each sequence is equal to @c '%', Chris@16: * followed by a pattern specifier character @c spec, which can be @c 'd' for Chris@16: * the duration value or @c 'e' for the epoch. Chris@16: * For each valid pattern sequence identified, calls Chris@16: * put_duration(s, ios, fill, tp.time_since_epoch()) or put_epoch(s, ios). Chris@16: * Chris@16: * @Returns An iterator pointing immediately after the last character produced. Chris@16: */ Chris@16: Chris@16: template Chris@16: iter_type put(iter_type i, std::ios_base& ios, char_type fill, time_point const& tp, const CharT* pattern, Chris@16: const CharT* pat_end) const Chris@16: { Chris@16: if (std::has_facet >(ios.getloc())) Chris@16: { Chris@16: time_point_units const &facet = Chris@16: std::use_facet >(ios.getloc()); Chris@16: return put(facet, i, ios, fill, tp, pattern, pat_end); Chris@16: } Chris@16: else Chris@16: { Chris@16: time_point_units_default facet; Chris@16: return put(facet, i, ios, fill, tp, pattern, pat_end); Chris@16: } Chris@16: } Chris@16: Chris@16: template Chris@16: iter_type put(time_point_units const& units_facet, iter_type s, std::ios_base& ios, char_type fill, Chris@16: time_point const& tp, const CharT* pattern, const CharT* pat_end) const Chris@16: { Chris@16: Chris@16: const std::ctype& ct = std::use_facet >(ios.getloc()); Chris@16: for (; pattern != pat_end; ++pattern) Chris@16: { Chris@16: if (ct.narrow(*pattern, 0) == '%') Chris@16: { Chris@16: if (++pattern == pat_end) Chris@16: { Chris@16: *s++ = pattern[-1]; Chris@16: break; Chris@16: } Chris@16: char fmt = ct.narrow(*pattern, 0); Chris@16: switch (fmt) Chris@16: { Chris@16: case 'd': Chris@16: { Chris@16: s = put_duration(s, ios, fill, tp.time_since_epoch()); Chris@16: break; Chris@16: } Chris@16: case 'e': Chris@16: { Chris@16: s = put_epoch (units_facet, s, ios); Chris@16: break; Chris@16: } Chris@16: default: Chris@16: BOOST_ASSERT(false && "Boost::Chrono internal error."); Chris@16: break; Chris@16: } Chris@16: } Chris@16: else Chris@16: *s++ = *pattern; Chris@16: } Chris@16: return s; Chris@16: } Chris@16: Chris@16: /** Chris@16: * @param i an output stream iterator Chris@16: * @param ios a reference to a ios_base Chris@16: * @param fill the character used as filler Chris@16: * @param tp the @c time_point Chris@16: * @param pattern begin of the formatting pattern Chris@16: * @param pat_end end of the formatting pattern Chris@16: * Chris@16: * @Effects Stores the time_point pattern from the @c time_point_unit facet in let say @c str. Last as if Chris@16: * @code Chris@16: * return put(s, ios, dill, tp, str.data(), str.data() + str.size()); Chris@16: * @endcode Chris@16: * @Returns An iterator pointing immediately after the last character produced. Chris@16: */ Chris@16: template Chris@16: iter_type put(iter_type i, std::ios_base& ios, char_type fill, time_point const& tp) const Chris@16: { Chris@16: if (std::has_facet >(ios.getloc())) Chris@16: { Chris@16: time_point_units const &facet = Chris@16: std::use_facet >(ios.getloc()); Chris@16: std::basic_string str = facet.get_pattern(); Chris@16: return put(facet, i, ios, fill, tp, str.data(), str.data() + str.size()); Chris@16: } Chris@16: else Chris@16: { Chris@16: time_point_units_default facet; Chris@16: std::basic_string str = facet.get_pattern(); Chris@16: return put(facet, i, ios, fill, tp, str.data(), str.data() + str.size()); Chris@16: } Chris@16: } Chris@16: Chris@16: /** Chris@16: * @param i an output stream iterator Chris@16: * @param ios a reference to a ios_base Chris@16: * @param fill the character used as filler Chris@16: * @param d the @c duration Chris@16: * @Effects As if facet.put(s, ios, fill, d) where facet is the @c duration_put facet associated Chris@16: * to the @c ios or a new instance of @c duration_put. Chris@16: * @Returns An iterator pointing immediately after the last character produced. Chris@16: */ Chris@16: template Chris@16: iter_type put_duration(iter_type i, std::ios_base& ios, char_type fill, duration const& d) const Chris@16: { Chris@16: if (std::has_facet >(ios.getloc())) Chris@16: { Chris@16: duration_put const &facet = std::use_facet >(ios.getloc()); Chris@16: return facet.put(i, ios, fill, d); Chris@16: } Chris@16: else Chris@16: { Chris@16: duration_put facet; Chris@16: return facet.put(i, ios, fill, d); Chris@16: } Chris@16: } Chris@16: Chris@16: /** Chris@16: * Chris@16: * @param i an output stream iterator Chris@16: * @param ios a reference to a ios_base Chris@16: * @Effects As if Chris@16: * @code Chris@16: * string_type str = facet.template get_epoch(); Chris@16: * s=std::copy(str.begin(), str.end(), s); Chris@16: * @endcode Chris@16: * where facet is the @c time_point_units facet associated Chris@16: * to the @c ios or a new instance of @c time_point_units_default. Chris@16: * @Returns s, iterator pointing immediately after the last character produced. Chris@16: */ Chris@16: Chris@16: template Chris@16: iter_type put_epoch(iter_type i, std::ios_base& os) const Chris@16: { Chris@16: if (std::has_facet >(os.getloc())) Chris@16: { Chris@16: time_point_units const &facet = std::use_facet >(os.getloc()); Chris@16: return put_epoch (facet, i, os); Chris@16: } Chris@16: else Chris@16: { Chris@16: time_point_units_default facet; Chris@16: return put_epoch (facet, i, os); Chris@16: } Chris@16: } Chris@16: Chris@16: template Chris@16: iter_type put_epoch(time_point_units const& facet, iter_type s, std::ios_base&) const Chris@16: { Chris@16: string_type str = facet.template get_epoch(); Chris@16: s= std::copy(str.begin(), str.end(), s); Chris@16: return s; Chris@16: } Chris@16: Chris@16: /** Chris@16: * Unique identifier for this type of facet. Chris@16: */ Chris@16: static std::locale::id id; Chris@16: Chris@16: /** Chris@16: * @Effects Destroy the facet Chris@16: */ Chris@16: ~time_point_put() Chris@16: { Chris@16: } Chris@16: Chris@16: }; Chris@16: Chris@16: template Chris@16: std::locale::id time_point_put::id; Chris@16: Chris@16: } // chrono Chris@16: } // boost Chris@16: Chris@16: #endif // header