Anklang-0.3.0-1416-gfa8ce8d2 anklang-0.3.0-1416-gfa8ce8d2
ASE — Anklang Sound Engine (C++)

« « « Anklang Documentation
Loading...
Searching...
No Matches
gadget.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/object.hh>
5#include <ase/utils.hh>
6#include <ase/properties.hh>
7
8namespace Ase {
9
11class GadgetImpl : public ObjectImpl, public virtual Gadget {
12 GadgetImpl *parent_ = nullptr;
13 uint64_t gadget_flags_ = 0;
14 ValueR session_data_;
15protected:
16 PropertyImplS props_;
17 enum : uint64_t { GADGET_DESTROYED = 0x1, DEVICE_ACTIVE = 0x2, MASTER_TRACK = 0x4 };
18 uint64_t gadget_flags () const { return gadget_flags_; }
19 uint64_t gadget_flags (uint64_t setbits, uint64_t mask = ~uint64_t (0));
20 static String canonify_key (const String &input);
21 virtual ~GadgetImpl ();
22 virtual void create_properties ();
23public:
24 void _set_parent (GadgetImpl *parent) override;
25 GadgetImpl* _parent () const override { return parent_; }
26 String type_nick () const override;
27 PropertyS access_properties () override;
28 bool set_data (const String &key, const Value &v) override;
29 Value get_data (const String &key) const override;
30 void remove_self () override;
31 template<class O, class M> void _register_parameter (O*, M*, const Param::ExtraVals&) const;
32 using MemberAccessF = std::function<bool(GadgetImpl*,const Value*,Value*)>;
33 using MemberInfosP = const StringS& (*) ();
34 using MemberClassT = bool (*) (const SharedBase&);
35private:
36 static bool requires_accessor (const char *ot, const char *mt, ptrdiff_t offset);
37 static void register_accessor (const char *ot, const char *mt, ptrdiff_t offset, MemberClassT,
38 const Param::ExtraVals&, MemberAccessF&&, MemberInfosP, uint64_t);
39};
40
41template<class O, class M> void
42GadgetImpl::_register_parameter (O *obj, M *memb, const Param::ExtraVals &ev) const
43{
44 static_assert (M::is_unique_per_member); // allows indexing per typeid, instead of per instance
45 GadgetImpl *gadget = obj;
46 ASE_ASSERT_RETURN (this == gadget);
47 const auto object_typeid_name = typeid_name<O>();
48 const auto member_typeid_name = typeid_name<M>();
49 const ptrdiff_t offset = ptrdiff_t (memb) - ptrdiff_t (obj);
50 if (!requires_accessor (object_typeid_name, member_typeid_name, offset))
51 return;
52 const MemberClassT classtest = [] (const SharedBase &b) -> bool { return !!dynamic_cast<const O*> (&b); };
53 using value_type = decltype (memb->get());
54 auto accessor = [offset] (GadgetImpl *g, const Value *in, Value *out) {
55 O &o = dynamic_cast<O&> (*g);
56 const ptrdiff_t maddr = ptrdiff_t (&o) + offset;
57 M *m = reinterpret_cast<M*> (maddr);
58 bool r = false;
59 if (in) {
60 value_type v = {};
62 v = in->as_int();
63 else if constexpr (std::is_floating_point_v<value_type>)
64 v = in->as_double();
66 v = in->as_string();
68 v = in->as_array();
70 v = in->as_record();
71 else
72 static_assert (sizeof (value_type) < 0, "unhandled <value_type>");
73 r = m->set (v);
74 }
75 if (out)
76 *out = Value (m->get());
77 return r;
78 };
79 register_accessor (object_typeid_name, member_typeid_name, offset, classtest, ev, accessor, &memb->infos, memb->hints());
80}
81
82} // Ase
83
Base type for classes that have a Property.
Definition gadget.hh:11
GadgetImpl * _parent() const override
Retrieve parent container.
Definition gadget.hh:25
void remove_self() override
Remove self from parent container.
Definition gadget.cc:86
PropertyS access_properties() override
Retrieve handles for all properties.
Definition gadget.cc:77
Value get_data(const String &key) const override
Retrieve session data.
Definition gadget.cc:48
bool set_data(const String &key, const Value &v) override
Assign session data, prefix ephemerals with '_'.
Definition gadget.cc:55
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
Implementation type for classes with Property interfaces.
Definition object.hh:40
Common base type for polymorphic classes managed by std::shared_ptr<>.
Definition api.hh:18
#define ASE_ASSERT_RETURN(expr,...)
Return from the current function if expr evaluates to false and issue an assertion warning.
Definition cxxaux.hh:81
The Anklang C++ API namespace.
Definition api.hh:8
typedef uint64_t
Helper to specify parameter ranges.
Definition parameter.hh:20
Value type used to interface with various property types.
Definition value.hh:53