Switchboard Extension SDK
Loading...
Searching...
No Matches
SwitchboardObject.hpp
1//
2// SwitchboardObject.hpp
3// SwitchboardSDK
4//
5// Created by Balazs Kiss on 2025. 01. 22..
6//
7
8#pragma once
9
10#include <any>
11#include <map>
12#include <memory>
13#include <string>
14#include <switchboard/Result.hpp>
15#include <switchboard/SBAny.hpp>
16#include <switchboard/export.h>
17#include <switchboard_core/Action.hpp>
18#include <switchboard_core/Configuration.hpp>
19#include <switchboard_core/Event.hpp>
20#include <switchboard_core/EventEmitter.hpp>
21#include <switchboard_core/ObjectTypeInfo.hpp>
22#include <switchboard_core/Property.hpp>
23#include <switchboard_core/SwitchboardCore.hpp>
24
25namespace switchboard {
26
30class SWITCHBOARDSDK_EXPORT SwitchboardObject : public EventEmitter {
31public:
38 static SBAnyMap getObjectTypeInfo() {
39 return SBAnyMap(
40 {
41 { OBJECT_TYPE_FIELD_PROPERTIES,
42 SBAnyMap(
43 {
44 { "hasActions",
45 SBAnyMap(
46 {
47 { PROPERTY_FIELD_TYPE, PROPERTY_TYPE_BOOLEAN },
48 { PROPERTY_FIELD_READ_ONLY, true },
49 { PROPERTY_FIELD_DESCRIPTION,
50 "Whether the object has any registered actions. Every object registers `setProperty`, so this is "
51 "currently always true." },
52 }
53 ) },
54 { "hasEvents",
55 SBAnyMap(
56 {
57 { PROPERTY_FIELD_TYPE, PROPERTY_TYPE_BOOLEAN },
58 { PROPERTY_FIELD_READ_ONLY, true },
59 { PROPERTY_FIELD_DESCRIPTION, "Whether the object declares any events." },
60 }
61 ) },
62 { "objectID",
63 SBAnyMap(
64 {
65 { PROPERTY_FIELD_TYPE, PROPERTY_TYPE_STRING },
66 { PROPERTY_FIELD_READ_ONLY, false },
67 { PROPERTY_FIELD_DESCRIPTION,
68 "The unique identifier of the object. Taken from the `id` or `objectID` configuration "
69 "key when the object is created. Other objects refer to it by this ID, so changing it "
70 "later does not update those references." },
71 }
72 ) },
73 { "objectType",
74 SBAnyMap(
75 {
76 { PROPERTY_FIELD_TYPE, PROPERTY_TYPE_STRING },
77 { PROPERTY_FIELD_READ_ONLY, true },
78 { PROPERTY_FIELD_DESCRIPTION, "The kind of the object." },
79 { PROPERTY_FIELD_OPTIONS,
80 SBAnyVector { std::string(SwitchboardObjectType::SWITCHBOARD),
83 std::string(SwitchboardObjectType::NODE),
84 std::string(SwitchboardObjectType::EXTENSION) } },
85 }
86 ) },
87 }
88 ) },
89 { OBJECT_TYPE_FIELD_ACTIONS,
90 SBAnyMap(
91 {
92 { "getState",
93 SBAnyMap(
94 {
95 { ACTION_FIELD_DESCRIPTION,
96 "Returns the full state of the object including identity, configuration, and current property values." },
97 { ACTION_FIELD_RETURN_TYPE, ACTION_RETURN_TYPE_OBJECT },
98 { ACTION_FIELD_RETURN_DESCRIPTION,
99 "Map with `objectID`, `objectURI` (the object's ID prefixed by its parents' IDs, separated by dots), "
100 "`objectType`, `objectSubtype`, a legacy `id` copy of `objectID`, `configuration` (the "
101 "configuration values the object was created with) and `properties` (the current value "
102 "of every property). Nodes also include `type`, the full node type such as "
103 "`Switchboard.Gain`." },
104 }
105 ) },
106 { "getConfiguration",
107 SBAnyMap(
108 {
109 { ACTION_FIELD_DESCRIPTION,
110 "Returns the configuration parameters of this object with their schema and the value it was created with." },
111 { ACTION_FIELD_RETURN_TYPE, ACTION_RETURN_TYPE_OBJECT },
112 { ACTION_FIELD_RETURN_DESCRIPTION,
113 "Map keyed by configuration name; each entry holds the schema fields and the value used for this instance." },
114 }
115 ) },
116 { "getProperties",
117 SBAnyMap(
118 {
119 { ACTION_FIELD_DESCRIPTION,
120 "Returns the properties of this object with their schema and current value." },
121 { ACTION_FIELD_RETURN_TYPE, ACTION_RETURN_TYPE_OBJECT },
122 { ACTION_FIELD_RETURN_DESCRIPTION,
123 "Map keyed by property name; each entry holds the schema fields and the current value." },
124 }
125 ) },
126 { "getActions",
127 SBAnyMap(
128 {
129 { ACTION_FIELD_DESCRIPTION,
130 "Returns the schema of the actions registered on this object." },
131 { ACTION_FIELD_RETURN_TYPE, ACTION_RETURN_TYPE_OBJECT },
132 { ACTION_FIELD_RETURN_DESCRIPTION,
133 "Map keyed by action name with the description, parameters and return type of each action. The built-in "
134 "`getState`, `getConfiguration`, `getProperties`, `getActions` and `getEvents` actions "
135 "are not listed." },
136 }
137 ) },
138 { "getEvents",
139 SBAnyMap(
140 {
141 { ACTION_FIELD_DESCRIPTION,
142 "Returns the schema of the events this object can emit." },
143 { ACTION_FIELD_RETURN_TYPE, ACTION_RETURN_TYPE_OBJECT },
144 { ACTION_FIELD_RETURN_DESCRIPTION,
145 "Map keyed by event name with the description and data fields of each event." },
146 }
147 ) },
148 { "setProperty",
149 SBAnyMap(
150 {
151 { ACTION_FIELD_DESCRIPTION,
152 "Sets a property to a new value by name. Works for any writable property, so it can be the target of an "
153 "event-to-action connection in a graph. Fails if the property does not exist or is read-only." },
154 { ACTION_FIELD_PARAMETERS,
155 SBAnyMap(
156 {
157 { "name",
158 SBAnyMap(
159 {
160 { ACTION_PARAMETER_FIELD_NAME, "name" },
161 { ACTION_PARAMETER_FIELD_DESCRIPTION,
162 "Name of the property to set." },
163 { ACTION_PARAMETER_FIELD_TYPE, ACTION_PARAMETER_TYPE_STRING },
164 { ACTION_PARAMETER_FIELD_IS_OPTIONAL, false },
165 }
166 ) },
167 { "value",
168 SBAnyMap(
169 {
170 { ACTION_PARAMETER_FIELD_NAME, "value" },
171 { ACTION_PARAMETER_FIELD_DESCRIPTION,
172 "New value for the property, of the property's type." },
173 { ACTION_PARAMETER_FIELD_TYPE, ACTION_PARAMETER_TYPE_OBJECT },
174 { ACTION_PARAMETER_FIELD_IS_OPTIONAL, false },
175 }
176 ) },
177 }
178 ) },
179 }
180 ) },
181 }
182 ) },
183 }
184 );
185 }
186
192 explicit SwitchboardObject(const std::string& objectType);
193
198
204 std::string getObjectID() const;
205
211 void setObjectID(const std::string& objectID);
212
218 std::string getURI();
219
225 std::string getObjectType() const;
226
232 std::optional<std::string> getObjectSubtype() const;
233
239 void setObjectSubtype(std::optional<std::string> subtype);
240
247
254
263 virtual Result<void> setValue(const std::string& key, const SBAny& value);
264
272 virtual Result<SBAny> getValue(const std::string& key);
273
282 virtual Result<SBAny> callAction(const std::string& actionName, const SBAnyMap& params);
283
294 void registerConfiguration(const std::string& key, const SBAnyMap& schema, const SBAny& value);
295
302 void setConfigurationValue(const std::string& key, const SBAny& value);
303
322 const std::string& key,
323 const SBAnyMap& schema,
324 const SBAny& getter = SBAny(),
325 const SBAny& setter = SBAny()
326 );
327
340 void registerAction(const std::string& key, const SBAnyMap& schema, ActionHandler handler = {});
341
348 void registerEvent(const std::string& eventName, const SBAnyMap& eventInfo);
349
350private:
351 class Impl;
352 std::unique_ptr<Impl> impl;
353};
354
355}
EventEmitter()
Constructor for the EventEmitter class.
~SwitchboardObject() override
SwitchboardObject destructor.
void registerProperty(const std::string &key, const SBAnyMap &schema, const SBAny &getter=SBAny(), const SBAny &setter=SBAny())
Registers a property for the object.
void setObjectID(const std::string &objectID)
Sets the ID of the object.
void setConfigurationValue(const std::string &key, const SBAny &value)
Sets a configuration value for the object.
std::string getObjectType() const
Gets the type of the object.
std::optional< std::string > getObjectSubtype() const
Gets the subtype of the object.
SwitchboardObject(const std::string &objectType)
SwitchboardObject constructor.
void registerEvent(const std::string &eventName, const SBAnyMap &eventInfo)
Registers an event for the object.
void registerAction(const std::string &key, const SBAnyMap &schema, ActionHandler handler={})
Registers an action for the object.
virtual Result< void > setValue(const std::string &key, const SBAny &value)
Sets a value on the object.
std::string getURI()
Gets the URI of the object.
std::string getObjectID() const
Gets the ID of the object.
virtual Result< SBAny > getValue(const std::string &key)
Gets a value from the object.
static SBAnyMap getObjectTypeInfo()
Static type-info fragment for the SwitchboardObject base. Concrete subtypes should merge this with th...
Definition SwitchboardObject.hpp:38
SwitchboardObject * getParentObject() const
Gets the parent object of the object.
void registerConfiguration(const std::string &key, const SBAnyMap &schema, const SBAny &value)
Registers a configuration for the object using a static schema and an instance value.
virtual Result< SBAny > callAction(const std::string &actionName, const SBAnyMap &params)
Calls an action on the object.
void setObjectSubtype(std::optional< std::string > subtype)
Sets the subtype of the object.
void setParentObject(SwitchboardObject *parentObject)
Sets the parent object of the object.
constexpr const char * SWITCHBOARD
The SDK singleton — parent of all engines, graphs, and extensions.
Definition ObjectTypeInfo.hpp:16
constexpr const char * EXTENSION
A Switchboard SDK extension that contributes additional object types.
Definition ObjectTypeInfo.hpp:28
constexpr const char * NODE
A node inside an audio graph (source, sink, or processor).
Definition ObjectTypeInfo.hpp:25
constexpr const char * ENGINE
An audio engine instance (e.g. realtime, offline, manual, WebSocket).
Definition ObjectTypeInfo.hpp:19
constexpr const char * GRAPH
An audio graph that owns nodes and their connections.
Definition ObjectTypeInfo.hpp:22