blob: 2aed50e388995c384ae4ec10c77d937c3dfd9800 (
plain)
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
#ifndef ADHOCUTIL_LAZYPOINTER_H
#define ADHOCUTIL_LAZYPOINTER_H
#include <boost/function.hpp>
#include <boost/variant.hpp>
#include <boost/intrusive_ptr.hpp>
template <typename T, typename P = boost::intrusive_ptr<T>>
class LazyPointer {
public:
typedef T element_type;
typedef P pointer_type;
typedef boost::function0<P> Factory;
typedef boost::variant<P, Factory> Source;
// Constructors
LazyPointer(const Factory & f) :
source(f)
{
}
LazyPointer(const P & p) :
source(p)
{
}
LazyPointer(T * p) :
source(P(p))
{
}
LazyPointer() :
source(P(NULL))
{
}
// Getters
operator P() const
{
return deref();
}
T * operator->() const
{
return get();
}
T & operator*() const
{
return *get();
}
T * get() const
{
return deref().get();
}
P deref() const
{
if (Factory * f = boost::get<Factory>(&source)) {
P p = (*f)();
source = p;
return p;
}
else {
return boost::get<P>(source);
}
}
bool operator!() const
{
return get() == nullptr;
}
operator bool() const
{
return get() != nullptr;
}
bool operator==(const P & o) const
{
return (deref() == o);
}
bool operator==(const T * o) const
{
return (deref().get() == o);
}
// Setters
LazyPointer<T, P> & operator=(const P & p)
{
source = p;
return *this;
}
LazyPointer<T, P> & operator=(T * t)
{
source = P(t);
return *this;
}
LazyPointer<T, P> & operator=(const Factory & f)
{
source = f;
return *this;
}
bool hasValue() const
{
return boost::get<P>(&source);
}
private:
mutable Source source;
};
namespace boost {
template <typename R, typename T, typename P>
R * dynamic_pointer_cast(const LazyPointer<T, P> & p) {
return dynamic_cast<R *>(p.get());
}
}
#endif
|