Anklang-0.3.0.dev956+gd75ac925 anklang-0.3.0.dev956+gd75ac925
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#pragma once
3
4#include <ase/api.hh>
5#include <ase/object.hh>
6#include <ase/jsonapi.hh>
7
8namespace Ase {
9
11class ParameterProperty : public EmittableImpl, public virtual Property {
12protected:
13 ParameterC parameter_;
14 StringS metadata () const override { return parameter_->metadata(); }
15public:
16 String ident () const override { return parameter_->cident; }
17 String label () const override { return parameter_->label(); }
18 String nick () const override { return parameter_->nick(); }
19 String unit () const override { return parameter_->unit(); }
20 double get_min () const override { return std::get<0> (parameter_->range()); }
21 double get_max () const override { return std::get<1> (parameter_->range()); }
22 double get_step () const override { return std::get<2> (parameter_->range()); }
23 bool is_numeric () const override { return parameter_->is_numeric(); }
24 ChoiceS choices () const override { return parameter_->choices(); }
25 void reset () override { value (parameter_->initial()); }
26 double get_normalized () const override { return !is_numeric() ? 0 : parameter_->normalize (value_as_double()); }
27 bool set_normalized (double v) override { return is_numeric() && value (parameter_->rescale (v)); }
28 String get_text () const override { return parameter_->value_to_text (value()); }
29 bool set_text (String txt) override { value (parameter_->value_from_text (txt)); return !txt.empty(); }
30 Value value () const override = 0;
31 bool value (const Value &v) override = 0;
32 double get_double () const { return !is_numeric() ? 0 : value().as_double(); }
33 ParameterC parameter () const { return parameter_; }
34 Value initial () const { return parameter_->initial(); }
35 MinMaxStep range () const { return parameter_->range(); }
36 double value_as_double () const { return value().as_double(); }
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)->value().as_string(); }
48 bool getb () const { return const_cast<Preference*> (this)->value().as_int(); }
49 int64 getn () const { return const_cast<Preference*> (this)->value().as_int(); }
50 uint64 getu () const { return const_cast<Preference*> (this)->value().as_int(); }
51 double getd () const { return const_cast<Preference*> (this)->value().as_double(); }
52 bool set (const Value &v) { return value (v); }
53 bool set (const String &s) { return value (s); }
54 Value value () const override;
55 bool 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 value () const override { Value v; getter_ (v); return v; }
84 bool 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
Implementation type for classes with Event subscription.
Definition object.hh:10
Abstract base type for Property implementations with Parameter meta data.
Definition properties.hh:11
double get_step() const override
Get the property value stepping, converted to double.
Definition properties.hh:22
bool is_numeric() const override
Whether the property settings can be represented as a floating point number.
Definition properties.hh:23
double get_min() const override
Get the minimum property value, converted to double.
Definition properties.hh:20
String nick() const override
Abbreviated user interface name, usually not more than 6 characters.
Definition properties.hh:18
String get_text() const override
Get the current property value, converted to a text String.
Definition properties.hh:28
String unit() const override
Units of the values within range.
Definition properties.hh:19
String ident() const override
Unique name (per owner) of this Property.
Definition properties.hh:16
bool set_normalized(double v) override
Set the normalized property value as double.
Definition properties.hh:27
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:26
bool set_text(String txt) override
Set the current property value as a text String.
Definition properties.hh:29
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:21
ChoiceS choices() const override
Enumerate choices for choosable properties.
Definition properties.hh:24
String label() const override
Preferred user interface name.
Definition properties.hh:17
void reset() override
Assign default as normalized property value.
Definition properties.hh:25
Class for preference parameters (global settings)
Definition properties.hh:40
Value value() const override
Get 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 value() const override
Get the native property value.
Definition properties.hh:83
bool 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:134
#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:78
T empty(T... args)
The Anklang C++ API namespace.
Definition api.hh:8
uint64_t uint64
A 64-bit unsigned integer, use PRI*64 in format strings.
Definition cxxaux.hh:24
std::tuple< double, double, double > MinMaxStep
Min, max, stepping for double ranges.
Definition parameter.hh:11
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:28
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:95
Structured initializer for Parameter.
Definition parameter.hh:30
Value type used to interface with various property types.
Definition value.hh:53
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
Registers C++ enum values with string names for JSON serialization and TypeScript binding generation.
Definition jsonipc.hh:1086