CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
LazyResult.h
Go to the documentation of this file.
1 #ifndef CommonTools_Utils_LazyResult_h
2 #define CommonTools_Utils_LazyResult_h
3 // -*- C++ -*-
4 //
5 // Package: CommonTools/Utils
6 // Class : LazyResult
7 //
37 //
38 // Original Author: Jonas Rembser
39 // Created: Mon, 14 Jun 2020 00:00:00 GMT
40 //
41 
42 #include <tuple>
43 #include <type_traits>
44 
45 template <class Func, class... Args>
46 class LazyResult {
47 public:
48  using Result = typename std::invoke_result<Func, Args...>::type;
49 
50  LazyResult(Func func, Args const&... args) : func_(func), args_(args...) {}
51 
52  Result const& value() {
53  if (!evaluated_) {
54  evaluate();
55  }
56  return result_;
57  }
58 
59 private:
60  void evaluate() { evaluateImpl(std::make_index_sequence<sizeof...(Args)>{}); }
61 
62  template <std::size_t... ArgIndices>
63  void evaluateImpl(std::index_sequence<ArgIndices...>) {
64  result_ = func_(std::get<ArgIndices>(args_)...);
65  evaluated_ = true;
66  }
67 
68  // having evaluated_ and the potentially small func_ together might
69  // save alignemnt bits (e.g. a lambda function is just 1 byte)
70  bool evaluated_ = false;
71  Func func_;
72  std::tuple<Args const&...> args_;
74 };
75 
76 #endif
Func func_
Definition: LazyResult.h:71
typename std::invoke_result< Func, Args...>::type Result
Definition: LazyResult.h:48
void evaluateImpl(std::index_sequence< ArgIndices...>)
Definition: LazyResult.h:63
uint32_t T const *__restrict__ uint32_t const *__restrict__ int32_t int Histo::index_type cudaStream_t Func __host__ __device__ V int Func func
Result const & value()
Definition: LazyResult.h:52
Result result_
Definition: LazyResult.h:73
LazyResult(Func func, Args const &...args)
Definition: LazyResult.h:50
std::tuple< Args const &...> args_
Definition: LazyResult.h:72
void evaluate()
Definition: LazyResult.h:60
bool evaluated_
Definition: LazyResult.h:70