https://github.com/ostanton/diabolicaldelegates
Single-file C++23 Delegates and Events
https://github.com/ostanton/diabolicaldelegates
cmake cpp-modules cpp20 cpp23 delegate delegates events interface modern-cpp
Last synced: 11 months ago
JSON representation
Single-file C++23 Delegates and Events
- Host: GitHub
- URL: https://github.com/ostanton/diabolicaldelegates
- Owner: ostanton
- License: mit
- Created: 2024-07-30T02:50:45.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2025-01-05T18:42:32.000Z (about 1 year ago)
- Last Synced: 2025-04-08T01:42:42.898Z (11 months ago)
- Topics: cmake, cpp-modules, cpp20, cpp23, delegate, delegates, events, interface, modern-cpp
- Language: C++
- Homepage:
- Size: 1.79 MB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Diabolical Delegates
A diabolical approach to delegates and events in C++. Uses C++20's modules, and C++23's modularised standard library.
## Dependencies
None. Except C++23.
## How to include
Copy & paste the `diabolical_delegates.cpp` file wherever, then `import` the module.
## How to use
There are currently 5 classes:
- Delegate
- MemberDelegate
- FunctorDelegate
- Event
- MultiEvent
`Delegate`s and `Event`s have return values and variadic template parameters.
These are what define the signatures of the functions that can be bound to them.
### Delegate
A delegate is simply a wrapper for any kind of function, implemented in its derived classes.
### MemberDelegate
A subclass of `Delegate`, it implements member functions with their object context.
### FunctorDelegate
A subclass of `Delegate`, it implements free-floating/global functions via a simple function pointer.
### Event
A wrapper for a delegate of any kind. It can be bound and unbound, supports whatever subclasses of `Delegate` there are.
### MultiEvent
Another version of `Event` which supports any number of `Delegate`s, not just one. These are `add`ed instead of `bind`ed.
Since `MultiEvent`s can have any number of `Delegate`s, their `broadcast` function is split into two. One executes all
the delegates without returning anything, and the other takes in a function to put the return value of each delegate execution
into.