File: IObject.h

package info (click to toggle)
sdbus-cpp 0.8.3-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,184 kB
  • sloc: cpp: 9,207; xml: 160; ansic: 115; makefile: 22
file content (496 lines) | stat: -rw-r--r-- 21,941 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
/**
 * (C) 2016 - 2017 KISTLER INSTRUMENTE AG, Winterthur, Switzerland
 * (C) 2016 - 2019 Stanislav Angelovic <angelovic.s@gmail.com>
 *
 * @file IObject.h
 *
 * Created on: Nov 8, 2016
 * Project: sdbus-c++
 * Description: High-level D-Bus IPC C++ library based on sd-bus
 *
 * This file is part of sdbus-c++.
 *
 * sdbus-c++ is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * sdbus-c++ is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with sdbus-c++. If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef SDBUS_CXX_IOBJECT_H_
#define SDBUS_CXX_IOBJECT_H_

#include <sdbus-c++/ConvenienceApiClasses.h>
#include <sdbus-c++/TypeTraits.h>
#include <sdbus-c++/Flags.h>
#include <functional>
#include <string>
#include <memory>
#include <vector>

// Forward declarations
namespace sdbus {
    class Signal;
    class IConnection;
}

namespace sdbus {

    /********************************************//**
     * @class IObject
     *
     * IObject class represents a D-Bus object instance identified by a specific object path.
     * D-Bus object provides its interfaces, methods, signals and properties on a bus
     * identified by a specific bus name.
     *
     * All IObject member methods throw @c sdbus::Error in case of D-Bus or sdbus-c++ error.
     * The IObject class has been designed as thread-aware. However, the operation of
     * creating and sending asynchronous method replies, as well as creating and emitting
     * signals, is thread-safe by design.
     *
     ***********************************************/
    class IObject
    {
    public:
        virtual ~IObject() = default;

        /*!
         * @brief Registers method that the object will provide on D-Bus
         *
         * @param[in] interfaceName Name of an interface that the method will belong to
         * @param[in] methodName Name of the method
         * @param[in] inputSignature D-Bus signature of method input parameters
         * @param[in] outputSignature D-Bus signature of method output parameters
         * @param[in] methodCallback Callback that implements the body of the method
         * @param[in] flags D-Bus method flags (privileged, deprecated, or no reply)
         *
         * @throws sdbus::Error in case of failure
         */
        virtual void registerMethod( const std::string& interfaceName
                                   , std::string methodName
                                   , std::string inputSignature
                                   , std::string outputSignature
                                   , method_callback methodCallback
                                   , Flags flags = {} ) = 0;

        /*!
         * @brief Registers method that the object will provide on D-Bus
         *
         * @param[in] interfaceName Name of an interface that the method will belong to
         * @param[in] methodName Name of the method
         * @param[in] inputSignature D-Bus signature of method input parameters
         * @param[in] inputNames Names of input parameters
         * @param[in] outputSignature D-Bus signature of method output parameters
         * @param[in] outputNames Names of output parameters
         * @param[in] methodCallback Callback that implements the body of the method
         * @param[in] flags D-Bus method flags (privileged, deprecated, or no reply)
         *
         * Provided names of input and output parameters will be included in the introspection
         * description (given that at least version 242 of underlying libsystemd library is
         * used; otherwise, names of parameters are ignored). This usually helps better describe
         * the API to the introspector.
         *
         * @throws sdbus::Error in case of failure
         */
        virtual void registerMethod( const std::string& interfaceName
                                   , std::string methodName
                                   , std::string inputSignature
                                   , const std::vector<std::string>& inputNames
                                   , std::string outputSignature
                                   , const std::vector<std::string>& outputNames
                                   , method_callback methodCallback
                                   , Flags flags = {} ) = 0;

        /*!
         * @brief Registers signal that the object will emit on D-Bus
         *
         * @param[in] interfaceName Name of an interface that the signal will fall under
         * @param[in] signalName Name of the signal
         * @param[in] signature D-Bus signature of signal parameters
         * @param[in] flags D-Bus signal flags (deprecated)
         *
         * @throws sdbus::Error in case of failure
         */
        virtual void registerSignal( const std::string& interfaceName
                                   , std::string signalName
                                   , std::string signature
                                   , Flags flags = {} ) = 0;

        /*!
         * @brief Registers signal that the object will emit on D-Bus
         *
         * @param[in] interfaceName Name of an interface that the signal will fall under
         * @param[in] signalName Name of the signal
         * @param[in] signature D-Bus signature of signal parameters
         * @param[in] paramNames Names of parameters of the signal
         * @param[in] flags D-Bus signal flags (deprecated)
         *
         * Provided names of signal output parameters will be included in the introspection
         * description (given that at least version 242 of underlying libsystemd library is
         * used; otherwise, names of parameters are ignored). This usually helps better describe
         * the API to the introspector.
         *
         * @throws sdbus::Error in case of failure
         */
        virtual void registerSignal( const std::string& interfaceName
                                   , std::string signalName
                                   , std::string signature
                                   , const std::vector<std::string>& paramNames
                                   , Flags flags = {} ) = 0;

        /*!
         * @brief Registers read-only property that the object will provide on D-Bus
         *
         * @param[in] interfaceName Name of an interface that the property will fall under
         * @param[in] propertyName Name of the property
         * @param[in] signature D-Bus signature of property parameters
         * @param[in] getCallback Callback that implements the body of the property getter
         * @param[in] flags D-Bus property flags (deprecated, property update behavior)
         *
         * @throws sdbus::Error in case of failure
         */
        virtual void registerProperty( const std::string& interfaceName
                                     , std::string propertyName
                                     , std::string signature
                                     , property_get_callback getCallback
                                     , Flags flags = {} ) = 0;

        /*!
         * @brief Registers read/write property that the object will provide on D-Bus
         *
         * @param[in] interfaceName Name of an interface that the property will fall under
         * @param[in] propertyName Name of the property
         * @param[in] signature D-Bus signature of property parameters
         * @param[in] getCallback Callback that implements the body of the property getter
         * @param[in] setCallback Callback that implements the body of the property setter
         * @param[in] flags D-Bus property flags (deprecated, property update behavior)
         *
         * @throws sdbus::Error in case of failure
         */
        virtual void registerProperty( const std::string& interfaceName
                                     , std::string propertyName
                                     , std::string signature
                                     , property_get_callback getCallback
                                     , property_set_callback setCallback
                                     , Flags flags = {} ) = 0;

        /*!
         * @brief Sets flags for a given interface
         *
         * @param[in] interfaceName Name of an interface whose flags will be set
         * @param[in] flags Flags to be set
         *
         * @throws sdbus::Error in case of failure
         */
        virtual void setInterfaceFlags(const std::string& interfaceName, Flags flags) = 0;

        /*!
         * @brief Finishes object API registration and publishes the object on the bus
         *
         * The method exports all up to now registered methods, signals and properties on D-Bus.
         * Must be called after all methods, signals and properties have been registered.
         *
         * @throws sdbus::Error in case of failure
         */
        virtual void finishRegistration() = 0;

        /*!
         * @brief Unregisters object's API and removes object from the bus
         *
         * This method unregisters the object, its interfaces, methods, signals and properties
         * from the bus. Unregistration is done automatically also in object's destructor. This
         * method makes sense if, in the process of object removal, we need to make sure that
         * callbacks are unregistered explicitly before the final destruction of the object instance.
         *
         * @throws sdbus::Error in case of failure
         */
        virtual void unregister() = 0;

        /*!
         * @brief Creates a signal message
         *
         * @param[in] interfaceName Name of an interface that the signal belongs under
         * @param[in] signalName Name of the signal
         * @return A signal message
         *
         * Serialize signal arguments into the returned message and emit the signal by passing
         * the message with serialized arguments to the @c emitSignal function.
         * Alternatively, use higher-level API @c emitSignal(const std::string& signalName) defined below.
         *
         * @throws sdbus::Error in case of failure
         */
        virtual Signal createSignal(const std::string& interfaceName, const std::string& signalName) = 0;

        /*!
         * @brief Emits signal for this object path
         *
         * @param[in] message Signal message to be sent out
         *
         * Note: To avoid messing with messages, use higher-level API defined below.
         *
         * @throws sdbus::Error in case of failure
         */
        virtual void emitSignal(const sdbus::Signal& message) = 0;

        /*!
         * @brief Emits PropertyChanged signal for specified properties under a given interface of this object path
         *
         * @param[in] interfaceName Name of an interface that properties belong to
         * @param[in] propNames Names of properties that will be included in the PropertiesChanged signal
         *
         * @throws sdbus::Error in case of failure
         */
        virtual void emitPropertiesChangedSignal(const std::string& interfaceName, const std::vector<std::string>& propNames) = 0;

        /*!
         * @brief Emits PropertyChanged signal for all properties on a given interface of this object path
         *
         * @param[in] interfaceName Name of an interface
         *
         * @throws sdbus::Error in case of failure
         */
        virtual void emitPropertiesChangedSignal(const std::string& interfaceName) = 0;

        /*!
         * @brief Emits InterfacesAdded signal on this object path
         *
         * This emits an InterfacesAdded signal on this object path, by iterating all registered
         * interfaces on the path. All properties are queried and included in the signal.
         * This call is equivalent to emitInterfacesAddedSignal() with an explicit list of
         * registered interfaces. However, unlike emitInterfacesAddedSignal(interfaces), this
         * call can figure out the list of supported interfaces itself. Furthermore, it properly
         * adds the builtin org.freedesktop.DBus.* interfaces.
         *
         * @throws sdbus::Error in case of failure
         */
        virtual void emitInterfacesAddedSignal() = 0;

        /*!
         * @brief Emits InterfacesAdded signal on this object path
         *
         * This emits an InterfacesAdded signal on this object path with explicitly provided list
         * of registered interfaces. As sdbus-c++ does currently not supported adding/removing
         * interfaces of an existing object at run time (an object has a fixed set of interfaces
         * registered by the time of invoking finishRegistration()), emitInterfacesAddedSignal(void)
         * is probably what you are looking for.
         *
         * @throws sdbus::Error in case of failure
         */
        virtual void emitInterfacesAddedSignal(const std::vector<std::string>& interfaces) = 0;

        /*!
         * @brief Emits InterfacesRemoved signal on this object path
         *
         * This is like sd_bus_emit_object_added(), but emits an InterfacesRemoved signal on this
         * object path. This only includes any registered interfaces but skips the properties.
         * This function shall be called (just) before destroying the object.
         *
         * @throws sdbus::Error in case of failure
         */
        virtual void emitInterfacesRemovedSignal() = 0;

        /*!
         * @brief Emits InterfacesRemoved signal on this object path
         *
         * This emits an InterfacesRemoved signal on the given path with explicitly provided list
         * of registered interfaces. As sdbus-c++ does currently not supported adding/removing
         * interfaces of an existing object at run time (an object has a fixed set of interfaces
         * registered by the time of invoking finishRegistration()), emitInterfacesRemovedSignal(void)
         * is probably what you are looking for.
         *
         * @throws sdbus::Error in case of failure
         */
        virtual void emitInterfacesRemovedSignal(const std::vector<std::string>& interfaces) = 0;

        /*!
         * @brief Adds an ObjectManager interface at the path of this D-Bus object
         *
         * Creates an ObjectManager interface at the specified object path on
         * the connection. This is a convenient way to interrogate a connection
         * to see what objects it has.
         *
         * @throws sdbus::Error in case of failure
         */
        virtual void addObjectManager() = 0;

        /*!
         * @brief Removes an ObjectManager interface from the path of this D-Bus object
         *
         * @throws sdbus::Error in case of failure
         */
        virtual void removeObjectManager() = 0;

        /*!
         * @brief Tests whether ObjectManager interface is added at the path of this D-Bus object
         * @return True if ObjectManager interface is there, false otherwise
         */
        virtual bool hasObjectManager() const = 0;

        /*!
         * @brief Provides D-Bus connection used by the object
         *
         * @return Reference to the D-Bus connection
         */
        virtual sdbus::IConnection& getConnection() const = 0;

        /*!
         * @brief Registers method that the object will provide on D-Bus
         *
         * @param[in] methodName Name of the method
         * @return A helper object for convenient registration of the method
         *
         * This is a high-level, convenience way of registering D-Bus methods that abstracts
         * from the D-Bus message concept. Method arguments/return value are automatically (de)serialized
         * in a message and D-Bus signatures automatically deduced from the parameters and return type
         * of the provided native method implementation callback.
         *
         * Example of use:
         * @code
         * object.registerMethod("doFoo").onInterface("com.kistler.foo").implementedAs([this](int value){ return this->doFoo(value); });
         * @endcode
         *
         * @throws sdbus::Error in case of failure
         */
        [[nodiscard]] MethodRegistrator registerMethod(const std::string& methodName);

        /*!
         * @brief Registers signal that the object will provide on D-Bus
         *
         * @param[in] signalName Name of the signal
         * @return A helper object for convenient registration of the signal
         *
         * This is a high-level, convenience way of registering D-Bus signals that abstracts
         * from the D-Bus message concept. Signal arguments are automatically (de)serialized
         * in a message and D-Bus signatures automatically deduced from the provided native parameters.
         *
         * Example of use:
         * @code
         * object.registerSignal("paramChange").onInterface("com.kistler.foo").withParameters<std::map<int32_t, std::string>>();
         * @endcode
         *
         * @throws sdbus::Error in case of failure
         */
        [[nodiscard]] SignalRegistrator registerSignal(const std::string& signalName);

        /*!
         * @brief Registers property that the object will provide on D-Bus
         *
         * @param[in] propertyName Name of the property
         * @return A helper object for convenient registration of the property
         *
         * This is a high-level, convenience way of registering D-Bus properties that abstracts
         * from the D-Bus message concept. Property arguments are automatically (de)serialized
         * in a message and D-Bus signatures automatically deduced from the provided native callbacks.
         *
         * Example of use:
         * @code
         * object_.registerProperty("state").onInterface("com.kistler.foo").withGetter([this](){ return this->state(); });
         * @endcode
         *
         * @throws sdbus::Error in case of failure
         */
        [[nodiscard]] PropertyRegistrator registerProperty(const std::string& propertyName);

        /*!
         * @brief Sets flags (annotations) for a given interface
         *
         * @param[in] interfaceName Name of an interface whose flags will be set
         * @return A helper object for convenient setting of Interface flags
         *
         * This is a high-level, convenience alternative to the other setInterfaceFlags overload.
         *
         * Example of use:
         * @code
         * object_.setInterfaceFlags("com.kistler.foo").markAsDeprecated().withPropertyUpdateBehavior(sdbus::Flags::EMITS_NO_SIGNAL);
         * @endcode
         *
         * @throws sdbus::Error in case of failure
         */
        [[nodiscard]] InterfaceFlagsSetter setInterfaceFlags(const std::string& interfaceName);

        /*!
         * @brief Emits signal on D-Bus
         *
         * @param[in] signalName Name of the signal
         * @return A helper object for convenient emission of signals
         *
         * This is a high-level, convenience way of emitting D-Bus signals that abstracts
         * from the D-Bus message concept. Signal arguments are automatically serialized
         * in a message and D-Bus signatures automatically deduced from the provided native arguments.
         *
         * Example of use:
         * @code
         * int arg1 = ...;
         * double arg2 = ...;
         * object_.emitSignal("fooSignal").onInterface("com.kistler.foo").withArguments(arg1, arg2);
         * @endcode
         *
         * @throws sdbus::Error in case of failure
         */
        [[nodiscard]] SignalEmitter emitSignal(const std::string& signalName);

        /*!
         * @brief Returns object path of the underlying DBus object
         */
        virtual const std::string& getObjectPath() const = 0;
    };

    // Out-of-line member definitions

    inline MethodRegistrator IObject::registerMethod(const std::string& methodName)
    {
        return MethodRegistrator(*this, methodName);
    }

    inline SignalRegistrator IObject::registerSignal(const std::string& signalName)
    {
        return SignalRegistrator(*this, signalName);
    }

    inline PropertyRegistrator IObject::registerProperty(const std::string& propertyName)
    {
        return PropertyRegistrator(*this, propertyName);
    }

    inline InterfaceFlagsSetter IObject::setInterfaceFlags(const std::string& interfaceName)
    {
        return InterfaceFlagsSetter(*this, interfaceName);
    }

    inline SignalEmitter IObject::emitSignal(const std::string& signalName)
    {
        return SignalEmitter(*this, signalName);
    }

    /*!
     * @brief Creates instance representing a D-Bus object
     *
     * @param[in] connection D-Bus connection to be used by the object
     * @param[in] objectPath Path of the D-Bus object
     * @return Pointer to the object representation instance
     *
     * The provided connection will be used by the object to export methods,
     * issue signals and provide properties.
     *
     * Creating a D-Bus object instance is (thread-)safe even upon the connection
     * which is already running its I/O event loop.
     *
     * Code example:
     * @code
     * auto proxy = sdbus::createObject(connection, "/com/kistler/foo");
     * @endcode
     */
    [[nodiscard]] std::unique_ptr<sdbus::IObject> createObject(sdbus::IConnection& connection, std::string objectPath);

}

#include <sdbus-c++/ConvenienceApiClasses.inl>

#endif /* SDBUS_CXX_IOBJECT_H_ */