forked from jancarlsson/snarkfront
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLazy.hpp
45 lines (36 loc) · 825 Bytes
/
Lazy.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#ifndef _SNARKFRONT_LAZY_HPP_
#define _SNARKFRONT_LAZY_HPP_
namespace snarkfront {
////////////////////////////////////////////////////////////////////////////////
// laziness box, primary purpose is to defer creation of variables
//
template <typename T, typename A>
class Lazy
{
public:
// not lazy, object already created
Lazy(const T& a)
: m_obj(a),
m_init(true)
{}
// lazy, defer object creation until referenced
Lazy(const A& a)
: m_arg(a),
m_init(false)
{}
// dereferencing is unboxing
const T& operator* () {
if (! m_init) {
// unboxing
m_init = true;
m_obj = T(m_arg);
}
return m_obj;
}
private:
T m_obj;
A m_arg;
bool m_init;
};
} // namespace snarkfront
#endif