diff options
author | Roman Smrž <roman.smrz@seznam.cz> | 2023-02-22 20:58:39 +0100 |
---|---|---|
committer | Roman Smrž <roman.smrz@seznam.cz> | 2023-03-22 21:53:54 +0100 |
commit | a13474cd11283ae648e0dcaa156ce9058df15aa1 (patch) | |
tree | b341997047a3f2e4f93c9a30d7225f212a5eafc1 /include | |
parent | c3dc649badc6b93a0ec052c3a3eefbe5761c7213 (diff) |
Pass weak pointer of HeadBhv to callback
Diffstat (limited to 'include')
-rw-r--r-- | include/erebos/storage.h | 31 |
1 files changed, 25 insertions, 6 deletions
diff --git a/include/erebos/storage.h b/include/erebos/storage.h index 01aeada..c506dfd 100644 --- a/include/erebos/storage.h +++ b/include/erebos/storage.h @@ -528,6 +528,7 @@ void filterAncestors(std::vector<Stored<T>> & xs) } template<class T> class WatchedHead; +template<class T> class HeadBhv; template<class T> class Head @@ -562,8 +563,13 @@ template<class T> class WatchedHead : public Head<T> { friend class Head<T>; + friend class HeadBhv<T>; + + WatchedHead(const Head<T> & h): + Head<T>(h), watcherId(-1) {} WatchedHead(const Head<T> & h, int watcherId): Head<T>(h), watcherId(watcherId) {} + int watcherId; public: @@ -588,15 +594,26 @@ class HeadBhv : public BhvSource<T> { public: HeadBhv(const Head<T> & head): - whead(head.watch([this] (const Head<T> & cur) { - BhvCurTime ctime; - whead = cur; - BhvImplBase::updated(ctime); - })) {} + whead(head) + {} T get(const BhvCurTime &, const std::monostate &) const { return *whead; } private: + friend class Head<T>; + + void init() + { + whead = whead.watch([wp = weak_ptr<BhvImplBase>(BhvImplBase::shared_from_this()), this] (const Head<T> & cur) { + // make sure this object still exists + if (auto ptr = wp.lock()) { + BhvCurTime ctime; + whead = cur; + BhvImplBase::updated(ctime); + } + }); + } + WatchedHead<T> whead; }; @@ -665,7 +682,9 @@ template<typename T> Bhv<T> Head<T>::behavior() const { auto cur = reload(); - return make_shared<HeadBhv<T>>(cur ? *cur : *this); + auto ret = make_shared<HeadBhv<T>>(cur ? *cur : *this); + ret->init(); + return ret; } template<class T> |