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 | |
parent | 8ac21c24e49bc3702c55d1c796f969f1d1f6128b (diff) |
Local and shared state
Diffstat (limited to 'include/erebos')
-rw-r--r-- | include/erebos/identity.h | 10 | ||||
-rw-r--r-- | include/erebos/state.h | 55 |
2 files changed, 64 insertions, 1 deletions
diff --git a/include/erebos/identity.h b/include/erebos/identity.h index 7ba3f29..66a4afb 100644 --- a/include/erebos/identity.h +++ b/include/erebos/identity.h @@ -7,8 +7,14 @@ namespace erebos { class Identity { public: + Identity(const Identity &) = default; + Identity(Identity &&) = default; + Identity & operator=(const Identity &) = default; + Identity & operator=(Identity &&) = default; + static std::optional<Identity> load(const Ref &); static std::optional<Identity> load(const std::vector<Ref> &); + std::vector<Ref> store(const Storage & st) const; std::optional<std::string> name() const; std::optional<Identity> owner() const; @@ -38,9 +44,11 @@ public: static Builder create(const Storage &); Builder modify() const; + static const UUID sharedTypeId; + private: struct Priv; - const std::shared_ptr<const Priv> p; + std::shared_ptr<const Priv> p; Identity(const Priv * p); Identity(std::shared_ptr<const Priv> && p); }; 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); +} + +} |