Implement Generic Event Handling System for C++ #13
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR introduces a comprehensive, type-safe generic event handling system for C++ that supports both observer patterns and callback-based event handling. The system is designed to unify and extend the existing event handling patterns found in the codebase.
Background
The existing codebase uses different event handling approaches:
DisplayManager
uses an observer pattern withDisplayListener
interfaceKeyboardEventHandler
uses callback-based handling withstd::function
These patterns work well but are not generic or unified, leading to code duplication and inconsistent approaches across different components.
Solution
The new generic event system provides:
Core Components
Event
&TypedEvent<T>
: Base classes for type-safe events with automatic type identificationEventListener
&TypedEventListener<T>
: Observer pattern interfaces for event handlingCallbackEventListener<T>
: Wrapper forstd::function
callbacksEventDispatcher
: Thread-safe event dispatcher supporting both synchronous and asynchronous dispatchEventListenerGuard
: RAII helper for automatic listener cleanupKey Features
Usage Examples
Observer pattern:
Callback pattern:
Event dispatch:
Integration
The system is designed to work alongside existing patterns without breaking changes. The
EventAwareDisplayManager
example shows how existing components can be enhanced to use the new system while maintaining backward compatibility.Common predefined events are provided (
DisplayAddedEvent
,KeyPressedEvent
,ApplicationStartedEvent
, etc.) that can be used immediately or serve as examples for custom events.Testing
The implementation includes comprehensive tests covering:
All tests pass and demonstrate the system's reliability and performance.
Documentation
Complete documentation is provided in
docs/EVENT_SYSTEM.md
with usage examples, best practices, and integration guidance.💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.