CMS 3D CMS Logo

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
writedatasetfile.args
args
Definition: writedatasetfile.py:18
LazyResult::evaluateImpl
void evaluateImpl(std::index_sequence< ArgIndices... >)
Definition: LazyResult.h:63
LazyResult::args_
std::tuple< Args const &... > args_
Definition: LazyResult.h:72
LazyResult::evaluate
void evaluate()
Definition: LazyResult.h:60
LazyResult::evaluated_
bool evaluated_
Definition: LazyResult.h:70
gainCalibHelper::gainCalibPI::type
type
Definition: SiPixelGainCalibHelper.h:39
LazyResult::value
Result const & value()
Definition: LazyResult.h:52
LazyResult::Result
typename std::invoke_result< Func, Args... >::type Result
Definition: LazyResult.h:48
LazyResult::result_
Result result_
Definition: LazyResult.h:73
TrackCollections2monitor_cff.func
func
Definition: TrackCollections2monitor_cff.py:359
LazyResult::func_
Func func_
Definition: LazyResult.h:71
LazyResult
Definition: LazyResult.h:46
LazyResult::LazyResult
LazyResult(Func func, Args const &... args)
Definition: LazyResult.h:50