Anklang-0.3.0.dev797+g4e3241f3 anklang-0.3.0.dev797+g4e3241f3
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 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 { value (parameter_->initial()); }
27 double get_normalized () const override { return !is_numeric() ? 0 : parameter_->normalize (value_as_double()); }
28 bool set_normalized (double v) override { return is_numeric() && value (parameter_->rescale (v)); }
29 String get_text () const override { return parameter_->value_to_text (value()); }
30 bool set_text (String txt) override { value (parameter_->value_from_text (txt)); return !txt.empty(); }
31 Value value () const override = 0;
32 bool value (const Value &v) override = 0;
33 double get_double () const { return !is_numeric() ? 0 : value().as_double(); }
34 ParameterC parameter () const { return parameter_; }
35 Value initial () const { return parameter_->initial(); }
36 MinMaxStep range () const { return parameter_->range(); }
37 double value_as_double () const { return value().as_double(); }
38};
39
42 /*ctor*/ Preference (ParameterC parameter);
43public:
44 using DelCb = std::function<void()>;
45 using StringValueF = std::function<void(const String&, const Value&)>;
46 virtual ~Preference ();
47 /*ctor*/ Preference (const Param&, const StringValueF& = nullptr);
48 String gets () const { return const_cast<Preference*> (this)->value().as_string(); }
49 bool getb () const { return const_cast<Preference*> (this)->value().as_int(); }
50 int64 getn () const { return const_cast<Preference*> (this)->value().as_int(); }
51 uint64 getu () const { return const_cast<Preference*> (this)->value().as_int(); }
52 double getd () const { return const_cast<Preference*> (this)->value().as_double(); }
53 bool set (const Value &v) { return value (v); }
54 bool set (const String &s) { return value (s); }
55 Value value () const override;
56 bool value (const Value &v) override;
57 static Value get (const String &ident);
58 static PreferenceP find (const String &ident);
59 static CStringS list ();
60 static DelCb listen (const std::function<void(const CStringS&)>&);
61 static void save_preferences ();
62 static void load_preferences (bool autosave);
63private:
64 DelCb sigh_;
65 Connection *connection_ = nullptr;
67};
68
71
74
77
80 PropertyGetter getter_; PropertySetter setter_; PropertyLister lister_;
81 PropertyImpl (const Param&, const PropertyGetter&, const PropertySetter&, const PropertyLister&);
82public:
84 Value value () const override { Value v; getter_ (v); return v; }
85 bool value (const Value &v) override { return setter_ (v); }
86 ChoiceS choices () const override { return lister_ ? lister_ (*this) : parameter_->choices(); }
87};
88
90template<typename Enum> std::function<void(Value&)>
92{
93 using EnumType = Jsonipc::Enum<Enum>;
94 return [v] (Value &val) {
95 if (EnumType::has_names())
96 {
97 const String &name = EnumType::get_name (*v);
98 if (!name.empty())
99 {
100 val = name;
101 return;
102 }
103 }
104 val = int64_t (*v);
105 };
106}
107
109template<typename Enum> std::function<bool(const Value&)>
111{
112 using EnumType = Jsonipc::Enum<Enum>;
113 return [v] (const Value &val) {
114 Enum e = *v;
115 if (val.index() == Value::STRING)
116 e = EnumType::get_value (val.as_string(), e);
117 else if (val.index() == Value::INT64)
118 e = Enum (val.as_int());
119 ASE_RETURN_UNLESS (e != *v, false);
120 *v = e;
121 return true;
122 };
123}
124
125template<typename T> concept IsEnum = std::is_enum_v<T>;
126
128template<typename Enum> requires IsEnum<Enum>
129ChoiceS
131{
132 using EnumType = Jsonipc::Enum<Enum>;
133 ChoiceS choices;
134 for (const auto &evalue : EnumType::list_values())
135 {
136 Choice choice (evalue.second, evalue.second);
137 choices.push_back (choice);
138 }
139 return choices;
140}
141
142} // Ase
143
144#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
String ident() const override
Unique name (per owner) of this Property.
Definition properties.hh:17
bool set_normalized(double v) override
Set the normalized property value as double.
Definition properties.hh:28
Value value() const override=0
Get the native property value.
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
bool value(const Value &v) override=0
Set the native property value.
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:41
Value value() const override
Get the native property value.
Property implementation for GadgetImpl, using lambdas as accessors.
Definition properties.hh:79
ChoiceS choices() const override
Enumerate choices for choosable properties.
Definition properties.hh:86
Value value() const override
Get the native property value.
Definition properties.hh:84
bool value(const Value &v) override
Set the native property value.
Definition properties.hh:85
A Property allows querying, setting and monitoring of an object property.
Definition api.hh:135
#define ASE_DEFINE_MAKE_SHARED(CLASS)
Define a member function static shared_ptr<CLASS> make_shared(ctorargs...);.
Definition cxxaux.hh:274
#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:91
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