Portals0.7.0 · dev (544a5c68c72e · 2026-11-28 UTC)
Loading...
Searching...
No Matches
managed_model.hpp
1// Copyright (c) 2026 ttldtor.
2// SPDX-License-Identifier: BSL-1.0
3#pragma once
4
5#include <portals/chart.hpp>
6#include <portals/detail/managed_origin.hpp>
7#include <portals/model_position.hpp>
8
9#include <limits>
10
11namespace portals {
12namespace ui::detail {
13struct Access;
14struct RowAccess;
15} // namespace ui::detail
16class ManagedScene;
17
24template <class T>
26 std::function<std::expected<void, TreeError>(const ModelFrame<T> &, const std::optional<ModelRequest<T>> &)>;
27
29namespace detail {
30struct ManagedModelAccess;
31
32struct ManagedModelSource {
33 virtual ~ManagedModelSource() = default;
34 virtual std::expected<void, TreeError> pin() = 0;
35 virtual std::expected<std::optional<TreeError>, TreeError> dataError() const = 0;
36 virtual bool bound() const noexcept = 0;
37 virtual std::expected<bool, TreeError> synchronize() = 0;
38 virtual void retire() noexcept = 0;
39 std::shared_ptr<ManagedOrigin> origin;
40 std::shared_ptr<void> editDeclaration;
41 bool retired{};
42 Node<Widget> receiver;
43 ModelResource navigationResource;
44 bool navigationExplicit{};
45 PositionChangeMap navigationChanges;
46 std::uint64_t navigationRevision{};
47};
48
49template <class T> struct ManagedPreparedRegistration {
50 std::shared_ptr<ManagedViewHandler<T>> action;
51};
52
53template <class T> struct ManagedModelSourceOf final : ManagedModelSource {
54 std::unique_ptr<Model<T>> model;
55 std::optional<ModelFrame<T>> frame;
56 NodeId scene;
57 bool sinkOnly{};
58 std::shared_ptr<ManagedViewHandler<T>> preparation;
59 std::function<std::expected<bool, TreeError>()> sync;
60 std::vector<std::weak_ptr<ManagedPreparedRegistration<T>>> prepared =
61 std::vector<std::weak_ptr<ManagedPreparedRegistration<T>>>(0);
62
63 bool bound() const noexcept override {
64 return sinkOnly;
65 }
66
67 void retire() noexcept override {
68 retired = true;
69 if (model) {
70 auto latest = model->pin();
71 if (latest) {
72 frame = std::move(*latest);
73 }
74 }
75 // Registration ownership ends immediately. An admitted caller retains its separate action owner.
76 for (const auto &weak : prepared) {
77 if (auto registration = weak.lock()) {
78 registration->action.reset();
79 }
80 }
81 prepared.clear();
82 preparation.reset();
83 sync = {};
84 model.reset();
85 editDeclaration.reset();
86 }
87
88 std::expected<bool, TreeError> synchronize() override {
89 return sync ? sync() : std::expected<bool, TreeError>(false);
90 }
91
92 std::expected<std::optional<TreeError>, TreeError> dataError() const override {
93 return model ? model->lastError() : std::expected<std::optional<TreeError>, TreeError>{std::nullopt};
94 }
95
96 std::expected<void, TreeError> pin() override {
97 if (retired) {
98 return {};
99 }
100 auto value = model->pin();
101
102 if (!value) {
103 return std::unexpected(value.error());
104 }
105
106 frame = std::move(*value);
107
108 return {};
109 }
110};
111} // namespace detail
112
114
120template <class T> class ManagedModel {
121public:
123 ManagedModel() = default;
124
129 std::expected<void, TreeError> setChangeMap(PositionChangeMap changes) const {
130 auto source = lock();
131 if (!source || (*source)->retired) {
132 return std::unexpected(!source ? source.error() : TreeError::CLOSED);
133 }
134 if ((*source)->navigationRevision == std::numeric_limits<std::uint64_t>::max()) {
135 return std::unexpected(TreeError::IDENTITY_EXHAUSTED);
136 }
137 (*source)->navigationChanges = std::move(changes);
138 ++(*source)->navigationRevision;
139 return {};
140 }
141
147 std::expected<Publisher<ModelSnapshot<T>>, TreeError> publisher() const {
148 auto source = lock();
149
150 if (source && (*source)->sinkOnly) {
151 return std::unexpected(TreeError::WOULD_BLOCK);
152 }
153
154 return source ? std::expected<Publisher<ModelSnapshot<T>>, TreeError>((*source)->model->publisher())
155 : std::unexpected(source.error());
156 }
157
158private:
159 friend class ManagedScene;
160 friend struct ui::detail::Access;
161 friend struct ui::detail::RowAccess;
162 friend struct detail::ManagedModelAccess;
163
164 std::expected<std::shared_ptr<detail::ManagedModelSourceOf<T>>, TreeError> lock() const {
165 if (owner_ != std::thread::id{} && owner_ != std::this_thread::get_id()) {
166 return std::unexpected(TreeError::WRONG_THREAD);
167 }
168
169 auto source = source_.lock();
170
171 if (!source) {
172 return std::unexpected(TreeError::CLOSED);
173 }
174 if (source->origin) {
175 auto live = source->origin->check();
176 if (!live) {
177 return std::unexpected(live.error());
178 }
179 }
180
181 return source;
182 }
183
184 std::weak_ptr<detail::ManagedModelSourceOf<T>> source_;
185 std::thread::id owner_{};
186};
187
189namespace detail {
190struct ManagedModelAccess {
191 template <class T> static std::expected<void, TreeError> validate(const ManagedModel<T> &model, NodeId scene) {
192 auto source = model.lock();
193
194 if (!source) {
195 return std::unexpected(source.error());
196 }
197
198 if ((*source)->retired || !(*source)->model) {
199 return std::unexpected(TreeError::CLOSED);
200 }
201
202 return (*source)->scene == scene ? std::expected<void, TreeError>{} : std::unexpected(TreeError::INVALID_ARGUMENT);
203 }
204
205 template <class T>
206 static std::expected<std::shared_ptr<const ModelSnapshot<T>>, TreeError> snapshot(const ManagedModel<T> &model,
207 NodeId scene) {
208 auto source = model.lock();
209
210 if (!source) {
211 return std::unexpected(source.error());
212 }
213
214 if ((*source)->retired || !(*source)->model) {
215 return std::unexpected(TreeError::CLOSED);
216 }
217
218 if ((*source)->scene != scene || !(*source)->frame) {
219 return std::unexpected(TreeError::INVALID_ARGUMENT);
220 }
221
222 return (*source)->frame->snapshot();
223 }
224};
225} // namespace detail
226
228
233template <class T> using ManagedListFormatter = std::function<GraphemeText(const ModelRow<T> &)>;
234
240template <class T> using ManagedChartAccessor = std::function<std::optional<Ohlc>(const T &)>;
241
245using ManagedChartOrigin = std::function<std::expected<std::uint64_t, TreeError>()>;
246} // namespace portals
Owning validated UTF-8 and immutable extended grapheme measurements.
Definition text.hpp:145
std::expected< void, TreeError > setChangeMap(PositionChangeMap changes) const
Definition managed_model.hpp:129
ManagedModel()=default
Construct an empty handle whose operations return CLOSED.
std::expected< Publisher< ModelSnapshot< T > >, TreeError > publisher() const
Copy the existing worker-safe publication endpoint on UI.
Definition managed_model.hpp:147
Additive lifecycle owner over one existing Application subtree. UI affine and nonmovable....
Definition managed.hpp:530
One immutable model revision retained by the frame owner for all its views. Obtain once before Widget...
Definition model.hpp:400
Copyable worker token; retains work storage and one reservation until its last owner dies....
Definition model.hpp:564
Copyable typed observer; never owns or exposes an unchecked widget pointer.
Definition widget.hpp:92
Immutable bounded provider proof between two revisions of a finite order. The provider must report al...
Definition model_position.hpp:224
auto live(ManagedScene &scene, const S &description)
Definition style_static.hpp:1703
Public Portals API.
Definition action.hpp:8
std::function< std::expected< std::uint64_t, TreeError >()> ManagedChartOrigin
Owning chart-origin callback, evaluated in registration order before chart preparation.
Definition managed_model.hpp:245
std::function< std::optional< Ohlc >(const T &)> ManagedChartAccessor
Owning projection of a borrowed domain row into an optional candle. The callback runs during preparat...
Definition managed_model.hpp:240
std::function< GraphemeText(const ModelRow< T > &)> ManagedListFormatter
Owning formatter of a borrowed visible list row into owning text. The callback runs during preparatio...
Definition managed_model.hpp:233
std::function< std::expected< void, TreeError >(const ModelFrame< T > &, const std::optional< ModelRequest< T > > &)> ManagedViewHandler
Owning callback after each successful virtual-view preparation, including layout retries....
Definition managed_model.hpp:25
TreeError
Checked tree failures; rejected operations never start the requested callback.
Definition widget_types.hpp:26
@ CLOSED
The owning tree or source is closed.
Definition widget_types.hpp:31
@ WRONG_THREAD
Operation requires the tree's UI thread.
Definition widget_types.hpp:28
@ WOULD_BLOCK
The current frame/reentry boundary or finite queue/work budget rejects this operation.
Definition widget_types.hpp:32
@ IDENTITY_EXHAUSTED
A session, generation or revision cannot advance without reuse.
Definition widget_types.hpp:34
@ INVALID_ARGUMENT
An option, enum value or operation argument is invalid.
Definition widget_types.hpp:27
Logical resource namespace, distinct from its current delivery session. Reuse an existing SourceStamp...
Definition model_position.hpp:21
A typed domain row and its stable identity; a table uses a typed record as T.
Definition model.hpp:55
Owning identity, independent of addresses; zero is never a live identity.
Definition widget_types.hpp:15
Internal bridge for checked access to managed scene and recipe state.
Definition ui_recipe_access.hpp:28