diff options
author | Roman Smrž <roman.smrz@seznam.cz> | 2020-04-16 22:42:01 +0200 |
---|---|---|
committer | Roman Smrž <roman.smrz@seznam.cz> | 2020-04-16 22:42:01 +0200 |
commit | a4db4f377b6d0d3c91aba8bc4a2392845c2ef18e (patch) | |
tree | 3297fbfc4ed0bf13c3e8acaff95fa65cfc104fe4 | |
parent | 6cf5244bc514042fe419fffe1cd26a7f5e3c778f (diff) |
Storage: lazy loading of Stored value
-rw-r--r-- | include/erebos/storage.h | 18 | ||||
-rw-r--r-- | src/storage.h | 1 |
2 files changed, 11 insertions, 8 deletions
diff --git a/include/erebos/storage.h b/include/erebos/storage.h index d46c056..09dc481 100644 --- a/include/erebos/storage.h +++ b/include/erebos/storage.h @@ -7,6 +7,7 @@ #include <array> #include <cstring> #include <filesystem> +#include <future> #include <memory> #include <optional> #include <string> @@ -313,7 +314,7 @@ std::optional<Stored<T>> RecordT<S>::Item::as() const template<typename T> class Stored { - Stored(Ref ref, std::shared_ptr<T> val): mref(ref), mval(val) {} + Stored(Ref ref, std::future<T> && val): mref(ref), mval(std::move(val)) {} friend class Storage; public: Stored(const Stored &) = default; @@ -337,30 +338,33 @@ public: bool operator>=(const Stored<T> & other) const { return mref.digest() >= other.mref.digest(); } - const T & operator*() const { return *mval; } - const T * operator->() const { return mval.get(); } + const T & operator*() const { return mval.get(); } + const T * operator->() const { return &mval.get(); } std::vector<Stored<T>> previous() const; bool precedes(const Stored<T> &) const; const Ref & ref() const { return mref; } - const std::shared_ptr<T> & value() const { return mval; } private: Ref mref; - std::shared_ptr<T> mval; + std::shared_future<T> mval; }; template<typename T> Stored<T> Storage::store(const T & val) const { - return Stored(val.store(*this), std::make_shared<T>(val)); + return Stored(val.store(*this), std::async(std::launch::deferred, [val] { + return val; + })); } template<typename T> Stored<T> Stored<T>::load(const Ref & ref) { - return Stored(ref, std::make_shared<T>(T::load(ref))); + return Stored(ref, std::async(std::launch::deferred, [ref] { + return T::load(ref); + })); } template<typename T> diff --git a/src/storage.h b/src/storage.h index c67be22..1635d40 100644 --- a/src/storage.h +++ b/src/storage.h @@ -2,7 +2,6 @@ #include "erebos/storage.h" -#include <future> #include <unordered_map> #include <unordered_set> |