# EventDispatcher
EventDispatcher is a C++ event dispatch system that uses a custom base class with inheritance to manage different types of event data. This project demonstrates how to handle events asynchronously using a worker thread, with different event types and data structures.
## Features
- Supports multiple event types
- Handles events with different data structures using inheritance
- Uses a worker thread to process events asynchronously
- Thread-safe event addition
- Flexible event handling system
## Requirements
- C++17 compatible compiler
- CMake 3.10 or higher
## Getting Started
### Clone the Repository
```bash
git clone https://github.com/yourusername/EventDispatcher.git
cd EventDispatcher
-
Create a build directory:
mkdir build cd build
-
Generate the build files:
cmake ..
-
Build the project:
make
./event_dispatcher
The EventDispatcher
class allows you to register event handlers, enqueue events, and handle events asynchronously. Here is a brief overview of how to use the EventDispatcher
class:
dispatcher.registerEventHandler(EventType::EVENT_TYPE_A, handleEventTypeA);
dispatcher.registerEventHandler(EventType::EVENT_TYPE_B, handleEventTypeB);
dispatcher.registerEventHandler(EventType::EVENT_TYPE_C, handleEventTypeC);
You can add events from any thread or function using the addEvent
method:
dispatcher.addEvent(EventType::EVENT_TYPE_A, EventDataA{42});
dispatcher.addEvent(EventType::EVENT_TYPE_B, EventDataB{"Hello, World!"});
dispatcher.addEvent(EventType::EVENT_TYPE_C, EventDataC{3.14});
Define event handlers to process the events:
void handleEventTypeA(const Event& event) {
auto data = std::dynamic_pointer_cast<EventDataA>(event.data);
if (data) {
data->extractData();
}
}
void handleEventTypeB(const Event& event) {
auto data = std::dynamic_pointer_cast<EventDataB>(event.data);
if (data) {
data->extractData();
}
}
void handleEventTypeC(const Event& event) {
auto data = std::dynamic_pointer_cast<EventDataC>(event.data);
if (data) {
data->extractData();
}
}
This project is licensed under the MIT License - see the LICENSE file for details.
This README.md
provides an overview of the project, instructions for building and running the project, and a brief guide on how to use the EventDispatcher
class. It also includes standard sections for license and contributing, making it suitable for a GitHub repository.