ITCH 5.0 Market Data Handler 1.0.0
NASDAQ ITCH 5.0 Market Data Feed Handler
Loading...
Searching...
No Matches
b2bits::Delegate< Fs > Class Template Reference

Multi-signature delegate (callback) container. More...

Detailed Description

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:

  1. Storing the handler object in inline storage (128 bytes)
  2. Creating type-erased callbacks for each supported signature
  3. 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
FsFunction 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

#include <core/src/Delegate.h>

Public Types

using Callbacks = typename delegate_detail::Callbacks<void(delegate_detail::Delete), Fs...>::type
 Type of the callbacks structure storing function pointers.

Public Member Functions

 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.

Static Public Member Functions

template<class Decl>
static constexpr bool is_invocable ()
 Check if delegate is invocable with a specific signature (compile-time).

Public Attributes

Callbacks callbacks_
 Function pointer storage for each signature.
std::aligned_storage< 128 >::type mem_
 Inline storage for handler object (128 bytes).

Member Typedef Documentation

◆ Callbacks

template<class... Fs>
using b2bits::Delegate< Fs >::Callbacks = typename delegate_detail::Callbacks<void(delegate_detail::Delete), Fs...>::type

Type of the callbacks structure storing function pointers.

Constructor & Destructor Documentation

◆ Delegate()

template<class... Fs>
b2bits::Delegate< Fs >::Delegate ( )
default

Default constructor - creates an empty delegate.

◆ ~Delegate()

template<class... Fs>
b2bits::Delegate< Fs >::~Delegate ( )
inline

Destructor.

Automatically detaches any attached handler, calling its destructor and cleaning up resources.

Member Function Documentation

◆ attach()

template<class... Fs>
template<typename Handler>
void b2bits::Delegate< Fs >::attach ( Handler && h)
inline

Attach a handler to the delegate.

Stores the provided handler in the delegate's inline storage and sets up type-erased callbacks for each supported signature. If a handler is already attached, it is detached first.

The handler must:

  • Be callable with at least one of the delegate's signatures
  • Fit within 128 bytes (compile-time check)
  • Support perfect forwarding of arguments
Template Parameters
HandlerType of the handler (lambda, functor, function object)
Parameters
hHandler to attach (perfect-forwarded)
Note
Detaches any previously attached handler
Fails compilation if handler is too large (>128 bytes)
Example:
Delegate<void(int), void(std::string)> delegate;
// Attach a generic lambda
delegate.attach([](auto value) {
process(value);
});
// Or attach a specific functor
struct Handler {
void operator()(int x) { std::cout << x; }
void operator()(std::string s) { std::cout << s; }
};
delegate.attach(Handler{});
void attach(Handler &&h)
Attach a handler to the delegate.
Definition Delegate.h:125
Delegate()=default
Default constructor - creates an empty delegate.

◆ cref()

template<class... Fs>
template<typename Handler>
auto b2bits::Delegate< Fs >::cref ( ) const
inline

Get a const reference to the stored handler.

Returns a std::reference_wrapper<const Handler> to the handler stored in the delegate. Const-qualified version of ref().

Template Parameters
HandlerType of the handler to retrieve
Returns
std::reference_wrapper<const Handler> to the stored handler
Note
Undefined behavior if Handler type doesn't match attached handler
Undefined behavior if no handler is attached

◆ detach()

template<class... Fs>
void b2bits::Delegate< Fs >::detach ( )
inline

Detach the current handler.

Removes and destroys the currently attached handler, if any. After this call, the delegate is empty and operator bool() returns false.

Note
Safe to call on empty delegate (no-op)
Automatically called by destructor

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)>::attach(), and 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)>::~Delegate().

◆ is_assigned()

template<class... Fs>
template<class Decl>
bool b2bits::Delegate< Fs >::is_assigned ( ) const
inline

◆ is_invocable()

template<class... Fs>
template<class Decl>
constexpr bool b2bits::Delegate< Fs >::is_invocable ( )
inlinestaticconstexpr

Check if delegate is invocable with a specific signature (compile-time).

Static compile-time check to determine if the delegate supports calling with a specific function signature.

Template Parameters
DeclFunction declaration to check (e.g., void(int))
Returns
true if the signature is supported
Note
This is a compile-time check (constexpr)

◆ operator bool()

template<class... Fs>
b2bits::Delegate< Fs >::operator bool ( ) const
inlineexplicit

Check if delegate has an attached handler.

Returns
true if a handler is attached, false if delegate is empty

◆ operator()()

template<class... Fs>
template<typename... Args>
std::invoke_result< Callbacks, void *, Args &&... >::type b2bits::Delegate< Fs >::operator() ( Args &&... args)
inline

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
ArgsArgument types (deduced from call site)
Parameters
argsArguments 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); // Calls void(int) signature
delegate("hello"); // Calls void(std::string) 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)>::detach().

◆ ref()

template<class... Fs>
template<typename Handler>
auto b2bits::Delegate< Fs >::ref ( )
inline

Get a reference to the stored handler.

Returns a std::reference_wrapper to the handler stored in the delegate. Useful when you need to access the handler object directly.

Template Parameters
HandlerType of the handler to retrieve
Returns
std::reference_wrapper<Handler> to the stored handler
Note
Undefined behavior if Handler type doesn't match attached handler
Undefined behavior if no handler is attached

Member Data Documentation

◆ callbacks_

◆ mem_

template<class... Fs>
std::aligned_storage<128>::type b2bits::Delegate< Fs >::mem_

Inline storage for handler object (128 bytes).


The documentation for this class was generated from the following file: