Portals0.7.0 · dev (544a5c68c72e · 2026-11-28 UTC)
Loading...
Searching...
No Matches
aggregate_columns.hpp
1// Copyright (c) 2026 ttldtor.
2// SPDX-License-Identifier: BSL-1.0
3#pragma once
4
5#include <portals/table_column.hpp>
6
7#include <boost/pfr/core.hpp>
8
9#include <array>
10#include <concepts>
11#include <cstddef>
12#include <type_traits>
13#include <utility>
14
15namespace portals {
21template <std::size_t I, class T> struct AggregateFieldProjection {
26 decltype(auto) operator()(const T &row) const {
27 return boost::pfr::get<I>(row);
28 }
29};
30
31namespace detail {
32template <class T, class Formatter, std::size_t... I>
33consteval bool aggregateFieldsFormattable(std::index_sequence<I...>) {
34 return (ColumnFormatter<T, AggregateFieldProjection<I, T>, Formatter> && ...);
35}
36
37template <class T, class Formatter, std::size_t N, std::size_t... I>
38std::array<TableColumn<T>, N> makeAggregateColumns(std::array<TableColumnOptions, N> options, Formatter formatter,
39 std::index_sequence<I...>) {
40 return {tableColumn<T>(std::move(options[I]), AggregateFieldProjection<I, T>{}, formatter)...};
41}
42} // namespace detail
43
54template <std::size_t I, class T, class Formatter>
55 requires(std::is_aggregate_v<T> && I < boost::pfr::tuple_size_v<T> &&
57TableColumn<T> aggregateColumn(TableColumnOptions options, Formatter formatter) {
58 return tableColumn<T>(std::move(options), AggregateFieldProjection<I, T>{}, std::move(formatter));
59}
60
72template <class T, std::size_t N, class Formatter>
73 requires(std::is_aggregate_v<T> && N == boost::pfr::tuple_size_v<T> &&
74 detail::aggregateFieldsFormattable<T, Formatter>(std::make_index_sequence<N>{}))
75std::array<TableColumn<T>, N> aggregateColumns(std::array<TableColumnOptions, N> options, Formatter formatter) {
76 return detail::makeAggregateColumns<T>(std::move(options), std::move(formatter), std::make_index_sequence<N>{});
77}
78
79namespace detail {
80template <class T, class Formatter, std::size_t N, std::size_t... I>
81std::expected<std::array<TableColumn<T>, N>, TextError> makeAggregateSchema(std::array<ColumnCaption, N> captions,
82 Formatter formatter, WidthPolicy policy,
83 std::index_sequence<I...>) {
84 if constexpr (N == 0) {
85 return std::array<TableColumn<T>, 0>{};
86 } else {
87 return tableColumns(
88 policy, tableColumn<T>(captions[I].header, AggregateFieldProjection<I, T>{}, formatter, captions[I].width)...);
89 }
90}
91} // namespace detail
92
105template <class T, std::size_t N, class Formatter>
106 requires(std::is_aggregate_v<T> && N == boost::pfr::tuple_size_v<T> &&
107 detail::aggregateFieldsFormattable<T, Formatter>(std::make_index_sequence<N>{}))
108std::expected<std::array<TableColumn<T>, N>, TextError> aggregateColumns(std::array<ColumnCaption, N> captions,
109 Formatter formatter, WidthPolicy policy = {}) {
110 return detail::makeAggregateSchema<T>(std::move(captions), std::move(formatter), policy,
111 std::make_index_sequence<N>{});
112}
113} // namespace portals
A copyable formatter returning exactly owning GraphemeText for a projection result.
Definition table_column.hpp:58
Public Portals API.
Definition action.hpp:8
std::array< TableColumn< T >, N > aggregateColumns(std::array< TableColumnOptions, N > options, Formatter formatter)
Create columns for every field in PFR's aggregate declaration order.
Definition aggregate_columns.hpp:75
std::expected< std::array< TableColumn< T >, 1+sizeof...(Rest)>, TextError > tableColumns(WidthPolicy policy, TableColumnSpec< T > first, Rest &&...rest)
Assemble a fixed schema and allocate deterministic table-local identities.
Definition table_column.hpp:265
TableColumn< T > tableColumn(TableColumnOptions options, Projection projection, Formatter formatter)
Compose an owning typed column from explicit projection and text formatting.
Definition table_column.hpp:84
TableColumn< T > aggregateColumn(TableColumnOptions options, Formatter formatter)
Create a typed column for one selected field of a PFR SimpleAggregate.
Definition aggregate_columns.hpp:57
Project one field from an immutable, borrowed aggregate row.
Definition aggregate_columns.hpp:21
Owning fixed-schema caption and sizing, without identity bookkeeping.
Definition table_column.hpp:37
Owning metadata for a typed column; validation remains part of table preparation.
Definition table_column.hpp:17
A typed owning column definition; identity is independent of visual order.
Definition virtual_table.hpp:25
Owning error value, independent of the failed input's lifetime.
Definition text.hpp:87
Owning deterministic width policy; no font or terminal matching promise.
Definition text.hpp:42