summaryrefslogtreecommitdiff
path: root/dnn/Reaction.cc
blob: c694ab50a2a322d9cba0a475a717d17a3804f2ca (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
127
128
129
130
/*
 *	DNN - Data News Network
 *
 *	by Stephane Chatty
 *
 *	Copyright 1993-1997
 *	Centre d'Etudes de la Navigation Aerienne (CENA)
 *
 *	Reactions.
 *
 *	$Id$
 *	$CurLog$
 */

#include "Reaction.h"
#include "Trigger.h"

/*?class IvlBaseReaction
The class \typ{IvlBaseReaction} is the base class for event handling.
A reaction implements a behaviour (through the virtual function \fun{Manage}),
and can be associated to one or more triggers, generally located in a sensor.
?*/

/*?
Create a reaction.
?*/
IvlBaseReaction :: IvlBaseReaction ()
: Triggers (),
  Grabbed ()
{
}

/*?nodoc?*/
IvlBaseReaction :: ~IvlBaseReaction ()
{
	IvlListIterOf <IvlTrigger> ti = Grabbed;
	while (++ti)
		(*ti)->Release (*this);

	ti = Triggers;
	while (++ti)
		(*ti)->Unsubscribe (*this);
}

/*!
Tell a reaction to forget a trigger. This special function is used only
when destroying a trigger.
!*/
void
IvlBaseReaction :: Forget (IvlTrigger& t)
{
	Triggers.Remove (&t);
	Grabbed.Remove (&t);
}

/*?nextdoc?*/
void
IvlBaseReaction :: SubscribeTo (IvlTrigger& t, REL_PRIORITY p)
{
	Triggers.Append (&t);
	t.Subscribe (*this, (IvlTrigger::REL_PRIORITY) p);
}

/*?
Tell that a reaction should (resp. should no more) be activated when the trigger \var{t}
causes the emission of an event.
?*/
void
IvlBaseReaction :: UnsubscribeTo (IvlTrigger& t)
{
	Triggers.Remove (&t);
	t.Unsubscribe (*this);
}

/*?nextdoc?*/
void
IvlBaseReaction :: Grab (IvlTrigger& t)
{
	Grabbed.Append (&t);
	t.Grab (*this);
}

/*?
Tell that a reaction should (resp. should no more) be the only one
activated when the trigger \var{t} causes the emission of an event.
?*/
void
IvlBaseReaction :: Release (IvlTrigger& t)
{
	Grabbed.Remove (&t);
	t.Release (*this);
}

/*?
This virtual function is called when an event is emitted because of a trigger to which
this reaction was attached. It should be redefined in derived classes to implement
behaviours.
?*/
void
IvlBaseReaction :: Manage (IvlEvent&)
{
}

/*?class IvlCallback
The class \typ{IvlCallback} is a ready-to-use derived class of \typ{IvlBaseReaction}.
In this class, the function \fun{Manage} is redefined to call a function passed when creating
the reaction.
?*/

/*?
Build a reaction that will call \var{f} to handle events.
\fun{f} has to take a \typ{IvlEvent\&} parameter and return \typ{void}.
?*/
IvlCallback :: IvlCallback (IvlHandlingFunction f)
: IvlBaseReaction (),
  Handler (f)
{
}

/*?nodoc?*/
IvlCallback :: ~IvlCallback ()
{
}

/*?nodoc?*/
void
IvlCallback :: Manage (IvlEvent& ev)
{
	(*Handler) (ev);
}