Portals0.7.0 · dev (544a5c68c72e · 2026-11-28 UTC)
Loading...
Searching...
No Matches
model_view_history.hpp
1// Copyright (c) 2026 ttldtor.
2// SPDX-License-Identifier: BSL-1.0
3#pragma once
4
5namespace portals::detail {
7struct ModelSelectionPositions {
8 ModelSelectionPositions() : positions(0) {
9 }
10
11 SourceCredit credit;
12 std::vector<ModelBookmark> positions;
13};
14
15struct ModelViewCheckpoint {
16 std::optional<ModelBookmark> current;
17 NavigationSelection selection;
18 std::optional<ModelBookmark> reading;
19 std::optional<std::int32_t> readingCellOffset;
20 std::uint64_t first{};
21 PositionResolution currentResolution{PositionResolution::EMPTY};
22 PositionResolution readingResolution{PositionResolution::EMPTY};
23 std::shared_ptr<const ModelSelectionPositions> selectedPositions;
24};
25
26struct ModelRestoreChannels {
27 bool current{true};
28 bool reading{true};
29 bool selection{true};
30};
31
32inline bool unresolvedPosition(PositionResolution value) noexcept {
33 return value != PositionResolution::EXACT && value != PositionResolution::NEAREST &&
35}
36
37inline bool pendingModelRestoration(const ModelViewCheckpoint &value, ModelRestoreChannels channels) noexcept {
38 return (channels.current && unresolvedPosition(value.currentResolution)) ||
39 (channels.reading && unresolvedPosition(value.readingResolution)) ||
40 (channels.selection && unresolvedPosition(value.selection.resolution()));
41}
42
43struct ModelCanonicalRestore {
44 SourceCredit credit;
45 ModelViewCheckpoint value;
46 ModelRestoreChannels channels;
47 std::uint64_t currentRevision{};
48 std::uint64_t readingRevision{};
49 std::uint64_t selectionRevision{};
50 std::uint64_t sequence{};
51 std::uint64_t publication{};
52 std::uint64_t sourceRevision{};
53};
54
55struct ModelViewHistoryEntry {
56 SourceCredit credit;
57 SourceCredit metadataCredit;
58 std::weak_ptr<ManagedModelSource> model;
59 ModelResource resource;
60 bool explicitResource{};
61 std::weak_ptr<const void> participant;
62 std::uint64_t role{};
63 std::uint64_t revision{};
64 ModelViewCheckpoint value;
65 std::unique_ptr<ModelViewHistoryEntry> next;
66};
67
68struct ModelViewHistoryChange {
69 SourceCredit metadataCredit;
70 std::unique_ptr<ModelViewHistoryEntry> added;
71 ModelViewHistoryEntry *existing{};
72 std::weak_ptr<ManagedModelSource> model;
73 ModelResource resource;
74 bool explicitResource{};
75 bool resourceOnly{};
76 std::weak_ptr<const void> participant;
77 std::uint64_t role{};
78 std::uint64_t revision{};
79 ModelViewCheckpoint value;
80 bool replaceMetadata{};
81};
82
83class ModelViewHistory {
84public:
85 static std::expected<std::shared_ptr<ModelViewHistory>, TreeError> create(std::weak_ptr<const void> lifetime,
86 ModelCheckpointOptions options);
87 ModelViewHistoryEntry *find(const std::shared_ptr<ManagedModelSource> &model, std::uint64_t role,
88 bool lineageFallback = true) const noexcept;
89 ModelViewHistoryEntry *find(ModelResource resource, std::uint64_t role, bool lineageFallback = true) const noexcept;
90 std::expected<ModelViewHistoryChange, TreeError> stageResource(ModelResource resource,
91 std::weak_ptr<const void> logicalLifetime,
92 std::uint64_t role, ModelViewCheckpoint value);
93 std::expected<ModelViewHistoryChange, TreeError> stage(std::shared_ptr<ManagedModelSource> model, std::uint64_t role,
94 ModelViewCheckpoint value,
95 std::weak_ptr<const void> participant = {});
96 bool valid(const ModelViewHistoryChange &change) const noexcept;
97 void commit(ModelViewHistoryChange change) noexcept;
98 std::expected<std::uint64_t, TreeError> role();
99 std::expected<SourceCredit, TreeError> reserveState();
100 std::expected<SourceCredit, TreeError> reserveRole();
101 std::expected<std::shared_ptr<ModelSelectionPositions>, TreeError> selectedPositions(std::size_t count);
102 std::expected<std::shared_ptr<ModelCanonicalRestore>, TreeError> canonicalRestore();
103 void prune() noexcept;
104 const ModelCheckpointOptions &options() const noexcept;
105 std::shared_ptr<ModelCanonicalRestore> pending;
106
107private:
108 std::expected<ModelViewHistoryChange, TreeError>
109 stageCommon(std::shared_ptr<ManagedModelSource> model, ModelResource resource, bool explicitResource,
110 bool resourceOnly, std::uint64_t role, ModelViewCheckpoint value, std::weak_ptr<const void> participant);
111 SourceCredit credit_;
112 std::shared_ptr<SourceBudget> budget_;
113 std::weak_ptr<const void> lifetime_;
114 ModelCheckpointOptions options_;
115 std::unique_ptr<ModelViewHistoryEntry> entries_;
116 std::uint64_t nextRole_{1};
117};
118
119struct ModelViewRestoreState {
120 SourceCredit credit;
121 std::shared_ptr<ModelViewHistory> history;
122 std::shared_ptr<SourceCredit> participant;
123 std::weak_ptr<ManagedNavigationRecord> context;
124 std::uint64_t currentRevision{};
125 std::uint64_t readingRevision{};
126 std::uint64_t selectionRevision{};
127 ModelRestoreChannels channels;
128 bool shareCurrent{};
129 bool shareReading{};
130 std::uint64_t role{};
131 std::optional<ModelViewCheckpoint> pending;
132 std::uint64_t attemptedRevision{};
133 std::uint64_t attemptedProofRevision{};
134 PositionResolution resolution{PositionResolution::EXACT};
135 bool attempted{};
136};
137
138std::expected<ModelResource, TreeError> modelNavigationResource(ManagedModelSource &source);
139
140struct PreparedModelViewChange {
141 virtual ~PreparedModelViewChange() = default;
142 virtual std::expected<void, TreeError> validate() const = 0;
143 virtual std::expected<void, TreeError> invalidate() const = 0;
144 virtual std::expected<void, TreeError> install() = 0;
145 virtual void commitHistory() noexcept = 0;
146 virtual void retire() noexcept = 0;
147};
148
150} // namespace portals::detail
PositionResolution
Successful protocol outcomes; absence of support differs from missing history.
Definition model_position.hpp:123
@ NEAREST
The original item was removed; a declared surviving neighbor was selected.
Definition model_position.hpp:125
@ EMPTY
Compatible source is empty and has no current item.
Definition model_position.hpp:126
@ EXACT
The same item or a proven mapped gap/mark is present.
Definition model_position.hpp:124
TreeError
Checked tree failures; rejected operations never start the requested callback.
Definition widget_types.hpp:26