From 567156b05183cc63aedbf57c03f26e0eaf43a39e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roman=20Smr=C5=BE?= Date: Tue, 27 Apr 2021 22:59:52 +0200 Subject: SharedState type and lens --- include/erebos/state.h | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) (limited to 'include/erebos') diff --git a/include/erebos/state.h b/include/erebos/state.h index 7060f22..cfa9532 100644 --- a/include/erebos/state.h +++ b/include/erebos/state.h @@ -8,6 +8,7 @@ namespace erebos { using std::optional; +using std::shared_ptr; using std::vector; class LocalState @@ -42,6 +43,24 @@ private: std::shared_ptr p; }; +class SharedState +{ +public: + template optional get() const; + template static T lens(const SharedState &); + + bool operator==(const SharedState &) const; + bool operator!=(const SharedState &) const; + +private: + vector lookup(UUID) const; + + struct Priv; + SharedState(shared_ptr && p): p(std::move(p)) {} + shared_ptr p; + friend class LocalState; +}; + template optional LocalState::shared() const { @@ -57,4 +76,16 @@ LocalState LocalState::shared(const vector> & v) const return updateShared(T::sharedTypeId, refs); } +template +optional SharedState::get() const +{ + return T::load(lookup(T::sharedTypeId)); +} + +template +T SharedState::lens(const SharedState & x) +{ + return T::value_type::load(x.lookup(T::value_type::sharedTypeId)); +} + } -- cgit v1.2.3