Add UI core node lifetime handles

This commit is contained in:
2026-06-06 09:00:24 +02:00
parent 7a9dd150e3
commit d963daae70
8 changed files with 763 additions and 5 deletions

View File

@@ -0,0 +1,379 @@
#include "ui_core/node_lifetime.h"
#include <algorithm>
#include <utility>
namespace pp::ui {
namespace {
void bump_generation(std::uint32_t& generation) noexcept
{
++generation;
if (generation == 0U) {
generation = 1U;
}
}
}
ScopedUiConnection::ScopedUiConnection(NodeLifetimeTree& tree, UiConnection connection) noexcept
: tree_(&tree)
, connection_(connection)
{
}
ScopedUiConnection::ScopedUiConnection(ScopedUiConnection&& other) noexcept
: tree_(std::exchange(other.tree_, nullptr))
, connection_(std::exchange(other.connection_, UiConnection {}))
{
}
ScopedUiConnection& ScopedUiConnection::operator=(ScopedUiConnection&& other) noexcept
{
if (this != &other) {
reset();
tree_ = std::exchange(other.tree_, nullptr);
connection_ = std::exchange(other.connection_, UiConnection {});
}
return *this;
}
ScopedUiConnection::~ScopedUiConnection()
{
reset();
}
void ScopedUiConnection::reset() noexcept
{
if (tree_ != nullptr && connection_.valid()) {
(void)tree_->disconnect(connection_);
}
tree_ = nullptr;
connection_ = {};
}
UiConnection ScopedUiConnection::connection() const noexcept
{
return connection_;
}
bool ScopedUiConnection::connected() const noexcept
{
return tree_ != nullptr && connection_.valid();
}
pp::foundation::Result<NodeHandle> NodeLifetimeTree::create_root()
{
return allocate_node(NodeHandle {});
}
pp::foundation::Result<NodeHandle> NodeLifetimeTree::create_child(NodeHandle parent)
{
if (node_slot(parent) == nullptr) {
return pp::foundation::Result<NodeHandle>::failure(
pp::foundation::Status::invalid_argument("UI child node requires a live parent"));
}
auto child = allocate_node(parent);
if (!child) {
return child;
}
node_slot(parent)->children.push_back(child.value());
return child;
}
bool NodeLifetimeTree::contains(NodeHandle node) const noexcept
{
return node_slot(node) != nullptr;
}
pp::foundation::Result<NodeHandle> NodeLifetimeTree::parent_of(NodeHandle node) const noexcept
{
const auto* slot = node_slot(node);
if (slot == nullptr) {
return pp::foundation::Result<NodeHandle>::failure(
pp::foundation::Status::invalid_argument("UI node handle is not live"));
}
if (!slot->parent.valid()) {
return pp::foundation::Result<NodeHandle>::failure(
pp::foundation::Status::invalid_argument("UI root node does not have a parent"));
}
return pp::foundation::Result<NodeHandle>::success(slot->parent);
}
pp::foundation::Result<std::size_t> NodeLifetimeTree::child_count(NodeHandle node) const noexcept
{
const auto* slot = node_slot(node);
if (slot == nullptr) {
return pp::foundation::Result<std::size_t>::failure(
pp::foundation::Status::invalid_argument("UI node handle is not live"));
}
return pp::foundation::Result<std::size_t>::success(slot->children.size());
}
pp::foundation::Result<NodeHandle> NodeLifetimeTree::child_at(NodeHandle node, std::size_t index) const noexcept
{
const auto* slot = node_slot(node);
if (slot == nullptr) {
return pp::foundation::Result<NodeHandle>::failure(
pp::foundation::Status::invalid_argument("UI node handle is not live"));
}
if (index >= slot->children.size()) {
return pp::foundation::Result<NodeHandle>::failure(
pp::foundation::Status::out_of_range("UI child index is outside the node"));
}
return pp::foundation::Result<NodeHandle>::success(slot->children[index]);
}
pp::foundation::Status NodeLifetimeTree::destroy_subtree(NodeHandle node) noexcept
{
auto* slot = node_slot(node);
if (slot == nullptr) {
return pp::foundation::Status::invalid_argument("UI node handle is not live");
}
const auto children = slot->children;
for (const auto child : children) {
(void)destroy_subtree(child);
}
const auto connections = slot->connections;
for (const auto connection : connections) {
release_connection(connection);
}
unlink_from_parent(node);
slot = node_slot(node);
if (slot == nullptr) {
return pp::foundation::Status::success();
}
slot->children.clear();
slot->connections.clear();
slot->parent = {};
slot->alive = false;
free_nodes_.push_back(node.slot);
return pp::foundation::Status::success();
}
pp::foundation::Result<UiConnection> NodeLifetimeTree::connect(NodeHandle node, Callback callback)
{
if (node_slot(node) == nullptr) {
return pp::foundation::Result<UiConnection>::failure(
pp::foundation::Status::invalid_argument("UI connection requires a live node"));
}
if (!callback) {
return pp::foundation::Result<UiConnection>::failure(
pp::foundation::Status::invalid_argument("UI connection requires a callback"));
}
UiConnection connection;
if (!free_connections_.empty()) {
connection.slot = free_connections_.back();
free_connections_.pop_back();
auto& slot = connections_[connection.slot];
bump_generation(slot.generation);
slot.alive = true;
slot.node = node;
slot.callback = std::move(callback);
connection.generation = slot.generation;
} else {
connection.slot = static_cast<std::uint32_t>(connections_.size());
connection.generation = 1U;
connections_.push_back(ConnectionSlot {
.generation = connection.generation,
.alive = true,
.node = node,
.callback = std::move(callback),
});
}
node_slot(node)->connections.push_back(connection);
return pp::foundation::Result<UiConnection>::success(connection);
}
pp::foundation::Result<ScopedUiConnection> NodeLifetimeTree::scoped_connect(NodeHandle node, Callback callback)
{
auto connection = connect(node, std::move(callback));
if (!connection) {
return pp::foundation::Result<ScopedUiConnection>::failure(connection.status());
}
return pp::foundation::Result<ScopedUiConnection>::success(ScopedUiConnection(*this, connection.value()));
}
pp::foundation::Status NodeLifetimeTree::disconnect(UiConnection connection) noexcept
{
if (connection_slot(connection) == nullptr) {
return pp::foundation::Status::invalid_argument("UI connection handle is not live");
}
release_connection(connection);
return pp::foundation::Status::success();
}
pp::foundation::Result<std::size_t> NodeLifetimeTree::live_connection_count(NodeHandle node) const noexcept
{
const auto* slot = node_slot(node);
if (slot == nullptr) {
return pp::foundation::Result<std::size_t>::failure(
pp::foundation::Status::invalid_argument("UI node handle is not live"));
}
return pp::foundation::Result<std::size_t>::success(slot->connections.size());
}
pp::foundation::Status NodeLifetimeTree::dispatch(NodeHandle node)
{
const auto* slot = node_slot(node);
if (slot == nullptr) {
return pp::foundation::Status::invalid_argument("UI dispatch requires a live node");
}
const auto connections = slot->connections;
for (const auto connection : connections) {
if (node_slot(node) == nullptr) {
break;
}
auto* connection_value = connection_slot(connection);
if (connection_value == nullptr || connection_value->node != node) {
continue;
}
connection_value->callback(node);
}
return pp::foundation::Status::success();
}
NodeLifetimeTree::NodeSlot* NodeLifetimeTree::node_slot(NodeHandle node) noexcept
{
if (!node.valid() || node.slot >= nodes_.size()) {
return nullptr;
}
auto& slot = nodes_[node.slot];
if (!slot.alive || slot.generation != node.generation) {
return nullptr;
}
return &slot;
}
const NodeLifetimeTree::NodeSlot* NodeLifetimeTree::node_slot(NodeHandle node) const noexcept
{
if (!node.valid() || node.slot >= nodes_.size()) {
return nullptr;
}
const auto& slot = nodes_[node.slot];
if (!slot.alive || slot.generation != node.generation) {
return nullptr;
}
return &slot;
}
NodeLifetimeTree::ConnectionSlot* NodeLifetimeTree::connection_slot(UiConnection connection) noexcept
{
if (!connection.valid() || connection.slot >= connections_.size()) {
return nullptr;
}
auto& slot = connections_[connection.slot];
if (!slot.alive || slot.generation != connection.generation) {
return nullptr;
}
return &slot;
}
const NodeLifetimeTree::ConnectionSlot* NodeLifetimeTree::connection_slot(UiConnection connection) const noexcept
{
if (!connection.valid() || connection.slot >= connections_.size()) {
return nullptr;
}
const auto& slot = connections_[connection.slot];
if (!slot.alive || slot.generation != connection.generation) {
return nullptr;
}
return &slot;
}
pp::foundation::Result<NodeHandle> NodeLifetimeTree::allocate_node(NodeHandle parent)
{
NodeHandle node;
if (!free_nodes_.empty()) {
node.slot = free_nodes_.back();
free_nodes_.pop_back();
auto& slot = nodes_[node.slot];
bump_generation(slot.generation);
slot.alive = true;
slot.parent = parent;
slot.children.clear();
slot.connections.clear();
node.generation = slot.generation;
} else {
node.slot = static_cast<std::uint32_t>(nodes_.size());
node.generation = 1U;
nodes_.push_back(NodeSlot {
.generation = node.generation,
.alive = true,
.parent = parent,
.children = {},
.connections = {},
});
}
return pp::foundation::Result<NodeHandle>::success(node);
}
void NodeLifetimeTree::unlink_from_parent(NodeHandle node) noexcept
{
const auto* slot = node_slot(node);
if (slot == nullptr || !slot->parent.valid()) {
return;
}
auto* parent = node_slot(slot->parent);
if (parent == nullptr) {
return;
}
parent->children.erase(
std::remove(parent->children.begin(), parent->children.end(), node),
parent->children.end());
}
void NodeLifetimeTree::release_connection(UiConnection connection) noexcept
{
auto* slot = connection_slot(connection);
if (slot == nullptr) {
return;
}
auto* node = node_slot(slot->node);
if (node != nullptr) {
node->connections.erase(
std::remove(node->connections.begin(), node->connections.end(), connection),
node->connections.end());
}
slot->alive = false;
slot->node = {};
slot->callback = nullptr;
free_connections_.push_back(connection.slot);
}
}

127
src/ui_core/node_lifetime.h Normal file
View File

@@ -0,0 +1,127 @@
#pragma once
#include "foundation/result.h"
#include <cstddef>
#include <cstdint>
#include <functional>
#include <vector>
namespace pp::ui {
inline constexpr std::uint32_t invalid_node_slot = 0xffffffffU;
inline constexpr std::uint32_t invalid_connection_slot = 0xffffffffU;
struct NodeHandle {
std::uint32_t slot = invalid_node_slot;
std::uint32_t generation = 0;
[[nodiscard]] constexpr bool valid() const noexcept
{
return slot != invalid_node_slot && generation != 0U;
}
};
[[nodiscard]] constexpr bool operator==(NodeHandle left, NodeHandle right) noexcept
{
return left.slot == right.slot && left.generation == right.generation;
}
[[nodiscard]] constexpr bool operator!=(NodeHandle left, NodeHandle right) noexcept
{
return !(left == right);
}
struct UiConnection {
std::uint32_t slot = invalid_connection_slot;
std::uint32_t generation = 0;
[[nodiscard]] constexpr bool valid() const noexcept
{
return slot != invalid_connection_slot && generation != 0U;
}
};
[[nodiscard]] constexpr bool operator==(UiConnection left, UiConnection right) noexcept
{
return left.slot == right.slot && left.generation == right.generation;
}
[[nodiscard]] constexpr bool operator!=(UiConnection left, UiConnection right) noexcept
{
return !(left == right);
}
class NodeLifetimeTree;
class ScopedUiConnection {
public:
ScopedUiConnection() noexcept = default;
ScopedUiConnection(NodeLifetimeTree& tree, UiConnection connection) noexcept;
ScopedUiConnection(const ScopedUiConnection&) = delete;
ScopedUiConnection& operator=(const ScopedUiConnection&) = delete;
ScopedUiConnection(ScopedUiConnection&& other) noexcept;
ScopedUiConnection& operator=(ScopedUiConnection&& other) noexcept;
~ScopedUiConnection();
void reset() noexcept;
[[nodiscard]] UiConnection connection() const noexcept;
[[nodiscard]] bool connected() const noexcept;
private:
NodeLifetimeTree* tree_ = nullptr;
UiConnection connection_ {};
};
class NodeLifetimeTree {
public:
using Callback = std::function<void(NodeHandle)>;
[[nodiscard]] pp::foundation::Result<NodeHandle> create_root();
[[nodiscard]] pp::foundation::Result<NodeHandle> create_child(NodeHandle parent);
[[nodiscard]] bool contains(NodeHandle node) const noexcept;
[[nodiscard]] pp::foundation::Result<NodeHandle> parent_of(NodeHandle node) const noexcept;
[[nodiscard]] pp::foundation::Result<std::size_t> child_count(NodeHandle node) const noexcept;
[[nodiscard]] pp::foundation::Result<NodeHandle> child_at(NodeHandle node, std::size_t index) const noexcept;
[[nodiscard]] pp::foundation::Status destroy_subtree(NodeHandle node) noexcept;
[[nodiscard]] pp::foundation::Result<UiConnection> connect(NodeHandle node, Callback callback);
[[nodiscard]] pp::foundation::Result<ScopedUiConnection> scoped_connect(NodeHandle node, Callback callback);
[[nodiscard]] pp::foundation::Status disconnect(UiConnection connection) noexcept;
[[nodiscard]] pp::foundation::Result<std::size_t> live_connection_count(NodeHandle node) const noexcept;
[[nodiscard]] pp::foundation::Status dispatch(NodeHandle node);
private:
struct NodeSlot {
std::uint32_t generation = 1;
bool alive = false;
NodeHandle parent {};
std::vector<NodeHandle> children;
std::vector<UiConnection> connections;
};
struct ConnectionSlot {
std::uint32_t generation = 1;
bool alive = false;
NodeHandle node {};
Callback callback;
};
[[nodiscard]] NodeSlot* node_slot(NodeHandle node) noexcept;
[[nodiscard]] const NodeSlot* node_slot(NodeHandle node) const noexcept;
[[nodiscard]] ConnectionSlot* connection_slot(UiConnection connection) noexcept;
[[nodiscard]] const ConnectionSlot* connection_slot(UiConnection connection) const noexcept;
[[nodiscard]] pp::foundation::Result<NodeHandle> allocate_node(NodeHandle parent);
void unlink_from_parent(NodeHandle node) noexcept;
void release_connection(UiConnection connection) noexcept;
std::vector<NodeSlot> nodes_;
std::vector<std::uint32_t> free_nodes_;
std::vector<ConnectionSlot> connections_;
std::vector<std::uint32_t> free_connections_;
};
}