CBMC
lazy.h
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: Util
4 
5 Author: Romain Brenguier, romain.brenguier@diffblue.com
6 
7 \*******************************************************************/
8 
9 #ifndef CPROVER_UTIL_LAZY_H
10 #define CPROVER_UTIL_LAZY_H
11 
12 #include <functional>
13 #include <optional>
14 
15 template <typename valuet>
16 class lazyt
17 {
18 public:
21  static lazyt from_fun(std::function<valuet()> fun)
22  {
23  return lazyt{std::move(fun)};
24  }
25 
28  valuet force()
29  {
30  if(value)
31  return *value;
33  return *value;
34  }
35 
36 private:
37  std::optional<valuet> value;
38  std::function<valuet()> evaluation_function;
39 
40  explicit lazyt(std::function<valuet()> fun)
41  : evaluation_function(std::move(fun))
42  {
43  }
44 };
45 
48 template <typename funt>
49 auto lazy(funt fun) -> lazyt<decltype(fun())>
50 {
51  return lazyt<decltype(fun())>::from_fun(std::move(fun));
52 }
53 
54 #endif // CPROVER_UTIL_LAZY_H
Definition: lazy.h:17
std::optional< valuet > value
Definition: lazy.h:37
static lazyt from_fun(std::function< valuet()> fun)
Delay the computation of fun to the next time the force method is called.
Definition: lazy.h:21
std::function< valuet()> evaluation_function
Definition: lazy.h:38
valuet force()
Force the computation of the value.
Definition: lazy.h:28
lazyt(std::function< valuet()> fun)
Definition: lazy.h:40
auto lazy(funt fun) -> lazyt< decltype(fun())>
Delay the computation of fun to the next time the force method is called.
Definition: lazy.h:49