os/ossrv/genericopenlibs/cppstdlib/stl/test/unit/cppunit/cppunit_timer.h
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 /*
     2  * Portions Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
     3  *
     4  * Copyright (c) 2006
     5  * Francois Dumont
     6  *
     7  * This material is provided "as is", with absolutely no warranty expressed
     8  * or implied. Any use is at your own risk.
     9  *
    10  * Permission to use or copy this software for any purpose is hereby granted
    11  * without fee, provided the above notices are retained on all copies.
    12  * Permission to modify the code and to distribute modified code is granted,
    13  * provided the above notices are retained, and a notice that the code was
    14  * modified is included with the above copyright notice.
    15  *
    16  */
    17 
    18 #ifndef CPPUNIT_TIMER_H
    19 #define CPPUNIT_TIMER_H
    20 
    21 #ifndef __SYMBIAN32__
    22 #if defined (_WIN32)
    23 #  define CPPUNIT_WIN32_TIMER
    24 #  include <windows.h>
    25 #endif
    26 #endif
    27 
    28 class Timer {
    29 public:
    30   Timer() {
    31 #if defined (CPPUNIT_WIN32_TIMER)
    32     m_start.LowPart = m_restart.LowPart = m_stop.LowPart = 0;
    33     m_start.HighPart = m_restart.HighPart = m_stop.HighPart = 0;
    34     QueryPerformanceFrequency(&m_frequency);
    35 #endif
    36   }
    37 
    38   void start() {
    39 #if defined (CPPUNIT_WIN32_TIMER)
    40     QueryPerformanceCounter(&m_start);
    41 #endif
    42   }
    43 
    44   void restart() {
    45 #if defined (CPPUNIT_WIN32_TIMER)
    46     QueryPerformanceCounter(&m_restart);
    47     if (m_start.HighPart == 0 && m_start.LowPart == 0) {
    48       m_start = m_restart;
    49     }
    50 #endif
    51   }
    52 
    53   void stop() {
    54 #if defined (CPPUNIT_WIN32_TIMER)
    55     LARGE_INTEGER stop;
    56     QueryPerformanceCounter(&stop);
    57     if ((m_stop.HighPart != 0 || m_stop.LowPart != 0) &&
    58         m_restart.HighPart != 0 && m_restart.LowPart != 0) {
    59       m_stop.HighPart += (stop.HighPart - m_restart.HighPart);
    60       if (stop.LowPart < m_restart.LowPart) {
    61         if (m_restart.LowPart - stop.LowPart > m_stop.LowPart) {
    62           m_stop.HighPart -= 1;
    63         }
    64         m_stop.LowPart -= m_restart.LowPart - stop.LowPart;
    65       }
    66       else {
    67         if (stop.LowPart - m_restart.LowPart > 0xFFFFFFFF - m_stop.LowPart) {
    68           m_stop.HighPart += 1;
    69         }
    70         m_stop.LowPart += stop.LowPart - m_restart.LowPart;
    71       }
    72     }
    73     else {
    74       m_stop = stop;
    75     }
    76 #endif
    77   }
    78 
    79   double elapsedMilliseconds() const {
    80 #if defined (CPPUNIT_WIN32_TIMER)
    81     LARGE_INTEGER elapsed;
    82     elapsed.HighPart = m_stop.HighPart - m_start.HighPart;
    83     elapsed.LowPart = m_stop.LowPart - m_start.LowPart;
    84     return (double)elapsed.QuadPart / (double)m_frequency.QuadPart * 1000;
    85 #else
    86     return 0;
    87 #endif
    88   }
    89 
    90   static bool supported() {
    91 #if defined (CPPUNIT_WIN32_TIMER)
    92     return true;
    93 #else
    94     return false;
    95 #endif
    96   }
    97 
    98 private:
    99 #if defined (CPPUNIT_WIN32_TIMER)
   100   LARGE_INTEGER m_frequency;
   101   LARGE_INTEGER m_start, m_stop, m_restart;
   102 #endif
   103 };
   104 
   105 #endif