9template <
class T>
class CollectionSnapshot final :
public ModelSnapshot<T> {
11 CollectionSnapshot(RowCollection<T> rows, std::uint64_t revision)
12 : ModelSnapshot<T>(revision, rows.rows().size()), rows_(std::move(rows)) {
15 ModelRows<T> read(RowRange range)
const override {
16 return {RangeState::READY, rows_.rows().subspan(
static_cast<std::size_t
>(range.first), range.count), {}};
19 RowLookup locateRow(RowId
id)
const override {
20 return rows_.locate(
id);
24 RowCollection<T> rows_;
27inline std::expected<RowCollection<TextSegment>,
TreeError> segmentRows(
const TextSegments &segments,
28 std::uint64_t generation, std::size_t maxRows) {
29 if (segments.size() > maxRows) {
33 std::vector<ModelRow<TextSegment>> rows(std::initializer_list<ModelRow<TextSegment>>{});
34 rows.reserve(segments.size());
35 for (std::size_t i = 0; i < segments.size(); ++i) {
36 rows.push_back({RowId{
static_cast<std::uint64_t
>(i) + 1, generation}, *segments.at(i)});
39 }
catch (
const std::bad_alloc &) {
49template <
class T,
class Collection,
class MakeRows>
54 return std::unexpected(allowed.error());
59 auto admitted = observerAdmission(root_);
61 return std::unexpected(admitted.error());
63 auto property =
input.lock();
64 if (!property ||
input.scene_ != root_.id() || !options.maxRows) {
67 if (models_.size() >= options_.maxModels) {
71 auto initial = (*property)->snapshot();
73 return fail(initial.error());
75 auto rows = makeRows((*initial)->value, 1, options.maxRows);
77 return fail(rows.error());
79 auto snapshot = std::make_shared<detail::CollectionSnapshot<T>>(std::move(*rows), 1);
80 auto created =
Model<T>::create(app_.tree(), root_, std::move(snapshot), options.model);
82 return fail(created.error());
84 auto source = std::make_shared<detail::ManagedModelSourceOf<T>>();
85 source->model = std::move(*created);
86 source->scene = root_.id();
88 source->origin = rowOrigin_;
89 source->receiver = rowOrigin_->node;
91 source->sinkOnly =
true;
92 source->origin =
input.origin_;
93 source->sync = [weak = std::weak_ptr(source),
property = std::weak_ptr(*property), origin =
input.origin_,
94 makeRows = std::move(makeRows), options, applied = (*initial)->revision,
95 generation = std::uint64_t{1}]()
mutable -> std::expected<bool, TreeError> {
98 auto live = origin->check();
100 return origin->retired || origin->closed ? std::expected<bool, TreeError>(
false)
101 : std::unexpected(
live.error());
104 auto target = weak.lock();
105 auto input =
property.lock();
106 if (!target || !
input) {
109 auto current =
input->snapshot();
111 return std::unexpected(current.error());
113 if ((*current)->revision == applied) {
116 if (generation == std::numeric_limits<std::uint64_t>::max()) {
120 auto rows = makeRows((*current)->value, generation + 1, options.maxRows);
122 return std::unexpected(rows.error());
124 auto next = std::make_shared<detail::CollectionSnapshot<T>>(std::move(*rows), generation + 1);
126 auto live = origin->check();
128 return origin->retired || origin->closed ? std::expected<bool, TreeError>(
false)
129 : std::unexpected(
live.error());
132 auto committed = Model<T>::applySnapshot(*target->model->state_, std::move(next));
134 return std::unexpected(committed.error());
138 applied = (*current)->revision;
140 }
catch (
const std::bad_alloc &) {
146 auto pinned = source->pin();
148 return fail(pinned.error());
150 ManagedModel<T> result;
151 result.source_ = source;
152 result.owner_ = owner_;
153 models_.push_back(std::move(source));
155 }
catch (
const std::bad_alloc &) {
162template <
class Source>
163 requires detail::ManagedCollectionSource<Source>
164std::expected<ManagedModel<typename detail::CollectionSource<std::remove_cvref_t<Source>>::Row>,
ManagedError>
166 using T =
typename detail::CollectionSource<std::remove_cvref_t<Source>>
::Row;
167 return collectionModel<T>(
168 source.state_, options,
170 return rows.rows().size() <= maxRows ? std::expected<RowCollection<T>, TreeError>(rows)
171 : std::unexpected(TreeError::NODE_LIMIT);
std::expected< bool, TreeError > input(Application &app, const InputEvent &event) override
std::expected< ManagedModel< T >, ManagedError > model(std::shared_ptr< const ModelSnapshot< T > > initial, ModelOptions options={}, ManagedViewHandler< T > prepared={})
Create a scene-owned model and a weak typed UI handle.
Definition managed_views.hpp:65
Weak UI handle to a scene-owned Property, never an owning alias. Copies and destruction of the handle...
Definition managed.hpp:367
static std::expected< std::unique_ptr< Model >, TreeError > create(WidgetTree &tree, Node< Widget > context, std::shared_ptr< const ModelSnapshot< T > > initial, ModelOptions options={})
Create two bounded Scheduler endpoints in the context's scope; initial snapshot is required.
Definition model.hpp:900
Immutable owning rows with explicit provider identity and bounded identity lookup.
Definition collection.hpp:19
static std::expected< RowCollection, TreeError > from(std::span< const ModelRow< T > > rows, CollectionLimits limits={})
Copy a checked whole collection into private immutable storage.
Definition collection.hpp:29
Lay visible children from left to right.
Definition layout.hpp:250
auto live(ManagedScene &scene, const S &description)
Definition style_static.hpp:1703
Public Portals API.
Definition action.hpp:8
@ STATE
Create and retain a scene-owned property.
Definition managed_diagnostics.hpp:50
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
@ CALLBACK_FAILED
A user extension or callback threw.
Definition widget_types.hpp:37
@ NODE_LIMIT
The configured live node, observer or consumer limit was reached.
Definition widget_types.hpp:35
@ 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
@ NO_MEMORY
Allocation failed inside the operation boundary.
Definition widget_types.hpp:33
Policies for a scene-owned in-memory model adapter.
Definition collection.hpp:120
Original error plus independent runner/restoration and cleanup failures.
Definition managed_diagnostics.hpp:62