Class Acts::Delegate< R(Args…)>

template<typename R, typename ...Args>
class Acts::Delegate<R(Args...)>

Delegate type that allows type erasure of a callable without allocation and with a single level of indirection.

This type can support:

  • a free function pointer

  • a pointer to a member function alongside an instance pointer

    Note

    Delegate does not assume ownership of the instance. You need to ensure that the lifetime of the callable instance is longer than that of the Delegate.

    Note

    Currently Delegate only supports callables that are const

    tparam R

    Return type of the function signature

    tparam Args

    Types of the arguments of the function signatures

Public Functions

Delegate() = default
inline Delegate(function_type callable)

Constructor with an explicit runtime callable.

Note

The function signature requires the first argument of the callable is const void*. i.e. if the signature of the delegate is void(int), the callable’s signature has to be void(const void*, int).

Parameters

callable – The runtime value of the callable

template<auto Callable>
inline void connect()

Connect a free function pointer.

Note

The function pointer must be constexpr for Delegate to accept it

Template Parameters

Callable – The compile-time free function pointer

inline void connect(function_type callable)

Connect anything that is assignable to the function pointer.

Note

The function signature requires the first argument of the callable is const void*. i.e. if the signature of the delegate is void(int), the callable’s signature has to be void(const void*, int).

Parameters

callable – The runtime value of the callable

template<auto Callable, typename Type>
inline void connect(const Type *instance)

Connect a member function to be called on an instance.

Note

Delegate does not assume owner ship over instance. You need to ensure it’s lifetime is longer than that of Delegate.

Template Parameters
  • Callable – The compile-time member function pointer

  • Type – The type of the instance the member function should be called on

Parameters

instance – The instance on which the member function pointer should be called on

inline bool connected() const

Return whether this delegate is currently connected.

Returns

True if this delegate is connected

inline void disconnect()

Disconnect this delegate, meaning it cannot be called anymore.

inline operator bool() const

Return whether this delegate is currently connected.

Returns

True if this delegate is connected

inline return_type operator()(Args... args) const

The call operator that exposes the functionality of the Delegate type.

Parameters

args – The arguments to call the contained function with

Returns

Return value of the contained function

inline void operator=(function_type callable)

Assignment operator with an explicit runtime callable.

Note

The function signature requires the first argument of the callable is const void*. i.e. if the signature of the delegate is void(int), the callable’s signature has to be void(const void*, int).

Parameters

callable – The runtime value of the callable