summaryrefslogtreecommitdiff
path: root/utils/Timer.h
blob: 733a44346e8beb339cddb45d12ddc4d3fe57f674 (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
121
122
123
124
125
126
/*
 *	CENA C++ Utilities
 *
 *	by Stephane Chatty
 *
 *	Copyright 1992-1995
 *	Centre d'Etudes de la Navigation Aerienne (CENA)
 *
 *	timers
 *
 *	$Id$
 *	$CurLog$
 */

#ifndef Timer_H_
#define Timer_H_

#ifdef __GNUG__
#pragma interface
#endif


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

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

class CcuCoreTimer;

class CcuTimerSet {
friend	class	CcuCoreTimer;

private:
	CcuCoreTimer*	FirstTimer;
#ifndef CPLUS_BUG19
	CcuListOf <CcuCoreTimer>	OtherTimers;
#else
	CcuList	OtherTimers;
#endif
	CcuCoreTimer*	ExtractNextActive ();
public:
inline		CcuTimerSet () : FirstTimer (0), OtherTimers ()	{}
inline		~CcuTimerSet ()		{}
inline	bool	IsEmpty () const	{ return bool (FirstTimer == 0); }
};



class CcuCoreTimer {
friend	class	CcuTimerSet;
public:
		enum timer_status	{ Active, Inactive };

protected:
static	int	IsInactive (CcuCoreTimer*);

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

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

/*?public?*/
		CcuCoreTimer (Millisecond, int, CcuTimerSet*);
virtual		~CcuCoreTimer ();

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 (CcuTimerSet*);
};

class CcuBaseTimer : public CcuCoreTimer {
private:
static	CcuSignalHandler*	TimeOutHandler;
static	CcuTimerSet*		TimerSet;
static	void	HandleSignal (int);
static	void	ClassInit ();

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

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

typedef void (*CcuTimerFun) (Millisecond);

class CcuTimer : public CcuBaseTimer {
protected:
	void	(*Handler) (Millisecond);
	void	Handle (Millisecond);

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

#endif	/* Timer_H_ */