-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandle.h
62 lines (47 loc) · 1.49 KB
/
handle.h
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
#ifndef TOG_HANDLE_H
#define TOG_HANDLE_H
#include <memory>
#include <string>
namespace tog {
// TODO: use C++20 concepts to enforce that T is a TogObject
template <class T>
// A handle represents a TogObject that may have not yet been loaded into
// memory.
class Handle {
public:
Handle() = default;
virtual ~Handle() = default;
// Constructs an unresolved handle to an existing object with a given hash.
Handle(std::string hash)
: _hash{std::move(hash)}, _dirty{false}, _object{nullptr} {};
// Constructs a resolved handle to an existing object with a given hash.
Handle(std::string hash, std::shared_ptr<T> object, bool dirty)
: _hash{std::move(hash)}, _dirty{dirty}, _object{std::move(object)} {};
void resolve(std::shared_ptr<T> object) {
_object = std::move(object);
_dirty = false;
}
bool dirty() const {
return _dirty;
}
bool resolved() const {
return _object != nullptr;
}
const std::string& hash() const {
return _hash;
}
const std::shared_ptr<T>& object() const {
return _object;
}
private:
// The hash of the object referenced by this handle.
std::string _hash;
// Indicates whether the object referenced by this handle needs to be
// persisted on disk.
bool _dirty;
// The object referenced by this handle. This may be nullptr if the handle
// is not resolved yet.
std::shared_ptr<T> _object;
};
} // namespace tog
#endif // TOG_REF_H