Loading...
Searching...
No Matches
Table1D.hpp
1#pragma once
2
3#include <ossia/network/value/value.hpp>
4#include <ossia/network/value/value_conversion.hpp>
5
6#include <halp/controls.hpp>
7#include <halp/meta.hpp>
8
9#include <algorithm>
10#include <vector>
11
12namespace avnd_tools
13{
14
15struct Table1D
16{
17 halp_meta(name, "Table (1D)")
18 halp_meta(author, "ossia team")
19 halp_meta(category, "Control/Data processing")
20 halp_meta(description, "Store arbitrary data in a 1-dimensional table")
21 halp_meta(c_name, "avnd_table_1d")
22 halp_meta(uuid, "a7b3c1d2-4e5f-6789-abcd-ef0123456781")
23 halp_meta(manual_url, "https://ossia.io/score-docs/processes/table.html")
24
25 using value_type = ossia::value;
26
27 struct
28 {
29 struct : halp::val_port<"Read", std::optional<int64_t>>
30 {
31 void update(Table1D& t)
32 {
33 if(value)
34 {
35 if(t.read(*value))
36 return;
37 }
38 t.outputs.output.value = ossia::value{};
39 }
40 } read;
41
42 struct : halp::val_port<"Set", std::pair<int64_t, ossia::value>>
43 {
44 void update(Table1D& t) { t.set(value.first, value.second); }
45 } set;
46
47 struct : halp::val_port<"Prepend", ossia::value>
48 {
49 void update(Table1D& t) { t.prepend(value); }
50 } prepend;
51
52 struct : halp::val_port<"Append", ossia::value>
53 {
54 void update(Table1D& t) { t.append(value); }
55 } append;
56
57 struct : halp::val_port<"Insert", std::pair<int64_t, ossia::value>>
58 {
59 void update(Table1D& t) { t.insert(value.first, value.second); }
60 } insert;
61
62 struct : halp::val_port<"Erase", int64_t>
63 {
64 void update(Table1D& t) { t.erase(value); }
65 } erase;
66
67 struct : halp::val_port<"Pop front", bool>
68 {
69 void update(Table1D& t)
70 {
71 if(value)
72 t.pop_front();
73 }
74 } pop_front;
75
76 struct : halp::val_port<"Pop back", bool>
77 {
78 void update(Table1D& t)
79 {
80 if(value)
81 t.pop_back();
82 }
83 } pop_back;
84
85 struct : halp::val_port<"Resize", int64_t>
86 {
87 void update(Table1D& t) { t.resize(value); }
88 } resize;
89
90 struct : halp::val_port<"Fill", ossia::value>
91 {
92 void update(Table1D& t) { t.fill(value); }
93 } fill_port;
94
95 halp::maintained_button<"Clear"> clear;
96 halp::maintained_button<"Lock"> lock;
97 struct : halp::impulse_button<"Dump">
98 {
99 void update(Table1D& t) { t.outputs.output.value = t.buffer; }
100 } dump;
101 halp::toggle<"Preserve"> preserve;
102
103 } inputs;
104
105 struct
106 {
107 halp::val_port<"Output", ossia::value> output;
108 halp::val_port<"Front", ossia::value> front;
109 halp::val_port<"Back", ossia::value> back;
110 halp::val_port<"Size", int64_t> size;
111 halp::val_port<"All", std::vector<ossia::value>> all;
112 } outputs;
113
114 std::vector<value_type> buffer;
115
116 bool read(int64_t index)
117 {
118 if(index < 0)
119 return false;
120
121 const std::size_t idx = static_cast<std::size_t>(index);
122 if(idx >= buffer.size())
123 return false;
124
125 outputs.output.value = buffer[idx];
126 return true;
127 }
128
129 void set(int64_t index, const value_type& value)
130 {
131 if(index < 0 || index >= INT_MAX)
132 return;
133
134 const std::size_t idx = static_cast<std::size_t>(index);
135
136 if(idx >= buffer.size())
137 buffer.resize(idx + 1);
138
139 buffer[idx] = value;
140 }
141
142 void prepend(const value_type& value) { buffer.insert(buffer.begin(), value); }
143
144 void append(const value_type& value) { buffer.push_back(value); }
145
146 void insert(int64_t index, const value_type& value)
147 {
148 if(index < 0)
149 return;
150
151 const std::size_t idx = static_cast<std::size_t>(index);
152
153 if(idx >= buffer.size())
154 {
155 set(index, value);
156 return;
157 }
158
159 buffer.insert(buffer.begin() + idx, value);
160 }
161
162 void erase(int64_t index)
163 {
164 if(index < 0)
165 return;
166
167 const std::size_t idx = static_cast<std::size_t>(index);
168
169 if(idx >= buffer.size())
170 return;
171
172 buffer.erase(buffer.begin() + idx);
173 }
174
175 void pop_front()
176 {
177 if(buffer.empty())
178 return;
179 buffer.erase(buffer.begin());
180 }
181
182 void pop_back()
183 {
184 if(buffer.empty())
185 return;
186 buffer.pop_back();
187 }
188
189 void resize(int64_t new_size)
190 {
191 if(new_size < 0)
192 return;
193 buffer.resize(static_cast<std::size_t>(new_size));
194 }
195
196 void fill(const value_type& value) { std::fill(buffer.begin(), buffer.end(), value); }
197
198 void do_clear() { buffer.clear(); }
199
200 void operator()()
201 {
202 if(inputs.clear)
203 {
204 do_clear();
205 }
206
207 const std::size_t sz = buffer.size();
208
209 outputs.size.value = static_cast<int64_t>(sz);
210
211 if(sz > 0)
212 {
213 outputs.front.value = buffer.front();
214 outputs.back.value = buffer.back();
215 }
216 else
217 {
218 outputs.front.value = ossia::value{};
219 outputs.back.value = ossia::value{};
220 }
221
222 outputs.all.value = buffer;
223 }
224};
225
226} // namespace avnd_tools
Definition Table1D.hpp:16