MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
ControlValueText.hpp
Go to the documentation of this file.
1#pragma once
2
4
6
7#include <glm/vec2.hpp>
8#include <glm/vec3.hpp>
9#include <glm/vec4.hpp>
10
11namespace MayaFlux::Reflect {
12
13/**
14 * @brief A numeric control slot from text: one number, `true` or `false`, a vector of two to four numbers, or a box of six.
15 *
16 * The count of numbers picks the shape: 1 a number, 2 to 4 a float vector, 6 a
17 * box of min then max. Any other count is Invalid. The slot takes the value as
18 * a new pending one, so the next due() is true. A slot cannot know an enum's
19 * type from text, so enumerators are written as their number.
20 */
21template <NumericControlSlot T>
22struct TextCodec<T> {
23 static constexpr size_t extent = 0;
24
25 static bool decode(std::string_view source, T& value, TextError& error)
26 {
27 if (source == "true" || source == "false") {
28 value.assign(T(source == "true"));
29 return true;
30 }
31
32 const auto numbers = parse_numbers<double>(source);
33 if (!numbers) {
34 error = TextError::Invalid;
35 return false;
36 }
37
38 const auto& n = *numbers;
39 const auto f = [&](size_t index) { return static_cast<float>(n[index]); };
40 switch (n.size()) {
41 case 1:
42 value.assign(T(n[0]));
43 return true;
44 case 2:
45 value.assign(T(glm::vec2(f(0), f(1))));
46 return true;
47 case 3:
48 value.assign(T(glm::vec3(f(0), f(1), f(2))));
49 return true;
50 case 4:
51 value.assign(T(glm::vec4(f(0), f(1), f(2), f(3))));
52 return true;
53 case 6: {
54 struct Box {
55 glm::vec3 min;
56 glm::vec3 max;
57 };
58 value.assign(T(Box { { f(0), f(1), f(2) }, { f(3), f(4), f(5) } }));
59 return true;
60 }
61 default:
62 error = TextError::Invalid;
63 return false;
64 }
65 }
66};
67
68}
std::optional< std::vector< T > > parse_numbers(std::string_view source)
Parse whitespace separated finite numbers.
Definition TextCodec.hpp:17
static bool decode(std::string_view source, T &value, TextError &error)
Conversion of authored text to one value type.
Definition TextCodec.hpp:81