|
With *event handler* here we mean anything that can continuously read from an event queue and process the received events.
|
|
With *event handler* here we mean anything that can continuously read from an event queue and process the received events.
|
|
|
|
|
|
### EventHandlerBase
|
|
## EventHandlerBase
|
|
|
|
|
|
It is the base interface extended by the **EventHandler** class:
|
|
It is the base interface extended by the **EventHandler** class:
|
|
```cpp
|
|
```cpp
|
... | @@ -15,18 +15,21 @@ public: |
... | @@ -15,18 +15,21 @@ public: |
|
};
|
|
};
|
|
```
|
|
```
|
|
|
|
|
|
### EventHanler
|
|
## EventHandler
|
|
The **EventHandler** class, in additions to EventHandlerBase, also extends the ActiveObejct interface.
|
|
The **EventHandler** class, in additions to EventHandlerBase, also extends the ActiveObejct interface.
|
|
In fact an EventHandler is a special ActiveObject that periodically checks a synchronized queue of events. When new events are present they can be removed from the queue and processed.
|
|
In fact an EventHandler is a special ActiveObject that periodically checks a synchronized queue of events. When new events are present they can be removed from the queue and processed.
|
|
|
|
|
|
|
|
### Methods
|
|
EventHadler offers two main methods:
|
|
EventHadler offers two main methods:
|
|
- **virtual void postEvent(const Event& ev)**: overridden from EventHandlerBase, it enqueues the event passed as a parameters.
|
|
- **virtual void postEvent(const Event& ev)**: overridden from EventHandlerBase, it enqueues the event passed as a parameters.
|
|
- **virtual void handleEvent(const Event& ev)**: protected method that is periodically called in the active objects thread. In fact in the *run()* method, whenever an event is present in the queue, it gets removed and the *handleEvent* method is called. The *handleEvent* method has to be overridden and implemented by any class that extends EventHandler.
|
|
- **virtual void handleEvent(const Event& ev)**: protected method that is periodically called in the active objects thread. In fact in the *run()* method, whenever an event is present in the queue, it gets removed and the *handleEvent* method is called. The *handleEvent* method has to be overridden and implemented by any class that extends EventHandler.
|
|
|
|
|
|
|
|
You can find the complete definition of EventHandler in `src/shared/events/EventHandler.h`.
|
|
|
|
|
|
### Example
|
|
### Example
|
|
This example shows how to define a very simple custom EventHandler.
|
|
This example shows how to define a very simple custom EventHandler.
|
|
|
|
|
|
##### MyEventHandler.h
|
|
#### MyEventHandler.h
|
|
The first thing we need to do is to import the required modules and to define a set of events. For more details you can look at the [Events](Events) page.
|
|
The first thing we need to do is to import the required modules and to define a set of events. For more details you can look at the [Events](Events) page.
|
|
Here we define three possible events: `EV_1`, `EV_2` and `EV_3`.
|
|
Here we define three possible events: `EV_1`, `EV_2` and `EV_3`.
|
|
```cpp
|
|
```cpp
|
... | @@ -76,7 +79,7 @@ private: |
... | @@ -76,7 +79,7 @@ private: |
|
};
|
|
};
|
|
```
|
|
```
|
|
|
|
|
|
##### test-myeventhandler.cpp
|
|
#### test-myeventhandler.cpp
|
|
In order to test that our class works we can create a MyEventHandler object and start it.
|
|
In order to test that our class works we can create a MyEventHandler object and start it.
|
|
We can then post some events and see what the output is.
|
|
We can then post some events and see what the output is.
|
|
```cpp
|
|
```cpp
|
... | @@ -110,7 +113,7 @@ int main() |
... | @@ -110,7 +113,7 @@ int main() |
|
return 0;
|
|
return 0;
|
|
}
|
|
}
|
|
```
|
|
```
|
|
##### Output
|
|
#### Output
|
|
Remember to add the `test-myeventhandler.cpp` to `sbs.conf` and to specify all the needed includes.
|
|
Remember to add the `test-myeventhandler.cpp` to `sbs.conf` and to specify all the needed includes.
|
|
After you run the example you should see the following output:
|
|
After you run the example you should see the following output:
|
|
```sh
|
|
```sh
|
... | | ... | |