Portals0.7.0 · dev (544a5c68c72e · 2026-11-28 UTC)
Loading...
Searching...
No Matches
current_scene.hpp
1// Copyright (c) 2026 ttldtor.
2// SPDX-License-Identifier: BSL-1.0
3#pragma once
4
5#include <concepts>
6#include <functional>
7#include <memory>
8#include <optional>
9#include <utility>
10
11namespace portals::ui::detail {
12struct SceneAnchor;
14
17public:
19 explicit SetupFailurePause(const std::weak_ptr<SceneAnchor> &scene) noexcept;
26
27private:
28 SetupFailureScope *previous_{};
29};
30
34void retireSceneCallback(void *storage, void (*reset)(void *) noexcept) noexcept;
35
38public:
40 explicit CurrentSceneScope(std::weak_ptr<SceneAnchor> scene = {}) noexcept
41 : scene_(std::move(scene)), previous_(current_) {
42 current_ = this;
43 }
44
49
52 current_ = previous_;
53 }
54
56 static std::weak_ptr<SceneAnchor> snapshot() noexcept {
57 return current_ ? current_->scene_ : std::weak_ptr<SceneAnchor>{};
58 }
59
60private:
61 std::weak_ptr<SceneAnchor> scene_;
62 CurrentSceneScope *previous_{};
63 static inline thread_local CurrentSceneScope *current_{};
64};
65
69template <class Callback> class CurrentSceneCallback {
70public:
74 CurrentSceneCallback(std::weak_ptr<SceneAnchor> scene, Callback &&callback)
75 : scene_(std::move(scene)), callback_(std::in_place, std::move(callback)) {
76 }
77
80
83 : scene_(std::move(other.scene_)), callback_(std::move(other.callback_)) {
84 }
85
88 retireSceneCallback(&callback_, [](void *storage) noexcept {
89 static_cast<std::optional<Callback> *>(storage)->reset();
90 });
91 }
92
94 explicit operator bool() const {
95 if (!callback_) {
96 return false;
97 }
98
99 if constexpr (requires { static_cast<bool>(*callback_); }) {
100 return static_cast<bool>(*callback_);
101 } else {
102 return true;
103 }
104 }
105
110 template <class... Args>
111 requires std::invocable<Callback &, Args...>
112 decltype(auto) operator()(Args &&...args) const {
113 SetupFailurePause setup(scene_);
114 CurrentSceneScope scope(scene_);
115
116 return std::invoke(*callback_, std::forward<Args>(args)...);
117 }
118
119private:
120 std::weak_ptr<SceneAnchor> scene_;
121 mutable std::optional<Callback> callback_;
122};
123
124} // namespace portals::ui::detail
CurrentSceneCallback(const CurrentSceneCallback &)=default
Copy the weak publication and the original owning callable.
CurrentSceneCallback(std::weak_ptr< SceneAnchor > scene, Callback &&callback)
Definition current_scene.hpp:74
~CurrentSceneCallback()
Retire owned captures while both setup and current-scene attribution are suspended.
Definition current_scene.hpp:87
CurrentSceneCallback(CurrentSceneCallback &&other)
Definition current_scene.hpp:82
Allocation-free UI execution publication; an empty scope suspends the caller's scene.
Definition current_scene.hpp:37
~CurrentSceneScope()
Restore the previous publication before returning to the caller.
Definition current_scene.hpp:51
CurrentSceneScope & operator=(const CurrentSceneScope &)=delete
A linked stack publication cannot be reassigned.
CurrentSceneScope(std::weak_ptr< SceneAnchor > scene={}) noexcept
Definition current_scene.hpp:40
CurrentSceneScope(const CurrentSceneScope &)=delete
A linked stack publication cannot be copied.
static std::weak_ptr< SceneAnchor > snapshot() noexcept
Definition current_scene.hpp:56
Suspend an unrelated setup collector during an independently owned callback.
Definition current_scene.hpp:16
SetupFailurePause(const SetupFailurePause &)=delete
A linked stack pause cannot be copied.
SetupFailurePause & operator=(const SetupFailurePause &)=delete
A linked stack pause cannot be reassigned.
~SetupFailurePause()
Restore the setup attribution active before this entered callback.
SetupFailurePause(const std::weak_ptr< SceneAnchor > &scene) noexcept
Allocation-free current-thread setup context; nested independent scopes restore their parent.
Definition setup_failure.hpp:57
Stable weak facade publication; mutable fields are accessed only after UI admission.
Definition ui_scene.hpp:209