annotate DEPENDENCIES/generic/include/boost/chrono/io/duration_put.hpp @ 16:2665513ce2d3

Add boost headers
author Chris Cannam
date Tue, 05 Aug 2014 11:11:38 +0100
parents
children c530137014c0
rev   line source
Chris@16 1 // (C) Copyright Howard Hinnant
Chris@16 2 // (C) Copyright 2011 Vicente J. Botet Escriba
Chris@16 3 // Use, modification and distribution are subject to the Boost Software License,
Chris@16 4 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
Chris@16 5 // http://www.boost.org/LICENSE_1_0.txt).
Chris@16 6 //
Chris@16 7
Chris@16 8 /**
Chris@16 9 * Duration formatting facet for output.
Chris@16 10 */
Chris@16 11 #ifndef BOOST_CHRONO_IO_DURATION_PUT_HPP
Chris@16 12 #define BOOST_CHRONO_IO_DURATION_PUT_HPP
Chris@16 13
Chris@16 14 #include <boost/chrono/config.hpp>
Chris@16 15 #include <boost/chrono/io/duration_units.hpp>
Chris@16 16 #include <boost/assert.hpp>
Chris@16 17 #include <locale>
Chris@16 18
Chris@16 19 namespace boost
Chris@16 20 {
Chris@16 21 namespace chrono
Chris@16 22 {
Chris@16 23
Chris@16 24 /**
Chris@16 25 * @tparam ChatT a character type
Chris@16 26 * @tparam OutputIterator a model of @c OutputIterator
Chris@16 27 *
Chris@16 28 * The @c duration_put facet provides facilities for formatted output of duration values.
Chris@16 29 * The member function of @c duration_put take a duration and format it into character string representation.
Chris@16 30 *
Chris@16 31 */
Chris@16 32 template <class CharT, class OutputIterator = std::ostreambuf_iterator<CharT> >
Chris@16 33 class duration_put: public std::locale::facet
Chris@16 34 {
Chris@16 35 public:
Chris@16 36 /**
Chris@16 37 * Type of character the facet is instantiated on.
Chris@16 38 */
Chris@16 39 typedef CharT char_type;
Chris@16 40 /**
Chris@16 41 * Type of character string passed to member functions.
Chris@16 42 */
Chris@16 43 typedef std::basic_string<CharT> string_type;
Chris@16 44 /**
Chris@16 45 * Type of iterator used to write in the character buffer.
Chris@16 46 */
Chris@16 47 typedef OutputIterator iter_type;
Chris@16 48
Chris@16 49 /**
Chris@16 50 * Construct a duration_put facet.
Chris@16 51 * @param refs
Chris@16 52 * @Effects Construct a duration_put facet.
Chris@16 53 * If the @c refs argument is @c 0 then destruction of the object is
Chris@16 54 * delegated to the @c locale, or locales, containing it. This allows
Chris@16 55 * the user to ignore lifetime management issues. On the other had,
Chris@16 56 * if @c refs is @c 1 then the object must be explicitly deleted;
Chris@16 57 * the @c locale will not do so. In this case, the object can be
Chris@16 58 * maintained across the lifetime of multiple locales.
Chris@16 59 */
Chris@16 60 explicit duration_put(size_t refs = 0) :
Chris@16 61 std::locale::facet(refs)
Chris@16 62 {
Chris@16 63 }
Chris@16 64
Chris@16 65 /**
Chris@16 66 *
Chris@16 67 * @param s an output stream iterator
Chris@16 68 * @param ios a reference to a ios_base
Chris@16 69 * @param fill the character used as filler
Chris@16 70 * @param d the duration
Chris@16 71 * @param pattern begin of the formatting pattern
Chris@16 72 * @param pat_end end of the formatting pattern
Chris@16 73 *
Chris@16 74 * @Effects Steps through the sequence from @c pattern to @c pat_end,
Chris@16 75 * identifying characters that are part of a pattern sequence. Each character
Chris@16 76 * that is not part of a pattern sequence is written to @c s immediately, and
Chris@16 77 * each pattern sequence, as it is identified, results in a call to
Chris@16 78 * @c put_value or @c put_unit;
Chris@16 79 * thus, pattern elements and other characters are interleaved in the output
Chris@16 80 * in the order in which they appear in the pattern. Pattern sequences are
Chris@16 81 * identified by converting each character @c c to a @c char value as if by
Chris@16 82 * @c ct.narrow(c,0), where @c ct is a reference to @c ctype<charT> obtained from
Chris@16 83 * @c ios.getloc(). The first character of each sequence is equal to @c '%',
Chris@16 84 * followed by a pattern specifier character @c spec, which can be @c 'v' for
Chris@16 85 * the duration value or @c 'u' for the duration unit. .
Chris@16 86 * For each valid pattern sequence identified, calls
Chris@16 87 * <c>put_value(s, ios, fill, d)</c> or <c>put_unit(s, ios, fill, d)</c>.
Chris@16 88 *
Chris@16 89 * @Returns An iterator pointing immediately after the last character produced.
Chris@16 90 */
Chris@16 91 template <typename Rep, typename Period>
Chris@16 92 iter_type put(iter_type s, std::ios_base& ios, char_type fill, duration<Rep, Period> const& d, const CharT* pattern,
Chris@16 93 const CharT* pat_end) const
Chris@16 94 {
Chris@16 95 if (std::has_facet<duration_units<CharT> >(ios.getloc()))
Chris@16 96 {
Chris@16 97 duration_units<CharT> const&facet = std::use_facet<duration_units<CharT> >(
Chris@16 98 ios.getloc());
Chris@16 99 return put(facet, s, ios, fill, d, pattern, pat_end);
Chris@16 100 }
Chris@16 101 else
Chris@16 102 {
Chris@16 103 duration_units_default<CharT> facet;
Chris@16 104 return put(facet, s, ios, fill, d, pattern, pat_end);
Chris@16 105 }
Chris@16 106 }
Chris@16 107
Chris@16 108 template <typename Rep, typename Period>
Chris@16 109 iter_type put(duration_units<CharT> const& units_facet, iter_type s, std::ios_base& ios, char_type fill,
Chris@16 110 duration<Rep, Period> const& d, const CharT* pattern, const CharT* pat_end) const
Chris@16 111 {
Chris@16 112
Chris@16 113 const std::ctype<char_type>& ct = std::use_facet<std::ctype<char_type> >(ios.getloc());
Chris@16 114 for (; pattern != pat_end; ++pattern)
Chris@16 115 {
Chris@16 116 if (ct.narrow(*pattern, 0) == '%')
Chris@16 117 {
Chris@16 118 if (++pattern == pat_end)
Chris@16 119 {
Chris@16 120 *s++ = pattern[-1];
Chris@16 121 break;
Chris@16 122 }
Chris@16 123 char fmt = ct.narrow(*pattern, 0);
Chris@16 124 switch (fmt)
Chris@16 125 {
Chris@16 126 case 'v':
Chris@16 127 {
Chris@16 128 s = put_value(s, ios, fill, d);
Chris@16 129 break;
Chris@16 130 }
Chris@16 131 case 'u':
Chris@16 132 {
Chris@16 133 s = put_unit(units_facet, s, ios, fill, d);
Chris@16 134 break;
Chris@16 135 }
Chris@16 136 default:
Chris@16 137 BOOST_ASSERT(false && "Boost::Chrono internal error.");
Chris@16 138 break;
Chris@16 139 }
Chris@16 140 }
Chris@16 141 else
Chris@16 142 *s++ = *pattern;
Chris@16 143 }
Chris@16 144 return s;
Chris@16 145 }
Chris@16 146
Chris@16 147 /**
Chris@16 148 *
Chris@16 149 * @param s an output stream iterator
Chris@16 150 * @param ios a reference to a ios_base
Chris@16 151 * @param fill the character used as filler
Chris@16 152 * @param d the duration
Chris@16 153 * @Effects imbue in @c ios the @c duration_units_default facet if not already present.
Chris@16 154 * Retrieves Stores the duration pattern from the @c duration_unit facet in let say @c str. Last as if
Chris@16 155 * @code
Chris@16 156 * return put(s, ios, d, str.data(), str.data() + str.size());
Chris@16 157 * @endcode
Chris@16 158 * @Returns An iterator pointing immediately after the last character produced.
Chris@16 159 */
Chris@16 160 template <typename Rep, typename Period>
Chris@16 161 iter_type put(iter_type s, std::ios_base& ios, char_type fill, duration<Rep, Period> const& d) const
Chris@16 162 {
Chris@16 163 if (std::has_facet<duration_units<CharT> >(ios.getloc()))
Chris@16 164 {
Chris@16 165 duration_units<CharT> const&facet = std::use_facet<duration_units<CharT> >(
Chris@16 166 ios.getloc());
Chris@16 167 std::basic_string<CharT> str = facet.get_pattern();
Chris@16 168 return put(facet, s, ios, fill, d, str.data(), str.data() + str.size());
Chris@16 169 }
Chris@16 170 else
Chris@16 171 {
Chris@16 172 duration_units_default<CharT> facet;
Chris@16 173 std::basic_string<CharT> str = facet.get_pattern();
Chris@16 174 return put(facet, s, ios, fill, d, str.data(), str.data() + str.size());
Chris@16 175 }
Chris@16 176 }
Chris@16 177
Chris@16 178 /**
Chris@16 179 *
Chris@16 180 * @param s an output stream iterator
Chris@16 181 * @param ios a reference to a ios_base
Chris@16 182 * @param fill the character used as filler
Chris@16 183 * @param d the duration
Chris@16 184 * @Effects As if s=std::use_facet<std::num_put<CharT, iter_type> >(ios.getloc()).put(s, ios, fill, static_cast<long int> (d.count())).
Chris@16 185 * @Returns s, iterator pointing immediately after the last character produced.
Chris@16 186 */
Chris@16 187 template <typename Rep, typename Period>
Chris@16 188 iter_type put_value(iter_type s, std::ios_base& ios, char_type fill, duration<Rep, Period> const& d) const
Chris@16 189 {
Chris@16 190 return std::use_facet<std::num_put<CharT, iter_type> >(ios.getloc()).put(s, ios, fill,
Chris@16 191 static_cast<long int> (d.count()));
Chris@16 192 }
Chris@16 193
Chris@16 194 /**
Chris@16 195 *
Chris@16 196 * @param s an output stream iterator
Chris@16 197 * @param ios a reference to a ios_base
Chris@16 198 * @param fill the character used as filler
Chris@16 199 * @param d the duration
Chris@16 200 * @Effects Let facet be the duration_units<CharT> facet associated to ios. If the associated unit is named,
Chris@16 201 * as if
Chris@16 202 * @code
Chris@16 203 string_type str = facet.get_unit(get_duration_style(ios), d);
Chris@16 204 s=std::copy(str.begin(), str.end(), s);
Chris@16 205 * @endcode
Chris@16 206 * Otherwise, format the unit as "[Period::num/Period::den]" followed by the unit associated to [N/D] obtained using facet.get_n_d_unit(get_duration_style(ios), d)
Chris@16 207 * @Returns s, iterator pointing immediately after the last character produced.
Chris@16 208 */
Chris@16 209 template <typename Rep, typename Period>
Chris@16 210 iter_type put_unit(iter_type s, std::ios_base& ios, char_type fill, duration<Rep, Period> const& d) const
Chris@16 211 {
Chris@16 212 if (std::has_facet<duration_units<CharT> >(ios.getloc()))
Chris@16 213 {
Chris@16 214 duration_units<CharT> const&facet = std::use_facet<duration_units<CharT> >(
Chris@16 215 ios.getloc());
Chris@16 216 return put_unit(facet, s, ios, fill, d);
Chris@16 217 }
Chris@16 218 else
Chris@16 219 {
Chris@16 220 duration_units_default<CharT> facet;
Chris@16 221 return put_unit(facet, s, ios, fill, d);
Chris@16 222 }
Chris@16 223 }
Chris@16 224
Chris@16 225 template <typename Rep, typename Period>
Chris@16 226 iter_type put_unit(duration_units<CharT> const& facet, iter_type s, std::ios_base& ios, char_type fill,
Chris@16 227 duration<Rep, Period> const& d) const
Chris@16 228 {
Chris@16 229 if (facet.template is_named_unit<Period>()) {
Chris@16 230 string_type str = facet.get_unit(get_duration_style(ios), d);
Chris@16 231 s=std::copy(str.begin(), str.end(), s);
Chris@16 232 } else {
Chris@16 233 *s++ = CharT('[');
Chris@16 234 std::use_facet<std::num_put<CharT, iter_type> >(ios.getloc()).put(s, ios, fill, Period::num);
Chris@16 235 *s++ = CharT('/');
Chris@16 236 std::use_facet<std::num_put<CharT, iter_type> >(ios.getloc()).put(s, ios, fill, Period::den);
Chris@16 237 *s++ = CharT(']');
Chris@16 238 string_type str = facet.get_n_d_unit(get_duration_style(ios), d);
Chris@16 239 s=std::copy(str.begin(), str.end(), s);
Chris@16 240 }
Chris@16 241 return s;
Chris@16 242 }
Chris@16 243
Chris@16 244 /**
Chris@16 245 * Unique identifier for this type of facet.
Chris@16 246 */
Chris@16 247 static std::locale::id id;
Chris@16 248
Chris@16 249 /**
Chris@16 250 * @Effects Destroy the facet
Chris@16 251 */
Chris@16 252 ~duration_put()
Chris@16 253 {
Chris@16 254 }
Chris@16 255
Chris@16 256 };
Chris@16 257
Chris@16 258 template <class CharT, class OutputIterator>
Chris@16 259 std::locale::id duration_put<CharT, OutputIterator>::id;
Chris@16 260
Chris@16 261 } // chrono
Chris@16 262 } // boost
Chris@16 263
Chris@16 264 #endif // header