template<class... Fs>
class b2bits::Delegate< Fs >
Multi-signature delegate (callback) container.
A type-safe delegate that can store a callable object and invoke it with multiple different signatures. The delegate uses small object optimization to store handlers inline (up to 128 bytes) without heap allocation.
The delegate works by:
- Storing the handler object in inline storage (128 bytes)
- Creating type-erased callbacks for each supported signature
- Dispatching calls to the appropriate handler method at runtime
This is particularly useful for event systems where a single listener needs to handle multiple event types with different parameter lists.
- Template Parameters
-
| Fs | Function signatures supported by this delegate (e.g., void(int), void(string)) |
- Note
- The delegate is non-copyable but handlers can be attached/detached
-
Maximum handler size is 128 bytes (compile-time enforced)
-
Thread-safety: Not thread-safe. Attach/detach and invocation must be synchronized
- See also
- SessionHandlers for a practical usage example
|
| | Delegate ()=default |
| | Default constructor - creates an empty delegate.
|
| | ~Delegate () |
| | Destructor.
|
| template<typename Handler> |
| void | attach (Handler &&h) |
| | Attach a handler to the delegate.
|
| | operator bool () const |
| | Check if delegate has an attached handler.
|
| template<class Decl> |
| bool | is_assigned () const |
| | Check if delegate has a handler for a specific signature (runtime).
|
| void | detach () |
| | Detach the current handler.
|
| template<typename... Args> |
| std::invoke_result< Callbacks, void *, Args &&... >::type | operator() (Args &&... args) |
| | Invoke the delegate with provided arguments.
|
| template<typename Handler> |
| auto | ref () |
| | Get a reference to the stored handler.
|
| template<typename Handler> |
| auto | cref () const |
| | Get a const reference to the stored handler.
|
template<class... Fs>
template<class Decl>
Check if delegate has a handler for a specific signature (runtime).
Runtime check to determine if a handler is currently attached for the specified signature.
- Template Parameters
-
| Decl | Function declaration to check (e.g., void(int)) |
- Returns
- true if handler is attached and supports this signature
Referenced by b2bits::Delegate< void(Session *, const Instrument *, Seqnum, const SystemEvent *), void(Session *, const Instrument *, Seqnum, const OrderBookState *), void(Session *, const Instrument *, Seqnum, const OrderbookDirectory *), void(Session *, const Instrument *, Seqnum, const AddOrder *), void(Session *, const Instrument *, Seqnum, const OrderExecuted *, const Instrument::Order *), void(Session *, const Instrument *, Seqnum, const OrderExecutedWithPrice *, const Instrument::Order *), void(Session *, const Instrument *, Seqnum, const OrderCancelMessage *, const Instrument::Order *), void(Session *, const Instrument *, Seqnum, const OrderReplace *, const Instrument::Order *), void(Session *, const Instrument *, Seqnum, const Trade *), void(Session *, const Instrument *, Seqnum, const ExecutionDone *), void(Session *, const Instrument *, Seqnum, const Information *), void(Session *, const Instrument *, Seqnum, const BrokenTrade *), void(Session *, const Instrument *, Timestamp timestamp, const Instrument::BookLevelUpdate &), void(Session *, EOP), void(Session *, Seqnum, const OrderbookDirectory *), void(Session *, Event)>::operator bool().
template<class... Fs>
template<typename... Args>
Invoke the delegate with provided arguments.
Calls the attached handler with the provided arguments. The signature is determined at compile-time based on the argument types, and the appropriate handler method is invoked.
- Template Parameters
-
| Args | Argument types (deduced from call site) |
- Parameters
-
| args | Arguments to forward to the handler |
- Returns
- Result of handler invocation (if any)
- Note
- Fails at compile-time if no matching signature exists
-
Undefined behavior if called on empty delegate
- Example:
Delegate<void(
int), void(std::string)> delegate;
delegate.
attach([](
auto x) { process(x); });
delegate(42);
delegate("hello");
Referenced by b2bits::Delegate< void(Session *, const Instrument *, Seqnum, const SystemEvent *), void(Session *, const Instrument *, Seqnum, const OrderBookState *), void(Session *, const Instrument *, Seqnum, const OrderbookDirectory *), void(Session *, const Instrument *, Seqnum, const AddOrder *), void(Session *, const Instrument *, Seqnum, const OrderExecuted *, const Instrument::Order *), void(Session *, const Instrument *, Seqnum, const OrderExecutedWithPrice *, const Instrument::Order *), void(Session *, const Instrument *, Seqnum, const OrderCancelMessage *, const Instrument::Order *), void(Session *, const Instrument *, Seqnum, const OrderReplace *, const Instrument::Order *), void(Session *, const Instrument *, Seqnum, const Trade *), void(Session *, const Instrument *, Seqnum, const ExecutionDone *), void(Session *, const Instrument *, Seqnum, const Information *), void(Session *, const Instrument *, Seqnum, const BrokenTrade *), void(Session *, const Instrument *, Timestamp timestamp, const Instrument::BookLevelUpdate &), void(Session *, EOP), void(Session *, Seqnum, const OrderbookDirectory *), void(Session *, Event)>::detach().