micro-test-plus 6.0.0
µTest++ Testing Framework
Loading...
Searching...
No Matches
micro_os_plus::micro_test_plus::detail::callable_ref< Self_T > Class Template Reference

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

#include "micro-os-plus/micro-test-plus.h"

Public Member Functions

template<typename Callable_T>
requires (!std::is_same_v<std::remove_cv_t<Callable_T>, callable_ref<Self_T>>)
 callable_ref (Callable_T &callable) noexcept
 Constructs a reference to a callable.
void operator() (Self_T &self) const
 Invokes the referred callable.

Static Private Member Functions

template<typename Callable_T>
static void invoke_callable (void *callable, Self_T &self)
 Invokes a callable of a given type, from its address.

Private Attributes

void * callable_
 The address of the referred callable.
void(*) invoke_ (void *callable, Self_T &self)
 The function that invokes the referred callable.

Detailed Description

template<typename Self_T>
class micro_os_plus::micro_test_plus::detail::callable_ref< Self_T >

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_TThe type referred to by the argument of the callable (test_case).

Definition at line 1046 of file test.h.

Constructor & Destructor Documentation

◆ callable_ref()

template<typename Self_T>
requires (!std::is_same_v<std::remove_cv_t<Callable_T>, callable_ref<Self_T>>)
template<typename Callable_T>
requires (!std::is_same_v<std::remove_cv_t<Callable_T>, callable_ref<Self_T>>)
micro_os_plus::micro_test_plus::detail::callable_ref< Self_T >::callable_ref ( Callable_T & callable)
explicitnoexcept

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

Template Parameters
Callable_TThe type of the callable object.
Constraints
Callable_T is not a callable_ref, so that the copy constructor is used for copies.
Parameters
callableThe 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.

Definition at line 483 of file test-inlines.h.

484 : callable_{ const_cast<void*> (
485 static_cast<const volatile void*> (std::addressof (callable))) },
487 {
488 }
Non-owning reference to a callable invoked with a Self_T& argument.
Definition test.h:1047
void * callable_
The address of the referred callable.
Definition test.h:1112
void(*) invoke_(void *callable, Self_T &self)
The function that invokes the referred callable.
Definition test.h:1121
static void invoke_callable(void *callable, Self_T &self)
Invokes a callable of a given type, from its address.

References callable_ref(), and callable_.

Referenced by callable_ref().

Member Function Documentation

◆ invoke_callable()

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

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

Template Parameters
Callable_TThe type of the callable object.
Parameters
callableThe address of the callable, with its type erased.
selfThe 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.

Definition at line 515 of file test-inlines.h.

516 {
518 {
519 std::invoke (*static_cast<Callable_T*> (callable), self);
520 }
521 else
522 {
523 (*static_cast<Callable_T*> (callable)) (self);
524 }
525 }

◆ operator()()

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

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
selfThe reference passed to the callable.
Returns
Nothing.

Passes the stored address and self to the stored function.

Definition at line 496 of file test-inlines.h.

497 {
499 }

References callable_, and invoke_.

Member Data Documentation

◆ callable_

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

Its type is erased; only invoke_ converts it back.

Definition at line 1112 of file test.h.

Referenced by callable_ref(), and operator()().

◆ invoke_

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

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.

Referenced by operator()().


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