File Delegate.hpp

namespace Acts

Note

This file is foreseen for the Geometry module to replace Extent

template<typename>
class Delegate
#include <Acts/Utilities/Delegate.hpp>
template<typename R, typename ...Args>
class Delegate<R(Args...)>
#include <Acts/Utilities/Delegate.hpp>

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

Template Parameters
  • R – Return type of the function signature

  • 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<typename Callable, typename = isNoFunPtr<Callable>>
inline Delegate(Callable &callable)

Constructor with a possibly stateful function object.

Note

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

Template Parameters

Callable – Type of the callable

Parameters

callable – The callable (function object or lambda)

template<typename Callable, typename = isNoFunPtr<Callable>>
Delegate(Callable&&) = delete

Constructor from rvalue reference is deleted, should catch construction with temporary objects and thus invalid pointers.

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

template<typename Callable, typename = isNoFunPtr<Callable>>
inline void connect(Callable &callable)

Assignment operator with possibly stateful function object.

Note

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

Template Parameters

Callable – Type of the callable

Parameters

callable – The callable (function object or lambda)

template<typename Callable, typename = isNoFunPtr<Callable>>
void connect(Callable&&) = delete

Connection with rvalue reference is deleted, should catch assignment from temporary objects and thus invalid pointers.

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

template<typename Callable, typename = isNoFunPtr<Callable>>
inline void operator=(Callable &callable)

Assignment operator with possibly stateful function object.

Note

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

Template Parameters

Callable – Type of the callable

Parameters

callable – The callable (function object or lambda)

template<typename Callable, typename = isNoFunPtr<Callable>>
void operator=(Callable&&) = delete

Assignment operator from rvalue reference is deleted, should catch assignment from temporary objects and thus invalid pointers.

Private Types

using function_ptr_type = return_type (*)(Args...)
using function_type = return_type (*)(const void*, Args...)

Alias to the function pointer type this class will store.

using isNoFunPtr = std::enable_if_t<not std::is_convertible_v<std::decay_t<T>, function_type>>
using isSignatureCompatible = decltype(std::declval<T&>() = std::declval<C>())
using return_type = R

Alias of the return type.

Private Members

function_type m_function = {nullptr}

Stores the function pointer wrapping the compile time function pointer given in connect().

const void *m_payload = {nullptr}

Stores the instance pointer.