class return_handler

Scope: stub

In header: #include <stub/return_handler.hpp>

Brief description

The return_handler is a helper object that is used e.g. in the function object to control which return values should be generated when called.

Template parameters

template <class R>
class return_handler

Member types (public)

using return_type

Member functions (public)

  return_handler ()
return_handler & set_return (Args &&… values)
void no_repeat ()
R operator() () const

Description

The return_handler provides the call operator() and when invoked it will return the specified return value. The type R will be the return value.

Example:

return_handler<uint32_t> v;
v.set_return(4U);

uint32_t a = v();
assert(a == 4U);

uint32_t b = v();
assert(b == 4U);

uint32_t c = v();
assert(c != 3U);
assert(c == 4U);

Or alternatively set multiple return values:

return_handler<uint32_t> v;
v.set_return({4U,3U});

uint32_t a = v();
assert(a == 4U);

uint32_t b = v();
assert(b == 3U);

uint32_t c = v();
assert(c == 4U);

uint32_t d = v();
assert(d != 4U);
assert(d == 3U);

The default behavior is to repeat the specified return values i.e.:

return_handler<uint32_t> v;
v.set_return(3U);

uint32_t a = v();
uint32_t b = v();
uint32_t c = v();

assert(a == 3U && b == 3U && c == 3U);

This behavior can be change by calling no_repeat() in which case the return_handler can only be invoked once per return value specified:

return_handler<uint32_t> v;
v.set_return(1U).no_repeat();

uint32_t a = v();
uint32_t b = v(); // <---- Crash

return_handler<uint32_t> v;
v.set_return({1U,2U,3U}).no_repeat();

uint32_t a = v();
uint32_t b = v();
uint32_t c = v();
uint32_t d = v(); // <---- Crash

Member Function Descriptions

return_handler ()
Constructor.

template <class… Args>
return_handler & set_return (Args &&… values)

Initializes the return_handler with the return values to use. Calling this function will also reset the return_handler state. So any previously specified returns values will be removed etc.

Parameter values:
The list of return values to use
Returns:
Reference to the return handler, this allows the caller to perform additional customization to the return handler such as turn on or off repeat.

void no_repeat ()
Set repeat off. This means that no values will be repeated the user has to specify exactly the number of values that should be return otherwise an assert will be triggered.

R operator() () const

The call operator which will generate a return value.

Returns:
The generated return value

Type Description

using return_type = typename unqualified_type< R >::type

Get the unqualified version of return type.
Versions
Development
latest