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