| [29] | 1 | /*============================================================================= | 
|---|
 | 2 |     Boost.Wave: A Standard compliant C++ preprocessor library | 
|---|
 | 3 |     http://www.boost.org/ | 
|---|
 | 4 |  | 
|---|
 | 5 |     Copyright (c) 2001-2007 Hartmut Kaiser. Distributed under the Boost | 
|---|
 | 6 |     Software License, Version 1.0. (See accompanying file | 
|---|
 | 7 |     LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | 
|---|
 | 8 | =============================================================================*/ | 
|---|
 | 9 |  | 
|---|
 | 10 | #if !defined(STOP_WATCH_HPP_HK040911_INCLUDED) | 
|---|
 | 11 | #define STOP_WATCH_HPP_HK040911_INCLUDED | 
|---|
 | 12 |  | 
|---|
 | 13 | #include <boost/config.hpp> | 
|---|
 | 14 | #include <boost/timer.hpp> | 
|---|
 | 15 |  | 
|---|
 | 16 | /////////////////////////////////////////////////////////////////////////////// | 
|---|
 | 17 | //   | 
|---|
 | 18 | class stop_watch : public boost::timer { | 
|---|
 | 19 |  | 
|---|
 | 20 |     typedef boost::timer base_t; | 
|---|
 | 21 |      | 
|---|
 | 22 | public: | 
|---|
 | 23 |     stop_watch() : is_suspended_since(0), suspended_overall(0) {} | 
|---|
 | 24 |  | 
|---|
 | 25 |     void suspend() | 
|---|
 | 26 |     { | 
|---|
 | 27 |         if (0 == is_suspended_since) { | 
|---|
 | 28 |         // if not already suspended | 
|---|
 | 29 |             is_suspended_since = this->base_t::elapsed(); | 
|---|
 | 30 |         } | 
|---|
 | 31 |     } | 
|---|
 | 32 |     void resume() | 
|---|
 | 33 |     { | 
|---|
 | 34 |         if (0 != is_suspended_since) { | 
|---|
 | 35 |         // if really suspended | 
|---|
 | 36 |             suspended_overall += this->base_t::elapsed() - is_suspended_since; | 
|---|
 | 37 |             is_suspended_since = 0; | 
|---|
 | 38 |         } | 
|---|
 | 39 |     } | 
|---|
 | 40 |     double elapsed() const | 
|---|
 | 41 |     { | 
|---|
 | 42 |         if (0 == is_suspended_since) { | 
|---|
 | 43 |         // currently running | 
|---|
 | 44 |             return this->base_t::elapsed() - suspended_overall; | 
|---|
 | 45 |         } | 
|---|
 | 46 |  | 
|---|
 | 47 |     // currently suspended | 
|---|
 | 48 |         BOOST_ASSERT(is_suspended_since >= suspended_overall); | 
|---|
 | 49 |         return is_suspended_since - suspended_overall; | 
|---|
 | 50 |     } | 
|---|
 | 51 |      | 
|---|
 | 52 |     std::string format_elapsed_time() const | 
|---|
 | 53 |     { | 
|---|
 | 54 |     double current = elapsed(); | 
|---|
 | 55 |     char time_buffer[sizeof("1234:56:78.90 abcd.")+1]; | 
|---|
 | 56 |  | 
|---|
 | 57 |         using namespace std; | 
|---|
 | 58 |         if (current >= 3600) { | 
|---|
 | 59 |         // show hours | 
|---|
 | 60 |             sprintf (time_buffer, "%d:%02d:%02d.%03d hrs.", | 
|---|
 | 61 |                 (int)(current) / 3600, ((int)(current) % 3600) / 60, | 
|---|
 | 62 |                 ((int)(current) % 3600) % 60,  | 
|---|
 | 63 |                 (int)(current * 1000) % 1000); | 
|---|
 | 64 |         } | 
|---|
 | 65 |         else if (current >= 60) { | 
|---|
 | 66 |         // show minutes | 
|---|
 | 67 |             sprintf (time_buffer, "%d:%02d.%03d min.",  | 
|---|
 | 68 |                 (int)(current) / 60, (int)(current) % 60,  | 
|---|
 | 69 |                 (int)(current * 1000) % 1000); | 
|---|
 | 70 |         } | 
|---|
 | 71 |         else { | 
|---|
 | 72 |         // show seconds | 
|---|
 | 73 |             sprintf(time_buffer, "%d.%03d sec.", (int)current,  | 
|---|
 | 74 |                 (int)(current * 1000) % 1000); | 
|---|
 | 75 |         } | 
|---|
 | 76 |         return time_buffer; | 
|---|
 | 77 |     } | 
|---|
 | 78 |      | 
|---|
 | 79 | private: | 
|---|
 | 80 |     double is_suspended_since; | 
|---|
 | 81 |     double suspended_overall;  | 
|---|
 | 82 | }; | 
|---|
 | 83 |  | 
|---|
 | 84 | #endif // !defined(STOP_WATCH_HPP_HK040911_INCLUDED) | 
|---|