5#include <portals/chart.hpp>
6#include <portals/detail/managed_origin.hpp>
7#include <portals/model_position.hpp>
30struct ManagedModelAccess;
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;
44 bool navigationExplicit{};
46 std::uint64_t navigationRevision{};
49template <
class T>
struct ManagedPreparedRegistration {
50 std::shared_ptr<ManagedViewHandler<T>> action;
53template <
class T>
struct ManagedModelSourceOf final : ManagedModelSource {
54 std::unique_ptr<Model<T>> model;
55 std::optional<ModelFrame<T>> frame;
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);
63 bool bound()
const noexcept override {
67 void retire()
noexcept override {
70 auto latest = model->pin();
72 frame = std::move(*latest);
76 for (
const auto &weak : prepared) {
77 if (
auto registration = weak.lock()) {
78 registration->action.reset();
85 editDeclaration.reset();
88 std::expected<bool, TreeError> synchronize()
override {
89 return sync ? sync() : std::expected<bool, TreeError>(
false);
92 std::expected<std::optional<TreeError>,
TreeError> dataError()
const override {
93 return model ? model->lastError() : std::expected<std::optional<TreeError>,
TreeError>{std::nullopt};
96 std::expected<void, TreeError> pin()
override {
100 auto value = model->pin();
103 return std::unexpected(value.error());
106 frame = std::move(*value);
130 auto source = lock();
131 if (!source || (*source)->retired) {
134 if ((*source)->navigationRevision == std::numeric_limits<std::uint64_t>::max()) {
137 (*source)->navigationChanges = std::move(changes);
138 ++(*source)->navigationRevision;
148 auto source = lock();
150 if (source && (*source)->sinkOnly) {
154 return source ? std::expected<Publisher<ModelSnapshot<T>>,
TreeError>((*source)->model->publisher())
155 : std::unexpected(source.error());
161 friend struct ui::detail::RowAccess;
162 friend struct detail::ManagedModelAccess;
164 std::expected<std::shared_ptr<detail::ManagedModelSourceOf<T>>,
TreeError> lock()
const {
165 if (owner_ != std::thread::id{} && owner_ != std::this_thread::get_id()) {
169 auto source = source_.lock();
174 if (source->origin) {
175 auto live = source->origin->check();
177 return std::unexpected(
live.error());
184 std::weak_ptr<detail::ManagedModelSourceOf<T>> source_;
185 std::thread::id owner_{};
190struct ManagedModelAccess {
191 template <
class T>
static std::expected<void, TreeError> validate(
const ManagedModel<T> &model, NodeId scene) {
192 auto source = model.lock();
195 return std::unexpected(source.error());
198 if ((*source)->retired || !(*source)->model) {
199 return std::unexpected(TreeError::CLOSED);
202 return (*source)->scene == scene ? std::expected<void, TreeError>{} : std::unexpected(TreeError::INVALID_ARGUMENT);
206 static std::expected<std::shared_ptr<const ModelSnapshot<T>>,
TreeError> snapshot(
const ManagedModel<T> &model,
208 auto source = model.lock();
211 return std::unexpected(source.error());
214 if ((*source)->retired || !(*source)->model) {
218 if ((*source)->scene != scene || !(*source)->frame) {
222 return (*source)->frame->snapshot();
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