OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
ossimTimer.cpp
Go to the documentation of this file.
1 /*
2  * This code was taken directly from the OpenSceneGraph
3  */
4 //#include <stdlib.h>
5 #include <stdio.h>
6 #include <string.h>
9 
11 
12 // follows are the constructors of the Timer class, once version
13 // for each OS combination. The order is WIN32, FreeBSD, Linux, IRIX,
14 // and the rest of the world.
15 //
16 // all the rest of the timer methods are implemented within the header.
17 
18 
20 {
21  if(!m_instance)
22  {
23  m_instance = new ossimTimer;
24  }
25  return m_instance;
26 }
27 
28 // ---
29 // From: http://msdn.microsoft.com/en-us/library/b0084kay.aspx
30 // Defined for applications for Win32 and Win64. Always defined.
31 // ---
32 #if defined(_WIN32)
33 
34 #include <sys/types.h>
35 #include <fcntl.h>
36 #include <windows.h>
37 #include <winbase.h>
39 {
40  LARGE_INTEGER frequency;
41  if(QueryPerformanceFrequency(&frequency))
42  {
43  m_secsPerTick = 1.0/(double)frequency.QuadPart;
44  }
45  else
46  {
47  m_secsPerTick = 1.0;
48  ossimNotify(ossimNotifyLevel_NOTICE)<<"Error: ossimTimer::ossimTimer() unable to use QueryPerformanceFrequency, "<<std::endl;
49  ossimNotify(ossimNotifyLevel_NOTICE)<<"timing code will be wrong, Windows error code: "<<GetLastError()<<std::endl;
50  }
51 
52  setStartTick();
53 }
54 
56 {
57  LARGE_INTEGER qpc;
58  if (QueryPerformanceCounter(&qpc))
59  {
60  return qpc.QuadPart;
61  }
62  else
63  {
64  ossimNotify(ossimNotifyLevel_NOTICE)<<"Error: ossimTimer::ossimTimer() unable to use QueryPerformanceCounter, "<<std::endl;
65  ossimNotify(ossimNotifyLevel_NOTICE)<<"timing code will be wrong, Windows error code: "<<GetLastError()<<std::endl;
66  return 0;
67  }
68 }
69 
70 #else
71 
72 
73 #include <unistd.h>
74 
76 {
77  m_secsPerTick = (1.0 / (double) 1000000);
78 
79  setStartTick();
80 }
81 
82 
83  #if defined(_POSIX_TIMERS) && ( _POSIX_TIMERS > 0 ) && defined(_POSIX_MONOTONIC_CLOCK)
84  #include <time.h>
85 
87  {
88  struct timespec ts;
89  clock_gettime(CLOCK_MONOTONIC, &ts);
90  return ((ossimTimer::Timer_t)ts.tv_sec)*1000000+(ossimTimer::Timer_t)ts.tv_nsec/1000;
91  }
92  #else
93  #include <sys/time.h>
94 
96  {
97  struct timeval tv;
98  gettimeofday(&tv, NULL);
99  return ((ossimTimer::Timer_t)tv.tv_sec)*1000000+(ossimTimer::Timer_t)tv.tv_usec;
100  }
101  #endif
102 
103 // ossimTimer::Timer_t ossimTimer::tick() const
104 // {
105 // struct timeval tv;
106 // gettimeofday(&tv, NULL);
107 // return ((ossimTimer::Timer_t)tv.tv_sec)*1000000+(ossimTimer::Timer_t)tv.tv_usec;
108 // }
109 
110 #endif
void setStartTick()
Set the start.
Definition: ossimTimer.h:27
unsigned long long Timer_t
Definition: ossimTimer.h:16
static ossimTimer * instance()
Definition: ossimTimer.cpp:19
Timer class is used for measuring elapsed time or time between two points.
Definition: ossimTimer.h:10
double m_secsPerTick
Definition: ossimTimer.h:66
static ossimTimer * m_instance
Definition: ossimTimer.h:64
Timer_t tick() const
Get the timers tick value.
Definition: ossimTimer.cpp:95
OSSIMDLLEXPORT std::ostream & ossimNotify(ossimNotifyLevel level=ossimNotifyLevel_WARN)