Skip to main content

callable_ref Class Template

Non-owning reference to a callable invoked with a Self_T& argument. More...

Declaration​

template <typename Self_T>
class micro_os_plus::micro_test_plus::detail::callable_ref<Self_T> { ... }

Included Headers​

Public Constructors Index​

template <typename Callable_T>
callable_ref (Callable_T &callable) noexcept

Constructs a reference to a callable. More...

Public Operators Index​

template <typename Self_T>
voidoperator() (Self_T &self) const

Invokes the referred callable. More...

Private Member Attributes Index​

template <typename Self_T>
void *callable_

The address of the referred callable. More...

template <typename Self_T>
void(*)invoke_(void *callable, Self_T &self)

The function that invokes the referred callable. More...

Private Static Functions Index​

template <typename Callable_T>
static voidinvoke_callable (void *callable, Self_T &self)

Invokes a callable of a given type, from its address. More...

Description​

Non-owning reference to a callable invoked with a Self_T& argument.

Holds the address of the callable and a pointer to a function, instantiated for its type, which converts the address back and invokes the callable. Unlike a std::function, it neither copies the callable nor allocates memory, so the callable must outlive the reference.

A test case refers to its body this way, since the body is a local object of runnable_base::create_test_case(), which runs the test case and destroys it before returning.

Template Parameters
Self_T

The type referred to by the argument of the callable (test_case).

Definition at line 1046 of file test.h.

Public Constructors​

callable_ref()​

template <typename Callable_T>
micro_os_plus::micro_test_plus::detail::callable_ref< Self_T >::callable_ref (Callable_T & callable)
explicit noexcept

Constructs a reference to a callable.

The callable must be invocable with a Self_T& argument, and must outlive the reference.

Template Parameters
Callable_T

The type of the callable object.

Constraints

Callable_T is not a callable_ref, so that the copy constructor is used for copies.

Parameters
callable

The callable object; an lvalue, possibly const, in which case it must be callable as const.

Stores the address of the callable, and the address of the invoke_callable() instantiation for its type.

Declaration at line 1068 of file test.h, definition at line 483 of file test-inlines.h.

483 callable_ref<Self_T>::callable_ref (Callable_T& callable) noexcept
484 : callable_{ const_cast<void*> (
485 static_cast<const volatile void*> (std::addressof (callable))) },
486 invoke_{ &invoke_callable<Callable_T> }
487 {
488 }

References micro_os_plus::micro_test_plus::detail::callable_ref< Self_T >::callable_ref and micro_os_plus::micro_test_plus::detail::callable_ref< Self_T >::callable_.

Referenced by micro_os_plus::micro_test_plus::detail::callable_ref< Self_T >::callable_ref.

Public Operators​

operator()()​

template <typename Self_T>
void micro_os_plus::micro_test_plus::detail::callable_ref< Self_T >::operator() (Self_T & self)
inline

Invokes the referred callable.

Not noexcept, since a failed require() stops the body of a test case by throwing an exception.

Always inlined, so that, without optimisations, it adds no frame between test_case::run() and invoke_callable().

Parameters
self

The reference passed to the callable.

Returns

Nothing.

Passes the stored address and self to the stored function.

Declaration at line 1085 of file test.h, definition at line 496 of file test-inlines.h.

497 {
498 invoke_ (callable_, self);
499 }

References micro_os_plus::micro_test_plus::detail::callable_ref< Self_T >::callable_ and micro_os_plus::micro_test_plus::detail::callable_ref< Self_T >::invoke_.

Private Member Attributes​

callable_​

template <typename Self_T>
void* micro_os_plus::micro_test_plus::detail::callable_ref< Self_T >::callable_

The address of the referred callable.

Its type is erased; only invoke_ converts it back.

Definition at line 1112 of file test.h.

Referenced by micro_os_plus::micro_test_plus::detail::callable_ref< Self_T >::callable_ref and micro_os_plus::micro_test_plus::detail::callable_ref< Self_T >::operator().

invoke_​

template <typename Self_T>
void(*) micro_os_plus::micro_test_plus::detail::callable_ref< Self_T >::invoke_(void *callable, Self_T &self)

The function that invokes the referred callable.

The invoke_callable() instantiation for the type of the callable; it receives callable_ and the Self_T& argument.

Definition at line 1121 of file test.h.

1121 void (*invoke_) (void* callable, Self_T& self);

Referenced by micro_os_plus::micro_test_plus::detail::callable_ref< Self_T >::operator().

Private Static Functions​

invoke_callable()​

template <typename Callable_T>
void micro_os_plus::micro_test_plus::detail::callable_ref< Self_T >::invoke_callable (void * callable, Self_T & self)
static

Invokes a callable of a given type, from its address.

Instantiated by the constructor for the type of the callable; its address is stored in invoke_.

Template Parameters
Callable_T

The type of the callable object.

Parameters
callable

The address of the callable, with its type erased.

self

The reference passed to the callable.

Returns

Nothing.

Casts callable back to Callable_T*, the type it had when the reference was constructed, and invokes the callable through it, as an lvalue, with self.

A function pointer or a function object is called directly, not through std::invoke(), which, without optimisations, adds three frames to the call chain; a pointer to a member, which only std::invoke() can call, is still passed to it.

Declaration at line 1104 of file test.h, definition at line 515 of file test-inlines.h.

515 callable_ref<Self_T>::invoke_callable (void* callable, Self_T& self)
516 {
517 if constexpr (std::is_member_pointer_v<Callable_T>)
518 {
519 std::invoke (*static_cast<Callable_T*> (callable), self);
520 }
521 else
522 {
523 (*static_cast<Callable_T*> (callable)) (self);
524 }
525 }

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


Generated via doxygen2docusaurus 2.2.8 by Doxygen 1.18.0.