|
11 | 11 | #include <utility>
|
12 | 12 | #include <iostream>
|
13 | 13 | #include <optional>
|
| 14 | +#include <functional> |
14 | 15 |
|
15 | 16 | namespace DBusGlue
|
16 | 17 | {
|
| 18 | + struct custom_value |
| 19 | + { |
| 20 | + std::function<int(message&)> read; |
| 21 | + std::function<int(message&)> write; |
| 22 | + }; |
| 23 | + |
17 | 24 | class message
|
18 | 25 | {
|
19 | 26 | public:
|
@@ -256,6 +263,15 @@ namespace DBusGlue
|
256 | 263 | // read_proxy
|
257 | 264 | //-----------------------------------------------------------------------------------------------------------------
|
258 | 265 |
|
| 266 | + template <> |
| 267 | + struct message::read_proxy<custom_value, void> |
| 268 | + { |
| 269 | + static int read(message& msg, custom_value& reader) |
| 270 | + { |
| 271 | + return reader.read(msg); |
| 272 | + } |
| 273 | + }; |
| 274 | + |
259 | 275 | template <
|
260 | 276 | template <typename, typename...>
|
261 | 277 | typename ContainerT,
|
@@ -302,6 +318,78 @@ namespace DBusGlue
|
302 | 318 | }
|
303 | 319 | };
|
304 | 320 |
|
| 321 | + template < |
| 322 | + template <typename, typename...> |
| 323 | + typename ContainerOuterT, |
| 324 | + template <typename> |
| 325 | + typename AllocatorOuterT, |
| 326 | + template <typename, typename...> |
| 327 | + typename ContainerInnerT, |
| 328 | + template <typename> |
| 329 | + typename AllocatorInnerT, |
| 330 | + typename ValueT> |
| 331 | + struct message::read_proxy< |
| 332 | + ContainerOuterT< |
| 333 | + ContainerInnerT<ValueT, AllocatorInnerT<ValueT>>, |
| 334 | + AllocatorOuterT<ContainerInnerT<ValueT, AllocatorInnerT<ValueT>>>>, |
| 335 | + void> |
| 336 | + { |
| 337 | + using container_inner_type = ContainerInnerT<ValueT, AllocatorInnerT<ValueT>>; |
| 338 | + using container_type = ContainerOuterT<container_inner_type, AllocatorOuterT<container_inner_type>>; |
| 339 | + static int read(message& msg, container_type& container) |
| 340 | + { |
| 341 | + using namespace std::string_literals; |
| 342 | + |
| 343 | + sd_bus_message* smsg = static_cast<sd_bus_message*>(msg); |
| 344 | + auto type = msg.type(); |
| 345 | + |
| 346 | + if (type.type != 'a') |
| 347 | + throw std::invalid_argument("contained type is not an array ("s + type.string() + ")"); |
| 348 | + |
| 349 | + auto r = sd_bus_message_enter_container(smsg, SD_BUS_TYPE_ARRAY, type.contained.data()); |
| 350 | + if (r < 0) |
| 351 | + throw std::runtime_error("could not enter array: "s + strerror(-r)); |
| 352 | + |
| 353 | + auto reduced = type.contained.substr(1); |
| 354 | + |
| 355 | + container.clear(); |
| 356 | + r = 1; |
| 357 | + int r2 = 1; |
| 358 | + while (r > 0) |
| 359 | + { |
| 360 | + r2 = sd_bus_message_enter_container(smsg, SD_BUS_TYPE_ARRAY, reduced.data()); |
| 361 | + if (r2 <= 0) |
| 362 | + break; |
| 363 | + |
| 364 | + container_inner_type inner{}; |
| 365 | + while (r2 > 0) |
| 366 | + { |
| 367 | + ValueT v; |
| 368 | + r2 = msg.read(v); |
| 369 | + if (r2 < 0) |
| 370 | + { |
| 371 | + sd_bus_message_exit_container(smsg); |
| 372 | + throw std::runtime_error("could not read from array: "s + strerror(-r)); |
| 373 | + } |
| 374 | + else if (r2 > 0) |
| 375 | + inner.push_back(v); |
| 376 | + } |
| 377 | + |
| 378 | + r = sd_bus_message_exit_container(smsg); |
| 379 | + if (r < 0) |
| 380 | + throw std::runtime_error("could not exit array: "s + strerror(-r)); |
| 381 | + |
| 382 | + container.push_back(inner); |
| 383 | + } |
| 384 | + |
| 385 | + r = sd_bus_message_exit_container(smsg); |
| 386 | + if (r < 0) |
| 387 | + throw std::runtime_error("could not exit array: "s + strerror(-r)); |
| 388 | + |
| 389 | + return r; |
| 390 | + } |
| 391 | + }; |
| 392 | + |
305 | 393 | template <>
|
306 | 394 | struct message::read_proxy<std::string, void>
|
307 | 395 | {
|
@@ -563,6 +651,15 @@ namespace DBusGlue
|
563 | 651 | // append_proxy
|
564 | 652 | //-----------------------------------------------------------------------------------------------------------------
|
565 | 653 |
|
| 654 | + template <> |
| 655 | + struct message::append_proxy<custom_value, void> |
| 656 | + { |
| 657 | + static int write(message& msg, custom_value const& writer) |
| 658 | + { |
| 659 | + return writer.write(msg); |
| 660 | + } |
| 661 | + }; |
| 662 | + |
566 | 663 | template <>
|
567 | 664 | struct message::append_proxy<std::string, void>
|
568 | 665 | {
|
|
0 commit comments