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, "Whether the node has actions." },
50 }
51 ) },
52 { "hasEvents",
53 SBAnyMap(
54 {
55 { PROPERTY_FIELD_TYPE, PROPERTY_TYPE_BOOLEAN },
56 { PROPERTY_FIELD_READ_ONLY, true },
57 { PROPERTY_FIELD_DESCRIPTION, "Whether the node has events." },
58 }
59 ) },
60 }
61 ) },
62 { OBJECT_TYPE_FIELD_ACTIONS,
63 SBAnyMap(
64 {
65 { "getState",
66 SBAnyMap(
67 {
68 { ACTION_FIELD_DESCRIPTION,
69 "Returns the full state of the object including identity, configuration, and current property values." },
70 { ACTION_FIELD_RETURN_TYPE, ACTION_RETURN_TYPE_OBJECT },
71 { ACTION_FIELD_RETURN_DESCRIPTION,
72 "The object's state: objectID, objectURI, objectType and objectSubtype, plus a "
73 "configuration map and a properties map holding the current values." },
74 }
75 ) },
76 { "getConfiguration",
77 SBAnyMap(
78 {
79 { ACTION_FIELD_DESCRIPTION, "Returns the configuration schema for this object." },
80 { ACTION_FIELD_RETURN_TYPE, ACTION_RETURN_TYPE_OBJECT },
81 { ACTION_FIELD_RETURN_DESCRIPTION,
82 "The construction-time parameters this object accepts, keyed by parameter name." },
83 }
84 ) },
85 { "getProperties",
86 SBAnyMap(
87 {
88 { ACTION_FIELD_DESCRIPTION, "Returns the property schema for this object." },
89 { ACTION_FIELD_RETURN_TYPE, ACTION_RETURN_TYPE_OBJECT },
90 { ACTION_FIELD_RETURN_DESCRIPTION,
91 "The runtime properties this object exposes, keyed by property name." },
92 }
93 ) },
94 { "getActions",
95 SBAnyMap(
96 {
97 { ACTION_FIELD_DESCRIPTION, "Returns the action schema for this object." },
98 { ACTION_FIELD_RETURN_TYPE, ACTION_RETURN_TYPE_OBJECT },
99 { ACTION_FIELD_RETURN_DESCRIPTION,
100 "The actions this object can perform, keyed by action name." },
101 }
102 ) },
103 { "getEvents",
104 SBAnyMap(
105 {
106 { ACTION_FIELD_DESCRIPTION, "Returns the event schema for this object." },
107 { ACTION_FIELD_RETURN_TYPE, ACTION_RETURN_TYPE_OBJECT },
108 { ACTION_FIELD_RETURN_DESCRIPTION,
109 "The events this object emits, keyed by event name." },
110 }
111 ) },
112 { "setProperty",
113 SBAnyMap(
114 {
115 { ACTION_FIELD_DESCRIPTION, "Sets a property to a new value by name." },
116 { ACTION_FIELD_PARAMETERS,
117 SBAnyMap(
118 {
119 { "name",
120 SBAnyMap(
121 {
122 { ACTION_PARAMETER_FIELD_NAME, "name" },
123 { ACTION_PARAMETER_FIELD_DESCRIPTION,
124 "The name of the property to set." },
125 { ACTION_PARAMETER_FIELD_TYPE, ACTION_PARAMETER_TYPE_STRING },
126 { ACTION_PARAMETER_FIELD_IS_OPTIONAL, false },
127 }
128 ) },
129 { "value",
130 SBAnyMap(
131 {
132 { ACTION_PARAMETER_FIELD_NAME, "value" },
133 { ACTION_PARAMETER_FIELD_DESCRIPTION,
134 "The new value for the property." },
135 { ACTION_PARAMETER_FIELD_TYPE, ACTION_PARAMETER_TYPE_OBJECT },
136 { ACTION_PARAMETER_FIELD_IS_OPTIONAL, false },
137 }
138 ) },
139 }
140 ) },
141 }
142 ) },
143 }
144 ) },
145 }
146 );
147 }
148
154 explicit SwitchboardObject(const std::string& objectType);
155
160
166 std::string getObjectID() const;
167
173 void setObjectID(const std::string& objectID);
174
180 std::string getURI();
181
187 std::string getObjectType() const;
188
194 std::optional<std::string> getObjectSubtype() const;
195
201 void setObjectSubtype(std::optional<std::string> subtype);
202
209
216
225 virtual Result<void> setValue(const std::string& key, const SBAny& value);
226
234 virtual Result<SBAny> getValue(const std::string& key);
235
244 virtual Result<SBAny> callAction(const std::string& actionName, const SBAnyMap& params);
245
256 void registerConfiguration(const std::string& key, const SBAnyMap& schema, const SBAny& value);
257
264 void setConfigurationValue(const std::string& key, const SBAny& value);
265
284 const std::string& key,
285 const SBAnyMap& schema,
286 const SBAny& getter = SBAny(),
287 const SBAny& setter = SBAny()
288 );
289
302 void registerAction(const std::string& key, const SBAnyMap& schema, ActionHandler handler = {});
303
310 void registerEvent(const std::string& eventName, const SBAnyMap& eventInfo);
311
312private:
313 class Impl;
314 std::unique_ptr<Impl> impl;
315};
316
317}
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.