Portals0.7.0 · dev (544a5c68c72e · 2026-11-28 UTC)
Loading...
Searching...
No Matches
ui_log_source_model.hpp
1// Copyright (c) 2026 ttldtor.
2// SPDX-License-Identifier: BSL-1.0
3#pragma once
4
5#include <portals/log_file.hpp>
6#include <portals/model.hpp>
7#include <portals/ui_log_view.hpp>
8
9#include <concepts>
10
11namespace portals::ui::detail {
13template <class T, class Codec> class FiniteLogProvider final : public WindowProvider<LogLine> {
14public:
15 FiniteLogProvider(std::shared_ptr<const ModelSnapshot<T>> snapshot, Codec codec)
16 : snapshot_(std::move(snapshot)), codec_(std::move(codec)) {
17 }
18
19 SourceCapabilities capabilities() const noexcept override {
20 return {true, false, true};
21 }
22
23 std::expected<SourceReadReply<LogLine>, SourceError> load(const SourceReadCommand &command, PageBuilder<LogLine> &out,
24 std::stop_token stop) override {
25 if (!snapshot_ || command.dataRevision != snapshot_->revision()) {
26 return std::unexpected(SourceError::REVISION_CHANGED);
27 }
28
29 const auto total = snapshot_->rowCount();
30 std::uint64_t position{};
31
32 switch (command.anchor.kind) {
33 case SourceAnchorKind::ENTRY:
34 position = command.direction == SourceDirection::BACKWARD ? total : 0;
35 break;
36 case SourceAnchorKind::TAIL:
37 position = total;
38 break;
39 case SourceAnchorKind::CURSOR:
40 if (command.anchor.cursor.token[1] || command.anchor.cursor.token[0] > total) {
41 return std::unexpected(SourceError::INVALID_CURSOR);
42 }
43
44 position = command.anchor.cursor.token[0];
45 break;
46 case SourceAnchorKind::ROW:
47 return std::unexpected(SourceError::UNSUPPORTED);
48 }
49
50 const bool backwards =
51 command.direction == SourceDirection::BACKWARD || command.anchor.kind == SourceAnchorKind::TAIL;
52 const auto count =
53 static_cast<std::size_t>(std::min<std::uint64_t>(command.maxRows, backwards ? position : total - position));
54 const auto first = backwards ? position - count : position;
55 auto range = ModelRange<T>::read(snapshot_, {first, count}, command.maxRows);
56
57 if (!range) {
58 return std::unexpected(portals::detail::sourceTreeError(range.error()));
59 }
60
61 if (range->state() == RangeState::PENDING) {
62 SourceReadReply<LogLine> pending;
63
64 pending.observedRevision = command.dataRevision;
65
66 return pending;
67 }
68
69 if (range->state() != RangeState::READY) {
70 return std::unexpected(SourceError::CALLBACK_FAILED);
71 }
72
73 try {
74 // The throwing overload lets debug proxy allocation report workspace exhaustion.
75 std::pmr::string text("", 0, out.workspace());
76
77 for (const auto &row : range->rows()) {
78 if (stop.stop_requested()) {
79 return std::unexpected(SourceError::CANCELLED);
80 }
81
82 text.clear();
83
84 std::invoke(codec_, row.value, text);
85
86 auto added = out.emplace(row.id, snapshot_->revision(), 0, std::string_view(text), out.resource());
87
88 if (!added) {
89 return std::unexpected(added.error());
90 }
91 }
92 } catch (const std::bad_alloc &) {
93 return std::unexpected(SourceError::PAGE_TOO_LARGE);
94 }
95
96 const auto edge = (command.direction == SourceDirection::BACKWARD ? first == 0 : first + count == total)
97 ? SourceEdge::FINAL_END
99
100 return out.ready({command.source, {first, 0}}, {command.source, {first + count, 0}}, edge);
101 }
102
103private:
104 std::shared_ptr<const ModelSnapshot<T>> snapshot_;
105 Codec codec_;
106};
107
109} // namespace portals::ui::detail
110
111namespace portals::ui {
121template <class T, class Codec>
122 requires std::invocable<Codec &, const T &, std::pmr::string &>
123LogSource logSource(std::shared_ptr<const ModelSnapshot<T>> snapshot, Codec codec,
124 std::shared_ptr<SourceBudget> budget = {}) {
125 if (!snapshot) {
126 return {};
127 }
128
129 try {
130 return detail::finiteLogSource(std::make_shared<detail::FiniteLogProvider<T, Codec>>(snapshot, std::move(codec)),
131 snapshot->rowCount(), snapshot->revision(), std::move(budget));
132 } catch (const std::bad_alloc &) {
133 return detail::failedLogSource(SourceError::NO_MEMORY);
134 } catch (...) {
135 return detail::failedLogSource(SourceError::CALLBACK_FAILED);
136 }
137}
138
143template <class T>
144 requires std::convertible_to<const T &, std::string_view>
145LogSource logSource(std::shared_ptr<const ModelSnapshot<T>> snapshot, std::shared_ptr<SourceBudget> budget = {}) {
146 return logSource(
147 std::move(snapshot),
148 [](const T &value, std::pmr::string &out) {
149 out.append(std::string_view(value));
150 },
151 std::move(budget));
152}
153} // namespace portals::ui
Owning deferred external log binding; construction performs no file I/O. Copies share the provider an...
Definition ui_log_view.hpp:48
ui::ValueText text(std::format_string< const ui::detail::TextArgumentValue< Args > &... > pattern, Args &&...args)
Declare live formatted text over explicit scalar roots, fields or value properties.
Definition ui_value_text.hpp:567
SourceError
Source protocol and admission failures, independent of a provider's storage format.
Definition source_budget.hpp:14
@ CALLBACK_FAILED
Provider construction/preparation threw an unexpected exception.
Definition source_budget.hpp:21
@ NO_MEMORY
Allocation failed inside a fallible operation.
Definition source_budget.hpp:19
SourceEdge
State beyond a ready page in the requested direction.
Definition window_source.hpp:59
@ MORE
Continuation exists; short/empty pages do not imply end.
Definition window_source.hpp:60