#include #include struct TimerTraits { private: static boost::int64_t performance_freq_; public: // The time type. This type has no constructor that takes a boost::int64_t to ensure // that the timer can only be used with relative times. class time_type { public: time_type() : ticks_(0) {} private: friend struct TimerTraits; boost::int64_t ticks_; }; // The duration type. class duration_type { public: duration_type() : ticks_(0) {} duration_type(boost::int64_t ticks) : ticks_(ticks) {} private: friend struct TimerTraits; boost::int64_t ticks_; }; // Get the current time. static time_type now(); // Add a duration to a time. static time_type add(const time_type& t, const duration_type& d) { time_type result; result.ticks_ = t.ticks_ + d.ticks_; return result; } // Subtract one time from another. static duration_type subtract(const time_type& t1, const time_type& t2) { return duration_type(t1.ticks_ - t2.ticks_); } // Test whether one time is less than another. static bool less_than(const time_type& t1, const time_type& t2) { boost::int64_t difference = t2.ticks_ - t1.ticks_; // boost::int64_t tick count values wrap periodically, so we'll use a heuristic that // says that if subtracting t1 from t2 yields a value smaller than 2^63, // (i.e. a positive number in signed 64-bit arithmetic) // then t1 is probably less than t2. This means that we can't handle // durations larger than 2^63, which shouldn't be a problem in practice. return difference > 0; } // Convert to POSIX duration type. static boost::posix_time::time_duration to_posix_duration( const duration_type& d); // Convert milliseconds to duration type static boost::int64_t milliseconds_to_duration(boost::uint32_t milliseconds); }; typedef boost::asio::basic_deadline_timer< boost::int64_t, TimerTraits> TimerImpl;