CMS 3D CMS Logo

List of all members | Public Types | Public Member Functions | Private Member Functions | Private Attributes
LazyResult< Func, Args > Class Template Reference

#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_
 

Detailed Description

template<class Func, class... Args>
class LazyResult< Func, Args >

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.

Member Typedef Documentation

◆ Result

template<class Func , class... Args>
using LazyResult< Func, Args >::Result = typename std::invoke_result<Func, Args...>::type

Definition at line 48 of file LazyResult.h.

Constructor & Destructor Documentation

◆ LazyResult()

template<class Func , class... Args>
LazyResult< Func, Args >::LazyResult ( Func  func,
Args const &...  args 
)
inline

Definition at line 50 of file LazyResult.h.

50 : func_(func), args_(args...) {}
Func func_
Definition: LazyResult.h:71
std::tuple< Args const &... > args_
Definition: LazyResult.h:72

Member Function Documentation

◆ evaluate()

template<class Func , class... Args>
void LazyResult< Func, Args >::evaluate ( )
inlineprivate

Definition at line 60 of file LazyResult.h.

References LazyResult< Func, Args >::evaluateImpl().

Referenced by LazyResult< Func, Args >::value().

60 { evaluateImpl(std::make_index_sequence<sizeof...(Args)>{}); }
void evaluateImpl(std::index_sequence< ArgIndices... >)
Definition: LazyResult.h:63

◆ evaluateImpl()

template<class Func , class... Args>
template<std::size_t... ArgIndices>
void LazyResult< Func, Args >::evaluateImpl ( std::index_sequence< ArgIndices... >  )
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().

63  {
64  result_ = func_(std::get<ArgIndices>(args_)...);
65  evaluated_ = true;
66  }
Func func_
Definition: LazyResult.h:71
Result result_
Definition: LazyResult.h:73
std::tuple< Args const &... > args_
Definition: LazyResult.h:72
bool evaluated_
Definition: LazyResult.h:70

◆ value()

template<class Func , class... Args>
Result const& LazyResult< Func, Args >::value ( )
inline

Member Data Documentation

◆ args_

template<class Func , class... Args>
std::tuple<Args const&...> LazyResult< Func, Args >::args_
private

◆ evaluated_

template<class Func , class... Args>
bool LazyResult< Func, Args >::evaluated_ = false
private

◆ func_

template<class Func , class... Args>
Func LazyResult< Func, Args >::func_
private

Definition at line 71 of file LazyResult.h.

Referenced by LazyResult< Func, Args >::evaluateImpl().

◆ result_

template<class Func , class... Args>
Result LazyResult< Func, Args >::result_
private