27#ifndef SDBUS_CXX_MESSAGE_H_
28#define SDBUS_CXX_MESSAGE_H_
41# if __has_include(<span>)
47#include <unordered_map>
57 template <
typename... ValueTypes>
class Struct;
81 class [[nodiscard]] Message
84 Message(
const Message&)
noexcept;
85 Message& operator=(
const Message&)
noexcept;
86 Message(Message&& other)
noexcept;
87 Message& operator=(Message&& other)
noexcept;
90 Message& operator<<(
bool item);
91 Message& operator<<(int16_t item);
92 Message& operator<<(int32_t item);
93 Message& operator<<(int64_t item);
94 Message& operator<<(uint8_t item);
95 Message& operator<<(uint16_t item);
96 Message& operator<<(uint32_t item);
97 Message& operator<<(uint64_t item);
98 Message& operator<<(
double item);
99 Message& operator<<(
const char *item);
100 Message& operator<<(
const std::string &item);
101 Message& operator<<(std::string_view item);
102 Message& operator<<(
const Variant &item);
103 template <
typename ...Elements>
104 Message& operator<<(
const std::variant<Elements...>& value);
106 Message& operator<<(
const Signature &item);
107 Message& operator<<(
const UnixFd &item);
108 template <
typename Element,
typename Allocator>
109 Message& operator<<(
const std::vector<Element, Allocator>& items);
110 template <
typename Element, std::
size_t Size>
111 Message& operator<<(
const std::array<Element, Size>& items);
113 template <
typename Element, std::
size_t Extent>
114 Message& operator<<(
const std::span<Element, Extent>& items);
117 template <
typename Enum,
typename = std::enable_if_t<std::is_enum_v<Enum>>>
118 Message& operator<<(
const Enum& item);
119 template <
typename Key,
typename Value>
121 template <
typename Key,
typename Value,
typename Compare,
typename Allocator>
122 Message& operator<<(
const std::map<Key, Value, Compare, Allocator>& items);
123 template <
typename Key,
typename Value,
typename Hash,
typename KeyEqual,
typename Allocator>
124 Message& operator<<(
const std::unordered_map<Key, Value, Hash, KeyEqual, Allocator>& items);
125 template <
typename... ValueTypes>
127 template <
typename... ValueTypes>
128 Message& operator<<(
const std::tuple<ValueTypes...>& item);
130 Message& operator>>(
bool& item);
131 Message& operator>>(int16_t& item);
132 Message& operator>>(int32_t& item);
133 Message& operator>>(int64_t& item);
134 Message& operator>>(uint8_t& item);
135 Message& operator>>(uint16_t& item);
136 Message& operator>>(uint32_t& item);
137 Message& operator>>(uint64_t& item);
138 Message& operator>>(
double& item);
139 Message& operator>>(
char*& item);
140 Message& operator>>(std::string &item);
141 Message& operator>>(
Variant &item);
142 template <
typename ...Elements>
143 Message& operator>>(std::variant<Elements...>& value);
146 Message& operator>>(
UnixFd &item);
147 template <
typename Element,
typename Allocator>
148 Message& operator>>(std::vector<Element, Allocator>& items);
149 template <
typename Element, std::
size_t Size>
150 Message& operator>>(std::array<Element, Size>& items);
152 template <
typename Element, std::
size_t Extent>
153 Message& operator>>(std::span<Element, Extent>& items);
156 template <
typename Enum,
typename = std::enable_if_t<std::is_enum_v<Enum>>>
157 Message& operator>>(Enum& item);
158 template <
typename Key,
typename Value>
160 template <
typename Key,
typename Value,
typename Compare,
typename Allocator>
161 Message& operator>>(std::map<Key, Value, Compare, Allocator>& items);
162 template <
typename Key,
typename Value,
typename Hash,
typename KeyEqual,
typename Allocator>
163 Message& operator>>(std::unordered_map<Key, Value, Hash, KeyEqual, Allocator>& items);
164 template <
typename... ValueTypes>
166 template <
typename... ValueTypes>
167 Message& operator>>(std::tuple<ValueTypes...>& item);
169 template <
typename ElementType>
170 Message& openContainer();
171 Message& openContainer(
const char* signature);
172 Message& closeContainer();
173 template <
typename KeyType,
typename ValueType>
174 Message& openDictEntry();
175 Message& openDictEntry(
const char* signature);
176 Message& closeDictEntry();
177 template <
typename ValueType>
178 Message& openVariant();
179 Message& openVariant(
const char* signature);
180 Message& closeVariant();
181 template <
typename... ValueTypes>
182 Message& openStruct();
183 Message& openStruct(
const char* signature);
184 Message& closeStruct();
186 template <
typename ElementType>
187 Message& enterContainer();
188 Message& enterContainer(
const char* signature);
189 Message& exitContainer();
190 template <
typename KeyType,
typename ValueType>
191 Message& enterDictEntry();
192 Message& enterDictEntry(
const char* signature);
193 Message& exitDictEntry();
194 template <
typename ValueType>
195 Message& enterVariant();
196 Message& enterVariant(
const char* signature);
197 Message& exitVariant();
198 template <
typename... ValueTypes>
199 Message& enterStruct();
200 Message& enterStruct(
const char* signature);
201 Message& exitStruct();
203 Message& appendArray(
char type,
const void *ptr,
size_t size);
204 Message& readArray(
char type,
const void **ptr,
size_t *size);
206 template <
typename Key,
typename Value,
typename Callback>
207 Message& serializeDictionary(
const Callback& callback);
208 template <
typename Key,
typename Value>
210 template <
typename Key,
typename Value,
typename Callback>
211 Message& deserializeDictionary(
const Callback& callback);
213 explicit operator bool()
const;
216 const char* getInterfaceName()
const;
217 const char* getMemberName()
const;
218 const char* getSender()
const;
219 const char* getPath()
const;
220 const char* getDestination()
const;
221 uint64_t getCookie()
const;
223 std::pair<char, const char*> peekType()
const;
224 bool isValid()
const;
225 bool isEmpty()
const;
226 bool isAtEnd(
bool complete)
const;
228 void copyTo(Message& destination,
bool complete)
const;
230 void rewind(
bool complete);
232 enum class DumpFlags : uint64_t
235 WithHeader = 1ULL << 0,
236 SubtreeOnly = 1ULL << 1,
237 SubtreeOnlyWithHeader = WithHeader | SubtreeOnly
239 [[nodiscard]] std::string dumpToString(DumpFlags flags)
const;
241 pid_t getCredsPid()
const;
242 uid_t getCredsUid()
const;
243 uid_t getCredsEuid()
const;
244 gid_t getCredsGid()
const;
245 gid_t getCredsEgid()
const;
246 std::vector<gid_t> getCredsSupplementaryGids()
const;
247 std::string getSELinuxContext()
const;
252 template <
typename Array>
253 void serializeArray(
const Array& items);
254 template <
typename Array>
255 void deserializeArray(Array& items);
256 template <
typename Array>
257 void deserializeArrayFast(Array& items);
258 template <
typename Element,
typename Allocator>
259 void deserializeArrayFast(std::vector<Element, Allocator>& items);
260 template <
typename Array>
261 void deserializeArraySlow(Array& items);
262 template <
typename Element,
typename Allocator>
263 void deserializeArraySlow(std::vector<Element, Allocator>& items);
267 explicit Message(internal::IConnection* connection)
noexcept;
268 Message(
void *msg, internal::IConnection* connection)
noexcept;
269 Message(
void *msg, internal::IConnection* connection,
adopt_message_t)
noexcept;
275 internal::IConnection* connection_{};
276 mutable bool ok_{
true};
279 class MethodCall :
public Message
281 using Message::Message;
285 MethodCall() =
default;
288 [[nodiscard]] Slot send(
void* callback,
void* userData, uint64_t timeout,
return_slot_t)
const;
293 void dontExpectReply();
294 bool doesntExpectReply()
const;
297 MethodCall(
void *msg, internal::IConnection* connection,
adopt_message_t)
noexcept;
300 MethodReply sendWithReply(uint64_t timeout = 0)
const;
304 class MethodReply :
public Message
306 using Message::Message;
310 MethodReply() =
default;
312 uint64_t getReplyCookie()
const;
315 class Signal :
public Message
317 using Message::Message;
322 void setDestination(
const std::string& destination);
323 void setDestination(
const char* destination);
327 class PropertySetCall :
public Message
329 using Message::Message;
333 PropertySetCall() =
default;
336 class PropertyGetReply :
public Message
338 using Message::Message;
342 PropertyGetReply() =
default;
346 class PlainMessage :
public Message
348 using Message::Message;
352 PlainMessage() =
default;
357 template <
typename ...Elements>
358 inline Message& Message::operator<<(
const std::variant<Elements...>& value)
360 std::visit([
this](
const auto& inner)
362 openVariant<decltype(inner)>();
370 template <
typename Element,
typename Allocator>
371 inline Message& Message::operator<<(
const std::vector<Element, Allocator>& items)
373 serializeArray(items);
378 template <
typename Element, std::
size_t Size>
379 inline Message& Message::operator<<(
const std::array<Element, Size>& items)
381 serializeArray(items);
387 template <
typename Element, std::
size_t Extent>
388 inline Message& Message::operator<<(
const std::span<Element, Extent>& items)
390 serializeArray(items);
396 template <
typename Enum,
typename>
397 inline Message& Message::operator<<(
const Enum &item)
399 return operator<<(
static_cast<std::underlying_type_t<Enum>
>(item));
402 template <
typename Array>
403 inline void Message::serializeArray(
const Array& items)
405 using ElementType =
typename Array::value_type;
409 if constexpr (signature_of<ElementType>::is_trivial_dbus_type && !std::is_same_v<ElementType, bool>)
411 constexpr auto signature = as_null_terminated(signature_of_v<ElementType>);
412 appendArray(*signature.data(), items.data(), items.size() *
sizeof(ElementType));
416 openContainer<ElementType>();
418 for (
const auto& item : items)
425 template <
typename Key,
typename Value>
428 openDictEntry<Key, Value>();
429 *
this << value.first;
430 *
this << value.second;
436 template <
typename Key,
typename Value,
typename Compare,
typename Allocator>
437 inline Message& Message::operator<<(
const std::map<Key, Value, Compare, Allocator>& items)
439 serializeDictionary<Key, Value>([&items](Message& msg){
for (
const auto& item : items) msg << item; });
444 template <
typename Key,
typename Value,
typename Hash,
typename KeyEqual,
typename Allocator>
445 inline Message& Message::operator<<(
const std::unordered_map<Key, Value, Hash, KeyEqual, Allocator>& items)
447 serializeDictionary<Key, Value>([&items](Message& msg){
for (
const auto& item : items) msg << item; });
452 template <
typename Key,
typename Value>
455 serializeDictionary<Key, Value>([&](Message& msg){
for (
const auto& entry : dictEntries) msg << entry; });
460 template <
typename Key,
typename Value,
typename Callback>
461 inline Message& Message::serializeDictionary(
const Callback& callback)
463 openContainer<DictEntry<Key, Value>>();
472 template <
typename... Args>
473 void serialize_pack(Message& msg, Args&&... args)
475 (void)(msg << ... << std::forward<Args>(args));
478 template <
class Tuple, std::size_t... Is>
479 void serialize_tuple( Message& msg
481 , std::index_sequence<Is...>)
483 serialize_pack(msg, std::get<Is>(tuple)...);
487 template <
typename... ValueTypes>
490 openStruct<ValueTypes...>();
491 detail::serialize_tuple(*
this, item, std::index_sequence_for<ValueTypes...>{});
497 template <
typename... ValueTypes>
498 inline Message& Message::operator<<(
const std::tuple<ValueTypes...>& item)
500 detail::serialize_tuple(*
this, item, std::index_sequence_for<ValueTypes...>{});
506 template <
typename Element,
typename... Elements>
507 bool deserialize_variant(Message& msg, std::variant<Elements...>& value,
const char* signature)
509 constexpr auto elemSignature = as_null_terminated(signature_of_v<Element>);
510 if (std::strcmp(signature, elemSignature.data()) != 0)
514 msg.enterVariant(signature);
517 value = std::move(temp);
522 template <
typename... Elements>
523 inline Message& Message::operator>>(std::variant<Elements...>& value)
525 auto [type, contents] = peekType();
526 const bool result = (detail::deserialize_variant<Elements>(*
this, value, contents) || ...);
527 SDBUS_THROW_ERROR_IF(!result,
"Failed to deserialize variant: signature did not match any of the variant types", EINVAL);
531 template <
typename Element,
typename Allocator>
532 inline Message& Message::operator>>(std::vector<Element, Allocator>& items)
534 deserializeArray(items);
539 template <
typename Element, std::
size_t Size>
540 inline Message& Message::operator>>(std::array<Element, Size>& items)
542 deserializeArray(items);
548 template <
typename Element, std::
size_t Extent>
549 inline Message& Message::operator>>(std::span<Element, Extent>& items)
551 deserializeArray(items);
557 template <
typename Enum,
typename>
558 inline Message& Message::operator>>(Enum& item)
560 std::underlying_type_t<Enum> val;
562 item =
static_cast<Enum
>(val);
566 template <
typename Array>
567 inline void Message::deserializeArray(Array& items)
569 using ElementType =
typename Array::value_type;
573 if constexpr (signature_of<ElementType>::is_trivial_dbus_type && !std::is_same_v<ElementType, bool>)
575 deserializeArrayFast(items);
579 deserializeArraySlow(items);
583 template <
typename Array>
584 inline void Message::deserializeArrayFast(Array& items)
586 using ElementType =
typename Array::value_type;
589 const ElementType* arrayPtr{};
591 constexpr auto signature = as_null_terminated(signature_of_v<ElementType>);
592 readArray(*signature.data(),
reinterpret_cast<const void**
>(&arrayPtr), &arraySize);
594 const size_t elementsInMsg = arraySize /
sizeof(ElementType);
595 const bool notEnoughSpace = items.size() < elementsInMsg;
596 SDBUS_THROW_ERROR_IF(notEnoughSpace,
"Failed to deserialize array: not enough space in destination sequence", EINVAL);
598 std::copy_n(arrayPtr, elementsInMsg, items.begin());
601 template <
typename Element,
typename Allocator>
602 void Message::deserializeArrayFast(std::vector<Element, Allocator>& items)
605 const Element* arrayPtr{};
607 constexpr auto signature = as_null_terminated(signature_of_v<Element>);
608 readArray(*signature.data(),
reinterpret_cast<const void**
>(&arrayPtr), &arraySize);
611 items.insert(items.end(), arrayPtr, arrayPtr + (arraySize /
sizeof(Element)));
614 template <
typename Array>
615 inline void Message::deserializeArraySlow(Array& items)
617 using ElementType =
typename Array::value_type;
619 if(!enterContainer<ElementType>())
622 for (
auto& elem : items)
623 if (!(*
this >> elem))
626 SDBUS_THROW_ERROR_IF(!isAtEnd(
false),
"Failed to deserialize array: not enough space in destination sequence", EINVAL);
633 template <
typename Element,
typename Allocator>
634 void Message::deserializeArraySlow(std::vector<Element, Allocator>& items)
636 if(!enterContainer<Element>())
643 items.emplace_back(std::move(elem));
653 template <
typename Key,
typename Value>
656 if (!enterDictEntry<Key, Value>())
658 *
this >> value.first >> value.second;
664 template <
typename Key,
typename Value,
typename Compare,
typename Allocator>
665 inline Message& Message::operator>>(std::map<Key, Value, Compare, Allocator>& items)
667 deserializeDictionary<Key, Value>([&items](
auto dictEntry){ items.insert(std::move(dictEntry)); });
672 template <
typename Key,
typename Value,
typename Hash,
typename KeyEqual,
typename Allocator>
673 inline Message& Message::operator>>(std::unordered_map<Key, Value, Hash, KeyEqual, Allocator>& items)
675 deserializeDictionary<Key, Value>([&items](
auto dictEntry){ items.insert(std::move(dictEntry)); });
680 template <
typename Key,
typename Value,
typename Callback>
681 inline Message& Message::deserializeDictionary(
const Callback& callback)
683 if (!enterContainer<DictEntry<Key, Value>>())
688 DictEntry<Key, Value> dictEntry;
692 callback(std::move(dictEntry));
703 template <
typename... Args>
704 void deserialize_pack(Message& msg, Args&... args)
706 (void)(msg >> ... >> args);
709 template <
class Tuple, std::size_t... Is>
710 void deserialize_tuple( Message& msg
712 , std::index_sequence<Is...> )
714 deserialize_pack(msg, std::get<Is>(tuple)...);
718 template <
typename... ValueTypes>
721 if (!enterStruct<ValueTypes...>())
724 detail::deserialize_tuple(*
this, item, std::index_sequence_for<ValueTypes...>{});
731 template <
typename... ValueTypes>
732 inline Message& Message::operator>>(std::tuple<ValueTypes...>& item)
734 detail::deserialize_tuple(*
this, item, std::index_sequence_for<ValueTypes...>{});
738 template <
typename ElementType>
739 inline Message& Message::openContainer()
741 constexpr auto signature = as_null_terminated(signature_of_v<ElementType>);
742 return openContainer(signature.data());
745 template <
typename KeyType,
typename ValueType>
746 inline Message& Message::openDictEntry()
748 constexpr auto signature = as_null_terminated(signature_of_v<std::tuple<KeyType, ValueType>>);
749 return openDictEntry(signature.data());
752 template <
typename ValueType>
753 inline Message& Message::openVariant()
755 constexpr auto signature = as_null_terminated(signature_of_v<ValueType>);
756 return openVariant(signature.data());
759 template <
typename... ValueTypes>
760 inline Message& Message::openStruct()
762 constexpr auto signature = as_null_terminated(signature_of_v<std::tuple<ValueTypes...>>);
763 return openStruct(signature.data());
766 template <
typename ElementType>
767 inline Message& Message::enterContainer()
769 constexpr auto signature = as_null_terminated(signature_of_v<ElementType>);
770 return enterContainer(signature.data());
773 template <
typename KeyType,
typename ValueType>
774 inline Message& Message::enterDictEntry()
776 constexpr auto signature = as_null_terminated(signature_of_v<std::tuple<KeyType, ValueType>>);
777 return enterDictEntry(signature.data());
780 template <
typename ValueType>
781 inline Message& Message::enterVariant()
783 constexpr auto signature = as_null_terminated(signature_of_v<ValueType>);
784 return enterVariant(signature.data());
787 template <
typename... ValueTypes>
788 inline Message& Message::enterStruct()
790 constexpr auto signature = as_null_terminated(signature_of_v<std::tuple<ValueTypes...>>);
791 return enterStruct(signature.data());
std::pair< T1, T2 > DictEntry
Definition Types.h:415
Definition TypeTraits.h:96
Definition TypeTraits.h:90