MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
TextBinding.hpp
Go to the documentation of this file.
1#pragma once
2
4#include "TextCodec.hpp"
5
6namespace MayaFlux::Reflect {
7
8/**
9 * @brief Decode one authored text value into a described member.
10 * @param label Names the owner in the diagnostic.
11 */
12template <typename T>
13bool decode_text_field(std::string_view name, std::string_view source,
14 T& value, std::string_view label, std::string& error)
15{
16 if constexpr (TextDecodable<T>) {
18 if (TextCodec<T>::decode(source, value, reason))
19 return true;
20 if (reason == TextError::Cardinality) {
21 const char* shape = " numbers in ";
22 if constexpr (requires { TextCodec<T>::sequence; })
23 shape = " numbers per group in ";
24 error = "<" + std::string(label) + "> needs "
25 + std::to_string(TextCodec<T>::extent) + shape
26 + std::string(name);
27 } else if (reason == TextError::Unsupported) {
28 error = "<" + std::string(label) + "> has an unsupported "
29 + std::string(name) + " " + std::string(source);
30 } else {
31 error = "<" + std::string(label) + "> has an invalid "
32 + std::string(name) + " attribute";
33 }
34 } else {
35 error = "<" + std::string(label) + "> has an unsupported attribute "
36 + std::string(name);
37 }
38 return false;
39}
40
41/**
42 * @brief Overlay named text values onto a described value.
43 * @param fields Callable taking a visitor of (name, text) that returns false to stop.
44 * @note A failed overlay may have changed earlier members of the target.
45 */
46template <Described T, typename Fields>
47bool overlay_text_fields(T& target, Fields&& fields,
48 std::string_view label, std::string& error)
49{
50 return overlay_described_fields(target,
51 [&](const auto& visit) {
52 return fields([&](std::string_view name, std::string_view value) {
53 return visit(name, value);
54 });
55 },
56 [&](std::string_view name, std::string_view source, auto& value) {
57 return decode_text_field(name, source, value, label, error);
58 },
59 [&](std::string_view name) {
60 error = "<" + std::string(label) + "> has an unsupported attribute "
61 + std::string(name);
62 return false;
63 });
64}
65
66}
bool overlay_text_fields(T &target, Fields &&fields, std::string_view label, std::string &error)
Overlay named text values onto a described value.
bool decode_text_field(std::string_view name, std::string_view source, T &value, std::string_view label, std::string &error)
Decode one authored text value into a described member.
bool overlay_described_fields(T &target, Fields &&fields, Decode &&decode, Unknown &&unknown)
Apply present named fields to a described value.
Conversion of authored text to one value type.
Definition TextCodec.hpp:81