cprover
lazy.h
Go to the documentation of this file.
1/*******************************************************************\
2
3Module: Util
4
5Author: 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 <util/optional.h>
14
15template <typename valuet>
16class lazyt
17{
18public:
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
36private:
38 std::function<valuet()> evaluation_function;
39
40 explicit lazyt(std::function<valuet()> fun)
41 : evaluation_function(std::move(fun))
42 {
43 }
44};
45
48template <typename funt>
49auto 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
optionalt< 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
STL namespace.
nonstd::optional< T > optionalt
Definition: optional.h:35