diff --git a/furvm/include/furvm/context.hpp b/furvm/include/furvm/context.hpp index 35d41e5..55b5ddf 100644 --- a/furvm/include/furvm/context.hpp +++ b/furvm/include/furvm/context.hpp @@ -94,11 +94,6 @@ public: } thing_allocator thing_alloc() const { return m_thingAllocator; } -public: - /** - * @brief Removes unreferenced things from the thing list. - */ - void collect(); private: handle_container m_modules; handle_container m_things; diff --git a/furvm/include/furvm/handle.hpp b/furvm/include/furvm/handle.hpp index bf7c84a..097d978 100644 --- a/furvm/include/furvm/handle.hpp +++ b/furvm/include/furvm/handle.hpp @@ -5,6 +5,7 @@ #include "furvm/fwd.hpp" #include +#include #include #include #include @@ -24,20 +25,24 @@ public: using id_type = Id; using refcount_type = std::uint32_t; public: - template - refcount_header(IdFwd&& id, refcount_type refCount) - : m_id(std::forward(id)), m_refCount(refCount) {} + template + refcount_header(IdFwd&& id, refcount_type refCount, Func&& onRelease) + : m_id(std::forward(id)), m_refCount(refCount), m_onRelease(std::forward(onRelease)) {} public: refcount_type reference_count() const { return m_refCount; } void acquire() { ++m_refCount; } - void release() { --m_refCount; } + void release() { + --m_refCount; + if (m_refCount == 0) m_onRelease(m_id); + } public: id_type id() const { return m_id; } private: - id_type m_id; - std::atomic m_refCount; + id_type m_id; + std::atomic m_refCount; + std::function m_onRelease; }; template @@ -153,7 +158,7 @@ public: id_type idFwd = std::forward(id); if (auto it = m_pairs.find(idFwd); it != m_pairs.end()) delete it->second; auto pair = new pair_type(std::piecewise_construct, - std::forward_as_tuple(idFwd, 0), + std::forward_as_tuple(idFwd, 0, [&](const id_type& id) { erase(id); }), std::forward_as_tuple(std::forward(args)...)); m_pairs.emplace(std::move(idFwd), pair); return { pair }; @@ -209,7 +214,7 @@ public: } pair_type* newPair = new pair_type(std::piecewise_construct, - std::forward_as_tuple(id, 0), + std::forward_as_tuple(id, 0, [&](const id_type& id) { erase(id); }), std::forward_as_tuple(std::forward(args)...)); m_pairs[id] = newPair;