From d563500c915de2f0a652513af03f101c99715db3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roman=20Smr=C5=BE?= Date: Sun, 9 May 2021 15:20:30 +0200 Subject: SharedType: type trait instead of member functions and typedef --- include/erebos/state.h | 50 +++++++++++++++++++++++++++++++++----------------- 1 file changed, 33 insertions(+), 17 deletions(-) (limited to 'include/erebos/state.h') diff --git a/include/erebos/state.h b/include/erebos/state.h index cfa9532..16be464 100644 --- a/include/erebos/state.h +++ b/include/erebos/state.h @@ -1,9 +1,11 @@ #pragma once -#include +#include #include +#include #include +#include namespace erebos { @@ -11,6 +13,26 @@ using std::optional; using std::shared_ptr; using std::vector; +template +struct SharedType +{ + static const UUID id; + static T(*const load)(const vector &); + static vector(*const store)(const T &); +}; + +#define DECLARE_SHARED_TYPE(T) \ + template<> const UUID erebos::SharedType::id; \ + template<> T(*const erebos::SharedType::load)(const std::vector &); \ + template<> std::vector(*const erebos::SharedType::store) (const T &); + +#define DEFINE_SHARED_TYPE(T, id_, load_, store_) \ + template<> const UUID erebos::SharedType::id { id_ }; \ + template<> T(*const erebos::SharedType::load)(const vector &) { load_ }; \ + template<> std::vector(*const erebos::SharedType::store) (const T &) { store_ }; + +class Identity; + class LocalState { public: @@ -24,11 +46,8 @@ public: 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)); } + template T shared() const; + template LocalState shared(const T & x) const; vector sharedRefs() const; LocalState sharedRefAdd(const Ref &) const; @@ -46,7 +65,7 @@ private: class SharedState { public: - template optional get() const; + template T get() const; template static T lens(const SharedState &); bool operator==(const SharedState &) const; @@ -62,30 +81,27 @@ private: }; template -optional LocalState::shared() const +T LocalState::shared() const { - return T::load(lookupShared(T::sharedTypeId)); + return SharedType::load(lookupShared(SharedType::id)); } template -LocalState LocalState::shared(const vector> & v) const +LocalState LocalState::shared(const T & x) const { - vector refs; - for (const auto & x : v) - refs.push_back(x.ref()); - return updateShared(T::sharedTypeId, refs); + return updateShared(SharedType::id, SharedType::store(x)); } template -optional SharedState::get() const +T SharedState::get() const { - return T::load(lookup(T::sharedTypeId)); + return SharedType::load(lookup(SharedType::id)); } template T SharedState::lens(const SharedState & x) { - return T::value_type::load(x.lookup(T::value_type::sharedTypeId)); + return x.get(); } } -- cgit v1.2.3