Portals0.7.0 · dev (544a5c68c72e · 2026-11-28 UTC)
Loading...
Searching...
No Matches
find_model_source.hpp
1// Copyright (c) 2026 ttldtor.
2// SPDX-License-Identifier: BSL-1.0
3#pragma once
4
5#include <portals/find_bar.hpp>
6#include <portals/managed_diagnostics.hpp>
7#include <portals/managed_model.hpp>
8
9#include <memory_resource>
10
11namespace portals::detail {
13bool validFindSearchOptions(const FindSearchOptions &options) noexcept;
14
15struct ProjectionLimit {};
16
17struct ProjectionMemory : std::pmr::memory_resource {
18 virtual void chargeExternal(std::size_t bytes) = 0;
19 virtual void releaseExternal(std::size_t bytes) noexcept = 0;
20};
21
22struct FindRowView {
23 RowId id;
24 std::uint64_t ordinal{};
25 std::string_view text;
26 std::shared_ptr<const std::pmr::string> owner;
27};
28
29// Only this immutable worker object crosses admission. Its UI model observer remains in FindModelSource.
30struct FindWorkerSource {
31 virtual ~FindWorkerSource() = default;
32 virtual std::expected<void, FindSearchError> read(RowRange range, std::vector<FindRowView> &rows,
33 ProjectionMemory *memory = nullptr) const = 0;
34 std::shared_ptr<const void> pin;
35 std::uint64_t revision{};
36 std::uint64_t rowCount{};
37};
38
39struct FindModelSource {
40 virtual ~FindModelSource() = default;
41 virtual std::expected<void, ManagedError> attach(ManagedScene &scene) = 0;
42 virtual std::expected<std::shared_ptr<const FindWorkerSource>, TreeError> capture(ManagedScene &scene) = 0;
43};
44
45template <class T, class Projection> struct FindWorkerSourceOf final : FindWorkerSource {
46 FindWorkerSourceOf(std::shared_ptr<const ModelSnapshot<T>> snapshot, std::shared_ptr<const Projection> projection)
47 : snapshot_(std::move(snapshot)), projection_(std::move(projection)) {
48 pin = snapshot_;
49 revision = snapshot_->revision();
50 rowCount = snapshot_->rowCount();
51 }
52
53 std::expected<void, FindSearchError> read(RowRange range, std::vector<FindRowView> &rows,
54 ProjectionMemory *memory = nullptr) const override {
55 auto input = ModelRange<T>::read(snapshot_, range, range.count);
56
57 if (!input) {
58 return std::unexpected(input.error() == TreeError::NO_MEMORY ? FindSearchError::NO_MEMORY
59 : input.error() == TreeError::CALLBACK_FAILED ? FindSearchError::CALLBACK_FAILED
60 : FindSearchError::INVALID_ARGUMENT);
61 }
62
63 if (input->state() != RangeState::READY) {
64 return std::unexpected(FindSearchError::NOT_READY);
65 }
66
67 rows.clear();
68
69 for (const auto &row : input->rows()) {
70 if (!row.id.value || !row.id.generation) {
71 return std::unexpected(FindSearchError::INVALID_ARGUMENT);
72 }
73
74 if constexpr (std::same_as<std::invoke_result_t<const Projection &, const T &>, std::string>) {
75 if (!memory) {
76 return std::unexpected(FindSearchError::INVALID_ARGUMENT);
77 }
78 try {
79 auto projected = std::invoke(*projection_, row.value);
80
81 struct ExternalCharge {
82 ProjectionMemory &memory;
83 std::size_t bytes;
84
85 ~ExternalCharge() {
86 memory.releaseExternal(bytes);
87 }
88 };
89
90 memory->chargeExternal(projected.capacity() + 1);
91 ExternalCharge charge{*memory, projected.capacity() + 1};
92 auto owner = std::allocate_shared<const std::pmr::string>(
93 std::pmr::polymorphic_allocator<std::pmr::string>(memory), std::string_view(projected));
94 const auto text = std::string_view(*owner);
95 rows.push_back({row.id, range.first + rows.size(), text, std::move(owner)});
96 } catch (const ProjectionLimit &) {
97 return std::unexpected(FindSearchError::LIMIT_EXCEEDED);
98 }
99 } else {
100 rows.push_back({row.id, range.first + rows.size(), std::invoke(*projection_, row.value), {}});
101 }
102 }
103
104 return {};
105 }
106
107private:
108 std::shared_ptr<const ModelSnapshot<T>> snapshot_;
109 std::shared_ptr<const Projection> projection_;
110};
111
113} // namespace portals::detail
consteval auto id(StyleState state=StyleState::ANY, bool readOnly=false)
Definition style_static.hpp:865
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
TreeError
Checked tree failures; rejected operations never start the requested callback.
Definition widget_types.hpp:26