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/state.h | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 include/erebos/state.h (limited to 'include/erebos/state.h') 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