sdbus-c++ 2.1.0
High-level C++ D-Bus library based on systemd D-Bus implementation
Loading...
Searching...
No Matches
VTableItems.inl
Go to the documentation of this file.
1
26#ifndef SDBUS_CPP_VTABLEITEMS_INL_
27#define SDBUS_CPP_VTABLEITEMS_INL_
28
29#include <sdbus-c++/Error.h>
31
32#include <string>
33#include <type_traits>
34#include <vector>
35
36namespace sdbus {
37
38 /*** -------------------- ***/
39 /*** Method VTable Item ***/
40 /*** -------------------- ***/
41
42 template <typename _Function>
43 MethodVTableItem& MethodVTableItem::implementedAs(_Function&& callback)
44 {
45 inputSignature = signature_of_function_input_arguments_v<_Function>;
46 outputSignature = signature_of_function_output_arguments_v<_Function>;
47 callbackHandler = [callback = std::forward<_Function>(callback)](MethodCall call)
48 {
49 // Create a tuple of callback input arguments' types, which will be used
50 // as a storage for the argument values deserialized from the message.
51 tuple_of_function_input_arg_types_t<_Function> inputArgs;
52
53 // Deserialize input arguments from the message into the tuple.
54 call >> inputArgs;
55
56 if constexpr (!is_async_method_v<_Function>)
57 {
58 // Invoke callback with input arguments from the tuple.
59 auto ret = sdbus::apply(callback, inputArgs);
60
61 // Store output arguments to the reply message and send it back.
62 auto reply = call.createReply();
63 reply << ret;
64 reply.send();
65 }
66 else
67 {
68 // Invoke callback with input arguments from the tuple and with result object to be set later
69 using AsyncResult = typename function_traits<_Function>::async_result_t;
70 sdbus::apply(callback, AsyncResult{std::move(call)}, std::move(inputArgs));
71 }
72 };
73
74 return *this;
75 }
76
77 inline MethodVTableItem& MethodVTableItem::withInputParamNames(std::vector<std::string> names)
78 {
79 inputParamNames = std::move(names);
80
81 return *this;
82 }
83
84 template <typename... _String>
85 inline MethodVTableItem& MethodVTableItem::withInputParamNames(_String... names)
86 {
87 static_assert(std::conjunction_v<std::is_convertible<_String, std::string>...>, "Parameter names must be (convertible to) strings");
88
89 return withInputParamNames({names...});
90 }
91
92 inline MethodVTableItem& MethodVTableItem::withOutputParamNames(std::vector<std::string> names)
93 {
94 outputParamNames = std::move(names);
95
96 return *this;
97 }
98
99 template <typename... _String>
100 inline MethodVTableItem& MethodVTableItem::withOutputParamNames(_String... names)
101 {
102 static_assert(std::conjunction_v<std::is_convertible<_String, std::string>...>, "Parameter names must be (convertible to) strings");
103
104 return withOutputParamNames({names...});
105 }
106
107 inline MethodVTableItem& MethodVTableItem::markAsDeprecated()
108 {
109 flags.set(Flags::DEPRECATED);
110
111 return *this;
112 }
113
114 inline MethodVTableItem& MethodVTableItem::markAsPrivileged()
115 {
116 flags.set(Flags::PRIVILEGED);
117
118 return *this;
119 }
120
121 inline MethodVTableItem& MethodVTableItem::withNoReply()
122 {
123 flags.set(Flags::METHOD_NO_REPLY);
124
125 return *this;
126 }
127
128 inline MethodVTableItem registerMethod(MethodName methodName)
129 {
130 return {std::move(methodName), {}, {}, {}, {}, {}, {}};
131 }
132
133 inline MethodVTableItem registerMethod(std::string methodName)
134 {
135 return registerMethod(MethodName{std::move(methodName)});
136 }
137
138 /*** -------------------- ***/
139 /*** Signal VTable Item ***/
140 /*** -------------------- ***/
141
142 template <typename... _Args>
143 inline SignalVTableItem& SignalVTableItem::withParameters()
144 {
145 signature = signature_of_function_input_arguments_v<void(_Args...)>;
146
147 return *this;
148 }
149
150 template <typename... _Args>
151 inline SignalVTableItem& SignalVTableItem::withParameters(std::vector<std::string> names)
152 {
153 paramNames = std::move(names);
154
155 return withParameters<_Args...>();
156 }
157
158 template <typename... _Args, typename... _String>
159 inline SignalVTableItem& SignalVTableItem::withParameters(_String... names)
160 {
161 static_assert(std::conjunction_v<std::is_convertible<_String, std::string>...>, "Parameter names must be (convertible to) strings");
162 static_assert(sizeof...(_Args) == sizeof...(_String), "Numbers of signal parameters and their names don't match");
163
164 return withParameters<_Args...>({names...});
165 }
166
167 inline SignalVTableItem& SignalVTableItem::markAsDeprecated()
168 {
169 flags.set(Flags::DEPRECATED);
170
171 return *this;
172 }
173
174 inline SignalVTableItem registerSignal(SignalName signalName)
175 {
176 return {std::move(signalName), {}, {}, {}};
177 }
178
179 inline SignalVTableItem registerSignal(std::string signalName)
180 {
181 return registerSignal(SignalName{std::move(signalName)});
182 }
183
184 /*** -------------------- ***/
185 /*** Property VTable Item ***/
186 /*** -------------------- ***/
187
188 template <typename _Function>
189 inline PropertyVTableItem& PropertyVTableItem::withGetter(_Function&& callback)
190 {
191 static_assert(function_argument_count_v<_Function> == 0, "Property getter function must not take any arguments");
192 static_assert(!std::is_void<function_result_t<_Function>>::value, "Property getter function must return property value");
193
194 if (signature.empty())
195 signature = signature_of_function_output_arguments_v<_Function>;
196
197 getter = [callback = std::forward<_Function>(callback)](PropertyGetReply& reply)
198 {
199 // Get the propety value and serialize it into the pre-constructed reply message
200 reply << callback();
201 };
202
203 return *this;
204 }
205
206 template <typename _Function>
207 inline PropertyVTableItem& PropertyVTableItem::withSetter(_Function&& callback)
208 {
209 static_assert(function_argument_count_v<_Function> == 1, "Property setter function must take one parameter - the property value");
210 static_assert(std::is_void<function_result_t<_Function>>::value, "Property setter function must not return any value");
211
212 if (signature.empty())
213 signature = signature_of_function_input_arguments_v<_Function>;
214
215 setter = [callback = std::forward<_Function>(callback)](PropertySetCall call)
216 {
217 // Default-construct property value
218 using property_type = function_argument_t<_Function, 0>;
219 std::decay_t<property_type> property;
220
221 // Deserialize property value from the incoming call message
222 call >> property;
223
224 // Invoke setter with the value
225 callback(property);
226 };
227
228 return *this;
229 }
230
231 inline PropertyVTableItem& PropertyVTableItem::markAsDeprecated()
232 {
233 flags.set(Flags::DEPRECATED);
234
235 return *this;
236 }
237
238 inline PropertyVTableItem& PropertyVTableItem::markAsPrivileged()
239 {
240 flags.set(Flags::PRIVILEGED);
241
242 return *this;
243 }
244
245 inline PropertyVTableItem& PropertyVTableItem::withUpdateBehavior(Flags::PropertyUpdateBehaviorFlags behavior)
246 {
247 flags.set(behavior);
248
249 return *this;
250 }
251
252 inline PropertyVTableItem registerProperty(PropertyName propertyName)
253 {
254 return {std::move(propertyName), {}, {}, {}, {}};
255 }
256
257 inline PropertyVTableItem registerProperty(std::string propertyName)
258 {
259 return registerProperty(PropertyName{std::move(propertyName)});
260 }
261
262 /*** --------------------------- ***/
263 /*** Interface Flags VTable Item ***/
264 /*** --------------------------- ***/
265
266 inline InterfaceFlagsVTableItem& InterfaceFlagsVTableItem::markAsDeprecated()
267 {
268 flags.set(Flags::DEPRECATED);
269
270 return *this;
271 }
272
273 inline InterfaceFlagsVTableItem& InterfaceFlagsVTableItem::markAsPrivileged()
274 {
275 flags.set(Flags::PRIVILEGED);
276
277 return *this;
278 }
279
280 inline InterfaceFlagsVTableItem& InterfaceFlagsVTableItem::withNoReplyMethods()
281 {
282 flags.set(Flags::METHOD_NO_REPLY);
283
284 return *this;
285 }
286
287 inline InterfaceFlagsVTableItem& InterfaceFlagsVTableItem::withPropertyUpdateBehavior(Flags::PropertyUpdateBehaviorFlags behavior)
288 {
289 flags.set(behavior);
290
291 return *this;
292 }
293
294 inline InterfaceFlagsVTableItem setInterfaceFlags()
295 {
296 return {};
297 }
298
299} // namespace sdbus
300
301#endif /* SDBUS_CPP_VTABLEITEMS_INL_ */