-
Notifications
You must be signed in to change notification settings - Fork 0
/
timer.cpp
88 lines (67 loc) · 2.63 KB
/
timer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//------------------------------------------------------------------------------
#include <stdio.h>
#include "macros.h"
#include "timer.h"
//==============================================================================
// Keep the linker happy
double (*Timer_t::pClock)(void) = 0;
map<int,double> Timer_t::Tdat;
//==============================================================================
Timer_t::Timer_t()
{
}
//------------------------------------------------------------------------------
Timer_t::~Timer_t()
{
}
//------------------------------------------------------------------------------
void Timer_t::Clock(double(* pC)(void))
{
pClock = pC; // Attach the clock
}
//------------------------------------------------------------------------------
void Timer_t::Dump()
{
printf("\n==============================\nTimer_t dump\n");
printf("Clock : %p\n",pClock);
if (pClock!=0) printf(" (Time now is %15.8e)\n",(*pClock)());
printf("Timer_t map :\n id : started\n");
WALKMAP(int,double,Tdat,i) printf("%3d : %15.8e\n",(*i).first,(*i).second);
printf("End of Timer_t dump===========\n");
}
//------------------------------------------------------------------------------
double Timer_t::Read(int t)
// Read the timer without disturbing it
{
if (pClock==0) return 0.0; // No clock?
if (Tdat.find(t)==Tdat.end()) return 0.0; // Timer not there?
return (*pClock)() - Tdat[t]; // And the elapsed time is....
}
//------------------------------------------------------------------------------
double Timer_t::Reset(int t)
// Reset a timer
{
if (pClock==0) return 0.0; // No clock?
if (Tdat.find(t)==Tdat.end()) return 0.0; // Timer NOT there? Ignore request
double ans = (*pClock)() - Tdat[t]; // What was the time?
Tdat[t] = (*pClock)(); // Reset it
return ans;
}
//------------------------------------------------------------------------------
void Timer_t::Start(int t)
// Start a new timer
{
if (pClock==0) return; // No clock?
if (Tdat.find(t)!=Tdat.end()) return; // Timer already there? Ignore request
Tdat[t] = (*pClock)(); // Then start/reset it......
}
//------------------------------------------------------------------------------
double Timer_t::Stop(int t)
// Stop the timer and output the delay
{
if (pClock==0) return 0.0; // No clock?
double ans = Read(t); // Read it
Tdat.erase(t); // erase() is tolerant of dud argument
return ans; // And the elapsed time was.....
}
//------------------------------------------------------------------------------