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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
|
/*
Ousía
Copyright (C) 2014, 2015 Benjamin Paaßen, Andreas Stöckel
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @file Events.hpp
*
* Contains classes and structures used for event handling within the Manager
* and Managed classes.
*
* @author Andreas Stöckel (astoecke@techfak.uni-bielefeld.de)
*/
#ifndef _OUSIA_EVENTS_HPP_
#define _OUSIA_EVENTS_HPP_
#include <string>
namespace ousia {
// Forward declarations
class Managed;
class Event;
using EventId = size_t;
/**
* EventType is an enum containing all possible node events. New event types
* should be added here.
*/
enum class EventType : int {
/**
* Generic update event which may be triggered if some important property
* of the node is changed.
*/
UPDATE,
/**
* The NAME_CHANGE event is used to inform listeners that the name of the
* node has changed.
*/
NAME_CHANGE,
/**
* The ADD_CHILD event is used to inform listeners that the node got a new
* child in any of its child node lists.
*/
ADD_CHILD,
/**
* The DELETE_CHILD event is used to inform listeners that the node got a
* new child in any of its child node lists.
*/
DELETE_CHILD
};
/**
* Definition of the EventHandler function.
*
* @param event is a reference to the object holding the event data.
* @param owner is a reference to the managed object that was given in the
* registerEventHandler function.
* @param data is some user defined data.
*/
using EventHandler = void (*)(const Event &event, Managed *owner, void *data);
/**
* The Event class and its child classes are responsible for containing the
* actual event data which further describes the event to the event handlers.
*/
struct Event {
/**
* Actual event type.
*/
EventType type;
/**
* Node on which the event was triggered.
*/
Managed *sender;
/**
* Constructor of the Event class.
*
* @param type is the actual event type.
*/
Event(EventType type) : type(type), sender(nullptr){};
Event(const Event &) = delete;
Event &operator=(const Event &) = delete;
};
/**
* Event used when the name of a node has changed.
*/
struct NameChangeEvent : public Event {
/**
* Reference to a string containing the old name of the node.
*/
const std::string &oldName;
/**
* Reference to a string containing the new name of the node.
*/
const std::string &newName;
/**
* Constructor of the NameChangeEvent class.
*
* @param oldName is the old name of the node. The given string is not
* copied, only the reference is passed around.
* @param newName is the new name of the node. The given string is not
* copied, only the reference is passed around.
*/
NameChangeEvent(const std::string &oldName, const std::string &newName)
: Event(EventType::NAME_CHANGE), oldName(oldName), newName(newName)
{
}
};
/**
* Struct containing the data which describes a single registered event handler.
*/
struct EventHandlerDescriptor {
/**
* Event type.
*/
EventType type;
/**
* Reference to the event handler containing the events.
*/
EventHandler handler;
/**
* Owner object.
*/
Managed *owner;
/**
* User defined data.
*/
void *data;
/**
* Constructor of the EventHandlerDescriptor struct.
*
* @param type is the event type for which the handler is registered.
* @param handler is the function pointer which is going to be called once
* the associated event handler has fired.
* @param owner is a user-specified object which owns the method that is
* going to be called. This can be used to make sure that the method which
* handles the events has access to its owned object as long as the event
* handler lives.
* @param data is some arbitrary user defined data.
*/
EventHandlerDescriptor(EventType type, EventHandler handler, Managed *owner,
void *data)
: type(type), handler(handler), owner(owner), data(data)
{
}
};
}
#endif /* _OUSIA_EVENTS_HPP_ */
|