5#include <portals/find_bar.hpp>
6#include <portals/managed_diagnostics.hpp>
7#include <portals/managed_model.hpp>
9#include <memory_resource>
11namespace portals::detail {
13bool validFindSearchOptions(
const FindSearchOptions &options)
noexcept;
15struct ProjectionLimit {};
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;
24 std::uint64_t ordinal{};
25 std::string_view
text;
26 std::shared_ptr<const std::pmr::string> owner;
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{};
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;
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)) {
49 revision = snapshot_->revision();
50 rowCount = snapshot_->rowCount();
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);
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);
63 if (input->state() != RangeState::READY) {
64 return std::unexpected(FindSearchError::NOT_READY);
69 for (
const auto &row : input->rows()) {
70 if (!row.id.value || !row.id.generation) {
71 return std::unexpected(FindSearchError::INVALID_ARGUMENT);
74 if constexpr (std::same_as<std::invoke_result_t<const Projection &, const T &>, std::string>) {
76 return std::unexpected(FindSearchError::INVALID_ARGUMENT);
79 auto projected = std::invoke(*projection_, row.value);
81 struct ExternalCharge {
82 ProjectionMemory &memory;
86 memory.releaseExternal(bytes);
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);
100 rows.push_back({row.id, range.first + rows.size(), std::invoke(*projection_, row.value), {}});
108 std::shared_ptr<const ModelSnapshot<T>> snapshot_;
109 std::shared_ptr<const Projection> projection_;
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