Portals0.7.0 · dev (544a5c68c72e · 2026-11-28 UTC)
Loading...
Searching...
No Matches
setup_failure.hpp
1// Copyright (c) 2026 ttldtor.
2// SPDX-License-Identifier: BSL-1.0
3#pragma once
4
5#include <portals/detail/current_scene.hpp>
6#include <portals/managed_diagnostics.hpp>
7
8namespace portals::ui::detail {
9
11enum class SetupFailureEvent {
12 LATCH,
13 NOTIFY,
14 END_ATTEMPT,
15 END_SCOPE
16};
17
19using SetupFailureSink = void (*)(void *, SetupFailureEvent, ManagedError *, const void *) noexcept;
20
22class SetupFailureTicket {
23public:
24 SetupFailureTicket() = default;
25 SetupFailureTicket(const SetupFailureTicket &) = delete;
26 SetupFailureTicket &operator=(const SetupFailureTicket &) = delete;
27
29 void notifying() const noexcept {
30 send(SetupFailureEvent::NOTIFY);
31 }
32
35 send(SetupFailureEvent::END_ATTEMPT);
36 }
37
38private:
39 friend class SetupFailureScope;
40
41 SetupFailureTicket(std::weak_ptr<void> owner, SetupFailureSink sink, const void *attempt) noexcept
42 : owner_(std::move(owner)), sink_(sink), attempt_(attempt) {
43 }
44
45 void send(SetupFailureEvent event) const noexcept {
46 if (const auto owner = owner_.lock(); owner && sink_) {
47 sink_(owner.get(), event, nullptr, attempt_);
48 }
49 }
50
51 std::weak_ptr<void> owner_;
52 SetupFailureSink sink_{};
53 const void *attempt_{};
54};
55
58public:
63 SetupFailureScope(std::weak_ptr<void> owner, SetupFailureSink sink, const char *execution) noexcept
64 : owner_(std::move(owner)), sink_(sink), execution_(execution), previous_(current_) {
65 current_ = this;
66 }
67
68 SetupFailureScope(const SetupFailureScope &) = delete;
69 SetupFailureScope &operator=(const SetupFailureScope &) = delete;
70
72 // Delivery belongs to this completed scope, never to its parent. A newly entered scope still works.
73 current_ = nullptr;
74
75 if (const auto owner = owner_.lock(); owner && sink_) {
76 sink_(owner.get(), SetupFailureEvent::END_SCOPE, nullptr, nullptr);
77 }
78
79 // Restore before the caller retires callback captures or continues ordinary setup statements.
80 current_ = previous_;
81 }
82
87 static SetupFailureTicket latch(ManagedError &failure, const void *attempt = nullptr) noexcept {
88 auto *scope = current_;
89
90 if (!scope || !scope->sink_) {
91 return {};
92 }
93
94 const auto owner = scope->owner_.lock();
95
96 if (!owner) {
97 return {};
98 }
99
100 failure.execution = scope->execution_;
101 failure.setupStopped = true;
102 scope->sink_(owner.get(), SetupFailureEvent::LATCH, &failure, attempt);
103
104 return SetupFailureTicket(scope->owner_, scope->sink_, attempt);
105 }
106
107private:
108 friend class SetupFailurePause;
109 CurrentSceneScope currentSceneMask_;
110 std::weak_ptr<void> owner_;
111 SetupFailureSink sink_{};
112 const char *execution_{};
113 SetupFailureScope *previous_{};
114 static inline thread_local SetupFailureScope *current_{};
115};
116
122template <class Callback> auto currentSceneCallback(std::weak_ptr<SceneAnchor> scene, Callback &&callback) {
123 SetupFailureScope mask({}, nullptr, nullptr);
124
125 return CurrentSceneCallback<std::decay_t<Callback>>(std::move(scene), std::forward<Callback>(callback));
126}
127} // namespace portals::ui::detail
Facade-aware callback storage; invocation publishes weak ownership, retirement masks its caller.
Definition current_scene.hpp:69
Allocation-free UI execution publication; an empty scope suspends the caller's scene.
Definition current_scene.hpp:37
Suspend an unrelated setup collector during an independently owned callback.
Definition current_scene.hpp:16
Allocation-free current-thread setup context; nested independent scopes restore their parent.
Definition setup_failure.hpp:57
SetupFailureScope(std::weak_ptr< void > owner, SetupFailureSink sink, const char *execution) noexcept
Definition setup_failure.hpp:63
static SetupFailureTicket latch(ManagedError &failure, const void *attempt=nullptr) noexcept
Definition setup_failure.hpp:87
Stack-local entered-report identity, valid only during one synchronous collector call.
Definition setup_failure.hpp:22
~SetupFailureTicket()
Revoke the borrowed attempt identity before its stack storage retires.
Definition setup_failure.hpp:34
void notifying() const noexcept
Mark notification before the observer can reenter; no user callback runs here.
Definition setup_failure.hpp:29
Original error plus independent runner/restoration and cleanup failures.
Definition managed_diagnostics.hpp:62
bool setupStopped
A scoped short failure stopped subsequent setup/run after the current callback returned.
Definition managed_diagnostics.hpp:86
const char * execution
Library static-storage facade boundary label; does not replace the original phase.
Definition managed_diagnostics.hpp:85