summaryrefslogtreecommitdiff
path: root/utils/Timer.h
blob: 1c5281bce8cef9d671bba007d73b3c3c8cb6cb29 (plain)
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
/*
 *	CENA C++ Utilities
 *
 *	by Stephane Chatty
 *
 *	Copyright 1992-1996
 *	Centre d'Etudes de la Navigation Aerienne (CENA)
 *
 *	timers
 *
 *	$Id$
 *	$CurLog$
 */

#ifndef Timer_H_
#define Timer_H_

#include "cplus_bugs.h"
#include "bool.h"
#include "Time.h"

class IvlSignalHandler;
#if 0
template <class ITEM> class IvlListOf;
#else
#include "List.h"
#endif

class IvlCoreTimer;

class IvlTimerSet {
friend	class	IvlCoreTimer;

private:
	IvlCoreTimer*	FirstTimer;
#ifndef CPLUS_BUG19
	IvlListOf <IvlCoreTimer>	OtherTimers;
#else
	IvlList	OtherTimers;
#endif
	bool	FirstIsUpdated;
	IvlCoreTimer*	ExtractNextActive ();
public:
inline		IvlTimerSet () : FirstTimer (0), OtherTimers (), FirstIsUpdated (false)	{}
inline		~IvlTimerSet ()		{}
//inline	bool	IsEmpty () const	{ return bool (FirstTimer == 0); }  seems useless (10 may 96)
};

class IvlCoreTimer {
friend	class	IvlTimerSet;
public:
		enum timer_status	{ Active, Inactive };

protected:
static	int	IsInactive (IvlCoreTimer*);

	IvlTimerSet*	MySet;
	Millisecond	NextDate;
	Millisecond	Period;
	int		PulsesLeft;
	timer_status StatusFlag;

	void	Activate ();
	void	Schedule (Millisecond);
	void	Reschedule ();

/*?public?*/
		IvlCoreTimer (Millisecond, int, IvlTimerSet*);
virtual		~IvlCoreTimer ();

public:
	void	ChangePeriod (Millisecond);
	void	ChangeNbPulses (int);
	void	Stop ();
	void	Restart ();
	void	Wait ();
inline	Millisecond	GetPeriod () const		{ return Period; }
inline	int		GetNbPulses () const		{ return PulsesLeft; }
inline	timer_status	GetStatus () const		{ return StatusFlag; }

protected:
/*?public?*/
virtual	void	Handle (Millisecond);
virtual	void	SetAlarm (Millisecond) = 0;
virtual	void	StopAlarm () = 0;

public:
static	void		Fire (IvlTimerSet*);
};

class IvlBaseTimer : public IvlCoreTimer {
private:
static	IvlSignalHandler*	TimeOutHandler;
static	IvlTimerSet*		TimerSet;
static	void	HandleSignal (int);
static	void	ClassInit ();

protected:
	void	SetAlarm (Millisecond);
	void	StopAlarm ();

public:	
		IvlBaseTimer (Millisecond, int = -1);
		~IvlBaseTimer ();
};

typedef void (*IvlTimerFun) (Millisecond);

class IvlTimer : public IvlBaseTimer {
protected:
	void	(*Handler) (Millisecond);
	void	Handle (Millisecond);

public:
		IvlTimer (Millisecond, void (*) (Millisecond), int = -1);
		~IvlTimer ();
inline	void	SetHandler (void (*h) (Millisecond))	{ Handler = h; }
};

#endif	/* Timer_H_ */