1 //  boost/timer/timer.hpp  -------------------------------------------------------------//
2 
3 //  Copyright Beman Dawes 1994-2007, 2011
4 
5 //  Distributed under the Boost Software License, Version 1.0.
6 //  See http://www.boost.org/LICENSE_1_0.txt
7 
8 #ifndef BOOST_TIMER_TIMER_HPP
9 #define BOOST_TIMER_TIMER_HPP
10 
11 #include <boost/config/warning_disable.hpp>
12 
13 #include <boost/timer/config.hpp>
14 #include <boost/chrono/chrono.hpp>
15 #include <boost/cstdint.hpp>
16 #include <string>
17 #include <cstring>
18 #include <ostream>
19 
20 #include <boost/config/abi_prefix.hpp> // must be the last #include
21 
22 #   if defined(_MSC_VER)
23 #     pragma warning(push)           // Save warning settings
24 #     pragma warning(disable : 4251) // disable warning: class 'std::basic_string<_Elem,_Traits,_Ax>'
25 #   endif                            // needs to have dll-interface...
26 
27 //--------------------------------------------------------------------------------------//
28 
29 namespace boost
30 {
31 namespace timer
32 {
33   class cpu_timer;
34   class auto_cpu_timer;
35 
36   typedef boost::int_least64_t nanosecond_type;
37 
38   struct cpu_times
39   {
40     nanosecond_type wall;
41     nanosecond_type user;
42     nanosecond_type system;
43 
clearboost::timer::cpu_times44     void clear() { wall = user = system = 0LL; }
45   };
46 
47   const short         default_places = 6;
48 
49   BOOST_TIMER_DECL
50   std::string format(const cpu_times& times, short places, const std::string& format);
51 
52   BOOST_TIMER_DECL
53   std::string format(const cpu_times& times, short places = default_places);
54 
55 //  cpu_timer  -------------------------------------------------------------------------//
56 
57   class BOOST_TIMER_DECL cpu_timer
58   {
59   public:
60 
61     //  constructor
cpu_timer()62     cpu_timer() BOOST_NOEXCEPT                                   { start(); }
63 
64     //  observers
is_stopped() const65     bool          is_stopped() const BOOST_NOEXCEPT              { return m_is_stopped; }
66     cpu_times     elapsed() const BOOST_NOEXCEPT;  // does not stop()
format(short places,const std::string & format) const67     std::string   format(short places, const std::string& format) const
68                         { return ::boost::timer::format(elapsed(), places, format); }
format(short places=default_places) const69     std::string   format(short places = default_places) const
70                         { return ::boost::timer::format(elapsed(), places); }
71     //  actions
72     void          start() BOOST_NOEXCEPT;
73     void          stop() BOOST_NOEXCEPT;
74     void          resume() BOOST_NOEXCEPT;
75 
76   private:
77     cpu_times     m_times;
78     bool          m_is_stopped;
79   };
80 
81 //  auto_cpu_timer  --------------------------------------------------------------------//
82 
83   class BOOST_TIMER_DECL auto_cpu_timer : public cpu_timer
84   {
85   public:
86 
87     //  Explicit defaults for os are not provided to avoid including <iostream>, which has
88     //  high costs even when the standard streams are not actually used. Explicit defaults
89     //  for format are not provided to avoid order-of-dynamic-initialization issues with a
90     //  std::string.
91 
92     explicit auto_cpu_timer(short places = default_places);                          // #1
93              auto_cpu_timer(short places, const std::string& format);                // #2
94     explicit auto_cpu_timer(const std::string& format);                              // #3
auto_cpu_timer(std::ostream & os,short places,const std::string & format)95              auto_cpu_timer(std::ostream& os, short places,
96                             const std::string& format)                               // #4
97                                    : m_places(places), m_os(&os), m_format(format)
98                                    { start(); }
99     explicit auto_cpu_timer(std::ostream& os, short places = default_places);        // #5
auto_cpu_timer(std::ostream & os,const std::string & format)100              auto_cpu_timer(std::ostream& os, const std::string& format)             // #6
101                                    : m_places(default_places), m_os(&os), m_format(format)
102                                    { start(); }
103 
104    ~auto_cpu_timer();
105 
106    //  observers
107    //    not particularly useful to users, but allow testing of constructor
108    //    postconditions and ease specification of other functionality without resorting
109    //    to "for exposition only" private members.
ostream() const110    std::ostream&       ostream() const       { return *m_os; }
places() const111    short               places() const        { return m_places; }
format_string() const112    const std::string&  format_string() const { return m_format; }
113 
114     //  actions
115     void   report();
116 
117   private:
118     short           m_places;
119     std::ostream*   m_os;      // stored as ptr so compiler can generate operator=
120     std::string     m_format;
121   };
122 
123 } // namespace timer
124 } // namespace boost
125 
126 #   if defined(_MSC_VER)
127 #     pragma warning(pop) // restore warning settings.
128 #   endif
129 
130 #include <boost/config/abi_suffix.hpp> // pops abi_prefix.hpp pragmas
131 
132 #endif  // BOOST_TIMER_TIMER_HPP
133