From 90021e1d335efac1017562c1d5dee43e99580319 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roman=20Smr=C5=BE?= Date: Wed, 22 Jul 2020 22:13:33 +0200 Subject: Local and shared state --- include/erebos/identity.h | 10 ++++++++- include/erebos/state.h | 55 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 include/erebos/state.h (limited to 'include') 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 load(const Ref &); static std::optional load(const std::vector &); + std::vector store(const Storage & st) const; std::optional name() const; std::optional 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 p; + std::shared_ptr p; Identity(const Priv * p); Identity(std::shared_ptr && 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 +#include + +#include + +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() const; + LocalState identity(const Identity &) const; + + template optional shared() const; + template LocalState shared(const vector> &) const; + template LocalState shared(const Stored & x) const { return shared({ x }); }; + template LocalState shared(const Storage & st, const T & x) + { return updateShared(T::sharedTypeId, x.store(st)); } + +private: + vector lookupShared(UUID) const; + LocalState updateShared(UUID, const vector &) const; + + struct Priv; + std::shared_ptr p; +}; + +template +optional LocalState::shared() const +{ + return T::load(lookupShared(T::sharedTypeId)); +} + +template +LocalState LocalState::shared(const vector> & v) const +{ + vector refs; + for (const auto x : v) + refs.push_back(x.ref()); + return updateShared(T::sharedTypeId, refs); +} + +} -- cgit v1.2.3