Add debug markers to react::value internal nodes to track their lifetime

This commit is contained in:
Nikita Lisitsa 2024-08-01 22:23:18 +03:00
parent 367f82a01f
commit 9f063218bf
3 changed files with 55 additions and 0 deletions

View file

@ -0,0 +1,15 @@
#pragma once
#include <psemek/react/value.hpp>
namespace psemek::react
{
template <typename T>
value<T> debug_marker(std::string marker, value<T> value)
{
value.node(detail::internal_tag{})->debug_marker = std::move(marker);
return value;
}
}

View file

@ -7,6 +7,7 @@
#include <memory> #include <memory>
#include <initializer_list> #include <initializer_list>
#include <type_traits> #include <type_traits>
#include <string>
namespace psemek::react namespace psemek::react
{ {
@ -20,6 +21,8 @@ namespace psemek::react
struct forever_tag struct forever_tag
{}; {};
void on_node_destroyed(std::string const & debug_marker);
template <typename T> template <typename T>
struct node struct node
{ {
@ -31,12 +34,19 @@ namespace psemek::react
std::optional<T> cached_value; std::optional<T> cached_value;
std::vector<std::shared_ptr<void>> subscriptions; std::vector<std::shared_ptr<void>> subscriptions;
std::string debug_marker;
T const & value() T const & value()
{ {
if (!cached_value) if (!cached_value)
cached_value.emplace(getter()); cached_value.emplace(getter());
return *cached_value; return *cached_value;
} }
~node()
{
on_node_destroyed(debug_marker);
}
}; };
template <> template <>
@ -47,8 +57,15 @@ namespace psemek::react
util::signal<> internal_signal; util::signal<> internal_signal;
util::signal<> external_signal; util::signal<> external_signal;
std::string debug_marker;
void value() void value()
{} {}
~node()
{
on_node_destroyed(debug_marker);
}
}; };
template <typename T> template <typename T>
@ -125,6 +142,11 @@ namespace psemek::react
return node_->internal_signal.subscribe(std::move(subscriber)); return node_->internal_signal.subscribe(std::move(subscriber));
} }
std::shared_ptr<detail::node<T>> node(detail::internal_tag)
{
return node_;
}
protected: protected:
std::shared_ptr<detail::node<T>> node_; std::shared_ptr<detail::node<T>> node_;

View file

@ -0,0 +1,18 @@
#include <psemek/react/value.hpp>
#include <psemek/util/unused.hpp>
namespace psemek::react
{
namespace detail
{
void on_node_destroyed(std::string const & debug_marker)
{
// Can put a breakpoint here to track destruction of specific nodes
unused(debug_marker);
}
}
}