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 | |
parent | 00e52ec8ee38a85737c093d90ebfba5069829608 (diff) |
SharedType: type trait instead of member functions and typedef
-rw-r--r-- | include/erebos/identity.h | 8 | ||||
-rw-r--r-- | include/erebos/state.h | 50 | ||||
-rw-r--r-- | src/identity.cpp | 20 | ||||
-rw-r--r-- | src/state.h | 1 | ||||
-rw-r--r-- | src/sync.cpp | 1 |
5 files changed, 60 insertions, 20 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>(); } } diff --git a/src/identity.cpp b/src/identity.cpp index a4c12f2..f55f6dd 100644 --- a/src/identity.cpp +++ b/src/identity.cpp @@ -1,5 +1,7 @@ #include "identity.h" +#include <erebos/state.h> + #include <algorithm> #include <set> #include <stdexcept> @@ -11,7 +13,14 @@ using std::nullopt; using std::runtime_error; using std::set; -const UUID Identity::sharedTypeId { "0c6c1fe0-f2d7-4891-926b-c332449f7871" }; +DEFINE_SHARED_TYPE(optional<Identity>, + "0c6c1fe0-f2d7-4891-926b-c332449f7871", + &Identity::load, + [](const optional<Identity> & id) { + if (id) + return id->store(); + return vector<Ref>(); + }) Identity::Identity(const Priv * p): p(p) {} Identity::Identity(shared_ptr<const Priv> && p): p(std::move(p)) {} @@ -34,6 +43,15 @@ optional<Identity> Identity::load(const vector<Ref> & refs) return nullopt; } +vector<Ref> Identity::store() const +{ + vector<Ref> res; + res.reserve(p->data.size()); + for (const auto & x : p->data) + res.push_back(x.ref()); + return res; +} + vector<Ref> Identity::store(const Storage & st) const { vector<Ref> res; diff --git a/src/state.h b/src/state.h index 91dea94..397a906 100644 --- a/src/state.h +++ b/src/state.h @@ -1,6 +1,7 @@ #pragma once #include <erebos/state.h> +#include <erebos/identity.h> #include "pubkey.h" diff --git a/src/sync.cpp b/src/sync.cpp index cf423ac..85fed96 100644 --- a/src/sync.cpp +++ b/src/sync.cpp @@ -1,5 +1,6 @@ #include <erebos/sync.h> +#include <erebos/identity.h> #include <erebos/network.h> using namespace erebos; |