annotate DEPENDENCIES/generic/include/boost/chrono/io/duration_put.hpp @ 133:4acb5d8d80b6 tip

Don't fail environmental check if README.md exists (but .txt and no-suffix don't)
author Chris Cannam
date Tue, 30 Jul 2019 12:25:44 +0100
parents c530137014c0
children
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@101 16 #include <boost/chrono/process_cpu_clocks.hpp>
Chris@16 17 #include <boost/assert.hpp>
Chris@16 18 #include <locale>
Chris@16 19
Chris@16 20 namespace boost
Chris@16 21 {
Chris@16 22 namespace chrono
Chris@16 23 {
Chris@16 24
Chris@101 25 namespace detail
Chris@101 26 {
Chris@101 27 template <class T>
Chris@101 28 struct propagate {
Chris@101 29 typedef T type;
Chris@101 30 };
Chris@101 31 template <>
Chris@101 32 struct propagate<boost::int_least32_t> {
Chris@101 33 typedef boost::int_least64_t type;
Chris@101 34 };
Chris@101 35 }
Chris@16 36 /**
Chris@16 37 * @tparam ChatT a character type
Chris@16 38 * @tparam OutputIterator a model of @c OutputIterator
Chris@16 39 *
Chris@16 40 * The @c duration_put facet provides facilities for formatted output of duration values.
Chris@16 41 * The member function of @c duration_put take a duration and format it into character string representation.
Chris@16 42 *
Chris@16 43 */
Chris@16 44 template <class CharT, class OutputIterator = std::ostreambuf_iterator<CharT> >
Chris@16 45 class duration_put: public std::locale::facet
Chris@16 46 {
Chris@16 47 public:
Chris@16 48 /**
Chris@16 49 * Type of character the facet is instantiated on.
Chris@16 50 */
Chris@16 51 typedef CharT char_type;
Chris@16 52 /**
Chris@16 53 * Type of character string passed to member functions.
Chris@16 54 */
Chris@16 55 typedef std::basic_string<CharT> string_type;
Chris@16 56 /**
Chris@16 57 * Type of iterator used to write in the character buffer.
Chris@16 58 */
Chris@16 59 typedef OutputIterator iter_type;
Chris@16 60
Chris@16 61 /**
Chris@16 62 * Construct a duration_put facet.
Chris@16 63 * @param refs
Chris@16 64 * @Effects Construct a duration_put facet.
Chris@16 65 * If the @c refs argument is @c 0 then destruction of the object is
Chris@16 66 * delegated to the @c locale, or locales, containing it. This allows
Chris@16 67 * the user to ignore lifetime management issues. On the other had,
Chris@16 68 * if @c refs is @c 1 then the object must be explicitly deleted;
Chris@16 69 * the @c locale will not do so. In this case, the object can be
Chris@16 70 * maintained across the lifetime of multiple locales.
Chris@16 71 */
Chris@16 72 explicit duration_put(size_t refs = 0) :
Chris@16 73 std::locale::facet(refs)
Chris@16 74 {
Chris@16 75 }
Chris@16 76
Chris@16 77 /**
Chris@16 78 *
Chris@16 79 * @param s an output stream iterator
Chris@16 80 * @param ios a reference to a ios_base
Chris@16 81 * @param fill the character used as filler
Chris@16 82 * @param d the duration
Chris@16 83 * @param pattern begin of the formatting pattern
Chris@16 84 * @param pat_end end of the formatting pattern
Chris@16 85 *
Chris@16 86 * @Effects Steps through the sequence from @c pattern to @c pat_end,
Chris@16 87 * identifying characters that are part of a pattern sequence. Each character
Chris@16 88 * that is not part of a pattern sequence is written to @c s immediately, and
Chris@16 89 * each pattern sequence, as it is identified, results in a call to
Chris@16 90 * @c put_value or @c put_unit;
Chris@16 91 * thus, pattern elements and other characters are interleaved in the output
Chris@16 92 * in the order in which they appear in the pattern. Pattern sequences are
Chris@16 93 * identified by converting each character @c c to a @c char value as if by
Chris@16 94 * @c ct.narrow(c,0), where @c ct is a reference to @c ctype<charT> obtained from
Chris@16 95 * @c ios.getloc(). The first character of each sequence is equal to @c '%',
Chris@16 96 * followed by a pattern specifier character @c spec, which can be @c 'v' for
Chris@16 97 * the duration value or @c 'u' for the duration unit. .
Chris@16 98 * For each valid pattern sequence identified, calls
Chris@16 99 * <c>put_value(s, ios, fill, d)</c> or <c>put_unit(s, ios, fill, d)</c>.
Chris@16 100 *
Chris@16 101 * @Returns An iterator pointing immediately after the last character produced.
Chris@16 102 */
Chris@16 103 template <typename Rep, typename Period>
Chris@16 104 iter_type put(iter_type s, std::ios_base& ios, char_type fill, duration<Rep, Period> const& d, const CharT* pattern,
Chris@101 105 const CharT* pat_end, const char_type* val = 0) const
Chris@16 106 {
Chris@16 107 if (std::has_facet<duration_units<CharT> >(ios.getloc()))
Chris@16 108 {
Chris@16 109 duration_units<CharT> const&facet = std::use_facet<duration_units<CharT> >(
Chris@16 110 ios.getloc());
Chris@101 111 return put(facet, s, ios, fill, d, pattern, pat_end, val);
Chris@16 112 }
Chris@16 113 else
Chris@16 114 {
Chris@16 115 duration_units_default<CharT> facet;
Chris@101 116 return put(facet, s, ios, fill, d, pattern, pat_end, val);
Chris@16 117 }
Chris@16 118 }
Chris@16 119
Chris@16 120 template <typename Rep, typename Period>
Chris@16 121 iter_type put(duration_units<CharT> const& units_facet, iter_type s, std::ios_base& ios, char_type fill,
Chris@101 122 duration<Rep, Period> const& d, const CharT* pattern, const CharT* pat_end, const char_type* val = 0) const
Chris@16 123 {
Chris@16 124
Chris@16 125 const std::ctype<char_type>& ct = std::use_facet<std::ctype<char_type> >(ios.getloc());
Chris@16 126 for (; pattern != pat_end; ++pattern)
Chris@16 127 {
Chris@16 128 if (ct.narrow(*pattern, 0) == '%')
Chris@16 129 {
Chris@16 130 if (++pattern == pat_end)
Chris@16 131 {
Chris@16 132 *s++ = pattern[-1];
Chris@16 133 break;
Chris@16 134 }
Chris@16 135 char fmt = ct.narrow(*pattern, 0);
Chris@16 136 switch (fmt)
Chris@16 137 {
Chris@16 138 case 'v':
Chris@16 139 {
Chris@101 140 s = put_value(s, ios, fill, d, val);
Chris@16 141 break;
Chris@16 142 }
Chris@16 143 case 'u':
Chris@16 144 {
Chris@16 145 s = put_unit(units_facet, s, ios, fill, d);
Chris@16 146 break;
Chris@16 147 }
Chris@16 148 default:
Chris@16 149 BOOST_ASSERT(false && "Boost::Chrono internal error.");
Chris@16 150 break;
Chris@16 151 }
Chris@16 152 }
Chris@16 153 else
Chris@16 154 *s++ = *pattern;
Chris@16 155 }
Chris@16 156 return s;
Chris@16 157 }
Chris@16 158
Chris@16 159 /**
Chris@16 160 *
Chris@16 161 * @param s an output stream iterator
Chris@16 162 * @param ios a reference to a ios_base
Chris@16 163 * @param fill the character used as filler
Chris@16 164 * @param d the duration
Chris@16 165 * @Effects imbue in @c ios the @c duration_units_default facet if not already present.
Chris@16 166 * Retrieves Stores the duration pattern from the @c duration_unit facet in let say @c str. Last as if
Chris@16 167 * @code
Chris@16 168 * return put(s, ios, d, str.data(), str.data() + str.size());
Chris@16 169 * @endcode
Chris@16 170 * @Returns An iterator pointing immediately after the last character produced.
Chris@16 171 */
Chris@16 172 template <typename Rep, typename Period>
Chris@101 173 iter_type put(iter_type s, std::ios_base& ios, char_type fill, duration<Rep, Period> const& d, const char_type* val = 0) const
Chris@16 174 {
Chris@16 175 if (std::has_facet<duration_units<CharT> >(ios.getloc()))
Chris@16 176 {
Chris@16 177 duration_units<CharT> const&facet = std::use_facet<duration_units<CharT> >(
Chris@16 178 ios.getloc());
Chris@16 179 std::basic_string<CharT> str = facet.get_pattern();
Chris@101 180 return put(facet, s, ios, fill, d, str.data(), str.data() + str.size(), val);
Chris@16 181 }
Chris@16 182 else
Chris@16 183 {
Chris@16 184 duration_units_default<CharT> facet;
Chris@16 185 std::basic_string<CharT> str = facet.get_pattern();
Chris@101 186
Chris@101 187 return put(facet, s, ios, fill, d, str.data(), str.data() + str.size(), val);
Chris@16 188 }
Chris@16 189 }
Chris@16 190
Chris@16 191 /**
Chris@16 192 *
Chris@16 193 * @param s an output stream iterator
Chris@16 194 * @param ios a reference to a ios_base
Chris@16 195 * @param fill the character used as filler
Chris@16 196 * @param d the duration
Chris@16 197 * @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 198 * @Returns s, iterator pointing immediately after the last character produced.
Chris@16 199 */
Chris@16 200 template <typename Rep, typename Period>
Chris@101 201 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
Chris@16 202 {
Chris@101 203 if (val)
Chris@101 204 {
Chris@101 205 while (*val) {
Chris@101 206 *s = *val;
Chris@101 207 s++; val++;
Chris@101 208 }
Chris@101 209 return s;
Chris@101 210 }
Chris@16 211 return std::use_facet<std::num_put<CharT, iter_type> >(ios.getloc()).put(s, ios, fill,
Chris@101 212 static_cast<typename detail::propagate<Rep>::type> (d.count()));
Chris@101 213 }
Chris@101 214
Chris@101 215 template <typename Rep, typename Period>
Chris@101 216 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
Chris@101 217 {
Chris@101 218 *s++ = CharT('{');
Chris@101 219 s = put_value(s, ios, fill, process_real_cpu_clock::duration(d.count().real));
Chris@101 220 *s++ = CharT(';');
Chris@101 221 s = put_value(s, ios, fill, process_user_cpu_clock::duration(d.count().user));
Chris@101 222 *s++ = CharT(';');
Chris@101 223 s = put_value(s, ios, fill, process_system_cpu_clock::duration(d.count().system));
Chris@101 224 *s++ = CharT('}');
Chris@101 225 return s;
Chris@16 226 }
Chris@16 227
Chris@16 228 /**
Chris@16 229 *
Chris@16 230 * @param s an output stream iterator
Chris@16 231 * @param ios a reference to a ios_base
Chris@16 232 * @param fill the character used as filler
Chris@16 233 * @param d the duration
Chris@16 234 * @Effects Let facet be the duration_units<CharT> facet associated to ios. If the associated unit is named,
Chris@16 235 * as if
Chris@16 236 * @code
Chris@16 237 string_type str = facet.get_unit(get_duration_style(ios), d);
Chris@16 238 s=std::copy(str.begin(), str.end(), s);
Chris@16 239 * @endcode
Chris@16 240 * 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 241 * @Returns s, iterator pointing immediately after the last character produced.
Chris@16 242 */
Chris@16 243 template <typename Rep, typename Period>
Chris@16 244 iter_type put_unit(iter_type s, std::ios_base& ios, char_type fill, duration<Rep, Period> const& d) const
Chris@16 245 {
Chris@16 246 if (std::has_facet<duration_units<CharT> >(ios.getloc()))
Chris@16 247 {
Chris@16 248 duration_units<CharT> const&facet = std::use_facet<duration_units<CharT> >(
Chris@16 249 ios.getloc());
Chris@16 250 return put_unit(facet, s, ios, fill, d);
Chris@16 251 }
Chris@16 252 else
Chris@16 253 {
Chris@16 254 duration_units_default<CharT> facet;
Chris@16 255 return put_unit(facet, s, ios, fill, d);
Chris@16 256 }
Chris@16 257 }
Chris@16 258
Chris@16 259 template <typename Rep, typename Period>
Chris@16 260 iter_type put_unit(duration_units<CharT> const& facet, iter_type s, std::ios_base& ios, char_type fill,
Chris@16 261 duration<Rep, Period> const& d) const
Chris@16 262 {
Chris@16 263 if (facet.template is_named_unit<Period>()) {
Chris@16 264 string_type str = facet.get_unit(get_duration_style(ios), d);
Chris@16 265 s=std::copy(str.begin(), str.end(), s);
Chris@16 266 } else {
Chris@16 267 *s++ = CharT('[');
Chris@16 268 std::use_facet<std::num_put<CharT, iter_type> >(ios.getloc()).put(s, ios, fill, Period::num);
Chris@16 269 *s++ = CharT('/');
Chris@16 270 std::use_facet<std::num_put<CharT, iter_type> >(ios.getloc()).put(s, ios, fill, Period::den);
Chris@16 271 *s++ = CharT(']');
Chris@16 272 string_type str = facet.get_n_d_unit(get_duration_style(ios), d);
Chris@16 273 s=std::copy(str.begin(), str.end(), s);
Chris@16 274 }
Chris@16 275 return s;
Chris@16 276 }
Chris@101 277 template <typename Rep, typename Period>
Chris@101 278 iter_type put_unit(duration_units<CharT> const& facet, iter_type s, std::ios_base& ios, char_type fill,
Chris@101 279 duration<process_times<Rep>, Period> const& d) const
Chris@101 280 {
Chris@101 281 duration<Rep,Period> real(d.count().real);
Chris@101 282 if (facet.template is_named_unit<Period>()) {
Chris@101 283 string_type str = facet.get_unit(get_duration_style(ios), real);
Chris@101 284 s=std::copy(str.begin(), str.end(), s);
Chris@101 285 } else {
Chris@101 286 *s++ = CharT('[');
Chris@101 287 std::use_facet<std::num_put<CharT, iter_type> >(ios.getloc()).put(s, ios, fill, Period::num);
Chris@101 288 *s++ = CharT('/');
Chris@101 289 std::use_facet<std::num_put<CharT, iter_type> >(ios.getloc()).put(s, ios, fill, Period::den);
Chris@101 290 *s++ = CharT(']');
Chris@101 291 string_type str = facet.get_n_d_unit(get_duration_style(ios), real);
Chris@101 292 s=std::copy(str.begin(), str.end(), s);
Chris@101 293 }
Chris@101 294 return s;
Chris@101 295 }
Chris@16 296
Chris@16 297 /**
Chris@16 298 * Unique identifier for this type of facet.
Chris@16 299 */
Chris@16 300 static std::locale::id id;
Chris@16 301
Chris@16 302 /**
Chris@16 303 * @Effects Destroy the facet
Chris@16 304 */
Chris@16 305 ~duration_put()
Chris@16 306 {
Chris@16 307 }
Chris@16 308
Chris@16 309 };
Chris@16 310
Chris@16 311 template <class CharT, class OutputIterator>
Chris@16 312 std::locale::id duration_put<CharT, OutputIterator>::id;
Chris@16 313
Chris@16 314 } // chrono
Chris@16 315 } // boost
Chris@16 316
Chris@16 317 #endif // header