Warning: This is a development version. The latest stable version is Version 7.1.1.
In header: #include <stub/return_handler.hpp>
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 <class R>
class return_handler
using | return_type |
return_handler () | |
return_handler & | set_return (Args &&… values) |
void | no_repeat () |
R | operator() () const |
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
Constructor.
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.
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.
The call operator which will generate a return value.
- Returns:
- The generated return value
using return_type = typename unqualified_type< R >::type
Get the unqualified version of return type.