#include "CommonTools/Utils/interface/LazyResult.h"
Public Types | |
using | Result = typename std::invoke_result< Func, Args...>::type |
Public Member Functions | |
LazyResult (Func func, Args const &...args) | |
Result const & | value () |
Private Member Functions | |
void | evaluate () |
template<std::size_t... ArgIndices> | |
void | evaluateImpl (std::index_sequence< ArgIndices...>) |
Private Attributes | |
std::tuple< Args const &...> | args_ |
bool | evaluated_ = false |
Func | func_ |
Result | result_ |
Description: Wrapper around a function call for lazy execution.
Usage: example: lazy addition auto result = LazyResult(std::plus<int>, x, x); std::cout << result.value() << std::endl;
Notes: The arguments for the delayed call are stored by reference (watch their lifetime). The overhead in memory compared to just storing the result is small: one reference per argument, one bool flag and a function pointer (on my system: 1 byte for lambda function, 8 bytes for global function and 16 bytes for member function due to possible index to virtual table).
Implementation:
For the Args... we explicitly add const& (also in the the args_ tuple). Otherwise, the arguments will be stored by value which comes with too much overhead. This implies that the lifetime of the arguments passed to LazyResult neet to live longer than the LazyResult instance. Function pointers are small, so no need for const& to the Func. An alternative to using a ::value() member function to get the result could be a cast operator: operator Result const &(). This might be pretty because the result is automatically evaluated the first time you try to bind it to a Result const &. I think this would however be too implicit and dangerous.
Definition at line 46 of file LazyResult.h.
using LazyResult< Func, Args >::Result = typename std::invoke_result<Func, Args...>::type |
Definition at line 48 of file LazyResult.h.
|
inline |
Definition at line 50 of file LazyResult.h.
|
inlineprivate |
Definition at line 60 of file LazyResult.h.
References LazyResult< Func, Args >::evaluateImpl().
Referenced by LazyResult< Func, Args >::value().
|
inlineprivate |
Definition at line 63 of file LazyResult.h.
References LazyResult< Func, Args >::args_, LazyResult< Func, Args >::evaluated_, LazyResult< Func, Args >::func_, and LazyResult< Func, Args >::result_.
Referenced by LazyResult< Func, Args >::evaluate().
|
inline |
Definition at line 52 of file LazyResult.h.
References LazyResult< Func, Args >::evaluate(), LazyResult< Func, Args >::evaluated_, and LazyResult< Func, Args >::result_.
Referenced by Types.int32::__nonzero__(), Types.uint32::__nonzero__(), Types.int64::__nonzero__(), Types.uint64::__nonzero__(), Types.double::__nonzero__(), Types.bool::__nonzero__(), Types.string::__nonzero__(), average.Average::average(), Types.string::configValue(), Types.FileInPath::configValue(), Mixins.UsingBlock::dumpPython(), Mixins.UsingBlock::insertInto(), Types.int32::insertInto(), Types.uint32::insertInto(), Types.int64::insertInto(), Types.uint64::insertInto(), Types.double::insertInto(), Types.bool::insertInto(), Types.string::insertInto(), Types.FileInPath::insertInto(), Types.vint32::insertInto(), Types.vuint32::insertInto(), Types.vint64::insertInto(), Types.vuint64::insertInto(), Types.vdouble::insertInto(), Types.vbool::insertInto(), and Types.vstring::insertInto().
|
private |
Definition at line 72 of file LazyResult.h.
Referenced by LazyResult< Func, Args >::evaluateImpl(), and batchmanager.BatchManager::ParseOptions().
|
private |
Definition at line 70 of file LazyResult.h.
Referenced by LazyResult< Func, Args >::evaluateImpl(), and LazyResult< Func, Args >::value().
|
private |
Definition at line 71 of file LazyResult.h.
Referenced by LazyResult< Func, Args >::evaluateImpl().
|
private |
Definition at line 73 of file LazyResult.h.
Referenced by LazyResult< Func, Args >::evaluateImpl(), and LazyResult< Func, Args >::value().