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

#ifndef IvlReaction_H_
#define IvlReaction_H_

#include "ivl/List.h"

class IvlTrigger;
class IvlEvent;

 
class IvlBaseReaction {
friend	class	IvlTrigger;

public:
enum	REL_PRIORITY { isFirstPriority, isNormalPriority, isLastPriority };

protected:
		IvlBaseReaction ();
	IvlListOf <IvlTrigger>	Triggers;
	IvlListOf <IvlTrigger>	Grabbed;

	void	Forget (IvlTrigger&);

public:
virtual		~IvlBaseReaction ();
	void	SubscribeTo (IvlTrigger&, REL_PRIORITY = isNormalPriority);
	void	UnsubscribeTo (IvlTrigger&);
	void	Grab (IvlTrigger&);
	void	Release (IvlTrigger&);
virtual	void	Manage (IvlEvent&);
};

typedef void (*IvlHandlingFunction) (IvlEvent&);

class IvlCallback : public IvlBaseReaction {
protected:
	IvlHandlingFunction	Handler;

public:
		IvlCallback (IvlHandlingFunction);
		~IvlCallback ();
	void	Manage (IvlEvent&);
};

template <class T> class IvlReactionOf : public IvlBaseReaction {
protected:
	T&	Object;
	void	(T::*Reaction)	(IvlEvent&);

public:
		IvlReactionOf (T& o, void (T::*r) (IvlEvent&)) : IvlBaseReaction (), Object (o), Reaction (r)	{}
		~IvlReactionOf ()	{}
	void	Manage (IvlEvent& ev)	{ (Object.*Reaction) (ev); }
};

#define SpecializedReaction(R,S) \
class R : public IvlBaseReaction { \
protected: \
	S&	Body; \
	void	(S::*React)	(IvlEvent&); \
public: \
		R (S& s, void (S::*sc) (IvlEvent&)) : IvlBaseReaction (), Body (s), React (sc)	{} \
		~R ()	{} \
	void	Manage (IvlEvent& ev)	{ (Body.*React) (ev); } \
};

/* Attempt at having reactions that locate the object which should be activated.
    IvlObjectReactionOf should be renamed IvlReactionOf,
    and IvlReactionOf should be renamed IvlBasicReactionOf */

class IvlObjectReaction : public IvlBaseReaction {
protected:
virtual	void*	LocateObject (IvlEvent&) = 0;

public:
		IvlObjectReaction () : IvlBaseReaction ()	{}
		~IvlObjectReaction ()	{}
};

template <class T> class IvlObjectReactionOf : public IvlObjectReaction {
protected:
	void	(T::*Reaction)	(IvlEvent&);
public:
		IvlObjectReactionOf (void (T::*r) (IvlEvent&)) : IvlObjectReaction (), Reaction (r)	{}
		~IvlObjectReactionOf ()	{}
	void	Manage (IvlEvent& ev) { T* o = (T*) LocateObject (ev); (o->*Reaction) (ev); }
};

#if 0
template <class T> class XtvReflexOf : public IvlObjectReactionOf <T> {
protected:
	void*	LocateObject (IvlEvent& ev) { return ((XtvEvent*)&ev)->GetTarget (); }
public:
		XtvReflexOf (void (T::*r) (IvlEvent&)) : IvlObjectReactionOf <T>  (r)	{}
};
#endif

#endif	/* IvlReaction_H_ */