OSSIA
Open Scenario System for Interactive Application
Loading...
Searching...
No Matches
clamp_visitors.hpp
1#pragma once
2#include <ossia/network/value/value.hpp>
3
4#define FAST_COMPILES
5namespace ossia
6{
7// For clamp, wrap, fold...
8template <typename TernaryFun>
9struct apply_ternary_fun_visitor
10{
11 template <typename T, typename U, typename V>
12 OSSIA_INLINE ossia::value operator()(const T& val, const U& min, const V& max)
13 {
14 return val;
15 }
16#if !defined(FAST_COMPILES)
17 template <typename U, typename V>
18 OSSIA_INLINE ossia::value operator()(List&& val, const U& min, const V& max)
19 {
20 return std::move(val);
21 }
22 template <typename U, typename V>
23 OSSIA_INLINE ossia::value operator()(const List& val, const U& min, const V& max)
24 {
25 return val;
26 }
27 template <typename U, typename V>
28 OSSIA_INLINE ossia::value operator()(String&& val, const U& min, const V& max)
29 {
30 return std::move(val);
31 }
32 template <typename U, typename V>
33 OSSIA_INLINE ossia::value operator()(const String& val, const U& min, const V& max)
34 {
35 return val;
36 }
37#endif
38
39 OSSIA_INLINE ossia::value operator()(int32_t val, int32_t min, int32_t max)
40 {
41 return TernaryFun::compute(val, min, max);
42 }
43 OSSIA_INLINE ossia::value operator()(float val, float min, float max)
44 {
45 return TernaryFun::compute(val, min, max);
46 }
47 OSSIA_INLINE ossia::value operator()(char val, char min, char max)
48 {
49 return (char)TernaryFun::compute((int32_t)val, (int32_t)min, (int32_t)max);
50 }
51 OSSIA_INLINE ossia::value operator()(bool val, bool min, bool max)
52 {
53 return (bool)TernaryFun::compute((int32_t)val, (int32_t)min, (int32_t)max);
54 }
55
56 template <std::size_t N>
57 ossia::value operator()(
58 std::array<float, N> val, const std::array<float, N>& min,
59 const std::array<float, N>& max)
60 {
61 for(std::size_t i = 0; i < N; i++)
62 {
63 val[i] = TernaryFun::compute(val[i], min[i], max[i]);
64 }
65 return val;
66 }
67
68 template <std::size_t N>
69 ossia::value operator()(std::array<float, N> val, float min, float max)
70 {
71 for(std::size_t i = 0; i < N; i++)
72 {
73 val[i] = TernaryFun::compute(val[i], min, max);
74 }
75 return val;
76 }
77
78 // TODO handle clamping of List between two values of other types
79 // (see apply_domain.hpp)
80 ossia::value operator()(
81 const std::vector<ossia::value>& incoming, const std::vector<ossia::value>& min,
82 const std::vector<ossia::value>& max)
83 {
84 std::vector<ossia::value> val;
85 const auto N = incoming.size();
86 const auto nmin = min.size();
87 const auto nmax = max.size();
88 if(N == nmin && N == nmax)
89 {
90 val.reserve(N);
91 for(std::size_t i = 0; i < N; i++)
92 {
93 val.push_back(ossia::apply(*this, incoming[i].v, min[i].v, max[i].v));
94 }
95 }
96 return ossia::value{std::move(val)};
97 }
98
99 ossia::value operator()(
100 std::vector<ossia::value>&& val, const std::vector<ossia::value>& min,
101 const std::vector<ossia::value>& max)
102 {
103 const auto N = val.size();
104 const auto nmin = min.size();
105 const auto nmax = max.size();
106 if(N == nmin && N == nmax)
107 {
108 for(std::size_t i = 0; i < N; i++)
109 {
110 val[i] = ossia::apply(*this, std::move(val)[i].v, min[i].v, max[i].v);
111 }
112 }
113 return ossia::value{std::move(val)};
114 }
115
116 ossia::value operator()(
117 const std::vector<ossia::value>& incoming, const ossia::value& min,
118 const ossia::value& max)
119 {
120 std::vector<ossia::value> val;
121 const auto N = incoming.size();
122 val.reserve(N);
123 for(std::size_t i = 0; i < N; i++)
124 {
125 val.push_back(ossia::apply(*this, incoming[i].v, min.v, max.v));
126 }
127 return ossia::value{std::move(val)};
128 }
129
130 ossia::value operator()(
131 std::vector<ossia::value>&& val, const ossia::value& min, const ossia::value& max)
132 {
133 const auto N = val.size();
134 for(std::size_t i = 0; i < N; i++)
135 {
136 val[i] = ossia::apply(*this, std::move(val)[i].v, min.v, max.v);
137 }
138 return ossia::value{std::move(val)};
139 }
140
142 operator()(const ossia::value& val, const ossia::value& min, const ossia::value& max)
143 {
144 return ossia::apply(*this, val.v, min.v, max.v);
145 }
146
148 operator()(ossia::value&& val, const ossia::value& min, const ossia::value& max)
149 {
150 return ossia::apply(*this, std::move(val.v), min.v, max.v);
151 }
152};
153
154// For clamp_min, clamp_max...
155template <typename BinaryFun>
156struct apply_binary_fun_visitor
157{
158 template <typename T, typename U>
159 OSSIA_INLINE ossia::value operator()(const T& val, const U& min)
160 {
161 return val;
162 }
163
164#if !defined(FAST_COMPILES)
165 template <typename U>
166 OSSIA_INLINE ossia::value operator()(List&& val, const U& min)
167 {
168 return std::move(val);
169 }
170 template <typename U>
171 OSSIA_INLINE ossia::value operator()(const List& val, const U& min)
172 {
173 return val;
174 }
175 template <typename U>
176 OSSIA_INLINE ossia::value operator()(String&& val, const U& min)
177 {
178 return std::move(val);
179 }
180 template <typename U>
181 OSSIA_INLINE ossia::value operator()(const String& val, const U& min)
182 {
183 return val;
184 }
185#endif
186
187 OSSIA_INLINE ossia::value operator()(int32_t val, int32_t min)
188 {
189 return int32_t{BinaryFun::compute(val, min)};
190 }
191 OSSIA_INLINE ossia::value operator()(float val, float min)
192 {
193 return float{BinaryFun::compute(val, min)};
194 }
195 OSSIA_INLINE ossia::value operator()(char val, char min)
196 {
197 return (char)BinaryFun::compute((int32_t)val, (int32_t)min);
198 }
199 OSSIA_INLINE ossia::value operator()(bool val, bool min)
200 {
201 return (bool)BinaryFun::compute((int32_t)val, (int32_t)min);
202 }
203
204 template <std::size_t N>
205 ossia::value operator()(std::array<float, N> val, const std::array<float, N>& min)
206 {
207 for(std::size_t i = 0; i < N; i++)
208 {
209 val[i] = BinaryFun::compute(val[i], min[i]);
210 }
211 return val;
212 }
213
214 template <std::size_t N>
215 ossia::value operator()(std::array<float, N> val, float min)
216 {
217 for(std::size_t i = 0; i < N; i++)
218 {
219 val[i] = BinaryFun::compute(val[i], min);
220 }
221 return val;
222 }
223
224 // TODO handle clamping of List between two values of other types
225 // (see apply_domain.hpp)
226 ossia::value operator()(
227 const std::vector<ossia::value>& incoming, const std::vector<ossia::value>& min)
228 {
229 std::vector<ossia::value> val;
230 const auto N = incoming.size();
231 const auto nmin = min.size();
232 if(N == nmin)
233 {
234 val.reserve(N);
235 for(std::size_t i = 0; i < N; i++)
236 {
237 val.push_back(ossia::apply(*this, incoming[i].v, min[i].v));
238 }
239 }
240 return ossia::value{std::move(val)};
241 }
242
244 operator()(std::vector<ossia::value>&& val, const std::vector<ossia::value>& min)
245 {
246 const auto N = val.size();
247 const auto nmin = min.size();
248 if(N == nmin)
249 {
250 for(std::size_t i = 0; i < N; i++)
251 {
252 val[i] = ossia::apply(*this, std::move(val)[i].v, min[i].v);
253 }
254 }
255 return ossia::value{std::move(val)};
256 }
257
259 operator()(const std::vector<ossia::value>& incoming, const ossia::value& min)
260 {
261 std::vector<ossia::value> val;
262 const auto N = incoming.size();
263 val.reserve(N);
264 for(std::size_t i = 0; i < N; i++)
265 {
266 val.push_back(ossia::apply(*this, incoming[i].v, min.v));
267 }
268 return ossia::value{std::move(val)};
269 }
270
271 ossia::value operator()(std::vector<ossia::value>&& val, const ossia::value& min)
272 {
273 const auto N = val.size();
274 for(std::size_t i = 0; i < N; i++)
275 {
276 val[i] = ossia::apply(*this, std::move(val)[i].v, min.v);
277 }
278 return ossia::value{std::move(val)};
279 }
280
281 ossia::value operator()(const ossia::value& val, const ossia::value& min)
282 {
283 return ossia::apply(*this, val.v, min.v);
284 }
285
286 ossia::value operator()(ossia::value&& val, const ossia::value& min)
287 {
288 return ossia::apply(*this, std::move(val.v), min.v);
289 }
290};
291}
The value class.
Definition value.hpp:173
Definition git_info.h:7
OSSIA_INLINE constexpr auto min(const T a, const U b) noexcept -> typename std::conditional<(sizeof(T) > sizeof(U)), T, U >::type
min function tailored for values
Definition math.hpp:125
OSSIA_INLINE constexpr auto max(const T a, const U b) noexcept -> typename std::conditional<(sizeof(T) > sizeof(U)), T, U >::type
max function tailored for values
Definition math.hpp:96