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 <switchboard/export.h>
11#include <switchboard_core/EventEmitter.hpp>
12#include <switchboard/SBAny.hpp>
13#include <switchboard_core/SwitchboardCore.hpp>
14#include <switchboard_core/Configuration.hpp>
15#include <switchboard_core/ObjectTypeInfo.hpp>
16#include <switchboard_core/Property.hpp>
17#include <switchboard_core/Action.hpp>
18#include <switchboard_core/Event.hpp>
19
20#include <any>
21#include <map>
22#include <memory>
23#include <string>
24#include <switchboard/Result.hpp>
25
26namespace switchboard {
27
31class SWITCHBOARDSDK_EXPORT SwitchboardObject : public EventEmitter {
32public:
33
40 static SBAnyMap getObjectTypeInfo() {
41 return SBAnyMap({
42 { OBJECT_TYPE_FIELD_PROPERTIES,
43 SBAnyMap({
44 { "hasActions",
45 SBAnyMap({
46 { PROPERTY_FIELD_TYPE, PROPERTY_TYPE_BOOLEAN },
47 { PROPERTY_FIELD_READ_ONLY, true },
48 { PROPERTY_FIELD_DESCRIPTION, "Whether the node has actions." },
49 }) },
50 { "hasEvents",
51 SBAnyMap({
52 { PROPERTY_FIELD_TYPE, PROPERTY_TYPE_BOOLEAN },
53 { PROPERTY_FIELD_READ_ONLY, true },
54 { PROPERTY_FIELD_DESCRIPTION, "Whether the node has events." },
55 }) },
56 }) },
57 { OBJECT_TYPE_FIELD_ACTIONS,
58 SBAnyMap({
59 { "getState",
60 SBAnyMap({
61 { ACTION_FIELD_DESCRIPTION,
62 "Returns the full state of the object including identity, configuration, and current property values." },
63 }) },
64 { "getConfiguration",
65 SBAnyMap({
66 { ACTION_FIELD_DESCRIPTION, "Returns the configuration schema for this object." },
67 }) },
68 { "getProperties",
69 SBAnyMap({
70 { ACTION_FIELD_DESCRIPTION, "Returns the property schema for this object." },
71 }) },
72 { "getActions",
73 SBAnyMap({
74 { ACTION_FIELD_DESCRIPTION, "Returns the action schema for this object." },
75 }) },
76 { "getEvents",
77 SBAnyMap({
78 { ACTION_FIELD_DESCRIPTION, "Returns the event schema for this object." },
79 }) },
80 }) },
81 });
82 }
83
89 explicit SwitchboardObject(const std::string& objectType);
90
95
101 std::string getObjectID() const;
102
108 void setObjectID(const std::string& objectID);
109
115 std::string getURI();
116
122 std::string getObjectType() const;
123
129 std::optional<std::string> getObjectSubtype() const;
130
136 void setObjectSubtype(std::optional<std::string> subtype);
137
144
151
160 virtual Result<void> setValue(const std::string& key, const SBAny& value);
161
169 virtual Result<SBAny> getValue(const std::string& key);
170
179 virtual Result<SBAny> callAction(const std::string& actionName, const SBAnyMap& params);
180
191 void registerConfiguration(const std::string& key, const SBAnyMap& schema, const SBAny& value);
192
199 void setConfigurationValue(const std::string& key, const SBAny& value);
200
219 const std::string& key,
220 const SBAnyMap& schema,
221 const SBAny& getter = SBAny(),
222 const SBAny& setter = SBAny()
223 );
224
237 void registerAction(const std::string& key, const SBAnyMap& schema, ActionHandler handler = {});
238
245 void registerEvent(const std::string& eventName, const SBAnyMap& eventInfo);
246
247private:
248 class Impl;
249 std::unique_ptr<Impl> impl;
250};
251
252}
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:40
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.