5#include <portals/log_file.hpp>
6#include <portals/model.hpp>
7#include <portals/ui_log_view.hpp>
11namespace portals::ui::detail {
13template <
class T,
class Codec>
class FiniteLogProvider final :
public WindowProvider<LogLine> {
15 FiniteLogProvider(std::shared_ptr<
const ModelSnapshot<T>> snapshot, Codec codec)
16 : snapshot_(std::move(snapshot)), codec_(std::move(codec)) {
19 SourceCapabilities capabilities() const noexcept
override {
20 return {
true,
false,
true};
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);
29 const auto total = snapshot_->rowCount();
30 std::uint64_t position{};
32 switch (command.anchor.kind) {
33 case SourceAnchorKind::ENTRY:
34 position = command.direction == SourceDirection::BACKWARD ? total : 0;
36 case SourceAnchorKind::TAIL:
39 case SourceAnchorKind::CURSOR:
40 if (command.anchor.cursor.token[1] || command.anchor.cursor.token[0] > total) {
41 return std::unexpected(SourceError::INVALID_CURSOR);
44 position = command.anchor.cursor.token[0];
46 case SourceAnchorKind::ROW:
47 return std::unexpected(SourceError::UNSUPPORTED);
50 const bool backwards =
51 command.direction == SourceDirection::BACKWARD || command.anchor.kind == SourceAnchorKind::TAIL;
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);
58 return std::unexpected(portals::detail::sourceTreeError(range.error()));
61 if (range->state() == RangeState::PENDING) {
62 SourceReadReply<LogLine> pending;
64 pending.observedRevision = command.dataRevision;
69 if (range->state() != RangeState::READY) {
70 return std::unexpected(SourceError::CALLBACK_FAILED);
75 std::pmr::string
text(
"", 0, out.workspace());
77 for (
const auto &row : range->rows()) {
78 if (stop.stop_requested()) {
79 return std::unexpected(SourceError::CANCELLED);
84 std::invoke(codec_, row.value, text);
86 auto added = out.emplace(row.id, snapshot_->revision(), 0, std::string_view(text), out.resource());
89 return std::unexpected(added.error());
92 }
catch (
const std::bad_alloc &) {
93 return std::unexpected(SourceError::PAGE_TOO_LARGE);
96 const auto edge = (command.direction == SourceDirection::BACKWARD ? first == 0 : first + count == total)
97 ? SourceEdge::FINAL_END
100 return out.ready({command.source, {first, 0}}, {command.source, {first + count, 0}}, edge);
104 std::shared_ptr<const ModelSnapshot<T>> snapshot_;
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 = {}) {
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 &) {
144 requires std::convertible_to<const T &, std::string_view>
145LogSource logSource(std::shared_ptr<
const ModelSnapshot<T>> snapshot, std::shared_ptr<SourceBudget> budget = {}) {
148 [](
const T &value, std::pmr::string &out) {
149 out.append(std::string_view(value));
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