From a4db4f377b6d0d3c91aba8bc4a2392845c2ef18e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Roman=20Smr=C5=BE?= <roman.smrz@seznam.cz>
Date: Thu, 16 Apr 2020 22:42:01 +0200
Subject: Storage: lazy loading of Stored value

---
 include/erebos/storage.h | 18 +++++++++++-------
 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>
 
-- 
cgit v1.2.3