diff options
author | Roman Smrž <roman.smrz@seznam.cz> | 2021-02-19 22:17:22 +0100 |
---|---|---|
committer | Roman Smrž <roman.smrz@seznam.cz> | 2021-02-19 22:17:22 +0100 |
commit | 52db636c108ab0a16ba0ccf8df55cf28142a230c (patch) | |
tree | 018deafe919cf338968a4e90ca53e1c587db04ae | |
parent | 5dc467310ddebeae8dcb6262f5499f37382711ab (diff) |
Storage: keep weak pointer to Storage in head watcher
-rw-r--r-- | include/erebos/storage.h | 10 | ||||
-rw-r--r-- | src/storage.cpp | 14 |
2 files changed, 13 insertions, 11 deletions
diff --git a/include/erebos/storage.h b/include/erebos/storage.h index 10ced57..75b8139 100644 --- a/include/erebos/storage.h +++ b/include/erebos/storage.h @@ -60,7 +60,7 @@ protected: friend erebos::PartialRef; struct Priv; const std::shared_ptr<const Priv> p; - PartialStorage(const std::shared_ptr<const Priv> p): p(p) {} + PartialStorage(const std::shared_ptr<const Priv> & p): p(p) {} }; class Storage : public PartialStorage @@ -102,7 +102,7 @@ protected: template<typename T> friend class Head; template<typename T> friend class WatchedHead; - Storage(const std::shared_ptr<const Priv> p): PartialStorage(p) {} + Storage(const std::shared_ptr<const Priv> & p): PartialStorage(p) {} std::optional<Ref> headRef(UUID type, UUID id) const; std::vector<std::tuple<UUID, Ref>> headRefs(UUID type) const; @@ -156,7 +156,7 @@ public: PartialRef & operator=(const PartialRef &) = default; PartialRef & operator=(PartialRef &&) = default; - static PartialRef create(PartialStorage, const Digest &); + static PartialRef create(const PartialStorage &, const Digest &); const Digest & digest() const; @@ -184,8 +184,8 @@ public: bool operator==(const Ref &) = delete; bool operator!=(const Ref &) = delete; - static std::optional<Ref> create(Storage, const Digest &); - static Ref zcreate(Storage); + static std::optional<Ref> create(const Storage &, const Digest &); + static Ref zcreate(const Storage &); explicit constexpr operator bool() const { return true; } const Object operator*() const; diff --git a/src/storage.cpp b/src/storage.cpp index f50b471..bd0beae 100644 --- a/src/storage.cpp +++ b/src/storage.cpp @@ -37,6 +37,7 @@ using std::shared_ptr; using std::string; using std::system_error; using std::to_string; +using std::weak_ptr; FilesystemStorage::FilesystemStorage(const fs::path & path): root(path) @@ -832,10 +833,11 @@ optional<Ref> Storage::updateHead(UUID type, UUID id, const Ref & old, const std int Storage::watchHead(UUID type, UUID wid, const std::function<void(const Ref &)> watcher) const { - return p->backend->watchHead(type, [this, wid, watcher] (UUID id, const Digest & dgst) { + return p->backend->watchHead(type, [wp = weak_ptr<const Priv>(p), wid, watcher] (UUID id, const Digest & dgst) { if (id == wid) - if (auto r = ref(dgst)) - watcher(*r); + if (auto p = wp.lock()) + if (auto r = Ref::create(Storage(p), dgst)) + watcher(*r); }); } @@ -889,7 +891,7 @@ Digest Digest::of(const vector<uint8_t> & content) } -PartialRef PartialRef::create(PartialStorage st, const Digest & digest) +PartialRef PartialRef::create(const PartialStorage & st, const Digest & digest) { auto p = new Priv { .storage = make_unique<PartialStorage>(st), @@ -926,7 +928,7 @@ const PartialStorage & PartialRef::storage() const return *p->storage; } -optional<Ref> Ref::create(Storage st, const Digest & digest) +optional<Ref> Ref::create(const Storage & st, const Digest & digest) { if (!st.p->backend->contains(digest)) return nullopt; @@ -939,7 +941,7 @@ optional<Ref> Ref::create(Storage st, const Digest & digest) return Ref(shared_ptr<Priv>(p)); } -Ref Ref::zcreate(Storage st) +Ref Ref::zcreate(const Storage & st) { auto p = new Priv { .storage = make_unique<PartialStorage>(st), |