OSSIA
Open Scenario System for Interactive Application
domain_base_impl.hpp
1 #pragma once
2 #include <ossia/detail/flat_set.hpp>
3 #include <ossia/detail/optional.hpp>
6 
7 #include <type_traits>
8 
9 namespace ossia
10 {
11 
12 OSSIA_EXPORT ossia::value
13 clamp(const ossia::value& val, const ossia::value& min, const ossia::value& max);
14 OSSIA_EXPORT ossia::value
15 wrap(const ossia::value& val, const ossia::value& min, const ossia::value& max);
16 OSSIA_EXPORT ossia::value
17 fold(const ossia::value& val, const ossia::value& min, const ossia::value& max);
18 OSSIA_EXPORT ossia::value clamp_min(const ossia::value& val, const ossia::value& min);
19 OSSIA_EXPORT ossia::value clamp_max(const ossia::value& val, const ossia::value& max);
20 
21 OSSIA_EXPORT ossia::value
22 clamp(ossia::value&& val, const ossia::value& min, const ossia::value& max);
23 OSSIA_EXPORT ossia::value
24 wrap(ossia::value&& val, const ossia::value& min, const ossia::value& max);
25 OSSIA_EXPORT ossia::value
26 fold(ossia::value&& val, const ossia::value& min, const ossia::value& max);
27 OSSIA_EXPORT ossia::value clamp_min(ossia::value&& val, const ossia::value& min);
28 OSSIA_EXPORT ossia::value clamp_max(ossia::value&& val, const ossia::value& max);
29 
30 template <typename T>
31 struct OSSIA_EXPORT domain_base
32 {
33  using value_type = typename value_trait<T>::value_type;
34  std::optional<value_type> min;
35  std::optional<value_type> max;
36  std::vector<value_type> values;
37 
38  domain_base() noexcept { }
39  domain_base(const domain_base& other) noexcept
40  : min{other.min}
41  , max{other.max}
42  , values{other.values}
43  {
44  }
45 
46  domain_base(domain_base&& other) noexcept
47  : min{std::move(other.min)}
48  , max{std::move(other.max)}
49  , values{std::move(other.values)}
50  {
51  }
52 
53  domain_base& operator=(const domain_base& other)
54  {
55  min = other.min;
56  max = other.max;
57  values = other.values;
58  return *this;
59  }
60 
61  domain_base& operator=(domain_base&& other) noexcept
62  {
63  min = std::move(other.min);
64  max = std::move(other.max);
65  values = std::move(other.values);
66  return *this;
67  }
68 
69  friend bool operator==(const domain_base<T>& lhs, const domain_base<T>& rhs)
70  {
71  return lhs.min == rhs.min && lhs.max == rhs.max && lhs.values == rhs.values;
72  }
73  friend bool operator!=(const domain_base<T>& lhs, const domain_base<T>& rhs)
74  {
75  return lhs.min != rhs.min || lhs.max != rhs.max || lhs.values != rhs.values;
76  }
77 
78  domain_base(value_type v1, value_type v2)
79  : min{v1}
80  , max{v2}
81  {
82  }
83  domain_base(value_type v1, value_type v2, const ossia::flat_set<value_type>& vals)
84  : min{v1}
85  , max{v2}
86  , values{vals}
87  {
88  }
89  domain_base(value_type v1, value_type v2, ossia::flat_set<value_type>&& vals)
90  : min{v1}
91  , max{v2}
92  , values{std::move(vals)}
93  {
94  }
95 };
96 
97 template <>
98 struct OSSIA_EXPORT domain_base<impulse>
99 {
100  using value_type = ossia::impulse;
101  friend bool
102  operator==(const domain_base<impulse>& lhs, const domain_base<impulse>& rhs)
103  {
104  return true;
105  }
106  friend bool
107  operator!=(const domain_base<impulse>& lhs, const domain_base<impulse>& rhs)
108  {
109  return false;
110  }
111 };
112 
113 template <>
114 struct OSSIA_EXPORT domain_base<bool>
115 {
116  using value_type = bool;
117  static const constexpr bool min = false;
118  static const constexpr bool max = true;
119  friend bool operator==(const domain_base<bool>& lhs, const domain_base<bool>& rhs)
120  {
121  return true;
122  }
123  friend bool operator!=(const domain_base<bool>& lhs, const domain_base<bool>& rhs)
124  {
125  return false;
126  }
127 };
128 
129 template <>
130 struct OSSIA_EXPORT domain_base<std::string>
131 {
132  std::vector<std::string> values;
133  friend bool
134  operator==(const domain_base<std::string>& lhs, const domain_base<std::string>& rhs)
135  {
136  return lhs.values == rhs.values;
137  }
138  friend bool
139  operator!=(const domain_base<std::string>& lhs, const domain_base<std::string>& rhs)
140  {
141  return lhs.values != rhs.values;
142  }
143 };
144 
145 struct OSSIA_EXPORT vector_domain
146 {
147  // If a value does not have a min / max the value won't be valid
148  using value_type = std::vector<ossia::value>;
149  value_type min;
150  value_type max;
151  std::vector<ossia::flat_set<ossia::value>> values;
152 
153  vector_domain() noexcept { }
154  vector_domain(const vector_domain& other) noexcept
155  : min(other.min)
156  , max(other.max)
157  , values(other.values)
158  {
159  }
160 
161  vector_domain(vector_domain&& other) noexcept
162  : min(std::move(other.min))
163  , max(std::move(other.max))
164  , values(std::move(other.values))
165  {
166  }
167 
168  vector_domain& operator=(const vector_domain& other)
169  {
170  min = other.min;
171  max = other.max;
172  values = other.values;
173  return *this;
174  }
175 
176  vector_domain& operator=(vector_domain&& other) noexcept
177  {
178  min = std::move(other.min);
179  max = std::move(other.max);
180  values = std::move(other.values);
181  return *this;
182  }
183 
184  vector_domain(std::nullopt_t, std::nullopt_t) { }
185 
186  vector_domain(const value_type& v1, const value_type& v2)
187  : min(v1)
188  , max(v2)
189  {
190  }
191  vector_domain(value_type&& v1, value_type&& v2)
192  : min(std::move(v1))
193  , max(std::move(v2))
194  {
195  }
196  vector_domain(
197  const value_type& v1, const value_type& v2,
198  const std::vector<ossia::flat_set<ossia::value>>& vals)
199  : min(v1)
200  , max(v2)
201  , values(vals)
202  {
203  }
204  vector_domain(
205  value_type&& v1, value_type&& v2,
206  std::vector<ossia::flat_set<ossia::value>>&& vals)
207  : min(std::move(v1))
208  , max(std::move(v2))
209  , values(std::move(vals))
210  {
211  }
212 
213  friend bool operator==(const vector_domain& lhs, const vector_domain& rhs)
214  {
215  return lhs.min == rhs.min && lhs.max == rhs.max && lhs.values == rhs.values;
216  }
217  friend bool operator!=(const vector_domain& lhs, const vector_domain& rhs)
218  {
219  return lhs.min != rhs.min || lhs.max != rhs.max || lhs.values != rhs.values;
220  }
221 };
222 
223 template <std::size_t N>
224 struct OSSIA_EXPORT vecf_domain
225 {
226  using value_type = std::array<float, N>;
227  std::array<std::optional<float>, N> min;
228  std::array<std::optional<float>, N> max;
229  std::array<ossia::flat_set<float>, N> values;
230 
231  vecf_domain() noexcept { }
232  vecf_domain(const vecf_domain& other) noexcept
233  : min{std::move(other.min)}
234  , max{std::move(other.max)}
235  , values{std::move(other.values)}
236  {
237  }
238 
239  vecf_domain(vecf_domain&& other) noexcept
240  : min{std::move(other.min)}
241  , max{std::move(other.max)}
242  , values{std::move(other.values)}
243  {
244  }
245 
246  vecf_domain& operator=(const vecf_domain& other)
247  {
248  min = other.min;
249  max = other.max;
250  values = other.values;
251  return *this;
252  }
253 
254  vecf_domain& operator=(vecf_domain&& other) noexcept
255  {
256  min = std::move(other.min);
257  max = std::move(other.max);
258  values = std::move(other.values);
259  return *this;
260  }
261 
262  vecf_domain(
263  const std::array<std::optional<float>, N>& v1,
264  const std::array<std::optional<float>, N>& v2)
265  : min{v1}
266  , max{v2}
267  {
268  }
269 
270  vecf_domain(const std::array<float, N>& v1, const std::array<float, N>& v2)
271  {
272  for(std::size_t i = 0; i < N; i++)
273  {
274  min[i] = v1[i];
275  max[i] = v2[i];
276  }
277  }
278  vecf_domain(
279  const std::array<std::optional<float>, N>& v1,
280  const std::array<std::optional<float>, N>& v2,
281  const std::array<ossia::flat_set<float>, N>& vals)
282  : min{v1}
283  , max{v2}
284  , values{vals}
285  {
286  }
287  vecf_domain(
288  const std::array<std::optional<float>, N>& v1,
289  const std::array<std::optional<float>, N>& v2,
290  std::array<ossia::flat_set<float>, N>&& vals)
291  : min{v1}
292  , max{v2}
293  , values{std::move(vals)}
294  {
295  }
296 
297  friend bool operator==(const vecf_domain& lhs, const vecf_domain& rhs)
298  {
299  return lhs.min == rhs.min && lhs.max == rhs.max && lhs.values == rhs.values;
300  }
301  friend bool operator!=(const vecf_domain& lhs, const vecf_domain& rhs)
302  {
303  return lhs.min != rhs.min || lhs.max != rhs.max || lhs.values != rhs.values;
304  }
305 };
306 
307 template <>
308 struct OSSIA_EXPORT domain_base<ossia::value>
309 {
310  using value_type = ossia::value;
311  std::optional<value_type> min;
312  std::optional<value_type> max;
313  std::vector<value_type> values;
314 
315  domain_base() noexcept { }
316  domain_base(const domain_base<value_type>& other) noexcept
317  : min{std::move(other.min)}
318  , max{std::move(other.max)}
319  , values{std::move(other.values)}
320  {
321  }
322 
323  domain_base(domain_base<value_type>&& other) noexcept
324  : min{std::move(other.min)}
325  , max{std::move(other.max)}
326  , values{std::move(other.values)}
327  {
328  }
329 
330  domain_base& operator=(const domain_base& other)
331  {
332  min = other.min;
333  max = other.max;
334  values = other.values;
335  return *this;
336  }
337 
338  domain_base& operator=(domain_base&& other) noexcept
339  {
340  min = std::move(other.min);
341  max = std::move(other.max);
342  values = std::move(other.values);
343  return *this;
344  }
345 
346  domain_base(const value_type& v1, const value_type& v2)
347  : min{v1}
348  , max{v2}
349  {
350  }
351  domain_base(value_type&& v1, value_type&& v2)
352  : min{std::move(v1)}
353  , max{std::move(v2)}
354  {
355  }
356  domain_base(
357  const value_type& v1, const value_type& v2, const std::vector<value_type>& vals)
358  : min{v1}
359  , max{v2}
360  , values{vals}
361  {
362  }
363  domain_base(value_type&& v1, value_type&& v2, std::vector<value_type>&& vals)
364  : min{std::move(v1)}
365  , max{std::move(v2)}
366  , values{std::move(vals)}
367  {
368  }
369 
370  friend bool
371  operator==(const domain_base<ossia::value>& lhs, const domain_base<ossia::value>& rhs)
372  {
373  return lhs.min == rhs.min && lhs.max == rhs.max && lhs.values == rhs.values;
374  }
375  friend bool
376  operator!=(const domain_base<ossia::value>& lhs, const domain_base<ossia::value>& rhs)
377  {
378  return lhs.min != rhs.min || lhs.max != rhs.max || lhs.values != rhs.values;
379  }
380 };
381 }
The value class.
Definition: value.hpp:173
Definition: git_info.h:7
constexpr OSSIA_INLINE T clamp(T d, const T min, const T max) noexcept
clamp Returns the value bounded by a min and a max
Definition: math.hpp:154
constexpr OSSIA_INLINE 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
constexpr OSSIA_INLINE 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