MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
DescribedRecords.hpp
Go to the documentation of this file.
1#pragma once
2
5
6namespace MayaFlux::IO {
7
8/**
9 * @brief Decode the same-named children of a source element into typed Composite rows.
10 *
11 * Each child named child_name is read through the binding and stored as one
12 * row of the layout derived from T. The source can be any type with a
13 * RecordAccess specialization, so the result does not depend on the file format.
14 *
15 * @param binding Configured binding for T.
16 * @param parent Source element whose children are read.
17 * @param child_name Name of the children that hold one record each.
18 * @param error Receives the reason when nullopt is returned.
19 * @param error_location Receives the position of the failing element when given.
20 * @return The rows, empty when no child matches, or nullopt on the first failure.
21 */
22template <Reflect::Described T, typename Source, typename Access>
23[[nodiscard]] std::optional<Kakshya::CompositeArray> collect_records(
25 const Source& parent, std::string_view child_name,
26 std::string& error, uint64_t* error_location = nullptr)
27{
29 Kakshya::CompositeInsertion insertion(rows);
30 const bool traversed = Access::for_each_child(parent, [&](const Source& child) {
31 if (Access::name(child) != child_name)
32 return true;
33 const auto record = binding.read(child, error, error_location);
34 if (!record)
35 return false;
36 return Kakshya::write_described(insertion, insertion.append(), *record, error);
37 });
38 if (!traversed)
39 return std::nullopt;
40 return rows;
41}
42
43/**
44 * @brief Decode every row of a typed Composite array into values.
45 *
46 * Works for rows from any source whose layout is the one derived from T:
47 * a delimited file loaded with that layout, or rows made by collect_records.
48 *
49 * @param rows Array whose layout matches described_composite_layout of T.
50 * @param error Receives the reason when nullopt is returned.
51 */
52template <Reflect::Described T>
53[[nodiscard]] std::optional<std::vector<T>> records_of(
54 const Kakshya::CompositeArray& rows, std::string& error)
55{
57 error = "Composite layout does not match the described type";
58 return std::nullopt;
59 }
60 std::vector<T> values;
61 values.reserve(rows.size());
62 for (size_t i = 0; i < rows.size(); ++i) {
63 const auto row = rows.at(i);
64 T value {};
65 if (!row || !Kakshya::overlay_described(*row, value, error))
66 return std::nullopt;
67 values.push_back(std::move(value));
68 }
69 return values;
70}
71
72}
size_t size() const noexcept
Number of packed elements.
const CompositeLayout & layout() const noexcept
Finalized layout shared by all elements.
std::optional< Composite > at(size_t index) const noexcept
Borrow an element by index.
Owns a sequence of values that share a CompositeLayout.
size_t append()
Append an empty element.
Mutable insertion adapter for a CompositeArray.
bool same_structure(const CompositeLayout &other) const noexcept
Compare declared field names, kinds, and nested layouts.
Definition Composite.cpp:71
std::optional< Input > read(const Source &source, std::string &error, uint64_t *error_location=nullptr) const
Decode the source element into a new Input.
Bind described attributes and configured child collections of a source element.
std::optional< std::vector< T > > records_of(const Kakshya::CompositeArray &rows, std::string &error)
Decode every row of a typed Composite array into values.
std::optional< Kakshya::CompositeArray > collect_records(const Reflect::RecordBinding< T, Source, Access > &binding, const Source &parent, std::string_view child_name, std::string &error, uint64_t *error_location=nullptr)
Decode the same-named children of a source element into typed Composite rows.
CompositeLayout described_composite_layout()
Derive a Composite layout from a generated member description.
bool overlay_described(const Composite &source, T &target, std::string &error)
Apply present Composite fields to a described value, preserving other members.
bool write_described(CompositeInsertion &insertion, size_t row, const T &value, std::string &error)
Write all described members into an existing Composite row.
std::pair< std::shared_ptr< Kriya::BufferPipeline >, std::shared_ptr< Buffers::AudioBuffer > > record(const std::shared_ptr< Kakshya::DynamicSoundStream > &stream, uint32_t input_channel, double seconds, std::optional< uint32_t > monitor, uint32_t channel)
Record a live input channel into a stream for a number of seconds.
Definition Rigs.cpp:262