EventBus
A lightweight, header-only C++ EventBus library providing a simple and easy-to-use publish-subscribe mechanism.
Install / Use
/learn @XQQYT/EventBusREADME
EventBus
A lightweight, cross-platform,header-only C++ event bus library that provides an easy-to-use publish-subscribe mechanism.
中文文档 Performance Test Report Tutorial Video
Table of Contents
- Features
- Getting Started
- Examples
- API Reference
- Supported Callback Types
- Integration Requirements
- Performance Tips
- License
- Contributing
- Support
Features
- Lightweight: Header-only, zero dependencies (except for the C++17 standard library)
- Easy Integration: Single-header design, ready to use once included
- Thread-Safe: Built-in thread pool for asynchronous event handling
- Type-Safe: Template-based type-safe event handling
- High Performance: Optimized with modern C++ features
- Ease of Use: Simple API design, quick to get started
Getting Started
Include the header
#include "EventBus/EventBus.hpp"
Initialization
It is generally recommended to wrap EventBus as a singleton in real projects, this is only a demonstration
EventBus bus;
EventBus::EventBusConfig config{
EventBus::ThreadModel::DYNAMIC, // Thread pool model
EventBus::TaskModel::NORMAL, // Task model
2, // Minimum threads
4, // Maximum threads
1024 // Queue capacity
};
bus.initEventBus(config);
Examples
1. Using Lambda
bus.registerEvent("LambdaTest");
bus.subscribe("LambdaTest", [](int a, int b) {
std::cout << "LambdaTest: a+b=" << a + b << std::endl;
});
bus.publish("LambdaTest", 77, 88);
2. Using Member Function
class Handler {
public:
void memberFunc(int a, int b) {
std::cout << "Member function: a+b=" << a + b << std::endl;
}
};
Handler obj;
bus.registerEvent("MemberFunc");
bus.subscribe("MemberFunc", std::bind(&Handler::memberFunc, obj, std::placeholders::_1, std::placeholders::_2));
bus.publish("MemberFunc", 10, 20);
3. Using Normal Function + Priority Tasks
void func(int a, int b) {
std::cout << "Normal function: a+b=" << a + b << std::endl;
}
bus.registerEvent("NormalFunc");
bus.subscribe("NormalFunc", func);
// Low priority
bus.publishWithPriority(EventBus::TaskPriority::LOW, "NormalFunc", 1, 2);
// High priority
bus.publishWithPriority(EventBus::TaskPriority::HIGH, "NormalFunc", 100, 200);
4. Unsubscribe
auto id = bus.subscribe("LambdaTest", [](int x) {
std::cout << "Received: " << x << std::endl;
});
bus.publish("LambdaTest", 42);
bus.unsubscribe("LambdaTest", id); // Unsubscribe
API Reference
| Method | Description |
|:-------|:------------|
| registerEvent(eventName) | Register an event type |
| subscribe(eventName, callback) | Subscribe to an event |
| subscribeSafe(eventName, callback) | Safe subscription (auto-registers event) |
| publish(eventName, args...) | Publish an event (normal task) |
| publishWithPriority(priority, eventName, args...) | Publish an event (with priority) |
| unsubscribe(eventName, id) | Unsubscribe from an event |
| isEventRegistered(eventName) | Check if an event is registered |
Supported Callback Types
- Lambda expressions
- Normal function pointers
- Member functions (via
std::bindor Lambda wrappers) std::function
Integration Requirements
- C++ Standard: C++17 or higher
- Compiler: GCC 7+, Clang 5+, MSVC 2017+
- Dependencies: Only the C++ standard library
Performance Tips
- Event names: Use
conststrings or string literals to avoid copies - Callbacks: Use reference capture to avoid unnecessary copies
- Thread safety: Thread-safe by default, but be cautious with shared resources inside callbacks
- Memory management: Use smart pointers or references for large objects
License
MIT License - See LICENSE for details
Contributing
Issues and Pull Requests are welcome!
Support
For questions, please submit:
GitHub Issues: Report an issue
Email: xqqyt0502@163.com
