Sygaldry
Loading...
Searching...
No Matches
sygah-endpoints.hpp
1#pragma once
2/*
3Copyright 2023 Travis J. West, https://traviswest.ca, Input Devices and Music
4Interaction Laboratory (IDMIL), Centre for Interdisciplinary Research in Music
5Media and Technology (CIRMMT), McGill University, Montréal, Canada, and Univ.
6Lille, Inria, CNRS, Centrale Lille, UMR 9189 CRIStAL, F-59000 Lille, France
7
8SPDX-License-Identifier: MIT
9*/
10
11#include <string_view>
12#include <string>
13#include <array>
14#include "sygah-consteval.hpp"
15#include "sygah-metadata.hpp"
16
17namespace sygaldry {
18
22
32
40
42template<typename T> requires std::integral<T> || std::floating_point<T>
44{
46 using type = T;
50 _consteval num_literal(T f) : value{f} {}
52 operator T() {return value;}
53};
55template<num_literal _min, num_literal _max, num_literal _init = _min>
56struct range_
57{
59 static _consteval auto range()
60 {
61 struct {
62 decltype(_min.value) min = _min.value;
63 decltype(_max.value) max = _max.value;
64 decltype(_init.value) init = _init.value;
65 } r;
66 return r;
67 }
69 static _consteval auto min() { return _min.value; }
71 static _consteval auto max() { return _max.value; }
73 static _consteval auto init() { return _init.value; }
74};
86template <typename T>
88{
90 using type = T;
94 constexpr persistent() noexcept : value{} {}
95
96 // convert from T
98 constexpr persistent(T&& t) noexcept {value = std::move(t);}
100 constexpr persistent(const T& t) noexcept : value{t} {}
102 constexpr auto& operator=(T&& t) noexcept {value = std::move(t); return *this;}
104 constexpr auto& operator=(const T& t) noexcept {value = t; return *this;}
105
106 // convert to T
108 constexpr operator T&() noexcept {return value;}
110 constexpr operator const T&() const noexcept {return value;}
111};
126template <typename T>
128{
130 using type = T;
135
137 constexpr occasional() noexcept : state{}, updated{false} {}
138
144 constexpr occasional(occasional<T>&& other)
145 {
146 if (other.updated)
147 {
148 state = std::move(other.state);
149 updated = true;
150 } else updated = false;
151 }
152
158 constexpr occasional(const occasional<T>& other)
159 {
160 if (other.updated)
161 {
162 state = other.state;
163 updated = true;
164 } else updated = false;
165 }
166
172 constexpr auto& operator=(occasional<T>&& other)
173 {
174 if (other.updated)
175 {
176 state = std::move(other.state);
177 updated = true;
178 } else updated = false;
179 return *this;
180 }
181
187 constexpr auto& operator=(const occasional<T>& other)
188 {
189 if (other.updated)
190 {
191 state = other.state;
192 updated = true;
193 } else updated = false; // keep current value
194 return *this;
195 }
196
197 // convenience access by conversion to underlying type; value semantics
199 constexpr operator T&() noexcept {return state;}
201 constexpr operator const T&() const noexcept {return state;}
202
203 // optional-like semantics
205 constexpr occasional(T&& t) noexcept : state{std::move(t)}, updated{true} {}
207 constexpr occasional(const T& t) noexcept : state{t}, updated{true} {}
209 constexpr auto& operator=(T&& t) noexcept {state = std::move(t); updated = true; return *this;}
211 constexpr auto& operator=(const T& t) noexcept {state = t; updated = true; return *this;}
213 constexpr T& operator *() noexcept {return state;}
215 constexpr const T& operator *() const noexcept {return state;}
217 constexpr T* operator ->() noexcept {return &state;}
219 constexpr const T* operator ->() const noexcept {return &state;}
221 constexpr T& value() noexcept {return state;}
223 constexpr const T& value() const noexcept {return state;}
225 constexpr void reset() noexcept {updated = false;} // maintains current state
226};
227
228
230
238#define tag(TAG) struct tag_##TAG {enum {TAG}; }
239
245tag(write_only);
246
252tag(session_data);
253#undef tag
255template<typename ... Tags>
256struct tagged_ : Tags... {};
264template<string_literal name_str, string_literal desc = "", char init = 0, typename ... Tags>
265struct button
266: occasional<char>
267, name_<name_str>
268, description_<desc>
269, range_<0, 1, init>
270, tagged_<Tags...>
271{
272 using occasional<char>::operator=;
273};
274
282template<string_literal name_str, string_literal desc = "", char init = 0, typename ... Tags>
283struct toggle
284: persistent<char>
285, name_<name_str>
286, description_<desc>
287, range_<0, 1, init>
288, tagged_<Tags...>
289{
290 using persistent<char>::operator=;
291};
292
298template<string_literal name_str, string_literal desc = "", typename ... Tags>
299struct text
300: persistent<std::string>
301, name_<name_str>
302, description_<desc>
303, tagged_<Tags...>
304{
305 using persistent<std::string>::operator=;
306};
307
314template<string_literal name_str, string_literal desc = "", typename ... Tags>
316: occasional<std::string>
317, name_<name_str>
318, description_<desc>
319, tagged_<Tags...>
320{
321 using occasional<std::string>::operator=;
322};
323
334template< string_literal name_str
335 , string_literal desc = ""
336 , typename T = float
337 , num_literal<T> min = 0.0f
338 , num_literal<T> max = 1.0f
339 , num_literal<T> init = min
340 , typename ... Tags
341 >
342struct slider
343: persistent<T>
344, name_<name_str>
345, description_<desc>
346, range_<min, max, init>
347, tagged_<Tags...>
348{
349 using persistent<T>::operator=;
350};
351
362template< string_literal name_str
363 , string_literal desc = ""
364 , typename T = float
365 , num_literal<T> min = 0.0f
366 , num_literal<T> max = 1.0f
367 , num_literal<T> init = min
368 , typename ... Tags
369 >
371: occasional<T>
372, name_<name_str>
373, description_<desc>
374, range_<min, max, init>
375, tagged_<Tags...>
376{
377 using occasional<T>::operator=;
378};
379
391template< string_literal name_str
392 , std::size_t N
393 , string_literal desc = ""
394 , typename T = float
395 , num_literal<T> min = 0.0f
396 , num_literal<T> max = 1.0f
397 , num_literal<T> init = min
398 , typename ... Tags
399 >
400struct array
401: persistent<std::array<T, N>>
402, name_<name_str>
403, description_<desc>
404, range_<min, max, init>
405, tagged_<Tags...>
406{
407 using persistent<std::array<T, N>>::operator=;
408 using type = T;
409 constexpr const auto& operator[](std::size_t i) const noexcept
410 {
412 }
413 constexpr auto& operator[](std::size_t i) noexcept
414 {
416 }
417 static _consteval auto size() noexcept
418 {
419 return N;
420 }
421};
422
434template< string_literal name_str
435 , std::size_t N
436 , string_literal desc = ""
437 , typename T = float
438 , num_literal<T> min = 0.0f
439 , num_literal<T> max = 1.0f
440 , num_literal<T> init = min
441 , typename ... Tags
442 >
444: occasional<std::array<T, N>>
445, name_<name_str>
446, description_<desc>
447, range_<min, max, init>
448, tagged_<Tags...>
449{
450 using occasional<std::array<T, N>>::operator=;
451 using type = T;
452 constexpr const auto& operator[](std::size_t i) const noexcept
453 {
455 }
456 constexpr auto& operator[](std::size_t i) noexcept
457 {
459 }
460 static _consteval auto size() noexcept
461 {
462 return N;
463 }
464 void set_updated() noexcept
465 {
467 }
468};
475template<string_literal name_str, string_literal desc = "", typename ... Tags>
476struct bng
477: persistent<bool>
478, name_<name_str>
479, description_<"">
480, tagged_<Tags...>
481{
482 using persistent<bool>::operator=;
483 enum {bang, impulse};
484 void operator()() {value = true;}
485 void reset() {value = false;}
486};
487
489} // namespaces
#define tag(TAG)
Helper struct for defining recognized tags. This is immediately undefed, so don't try to use it!
Definition sygah-endpoints.hpp:238
static _consteval auto min()
Returns the minimum value of the range.
Definition sygah-endpoints.hpp:69
static _consteval auto range()
Returns a struct with min, max, and init members containing the range.
Definition sygah-endpoints.hpp:59
bool updated
Flag indicating if the state has been changed.
Definition sygah-endpoints.hpp:134
constexpr T * operator->() noexcept
Mutable member access operator; provides access to the members of the underlying state.
Definition sygah-endpoints.hpp:217
constexpr auto & operator=(T &&t) noexcept
Move assignment from the underlying type.
Definition sygah-endpoints.hpp:209
T type
The underlying type.
Definition sygah-endpoints.hpp:130
constexpr occasional(const occasional< T > &other)
Copy constructor from another occasional.
Definition sygah-endpoints.hpp:158
constexpr auto & operator=(occasional< T > &&other)
Move assignment from another occasional.
Definition sygah-endpoints.hpp:172
static _consteval auto max()
Returns the maximum value of the range.
Definition sygah-endpoints.hpp:71
constexpr auto & operator=(const T &t) noexcept
Copy assignment from the underlying type.
Definition sygah-endpoints.hpp:211
constexpr persistent(const T &t) noexcept
Copy constructor from T
Definition sygah-endpoints.hpp:100
constexpr auto & operator=(T &&t) noexcept
Move assignment from T
Definition sygah-endpoints.hpp:102
constexpr occasional() noexcept
Default constructor; state is default initialized and updated is false.
Definition sygah-endpoints.hpp:137
constexpr T & value() noexcept
Mutable value access.
Definition sygah-endpoints.hpp:221
constexpr persistent() noexcept
Default constuctor defers to T's empty initializer.
Definition sygah-endpoints.hpp:94
constexpr const T & value() const noexcept
Immutable value access.
Definition sygah-endpoints.hpp:223
constexpr occasional(occasional< T > &&other)
Move constructor from another occasional.
Definition sygah-endpoints.hpp:144
T state
The wrapped state.
Definition sygah-endpoints.hpp:132
constexpr occasional(T &&t) noexcept
Move constructor from the underlying type.
Definition sygah-endpoints.hpp:205
T type
The underlying type.
Definition sygah-endpoints.hpp:90
constexpr auto & operator=(const occasional< T > &other)
Copy assignment from another occasional.
Definition sygah-endpoints.hpp:187
constexpr T & operator*() noexcept
Mutable dereference operator; provides access to the underlying state.
Definition sygah-endpoints.hpp:213
constexpr void reset() noexcept
Clear the updated flag. This can also be achieved by assignment from empty braces,...
Definition sygah-endpoints.hpp:225
constexpr occasional(const T &t) noexcept
Copy constructor from the underlying type.
Definition sygah-endpoints.hpp:207
static _consteval auto init()
Returns the initial value of the range.
Definition sygah-endpoints.hpp:73
T value
The wrapped value.
Definition sygah-endpoints.hpp:92
T value
The value of the constant.
Definition sygah-endpoints.hpp:48
T type
The underlying numeric type.
Definition sygah-endpoints.hpp:46
constexpr auto & operator=(const T &t) noexcept
Copy assignment from T
Definition sygah-endpoints.hpp:104
constexpr persistent(T &&t) noexcept
Move constructor from T
Definition sygah-endpoints.hpp:98
_consteval num_literal(T f)
Constructor enabling initialization from a literal constant.
Definition sygah-endpoints.hpp:50
A multi-dimensional numeric endpoint with user customizeable range and occasional message semantics.
Definition sygah-endpoints.hpp:449
A multi-dimensional numeric endpoint with user customizeable range and persistent value semantics.
Definition sygah-endpoints.hpp:406
A semantically value-less endpoint that signals an event.
Definition sygah-endpoints.hpp:481
A two-state integer endpoint with occasional message semantics.
Definition sygah-endpoints.hpp:271
Document a textual description of an entity, e.g. an endpoint, component or binding.
Definition sygah-metadata.hpp:35
Document the name of an entity, e.g. an endpoint, component, or binding.
Definition sygah-metadata.hpp:33
A wrapper around a numeric literal that enables using floats as template parameters.
Definition sygah-endpoints.hpp:44
Wrapper around a type T with optional-like semantics and persistent state.
Definition sygah-endpoints.hpp:128
A wrapper around a value-like type.
Definition sygah-endpoints.hpp:88
Document the expected minimum, maximum, and initial value of an endpoint.
Definition sygah-endpoints.hpp:57
A numeric endpoint with user customizeable range and occasional value semantics.
Definition sygah-endpoints.hpp:376
A numeric endpoint with user customizeable range and persistent value semantics.
Definition sygah-endpoints.hpp:348
A wrapper around a string lteral that enables using strings as template parameters.
Definition sygah-string_literal.hpp:25
Apply the tag helpers classes Tags to an entity, especially an endpoint.
Definition sygah-endpoints.hpp:256
A text string endpoint with occasional message semantics.
Definition sygah-endpoints.hpp:320
A text string endpoint with persistent value semantics.
Definition sygah-endpoints.hpp:304
A two-state integer endpoint with persistent value semantics.
Definition sygah-endpoints.hpp:289