Anklang-0.3.0.dev956+gd75ac925 anklang-0.3.0.dev956+gd75ac925
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 CustomDataContainer, 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 String fallback_name () const;
23 virtual void create_properties ();
24public:
25 String name () const override;
26 void name (const std::string &n) override;
27 void _set_parent (GadgetImpl *parent) override;
28 GadgetImpl* _parent () const override { return parent_; }
29 String type_nick () const override;
30 PropertyS access_properties () override;
31 bool set_data (const String &key, const Value &v) override;
32 Value get_data (const String &key) const override;
33 void remove_self () override;
34 template<class O, class M> void _register_parameter (O*, M*, const Param::ExtraVals&) const;
35 using MemberAccessF = std::function<bool(GadgetImpl*,const Value*,Value*)>;
36 using MemberInfosP = const StringS& (*) ();
37 using MemberClassT = bool (*) (const SharedBase&);
38private:
39 static bool requires_accessor (const char *ot, const char *mt, ptrdiff_t offset);
40 static void register_accessor (const char *ot, const char *mt, ptrdiff_t offset, MemberClassT,
41 const Param::ExtraVals&, MemberAccessF&&, MemberInfosP, uint64_t);
42};
43
44template<class O, class M> void
45GadgetImpl::_register_parameter (O *obj, M *memb, const Param::ExtraVals &ev) const
46{
47 static_assert (M::is_unique_per_member); // allows indexing per typeid, instead of per instance
48 GadgetImpl *gadget = obj;
49 ASE_ASSERT_RETURN (this == gadget);
50 const auto object_typeid_name = typeid_name<O>();
51 const auto member_typeid_name = typeid_name<M>();
52 const ptrdiff_t offset = ptrdiff_t (memb) - ptrdiff_t (obj);
53 if (!requires_accessor (object_typeid_name, member_typeid_name, offset))
54 return;
55 const MemberClassT classtest = [] (const SharedBase &b) -> bool { return !!dynamic_cast<const O*> (&b); };
56 using value_type = decltype (memb->get());
57 auto accessor = [offset] (GadgetImpl *g, const Value *in, Value *out) {
58 O &o = dynamic_cast<O&> (*g);
59 const ptrdiff_t maddr = ptrdiff_t (&o) + offset;
60 M *m = reinterpret_cast<M*> (maddr);
61 bool r = false;
62 if (in) {
63 value_type v = {};
65 v = in->as_int();
66 else if constexpr (std::is_floating_point_v<value_type>)
67 v = in->as_double();
69 v = in->as_string();
71 v = in->as_array();
73 v = in->as_record();
74 else
75 static_assert (sizeof (value_type) < 0, "unhandled <value_type>");
76 r = m->set (v);
77 }
78 if (out)
79 *out = Value (m->get());
80 return r;
81 };
82 register_accessor (object_typeid_name, member_typeid_name, offset, classtest, ev, accessor, &memb->infos, memb->hints());
83}
84
85} // Ase
86
DataListContainer - typesafe storage and retrieval of arbitrary members.
Definition utils.hh:85
Base type for classes that have a Property.
Definition gadget.hh:11
GadgetImpl * _parent() const override
Retrieve parent container.
Definition gadget.hh:28
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
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