File: timerchain.cpp

package info (click to toggle)
brutalchess 0.0.20060314cvs-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 6,496 kB
  • ctags: 447
  • sloc: cpp: 6,696; makefile: 118
file content (138 lines) | stat: -rw-r--r-- 2,221 bytes parent folder | download
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include "timerchain.h"
#include <iostream>
#include "console.h"

using namespace std;

TimerChain::TimerChain()
{
	starton = false;
	doneon = false;
}

void TimerChain::add( const Timer & t )
{
	timers.push_back( t );
}

void TimerChain::clear()
{
	// Can't remove timers while running
	if( starton )
		return;
	timers.clear();
}

/** Start the timer.
  *
  */
void TimerChain::start()
{
	// Can't start something twice
	if( starton )
		return;

	if( timers.size() <= 0 ) {
		starton = false;
		doneon = true;
		return;
	}
		
	starton = true;
	doneon = false;

	curtimer = 0;

	for( unsigned int i = 0; i < timers.size(); i++ ) {
		timers.at( i ).stop();
		timers.at( i ).resetDone();
	}
	
	timers.at( 0 ).start();
	val = timers.at( 0 ).startv();
}

bool TimerChain::started() const
{
	return starton;
}

void TimerChain::stop()
{
	starton = false;
}

bool TimerChain::done()
{
	return doneon;
}

void TimerChain::resetDone()
{
	doneon = false;
}

/** Update the timer.
 *  Modifies the value to reflect the time elapsed.
 */
void TimerChain::update()
{
	// Timer must be running to update
	if( !starton )
		return;
	
	lastval = val;
	
	timers.at( curtimer )++;

	val = timers.at( curtimer ).value();

	// As long as this isn't the last timer
	if( timers.at( curtimer ).done() && curtimer < timers.size() - 1 ) {
		curtimer++;
		timers.at( curtimer ).start();
	} else if( timers.at( curtimer ).done() && curtimer == timers.size() - 1 ) {
		lastval = val;
		starton = false;
		doneon = true;
	}	
}

/** Update the timer.
 *
 */
void TimerChain::operator++(int)
{
	update();
}

/** Returns the change in the value since the last update.
 *
 */
double TimerChain::change() const
{
	// No change if the timer isn't running
	if( !starton )
		return 0;
	
	return val - lastval;
}

/** Returns the current value within the range.
  * If the timer isn't running, returns the start of the range.
  * If the timer is finished, returns the end of the range.
  */
double TimerChain::value() const
{
	if( timers.size() <= 0 )
		return 0;

	if( doneon )
		return timers.at( timers.size() - 1 ) .end();

	// Still at the start if not running
	if( !starton )
		return timers.at( 0 ).startv();
	
	return timers.at( curtimer ).value();
}