diff options
author | Roman Smrž <roman.smrz@seznam.cz> | 2020-07-22 22:13:33 +0200 |
---|---|---|
committer | Roman Smrž <roman.smrz@seznam.cz> | 2020-07-22 22:40:34 +0200 |
commit | 90021e1d335efac1017562c1d5dee43e99580319 (patch) | |
tree | 2a6c70ab6664085f5a86f210ac63cb064c9cb5c8 /include/erebos/state.h | |
parent | 8ac21c24e49bc3702c55d1c796f969f1d1f6128b (diff) |
Local and shared state
Diffstat (limited to 'include/erebos/state.h')
-rw-r--r-- | include/erebos/state.h | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/include/erebos/state.h b/include/erebos/state.h new file mode 100644 index 0000000..543e03c --- /dev/null +++ b/include/erebos/state.h @@ -0,0 +1,55 @@ +#pragma once + +#include <erebos/identity.h> +#include <erebos/uuid.h> + +#include <optional> + +namespace erebos { + +using std::optional; +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)); } + +private: + vector<Ref> lookupShared(UUID) const; + LocalState updateShared(UUID, const vector<Ref> &) const; + + struct Priv; + std::shared_ptr<Priv> p; +}; + +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); +} + +} |