sl@0: /* sl@0: * Portions Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies). All rights reserved. sl@0: * sl@0: * Copyright (c) 2006 sl@0: * Francois Dumont sl@0: * sl@0: * This material is provided "as is", with absolutely no warranty expressed sl@0: * or implied. Any use is at your own risk. sl@0: * sl@0: * Permission to use or copy this software for any purpose is hereby granted sl@0: * without fee, provided the above notices are retained on all copies. sl@0: * Permission to modify the code and to distribute modified code is granted, sl@0: * provided the above notices are retained, and a notice that the code was sl@0: * modified is included with the above copyright notice. sl@0: * sl@0: */ sl@0: sl@0: #ifndef CPPUNIT_TIMER_H sl@0: #define CPPUNIT_TIMER_H sl@0: sl@0: #ifndef __SYMBIAN32__ sl@0: #if defined (_WIN32) sl@0: # define CPPUNIT_WIN32_TIMER sl@0: # include sl@0: #endif sl@0: #endif sl@0: sl@0: class Timer { sl@0: public: sl@0: Timer() { sl@0: #if defined (CPPUNIT_WIN32_TIMER) sl@0: m_start.LowPart = m_restart.LowPart = m_stop.LowPart = 0; sl@0: m_start.HighPart = m_restart.HighPart = m_stop.HighPart = 0; sl@0: QueryPerformanceFrequency(&m_frequency); sl@0: #endif sl@0: } sl@0: sl@0: void start() { sl@0: #if defined (CPPUNIT_WIN32_TIMER) sl@0: QueryPerformanceCounter(&m_start); sl@0: #endif sl@0: } sl@0: sl@0: void restart() { sl@0: #if defined (CPPUNIT_WIN32_TIMER) sl@0: QueryPerformanceCounter(&m_restart); sl@0: if (m_start.HighPart == 0 && m_start.LowPart == 0) { sl@0: m_start = m_restart; sl@0: } sl@0: #endif sl@0: } sl@0: sl@0: void stop() { sl@0: #if defined (CPPUNIT_WIN32_TIMER) sl@0: LARGE_INTEGER stop; sl@0: QueryPerformanceCounter(&stop); sl@0: if ((m_stop.HighPart != 0 || m_stop.LowPart != 0) && sl@0: m_restart.HighPart != 0 && m_restart.LowPart != 0) { sl@0: m_stop.HighPart += (stop.HighPart - m_restart.HighPart); sl@0: if (stop.LowPart < m_restart.LowPart) { sl@0: if (m_restart.LowPart - stop.LowPart > m_stop.LowPart) { sl@0: m_stop.HighPart -= 1; sl@0: } sl@0: m_stop.LowPart -= m_restart.LowPart - stop.LowPart; sl@0: } sl@0: else { sl@0: if (stop.LowPart - m_restart.LowPart > 0xFFFFFFFF - m_stop.LowPart) { sl@0: m_stop.HighPart += 1; sl@0: } sl@0: m_stop.LowPart += stop.LowPart - m_restart.LowPart; sl@0: } sl@0: } sl@0: else { sl@0: m_stop = stop; sl@0: } sl@0: #endif sl@0: } sl@0: sl@0: double elapsedMilliseconds() const { sl@0: #if defined (CPPUNIT_WIN32_TIMER) sl@0: LARGE_INTEGER elapsed; sl@0: elapsed.HighPart = m_stop.HighPart - m_start.HighPart; sl@0: elapsed.LowPart = m_stop.LowPart - m_start.LowPart; sl@0: return (double)elapsed.QuadPart / (double)m_frequency.QuadPart * 1000; sl@0: #else sl@0: return 0; sl@0: #endif sl@0: } sl@0: sl@0: static bool supported() { sl@0: #if defined (CPPUNIT_WIN32_TIMER) sl@0: return true; sl@0: #else sl@0: return false; sl@0: #endif sl@0: } sl@0: sl@0: private: sl@0: #if defined (CPPUNIT_WIN32_TIMER) sl@0: LARGE_INTEGER m_frequency; sl@0: LARGE_INTEGER m_start, m_stop, m_restart; sl@0: #endif sl@0: }; sl@0: sl@0: #endif