Anklang-0.3.0.dev956+gd75ac925 anklang-0.3.0.dev956+gd75ac925
ASE — Anklang Sound Engine (C++)

« « « Anklang Documentation
Loading...
Searching...
No Matches
gadget.cc
Go to the documentation of this file.
1 // This Source Code Form is licensed MPL-2.0: http://mozilla.org/MPL/2.0
2#include "gadget.hh"
3#include "jsonipc/jsonipc.hh"
4#include "strings.hh"
5#include "utils.hh"
6#include "project.hh"
7#include "internal.hh"
8#include "randomhash.hh"
9
10namespace Ase {
11
12// == Gadget ==
13Gadget::Gadget()
14{}
15
16// == GadgetImpl ==
17GadgetImpl::~GadgetImpl()
18{}
19
21GadgetImpl::gadget_flags (uint64_t setbits, uint64_t mask)
22{
23 gadget_flags_ &= mask;
24 gadget_flags_ |= setbits;
25 return gadget_flags_;
26}
27
28void
30{
31 if (parent)
32 assert_return (parent_ == nullptr);
33 else // !parent
34 assert_return (parent_ != nullptr);
35 parent_ = parent;
36}
37
39GadgetImpl::fallback_name () const
40{
41 return type_nick();
42}
43
45GadgetImpl::canonify_key (const String &input)
46{
47 String key = string_canonify (input, string_set_a2z() + string_set_A2Z() + "_0123456789.", "_");
48 if (key.size() && key[0] == '.')
49 key = "_" + key;
50 return key;
51}
52
53Value
54GadgetImpl::get_data (const String &key) const
55{
56 const String ckey = canonify_key (key);
57 return session_data_[ckey];
58}
59
60bool
61GadgetImpl::set_data (const String &key, const Value &v)
62{
63 const String ckey = canonify_key (key);
64 return_unless (ckey.size(), false);
65 session_data_[ckey] = v;
66 emit_event ("data", ckey);
67 return true;
68}
69
71GadgetImpl::type_nick () const
72{
73 String tname = Jsonipc::rtti_typename (*this);
74 ssize_t colon = tname.rfind (':');
75 if (colon != ssize_t (tname.npos))
76 tname = tname.substr (colon + 1);
77 if (string_endswith (tname, "Impl"))
78 tname = tname.substr (0, tname.size() - 4);
79 return tname;
80}
81
82static CustomDataKey<String> gadget_name_key;
83
85GadgetImpl::name () const
86{
87 if (!has_custom_data (&gadget_name_key))
88 return fallback_name();
89 return get_custom_data (&gadget_name_key);
90}
91
92void
93GadgetImpl::name (const std::string &n)
94{
95 String newname = string_strip (n);
96 if (newname.empty())
97 del_custom_data (&gadget_name_key);
98 else
99 set_custom_data (&gadget_name_key, newname);
100 emit_notify ("name");
101}
102
103PropertyS
105{
106 if (props_.empty())
107 create_properties();
108 return { begin (props_), end (props_) };
109}
110
111// == GadgetImpl ==
112void
114{
115 parent_ = nullptr;
116 emit_event ("object", "removed");
117}
118
119// == Gadget ==
122{
123 Gadget *last = const_cast<Gadget*> (this);
124 for (Gadget *parent = last->_parent(); parent; parent = last->_parent())
125 last = parent;
126 return dynamic_cast<ProjectImpl*> (last);
127}
128
131{
132 PropertyS props = access_properties();
133 StringS names;
134 names.reserve (props.size());
135 for (const PropertyP &prop : props)
136 names.push_back (prop->ident());
137 return names;
138}
139
140PropertyP
142{
143 for (const auto &p : access_properties())
144 if (p->ident() == ident)
145 return p;
146 return {};
147}
148
149Value
151{
152 PropertyP prop = access_property (ident);
153 return prop ? prop->value() : Value {};
154}
155
156bool
158{
159 PropertyP prop = access_property (ident);
160 return prop && prop->value (v);
161}
162
164 const char *member_typeid_name = nullptr;
165 ptrdiff_t memb_offset = -1;
167 GadgetImpl::MemberInfosP infosp = nullptr;
169 uint64_t flags = 0;
170};
171
173 /*key*/
174 const char *class_typeid_name = nullptr;
175 /*payload*/
176 GadgetImpl::MemberClassT classtest = nullptr;
178};
179
180static auto&
181cml_set()
182{
183 static auto gcml_hash = [] (const GadgetClassMemberList &m) {
184 return fnv1a_consthash64 (m.class_typeid_name);
185 };
186 static auto gcml_equal = [] (const GadgetClassMemberList &a, const GadgetClassMemberList &b) {
187 return !strcmp (a.class_typeid_name, b.class_typeid_name);
188 };
189 using MemberAccessorSet = std::unordered_set<GadgetClassMemberList, decltype (gcml_hash), decltype (gcml_equal)>;
190 static MemberAccessorSet mas (0, gcml_hash, gcml_equal);
191 return mas;
192}
193
194bool
195GadgetImpl::requires_accessor (const char *ot, const char *mt, ptrdiff_t offset)
196{
197 auto &cml = cml_set();
198 const GadgetClassMemberList key { .class_typeid_name = ot };
199 auto it = cml.find (key);
200 if (it != cml.end())
201 for (const MemberAccessor *maf : it->members)
202 if (!strcmp (mt, maf->member_typeid_name)) {
203 assert_return (maf->memb_offset == offset, false);
204 return false;
205 }
206 return true;
207}
208
209void
210GadgetImpl::register_accessor (const char *ot, const char *mt, ptrdiff_t offset, MemberClassT classtest,
211 const Param::ExtraVals &ev, MemberAccessF &&accessfunc,
212 MemberInfosP infosp, uint64_t flags)
213{
214 auto &cml = cml_set();
215 auto [celement, inserted] = cml.emplace (ot, classtest);
216 assert_return (celement != cml.end());
217 GadgetClassMemberList *element = const_cast<GadgetClassMemberList*> (&*celement);
218 element->members.push_back (new MemberAccessor {mt, offset, std::move (accessfunc), infosp, ev, flags});
219 //printerr ("%s: %s+%s=%+zd\n", __func__, ot, mt, offset);
220}
221
222void
223GadgetImpl::create_properties ()
224{
225 /* When creating the properties for an instance, walk through the known
226 * GadgetClassMemberList entries, test each via dynamic_cast and thus
227 * identify all class member lists that match the instance.
228 */
229 auto &cml = cml_set();
230 for (const GadgetClassMemberList &ml : cml)
231 if (ml.classtest (*this))
232 for (const MemberAccessor *m : ml.members) {
233 PropertyGetter getter = [this,m] (Value &value) { m->func (this, nullptr, &value); };
234 PropertySetter setter = [this,m] (const Value &value) { return m->func (this, &value, nullptr); };
235 PropertyLister lister = nullptr;
236 StringS infos = m->infosp();
237 String hints = kvpairs_fetch (infos, "hints");
238 if (m->flags & MemberDetails::READABLE)
239 hints += ":r";
240 if (m->flags & MemberDetails::WRITABLE)
241 hints += ":w";
242 if (m->flags & MemberDetails::STORAGE)
243 hints += ":S";
244 if (m->flags & MemberDetails::GUI)
245 hints += ":G";
246 kvpairs_assign (infos, "hints=" + hints);
247 Param param { .extras = m->ev, .metadata = infos };
248 this->props_.push_back (PropertyImpl::make_shared (param, getter, setter, lister));
249 }
250}
251
252} // Ase
T get_custom_data(CustomDataKey< T > *key) const
Retrieve contents of the custom keyed data member, returns DataKey::fallback if nothing was set.
Definition utils.hh:101
bool has_custom_data(CustomDataKey< T > *key) const
Retrieve wether contents of the custom keyed data member exists.
Definition utils.hh:104
bool del_custom_data(CustomDataKey< T > *key)
Delete the current contents of the custom keyed data member, invokes DataKey::destroy.
Definition utils.hh:107
void set_custom_data(CustomDataKey< T > *key, T data)
Assign data to the custom keyed data member, deletes any previously set data.
Definition utils.hh:98
void emit_notify(const String &detail) override
Emit notify:detail, multiple notifications maybe coalesced if a CoalesceNotifies instance exists.
Definition object.cc:164
Base type for classes that have a Property.
Definition gadget.hh:11
void remove_self() override
Remove self from parent container.
Definition gadget.cc:113
PropertyS access_properties() override
Retrieve handles for all properties.
Definition gadget.cc:104
Value get_data(const String &key) const override
Retrieve session data.
Definition gadget.cc:54
bool set_data(const String &key, const Value &v) override
Assign session data, prefix ephemerals with '_'.
Definition gadget.cc:61
void _set_parent(GadgetImpl *parent) override
Assign parent container.
Definition gadget.cc:29
Base type for classes that have a Property.
Definition api.hh:173
virtual StringS list_properties()
List all property identifiers.
Definition gadget.cc:130
virtual PropertyP access_property(String ident)
Retrieve handle for a Property.
Definition gadget.cc:141
virtual PropertyS access_properties()=0
Retrieve handles for all properties.
ProjectImpl * _project() const
Find Project in parent ancestry.
Definition gadget.cc:121
virtual GadgetImpl * _parent() const =0
Retrieve parent container.
Value get_value(String ident)
Get native property value.
Definition gadget.cc:150
bool set_value(String ident, const Value &v)
Set native property value.
Definition gadget.cc:157
T find(T... args)
#define assert_return(expr,...)
Return from the current function if expr is unmet and issue an assertion warning.
Definition internal.hh:28
#define return_unless(cond,...)
Return silently if cond does not evaluate to true with return value ...
Definition internal.hh:72
The Anklang C++ API namespace.
Definition api.hh:8
std::vector< String > StringS
Convenience alias for a std::vector<std::string>.
Definition cxxaux.hh:35
std::function< ChoiceS(const ParameterProperty &)> PropertyLister
Function type to list Choice Property values.
Definition properties.hh:75
const String & string_set_a2z()
Returns a string containing all of a-z.
Definition strings.cc:102
std::string String
Convenience alias for std::string.
Definition cxxaux.hh:34
String string_canonify(const String &string, const String &valid_chars, const String &substitute)
Enforce a canonical charset for a string.
Definition strings.cc:68
String string_strip(const String &input)
Strip whitespaces from the left and right of a string.
Definition strings.cc:1126
const String & string_set_A2Z()
Returns a string containing all of A-Z.
Definition strings.cc:110
std::function< void(Value &)> PropertyGetter
Function type for Property value getters.
Definition properties.hh:69
bool string_endswith(const String &string, const String &fragment)
Returns whether string ends with fragment.
Definition strings.cc:863
std::function< bool(const Value &)> PropertySetter
Function type for Property value setters.
Definition properties.hh:72
T push_back(T... args)
T reserve(T... args)
T rfind(T... args)
T size(T... args)
typedef uint64_t
strcmp
Helper to specify parameter ranges.
Definition parameter.hh:20
Value type used to interface with various property types.
Definition value.hh:53
T substr(T... args)
typedef ssize_t