diff options
author | Roman Smrž <roman.smrz@seznam.cz> | 2021-05-09 15:20:30 +0200 |
---|---|---|
committer | Roman Smrž <roman.smrz@seznam.cz> | 2021-05-09 19:34:01 +0200 |
commit | d563500c915de2f0a652513af03f101c99715db3 (patch) | |
tree | 735c0dd4d3b62737170b5328b455b74ea60f3822 /include | |
parent | 00e52ec8ee38a85737c093d90ebfba5069829608 (diff) |
SharedType: type trait instead of member functions and typedef
Diffstat (limited to 'include')
-rw-r--r-- | include/erebos/identity.h | 8 | ||||
-rw-r--r-- | include/erebos/state.h | 50 |
2 files changed, 39 insertions, 19 deletions
diff --git a/include/erebos/identity.h b/include/erebos/identity.h index f7b17b8..d0b60d5 100644 --- a/include/erebos/identity.h +++ b/include/erebos/identity.h @@ -1,9 +1,12 @@ #pragma once +#include <erebos/state.h> #include <erebos/storage.h> namespace erebos { +using std::optional; + class Identity { public: @@ -14,6 +17,7 @@ public: static std::optional<Identity> load(const Ref &); static std::optional<Identity> load(const std::vector<Ref> &); + std::vector<Ref> store() const; std::vector<Ref> store(const Storage & st) const; std::optional<std::string> name() const; @@ -47,8 +51,6 @@ public: static Builder create(const Storage &); Builder modify() const; - static const UUID sharedTypeId; - private: struct Priv; std::shared_ptr<const Priv> p; @@ -56,4 +58,6 @@ private: Identity(std::shared_ptr<const Priv> && p); }; +DECLARE_SHARED_TYPE(optional<Identity>) + } 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 <erebos/identity.h> +#include <erebos/storage.h> #include <erebos/uuid.h> +#include <memory> #include <optional> +#include <vector> namespace erebos { @@ -11,6 +13,26 @@ using std::optional; using std::shared_ptr; using std::vector; +template<typename T> +struct SharedType +{ + static const UUID id; + static T(*const load)(const vector<Ref> &); + static vector<Ref>(*const store)(const T &); +}; + +#define DECLARE_SHARED_TYPE(T) \ + template<> const UUID erebos::SharedType<T>::id; \ + template<> T(*const erebos::SharedType<T>::load)(const std::vector<erebos::Ref> &); \ + template<> std::vector<erebos::Ref>(*const erebos::SharedType<T>::store) (const T &); + +#define DEFINE_SHARED_TYPE(T, id_, load_, store_) \ + template<> const UUID erebos::SharedType<T>::id { id_ }; \ + template<> T(*const erebos::SharedType<T>::load)(const vector<Ref> &) { load_ }; \ + template<> std::vector<erebos::Ref>(*const erebos::SharedType<T>::store) (const T &) { store_ }; + +class Identity; + class LocalState { public: @@ -24,11 +46,8 @@ public: 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)); } + template<class T> T shared() const; + template<class T> LocalState shared(const T & x) const; vector<Ref> sharedRefs() const; LocalState sharedRefAdd(const Ref &) const; @@ -46,7 +65,7 @@ private: class SharedState { public: - template<class T> optional<T> get() const; + template<class T> T get() const; template<typename T> static T lens(const SharedState &); bool operator==(const SharedState &) const; @@ -62,30 +81,27 @@ private: }; template<class T> -optional<T> LocalState::shared() const +T LocalState::shared() const { - return T::load(lookupShared(T::sharedTypeId)); + return SharedType<T>::load(lookupShared(SharedType<T>::id)); } template<class T> -LocalState LocalState::shared(const vector<Stored<T>> & v) const +LocalState LocalState::shared(const T & x) const { - vector<Ref> refs; - for (const auto & x : v) - refs.push_back(x.ref()); - return updateShared(T::sharedTypeId, refs); + return updateShared(SharedType<T>::id, SharedType<T>::store(x)); } template<class T> -optional<T> SharedState::get() const +T SharedState::get() const { - return T::load(lookup(T::sharedTypeId)); + return SharedType<T>::load(lookup(SharedType<T>::id)); } template<class T> T SharedState::lens(const SharedState & x) { - return T::value_type::load(x.lookup(T::value_type::sharedTypeId)); + return x.get<T>(); } } |