5namespace portals::detail {
7struct ModelSelectionPositions {
8 ModelSelectionPositions() : positions(0) {
12 std::vector<ModelBookmark> positions;
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{};
23 std::shared_ptr<const ModelSelectionPositions> selectedPositions;
26struct ModelRestoreChannels {
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()));
43struct ModelCanonicalRestore {
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{};
55struct ModelViewHistoryEntry {
57 SourceCredit metadataCredit;
58 std::weak_ptr<ManagedModelSource> model;
59 ModelResource resource;
60 bool explicitResource{};
61 std::weak_ptr<const void> participant;
63 std::uint64_t revision{};
64 ModelViewCheckpoint value;
65 std::unique_ptr<ModelViewHistoryEntry> next;
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{};
76 std::weak_ptr<const void> participant;
78 std::uint64_t revision{};
79 ModelViewCheckpoint value;
80 bool replaceMetadata{};
83class ModelViewHistory {
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;
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};
119struct ModelViewRestoreState {
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;
130 std::uint64_t role{};
131 std::optional<ModelViewCheckpoint> pending;
132 std::uint64_t attemptedRevision{};
133 std::uint64_t attemptedProofRevision{};
138std::expected<ModelResource, TreeError> modelNavigationResource(ManagedModelSource &source);
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;
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