summaryrefslogtreecommitdiff
path: root/include/erebos/state.h
blob: cfa953264789d7ed7fe4fc052370ccfb781450ae (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
#pragma once

#include <erebos/identity.h>
#include <erebos/uuid.h>

#include <optional>

namespace erebos {

using std::optional;
using std::shared_ptr;
using std::vector;

class LocalState
{
public:
	LocalState();
	explicit LocalState(const Ref &);
	static LocalState load(const Ref & ref) { return LocalState(ref); }
	Ref store(const Storage &) const;

	static const UUID headTypeId;

	const optional<Identity> & identity() const;
	LocalState identity(const Identity &) const;

	template<class T> optional<T> shared() const;
	template<class T> LocalState shared(const vector<Stored<T>> &) const;
	template<class T> LocalState shared(const Stored<T> & x) const { return shared({ x }); };
	template<class T> LocalState shared(const Storage & st, const T & x)
	{ return updateShared(T::sharedTypeId, x.store(st)); }

	vector<Ref> sharedRefs() const;
	LocalState sharedRefAdd(const Ref &) const;

	template<typename T> static T lens(const LocalState &);

private:
	vector<Ref> lookupShared(UUID) const;
	LocalState updateShared(UUID, const vector<Ref> &) const;

	struct Priv;
	std::shared_ptr<Priv> p;
};

class SharedState
{
public:
	template<class T> optional<T> get() const;
	template<typename T> static T lens(const SharedState &);

	bool operator==(const SharedState &) const;
	bool operator!=(const SharedState &) const;

private:
	vector<Ref> lookup(UUID) const;

	struct Priv;
	SharedState(shared_ptr<Priv> && p): p(std::move(p)) {}
	shared_ptr<Priv> p;
	friend class LocalState;
};

template<class T>
optional<T> LocalState::shared() const
{
	return T::load(lookupShared(T::sharedTypeId));
}

template<class T>
LocalState LocalState::shared(const vector<Stored<T>> & v) const
{
	vector<Ref> refs;
	for (const auto & x : v)
		refs.push_back(x.ref());
	return updateShared(T::sharedTypeId, refs);
}

template<class T>
optional<T> SharedState::get() const
{
	return T::load(lookup(T::sharedTypeId));
}

template<class T>
T SharedState::lens(const SharedState & x)
{
	return T::value_type::load(x.lookup(T::value_type::sharedTypeId));
}

}