1 //  boost/chrono/timer.hpp  ------------------------------------------------------------//
2 
3 //  Copyright Beman Dawes 2008
4 //  Copyright 2009 Vicente J. Botet Escriba
5 
6 //  Distributed under the Boost Software License, Version 1.0. (See accompanying
7 //  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
8 
9 //  See http://www.boost.org/libs/system for documentation.
10 
11 #ifndef BOOSTEX_CHRONO_TIMER_HPP
12 #define BOOSTEX_CHRONO_TIMER_HPP
13 
14 #include <boost/chrono/chrono.hpp>
15 #include <boost/system/error_code.hpp>
16 
17 namespace boost_ex
18 {
19   namespace chrono
20   {
21 
22 //--------------------------------------------------------------------------------------//
23 //                                    timer                                             //
24 //--------------------------------------------------------------------------------------//
25 
26     template <class Clock=boost::chrono::high_resolution_clock>
27     class timer
28     {
29     public:
30       typedef Clock                       clock;
31       typedef typename Clock::duration    duration;
32       typedef typename Clock::time_point  time_point;
33 
timer(boost::system::error_code & ec=BOOST_CHRONO_THROWS)34       explicit timer( boost::system::error_code & ec = BOOST_CHRONO_THROWS )
35         {
36           start(ec);
37           }
38 
~timer()39      ~timer() {}  // never throws
40 
start(boost::system::error_code & ec=BOOST_CHRONO_THROWS)41       void start( boost::system::error_code & ec = BOOST_CHRONO_THROWS )
42         {
43           m_start = clock::now( ec );
44           }
45 
elapsed(boost::system::error_code & ec=BOOST_CHRONO_THROWS)46       duration elapsed( boost::system::error_code & ec = BOOST_CHRONO_THROWS )
47         { return clock::now( ec ) - m_start; }
48 
49     private:
50       time_point m_start;
51     };
52 
53     typedef chrono::timer< boost::chrono::system_clock > system_timer;
54 #ifdef BOOST_CHRONO_HAS_CLOCK_STEADY
55     typedef chrono::timer< boost::chrono::steady_clock > steady_timer;
56 #endif
57     typedef chrono::timer< boost::chrono::high_resolution_clock > high_resolution_timer;
58 
59   } // namespace chrono
60 } // namespace boost_ex
61 
62 #endif
63