| 1 | #ifdef _WIN32 |
| 2 | |
| 3 | #include "timestamp.h" |
| 4 | #define WIN32_LEAN_AND_MEAN |
| 5 | #include <windows.h> |
| 6 | |
| 7 | class TimestampImplWin32 : public TimestampImpl { |
| 8 | private: |
| 9 | LARGE_INTEGER performanceCount; |
| 10 | |
| 11 | public: |
| 12 | TimestampImplWin32() { |
| 13 | QueryPerformanceCounter(&this->performanceCount); |
| 14 | } |
| 15 | |
| 16 | TimestampImplWin32(double secs) { |
| 17 | LARGE_INTEGER freq; |
| 18 | QueryPerformanceFrequency(&freq); |
| 19 | QueryPerformanceCounter(&this->performanceCount); |
| 20 | this->performanceCount.QuadPart += static_cast<LONGLONG>(secs * (double)freq.QuadPart); |
| 21 | } |
| 22 | |
| 23 | virtual bool future() const { |
| 24 | LARGE_INTEGER now; |
| 25 | QueryPerformanceCounter(&now); |
| 26 | return this->performanceCount.QuadPart > now.QuadPart; |
| 27 | } |
| 28 | |
| 29 | virtual bool less(const TimestampImpl* other) const { |
| 30 | const TimestampImplWin32* other_impl = dynamic_cast<const TimestampImplWin32*>(other); |
| 31 | return this->performanceCount.QuadPart < other_impl->performanceCount.QuadPart; |
| 32 | } |
| 33 | |
| 34 | virtual bool greater(const TimestampImpl* other) const { |
| 35 | const TimestampImplWin32* other_impl = dynamic_cast<const TimestampImplWin32*>(other); |
| 36 | return this->performanceCount.QuadPart > other_impl->performanceCount.QuadPart; |
| 37 | } |
| 38 | |
| 39 | virtual double diff_secs(const TimestampImpl* other) const { |
| 40 | const TimestampImplWin32* other_impl = dynamic_cast<const TimestampImplWin32*>(other); |
| 41 | LONGLONG sec_diff = this->performanceCount.QuadPart - other_impl->performanceCount.QuadPart; |
| 42 | |
| 43 | LARGE_INTEGER freq; |
| 44 | QueryPerformanceFrequency(&freq); |
| 45 | |
| 46 | return (double)sec_diff / (double)freq.QuadPart; |
| 47 | } |
| 48 | }; |
| 49 | |
| 50 | Timestamp::Timestamp() : p_impl(new TimestampImplWin32()) {} |
| 51 | Timestamp::Timestamp(double secs) : p_impl(new TimestampImplWin32(secs)) {} |
| 52 | |
| 53 | #endif // WIN32 |