Anklang 0.3.0-460-gc4ef46ba
ASE — Anklang Sound Engine (C++)

« « « Anklang Documentation
Loading...
Searching...
No Matches
properties.hh
Go to the documentation of this file.
1 // This Source Code Form is licensed MPL-2.0: http://mozilla.org/MPL/2.0
2#ifndef __ASE_PROPERTIES_HH__
3#define __ASE_PROPERTIES_HH__
4
5#include <ase/api.hh>
6#include <ase/object.hh>
7#include <ase/jsonapi.hh>
8
9namespace Ase {
10
12class ParameterProperty : public EmittableImpl, public virtual Property {
13protected:
14 ParameterC parameter_;
15 StringS get_metadata () const override { return parameter_->metadata(); }
16public:
17 String ident () const override { return parameter_->cident; }
18 String label () const override { return parameter_->label(); }
19 String nick () const override { return parameter_->nick(); }
20 String unit () const override { return parameter_->unit(); }
21 double get_min () const override { return std::get<0> (parameter_->range()); }
22 double get_max () const override { return std::get<1> (parameter_->range()); }
23 double get_step () const override { return std::get<2> (parameter_->range()); }
24 bool is_numeric () const override { return parameter_->is_numeric(); }
25 ChoiceS choices () const override { return parameter_->choices(); }
26 void reset () override { set_value (parameter_->initial()); }
27 double get_normalized () const override { return !is_numeric() ? 0 : parameter_->normalize (get_double()); }
28 bool set_normalized (double v) override { return is_numeric() && set_value (parameter_->rescale (v)); }
29 String get_text () const override { return parameter_->value_to_text (get_value()); }
30 bool set_text (String txt) override { set_value (parameter_->value_from_text (txt)); return !txt.empty(); }
31 Value get_value () const override = 0;
32 bool set_value (const Value &v) override = 0;
33 double get_double () const { return !is_numeric() ? 0 : get_value().as_double(); }
34 ParameterC parameter () const { return parameter_; }
35 Value initial () const { return parameter_->initial(); }
36 MinMaxStep range () const { return parameter_->range(); }
37};
38
41 /*ctor*/ Preference (ParameterC parameter);
42public:
43 using DelCb = std::function<void()>;
44 using StringValueF = std::function<void(const String&, const Value&)>;
45 virtual ~Preference ();
46 /*ctor*/ Preference (const Param&, const StringValueF& = nullptr);
47 String gets () const { return const_cast<Preference*> (this)->get_value().as_string(); }
48 bool getb () const { return const_cast<Preference*> (this)->get_value().as_int(); }
49 int64 getn () const { return const_cast<Preference*> (this)->get_value().as_int(); }
50 uint64 getu () const { return const_cast<Preference*> (this)->get_value().as_int(); }
51 double getd () const { return const_cast<Preference*> (this)->get_value().as_double(); }
52 bool set (const Value &value) { return set_value (value); }
53 bool set (const String &string) { return set_value (string); }
54 Value get_value () const override;
55 bool set_value (const Value &v) override;
56 static Value get (const String &ident);
57 static PreferenceP find (const String &ident);
58 static CStringS list ();
59 static DelCb listen (const std::function<void(const CStringS&)>&);
60 static void save_preferences ();
61 static void load_preferences (bool autosave);
62private:
63 DelCb sigh_;
64 Connection *connection_ = nullptr;
66};
67
70
73
76
79 PropertyGetter getter_; PropertySetter setter_; PropertyLister lister_;
80 PropertyImpl (const Param&, const PropertyGetter&, const PropertySetter&, const PropertyLister&);
81public:
83 Value get_value () const override { Value v; getter_ (v); return v; }
84 bool set_value (const Value &v) override { return setter_ (v); }
85 ChoiceS choices () const override { return lister_ ? lister_ (*this) : parameter_->choices(); }
86};
87
89template<typename Enum> std::function<void(Value&)>
91{
92 using EnumType = Jsonipc::Enum<Enum>;
93 return [v] (Value &val) {
94 if (EnumType::has_names())
95 {
96 const String &name = EnumType::get_name (*v);
97 if (!name.empty())
98 {
99 val = name;
100 return;
101 }
102 }
103 val = int64_t (*v);
104 };
105}
106
108template<typename Enum> std::function<bool(const Value&)>
110{
111 using EnumType = Jsonipc::Enum<Enum>;
112 return [v] (const Value &val) {
113 Enum e = *v;
114 if (val.index() == Value::STRING)
115 e = EnumType::get_value (val.as_string(), e);
116 else if (val.index() == Value::INT64)
117 e = Enum (val.as_int());
118 ASE_RETURN_UNLESS (e != *v, false);
119 *v = e;
120 return true;
121 };
122}
123
124template<typename T> concept IsEnum = std::is_enum_v<T>;
125
127template<typename Enum> requires IsEnum<Enum>
128ChoiceS
130{
131 using EnumType = Jsonipc::Enum<Enum>;
132 ChoiceS choices;
133 for (const auto &evalue : EnumType::list_values())
134 {
135 Choice choice (evalue.second, evalue.second);
136 choices.push_back (choice);
137 }
138 return choices;
139}
140
141} // Ase
142
143#endif // __ASE_PROPERTIES_HH__
Implementation type for classes with Event subscription.
Definition object.hh:11
Abstract base type for Property implementations with Parameter meta data.
Definition properties.hh:12
double get_step() const override
Get the property value stepping, converted to double.
Definition properties.hh:23
bool is_numeric() const override
Whether the property settings can be represented as a floating point number.
Definition properties.hh:24
double get_min() const override
Get the minimum property value, converted to double.
Definition properties.hh:21
String nick() const override
Abbreviated user interface name, usually not more than 6 characters.
Definition properties.hh:19
String get_text() const override
Get the current property value, converted to a text String.
Definition properties.hh:29
String unit() const override
Units of the values within range.
Definition properties.hh:20
Value get_value() const override=0
Get the native property value.
String ident() const override
Unique name (per owner) of this Property.
Definition properties.hh:17
bool set_value(const Value &v) override=0
Set the native property value.
bool set_normalized(double v) override
Set the normalized property value as double.
Definition properties.hh:28
double get_normalized() const override
Get the normalized property value, converted to double.
Definition properties.hh:27
bool set_text(String txt) override
Set the current property value as a text String.
Definition properties.hh:30
double get_max() const override
Get the maximum property value, converted to double.
Definition properties.hh:22
ChoiceS choices() const override
Enumerate choices for choosable properties.
Definition properties.hh:25
String label() const override
Preferred user interface name.
Definition properties.hh:18
void reset() override
Assign default as normalized property value.
Definition properties.hh:26
Class for preference parameters (global settings)
Definition properties.hh:40
Value get_value() const override
Get the native property value.
bool set_value(const Value &v) override
Set the native property value.
Property implementation for GadgetImpl, using lambdas as accessors.
Definition properties.hh:78
ChoiceS choices() const override
Enumerate choices for choosable properties.
Definition properties.hh:85
Value get_value() const override
Get the native property value.
Definition properties.hh:83
bool set_value(const Value &v) override
Set the native property value.
Definition properties.hh:84
A Property allows querying, setting and monitoring of an object property.
Definition api.hh:135
#define ASE_DEFINE_MAKE_SHARED(CLASS)
Definition cxxaux.hh:269
#define ASE_RETURN_UNLESS(cond,...)
Return silently if cond does not evaluate to true, with return value ...
Definition cxxaux.hh:79
T empty(T... args)
The Anklang C++ API namespace.
Definition api.hh:9
uint64_t uint64
A 64-bit unsigned integer, use PRI*64 in format strings.
Definition cxxaux.hh:25
std::tuple< double, double, double > MinMaxStep
Min, max, stepping for double ranges.
Definition parameter.hh:12
ChoiceS enum_lister(const ParameterProperty &)
Helper to list Jsonipc::Enum<> type values as Choice.
int64_t int64
A 64-bit unsigned integer, use PRI*64 in format strings.
Definition cxxaux.hh:29
std::function< void(Value &)> make_enum_getter(Enum *v)
Value getter for enumeration types.
Definition properties.hh:90
std::function< bool(const Value &)> make_enum_setter(Enum *v)
Value setter for enumeration types.
typedef int64_t
Representation of one possible choice for selection properties.
Definition api.hh:96
Structured initializer for Parameter.
Definition parameter.hh:31
Value type used to interface with various property types.
Definition value.hh:54
double as_double() const
Convert Value to double or return 0.
Definition value.cc:77
int64 as_int() const
Convert Value to int64 or return 0.
Definition value.cc:59
String as_string() const
Convert Value to a string, not very useful for RECORD or ARRAY.
Definition value.cc:95