10template <Reflect::Described T>
13template <Reflect::Described T>
17template <Reflect::Described T>
26 static_assert(always_false_v<T>,
"No Composite field mapping for this described member type");
33 error =
"Cannot bind described field '" + std::string(name) +
"'";
40 requires(ArithmeticData<T> || GlmType<T>)
44 return layout.
add_field<T>(std::move(name));
48 std::string_view name, T value, std::string& error)
50 return insertion.
set(row, name, value)
55 T& value, std::string& error)
57 const auto decoded = source.get<T>(name);
69 return layout.
add_field<std::string>(std::move(name));
73 std::string_view name,
const std::string& value, std::string& error)
75 return insertion.
set_text(row, name, value)
80 std::string& value, std::string& error)
82 const auto decoded = source.text(name);
94 return layout.
add_field<std::vector<uint8_t>>(std::move(name));
98 std::string_view name,
const std::vector<uint8_t>& value, std::string& error)
100 return insertion.
set_blob(row, name, value)
105 std::vector<uint8_t>& value, std::string& error)
107 const auto decoded = source.blob(name);
110 value.assign(decoded->begin(), decoded->end());
116 requires std::is_enum_v<T>
117struct CompositeFieldCodec<T> {
126 std::string_view name, T value, std::string& error)
129 insertion, row, name,
static_cast<Underlying>(value), error);
133 T& value, std::string& error)
138 value =
static_cast<T
>(decoded);
143template <Reflect::Described T>
144struct CompositeFieldCodec<T> {
151 std::string_view name,
const T& value, std::string& error)
155 const size_t nested_row = nested_insertion.
append();
163 T& value, std::string& error)
165 const auto* nested = source.compound(name);
167 if (!nested || nested->size() != 1
168 || !nested->layout().same_structure(expected))
174template <Reflect::Described T>
182 std::string_view name,
const std::vector<T>& value, std::string& error)
186 for (
const T& item : value) {
190 return insertion.
set_list(row, name, nested)
195 std::vector<T>& value, std::string& error)
197 const auto* nested = source.list(name);
199 if (!nested || !nested->layout().same_structure(expected))
201 std::vector<T> decoded;
202 decoded.reserve(nested->size());
203 for (
size_t i = 0; i < nested->size(); ++i) {
205 const auto record = nested->at(i);
208 decoded.push_back(std::move(item));
210 value = std::move(decoded);
217struct CompositeFieldCodec<std::vector<T>> {
231 std::string_view name,
const std::vector<T>& value, std::string& error)
235 for (
const T& item : value) {
237 nested_insertion, nested_insertion.
append(),
"value", item, error))
240 return insertion.
set_list(row, name, nested)
245 std::vector<T>& value, std::string& error)
247 const auto* nested = source.list(name);
249 if (!nested || !nested->layout().same_structure(expected))
251 std::vector<T> decoded;
252 decoded.reserve(nested->size());
253 for (
size_t i = 0; i < nested->size(); ++i) {
254 const auto record = nested->at(i);
258 if (
record->has(
"value")) {
264 decoded.push_back(std::move(item));
266 value = std::move(decoded);
279 std::string_view name,
const std::optional<T>& value, std::string& error)
282 return insertion.
clear(row, name)
288 std::optional<T>& value, std::string& error)
295 value = std::move(decoded);
301template <Reflect::Described T>
306 std::apply([&](
const auto&... members) {
307 ((added = added &&
CompositeFieldCodec<
typename std::remove_cvref_t<
decltype(members)>::value_type>
308 ::add(layout, std::string(members.name))), ...);
311 throw std::invalid_argument(
"Cannot derive Composite layout from description");
319template <Reflect::Described T>
325 std::apply([&](
const auto&... members) {
326 ((written = written &&
CompositeFieldCodec<
typename std::remove_cvref_t<
decltype(members)>::value_type>
327 ::write(insertion, row, members.name, members.get(value), error)), ...);
336template <Reflect::Described T>
341 [&](
const auto& visit) {
342 return source.for_each_present_field([&](
const CompositeField& field) {
343 return visit(field.
name, field);
347 using Value = std::remove_cvref_t<
decltype(value)>;
350 [&](std::string_view name) {
351 error =
"Unknown described field '" + std::string(name) +
"'";
std::optional< Composite > at(size_t index) const noexcept
Borrow an element by index.
Owns a sequence of values that share a CompositeLayout.
bool set(size_t index, std::string_view field_name, T value)
Write an arithmetic or GLM field of an existing element.
bool clear(size_t index, std::string_view field_name)
Mark a named field absent in an existing element.
bool set_compound(size_t index, std::string_view field_name, const Composite &value)
Copy one record into a compound field.
bool set_text(size_t index, std::string_view field_name, std::string_view value)
Write UTF-8 bytes to a std::string field.
bool set_blob(size_t index, std::string_view field_name, std::span< const uint8_t > value)
Write binary bytes to a fixed or dynamic BLOB field.
size_t append()
Append an empty element.
bool set_list(size_t index, std::string_view field_name, const CompositeArray &values)
Copy ordered records into a list field.
Mutable insertion adapter for a CompositeArray.
bool add_compound_field(std::string name, CompositeLayout layout)
Declare a compound field containing one nested record.
bool add_list_field(std::string name, CompositeLayout item_layout)
Declare a list field containing ordered nested records.
bool add_field(std::string name)
Declare a named arithmetic, GLM, text, or binary field.
Shared field description and byte layout for a CompositeArray.
Read view of one value in a CompositeArray.
A type whose generated sidecar defines Generated::Description<T>::members().
bool described_field_error(std::string_view name, std::string &error)
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.
constexpr bool is_optional_v
bool overlay_described_fields(T &target, Fields &&fields, Decode &&decode, Unknown &&unknown)
Apply present named fields to a described value.
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.
static bool write(CompositeInsertion &insertion, size_t row, std::string_view name, T value, std::string &error)
static bool read(const Composite &source, std::string_view name, T &value, std::string &error)
static bool add(CompositeLayout &layout, std::string name)
static bool write(CompositeInsertion &insertion, size_t row, std::string_view name, const T &value, std::string &error)
std::underlying_type_t< T > Underlying
static bool add(CompositeLayout &layout, std::string name)
static bool write(CompositeInsertion &insertion, size_t row, std::string_view name, const std::optional< T > &value, std::string &error)
static bool read(const Composite &source, std::string_view name, std::optional< T > &value, std::string &error)
static bool read(const Composite &source, std::string_view name, std::string &value, std::string &error)
static bool write(CompositeInsertion &insertion, size_t row, std::string_view name, const std::string &value, std::string &error)
static bool add(CompositeLayout &layout, std::string name)
static bool write(CompositeInsertion &insertion, size_t row, std::string_view name, const std::vector< T > &value, std::string &error)
static bool read(const Composite &source, std::string_view name, std::vector< T > &value, std::string &error)
static CompositeLayout item_layout()
static bool add(CompositeLayout &layout, std::string name)
static bool read(const Composite &source, std::string_view name, std::vector< uint8_t > &value, std::string &error)
static bool add(CompositeLayout &layout, std::string name)
static bool write(CompositeInsertion &insertion, size_t row, std::string_view name, const std::vector< uint8_t > &value, std::string &error)
Mapping of one described member to a native Composite field.
Description of one named value in a Composite.