summaryrefslogtreecommitdiff
path: root/src/third_party/boost-1.56.0/libs/chrono/stopwatches/include/boost/chrono/stopwatches/formatters/elapsed_formatter.hpp
blob: 7ec23c8e4eabd460a0b2d058ec76b48584713262 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
//  boost/chrono/stopwatches/formatters/elapsed_formatter.hpp  ------------------------------------------------------------//
//  Copyright 2011 Vicente J. Botet Escriba
//  Distributed under the Boost Software License, Version 1.0.
//  See http://www.boost.org/LICENSE_1_0.txt
//  See http://www.boost.org/libs/chrono/stopwatches for documentation.

#ifndef BOOST_CHRONO_STOPWATCHES_FORMATTERS_ELAPSED_HPP
#define BOOST_CHRONO_STOPWATCHES_FORMATTERS_ELAPSED_HPP

#include <boost/chrono/stopwatches/formatters/base_formatter.hpp>
#include <boost/chrono/chrono_io.hpp>
#include <boost/current_function.hpp>
#include <boost/format.hpp>
#include <boost/format/group.hpp>
#include <boost/cstdint.hpp>
#include <boost/assert.hpp>
#include <string>
#include <iostream>
#include <cassert>
#include <iomanip>

#define BOOST_CHRONO_STOPWATCHES_ELAPSED_FORMAT_DEFAULT "%1%\n"

namespace boost
{
  namespace chrono
  {

    template<typename Ratio, typename CharT,
        typename Traits = std::char_traits<CharT>,
        class Alloc = std::allocator<CharT> >
    class basic_elapsed_formatter: public base_formatter<CharT, Traits>, public basic_format<CharT, Traits>
    {

    public:
      typedef base_formatter<CharT, Traits> base_type;
      typedef basic_format<CharT, Traits> format_type;
      typedef std::basic_string<CharT, Traits, Alloc> string_type;
      typedef CharT char_type;
      typedef std::basic_ostream<CharT, Traits> ostream_type;

      basic_elapsed_formatter() :
        base_type(),
        format_type(BOOST_CHRONO_STOPWATCHES_ELAPSED_FORMAT_DEFAULT)
      {
      }
      basic_elapsed_formatter(ostream_type& os) :
        base_type(os),
        format_type(BOOST_CHRONO_STOPWATCHES_ELAPSED_FORMAT_DEFAULT)
      {
      }
      basic_elapsed_formatter(const char* fmt, ostream_type& os = std::cout) :
        base_type(os), format_type(fmt)
      {
      }
      basic_elapsed_formatter(string_type const& fmt, ostream_type& os =
          std::cout) :
        base_type(os), format_type(fmt)
      {
      }

//      static string_type format(const char* s)
//      {
//        string_type res(s);
//        res += boost::chrono::detail::adaptive_string(" : ");
//        res += BOOST_CHRONO_STOPWATCHES_ELAPSED_FORMAT_DEFAULT;
//        return res;
//      }

      template<class Stopwatch>
      void operator()(Stopwatch & stopwatch_
          //, system::error_code & ec= BOOST_CHRONO_THROWS
          )
      {
        typedef typename Stopwatch::duration duration_t;
        duration_t d = stopwatch_.elapsed();
        //duration_t d = stopwatch_.elapsed(ec);

        if (d < duration_t::zero())
          return;

        duration_style_io_saver dsios(this->os_);
        this->os_ << static_cast<format_type&>(*this)
            % io::group(std::fixed, std::setprecision(this->precision_), duration_fmt(this->duration_style_), boost::chrono::duration<
                    double, Ratio>(d))
        ;

      }
    };

    typedef basic_elapsed_formatter<milli, char> elapsed_formatter;
    typedef basic_elapsed_formatter<milli, wchar_t> welapsed_formatter;

  } // namespace chrono
} // namespace boost

#if 0
#define BOOST_CHRONO_STOPWATCHES_ELAPSED_FORMAT(F) \
  boost::chrono::detail::adaptive_string(F " : " BOOST_CHRONO_STOPWATCHES_ELAPSED_FORMAT_DEFAULT)
#ifdef __GNUC__
#define BOOST_CHRONO_STOPWATCHES_ELAPSED_FUNCTION_FORMAT \
  boost::chrono::elapsed_formatter::format(BOOST_CURRENT_FUNCTION)
#else
#define BOOST_CHRONO_STOPWATCHES_ELAPSED_FUNCTION_FORMAT \
    BOOST_CHRONO_STOPWATCHES_ELAPSED_FORMAT(BOOST_CURRENT_FUNCTION)
#endif
#endif

#endif