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