ttx 0.1.0
Loading...
Searching...
No Matches
params.h
Go to the documentation of this file.
1#pragma once
2
3#include "di/container/interface/access.h"
4#include "di/container/string/string.h"
5#include "di/container/string/string_view.h"
6#include "di/container/vector/vector.h"
7#include "di/util/initializer_list.h"
8
9namespace ttx {
10// Paramaters are integer with the addition of nullability.
11// We use a special type so that we be more space efficent.
12class Param {
13public:
14 Param() = default;
15
16 constexpr Param(u32 value) : m_value(value + 1) {}
17
18 constexpr auto value() const -> u32 {
19 DI_ASSERT(has_value());
20 return m_value - 1;
21 }
22
23 constexpr auto has_value() const -> bool { return m_value != 0; }
24
25 constexpr auto value_or(u32 fallback) const -> u32 {
26 if (!has_value()) {
27 return fallback;
28 }
29 return value();
30 }
31
32 auto operator==(Param const&) const -> bool = default;
33
34private:
35 u32 m_value { 0 };
36};
37
38// Subparams are separated by `:` characters. The subparam
39// object implicitly holds a reference to its corresponding
40// `Params` class.
41class Subparams {
42public:
43 Subparams() = default;
44
45 constexpr auto get(usize index = 0, u32 fallback = 0) const -> u32 {
46 return m_subparams.at(index).value_or(Param(fallback)).value_or(fallback);
47 }
48
49 constexpr auto empty() const { return m_subparams.empty(); }
50 constexpr auto size() const { return m_subparams.size(); }
51
52 auto to_string() const -> di::String;
53
54 auto operator==(Subparams const& other) const -> bool = default;
55
56private:
57 friend class Params;
58
59 constexpr explicit Subparams(di::Span<Param const> subparams) : m_subparams(subparams) {}
60
61 di::Span<Param const> m_subparams;
62};
63
64// Represents a series of numeric parameters for an escape
65// sequence. Parameters are separated by `;` characeters, and
66// subparameters are separated by `:` characters.
67class Params {
68public:
69 static auto from_string(di::StringView view) -> Params;
70
71 Params() = default;
72
73 constexpr Params(std::initializer_list<std::initializer_list<Param>> params) {
74 for (auto const& subparams : params) {
75 m_parameters.emplace_back(subparams);
76 }
77 }
78
79 constexpr auto clone() const -> Params { return Params(m_parameters.clone()); }
80
81 constexpr auto get(usize index = 0, u32 fallback = 0) const -> u32 {
82 return m_parameters.at(index).and_then(di::at(0)).value_or(Param(fallback)).value_or(fallback);
83 }
84 constexpr auto get_subparam(usize index = 0, usize subindex = 1, u32 fallback = 0) const -> u32 {
85 return m_parameters.at(index).and_then(di::at(subindex)).value_or(Param(fallback)).value_or(fallback);
86 }
87
88 constexpr auto empty() const { return m_parameters.empty(); }
89 constexpr auto size() const { return m_parameters.size(); }
90
91 constexpr auto subparams(usize index = 0) const -> Subparams {
92 auto span = m_parameters.at(index)
93 .transform([&](auto const& subparams) {
94 return subparams.span();
95 })
96 .value_or(di::Span<Param const> {});
97 return Subparams(span);
98 }
99
100 constexpr void add_empty_param() { m_parameters.emplace_back(); }
101 constexpr void add_param(u32 value) { m_parameters.push_back({ value }); }
102
103 constexpr void add_subparam(u32 value) {
104 if (empty()) {
105 add_param(value);
106 } else {
107 m_parameters.back().value().push_back(value);
108 }
109 }
110 constexpr void add_empty_subparam() {
111 if (empty()) {
113 }
114 m_parameters.back().value().emplace_back();
115 }
116 constexpr void add_subparams(di::Vector<Param> subparams) { m_parameters.push_back(di::move(subparams)); }
117
118 auto to_string() const -> di::String;
119
120 auto operator==(Params const& other) const -> bool = default;
121
122private:
123 constexpr explicit Params(di::Vector<di::Vector<Param>> params) : m_parameters(di::move(params)) {}
124
125 di::Vector<di::Vector<Param>> m_parameters;
126};
127}
Definition params.h:12
constexpr Param(u32 value)
Definition params.h:16
constexpr auto has_value() const -> bool
Definition params.h:23
constexpr auto value_or(u32 fallback) const -> u32
Definition params.h:25
constexpr auto value() const -> u32
Definition params.h:18
auto operator==(Param const &) const -> bool=default
Param()=default
constexpr auto get(usize index=0, u32 fallback=0) const -> u32
Definition params.h:81
constexpr Params(std::initializer_list< std::initializer_list< Param > > params)
Definition params.h:73
constexpr void add_param(u32 value)
Definition params.h:101
auto to_string() const -> di::String
Definition params.cpp:33
constexpr auto empty() const
Definition params.h:88
constexpr void add_empty_subparam()
Definition params.h:110
constexpr auto clone() const -> Params
Definition params.h:79
constexpr void add_empty_param()
Definition params.h:100
constexpr void add_subparams(di::Vector< Param > subparams)
Definition params.h:116
constexpr auto subparams(usize index=0) const -> Subparams
Definition params.h:91
Params()=default
constexpr auto get_subparam(usize index=0, usize subindex=1, u32 fallback=0) const -> u32
Definition params.h:84
static auto from_string(di::StringView view) -> Params
Definition params.cpp:8
constexpr void add_subparam(u32 value)
Definition params.h:103
constexpr auto size() const
Definition params.h:89
Definition params.h:41
constexpr auto size() const
Definition params.h:50
Subparams()=default
friend class Params
Definition params.h:57
constexpr auto get(usize index=0, u32 fallback=0) const -> u32
Definition params.h:45
auto to_string() const -> di::String
Definition params.cpp:23
constexpr auto empty() const
Definition params.h:49
Definition test_params.cpp:4
Definition cursor_style.h:5