27#ifndef SDBUS_CXX_MESSAGE_H_
28#define SDBUS_CXX_MESSAGE_H_
46 template <
typename... _ValueTypes>
class Struct;
74 Message& operator<<(int16_t item);
75 Message& operator<<(int32_t item);
76 Message& operator<<(int64_t item);
77 Message& operator<<(uint8_t item);
78 Message& operator<<(uint16_t item);
79 Message& operator<<(uint32_t item);
80 Message& operator<<(uint64_t item);
81 Message& operator<<(
double item);
82 Message& operator<<(
const char *item);
83 Message& operator<<(
const std::string &item);
89 Message& operator>>(
bool& item);
90 Message& operator>>(int16_t& item);
91 Message& operator>>(int32_t& item);
92 Message& operator>>(int64_t& item);
93 Message& operator>>(uint8_t& item);
94 Message& operator>>(uint16_t& item);
95 Message& operator>>(uint32_t& item);
96 Message& operator>>(uint64_t& item);
97 Message& operator>>(
double& item);
98 Message& operator>>(
char*& item);
99 Message& operator>>(std::string &item);
105 Message& openContainer(
const std::string& signature);
107 Message& openDictEntry(
const std::string& signature);
109 Message& openVariant(
const std::string& signature);
111 Message& openStruct(
const std::string& signature);
114 Message& enterContainer(
const std::string& signature);
116 Message& enterDictEntry(
const std::string& signature);
118 Message& enterVariant(
const std::string& signature);
120 Message& enterStruct(
const std::string& signature);
123 explicit operator bool()
const;
126 std::string getInterfaceName()
const;
127 std::string getMemberName()
const;
128 std::string getSender()
const;
129 std::string getPath()
const;
130 std::string getDestination()
const;
131 void peekType(std::string& type, std::string& contents)
const;
132 bool isValid()
const;
133 bool isEmpty()
const;
135 void copyTo(
Message& destination,
bool complete)
const;
137 void rewind(
bool complete);
139 pid_t getCredsPid()
const;
140 uid_t getCredsUid()
const;
141 uid_t getCredsEuid()
const;
142 gid_t getCredsGid()
const;
143 gid_t getCredsEgid()
const;
144 std::vector<gid_t> getCredsSupplementaryGids()
const;
145 std::string getSELinuxContext()
const;
151 explicit Message(internal::ISdBus* sdbus)
noexcept;
152 Message(
void *msg, internal::ISdBus* sdbus)
noexcept;
166 internal::ISdBus* sdbus_{};
167 mutable bool ok_{
true};
172 using Message::Message;
179 [[deprecated(
"Use send overload with floating_slot instead")]]
void send(
void* callback,
void* userData, uint64_t timeout,
dont_request_slot_t)
const;
180 void send(
void* callback,
void* userData, uint64_t timeout,
floating_slot_t)
const;
181 [[nodiscard]] Slot send(
void* callback,
void* userData, uint64_t timeout)
const;
186 void dontExpectReply();
187 bool doesntExpectReply()
const;
193 MethodReply sendWithReply(uint64_t timeout = 0)
const;
195 const internal::IConnection* connection_{};
200 using Message::Message;
210 using Message::Message;
215 void setDestination(
const std::string& destination);
221 using Message::Message;
230 using Message::Message;
240 using Message::Message;
247 template <
typename _Element>
248 inline Message& operator<<(
Message& msg,
const std::vector<_Element>& items)
252 for (
const auto& item : items)
255 msg.closeContainer();
260 template <
typename _Key,
typename _Value>
261 inline Message& operator<<(Message& msg,
const std::map<_Key, _Value>& items)
263 const std::string dictEntrySignature = signature_of<_Key>::str() + signature_of<_Value>::str();
264 const std::string arraySignature =
"{" + dictEntrySignature +
"}";
266 msg.openContainer(arraySignature);
268 for (
const auto& item : items)
270 msg.openDictEntry(dictEntrySignature);
273 msg.closeDictEntry();
276 msg.closeContainer();
283 template <
typename... _Args>
284 void serialize_pack(Message& msg, _Args&&... args)
286 (void)(msg << ... << args);
289 template <
class _Tuple, std::size_t... _Is>
290 void serialize_tuple( Message& msg
292 , std::index_sequence<_Is...>)
294 serialize_pack(msg, std::get<_Is>(t)...);
298 template <
typename... _ValueTypes>
299 inline Message& operator<<(Message& msg,
const Struct<_ValueTypes...>& item)
301 auto structSignature = signature_of<Struct<_ValueTypes...>>::str();
302 assert(structSignature.size() > 2);
304 auto structContentSignature = structSignature.substr(1, structSignature.size()-2);
306 msg.openStruct(structContentSignature);
307 detail::serialize_tuple(msg, item, std::index_sequence_for<_ValueTypes...>{});
313 template <
typename... _ValueTypes>
314 inline Message& operator<<(Message& msg,
const std::tuple<_ValueTypes...>& item)
316 detail::serialize_tuple(msg, item, std::index_sequence_for<_ValueTypes...>{});
321 template <
typename _Element>
322 inline Message& operator>>(Message& msg, std::vector<_Element>& items)
324 if(!msg.enterContainer(signature_of<_Element>::str()))
331 items.emplace_back(std::move(elem));
343 template <
typename _Key,
typename _Value>
344 inline Message& operator>>(Message& msg, std::map<_Key, _Value>& items)
346 const std::string dictEntrySignature = signature_of<_Key>::str() + signature_of<_Value>::str();
347 const std::string arraySignature =
"{" + dictEntrySignature +
"}";
349 if (!msg.enterContainer(arraySignature))
354 if (!msg.enterDictEntry(dictEntrySignature))
361 items.emplace(std::move(key), std::move(value));
375 template <
typename... _Args>
376 void deserialize_pack(Message& msg, _Args&... args)
378 (void)(msg >> ... >> args);
381 template <
class _Tuple, std::size_t... _Is>
382 void deserialize_tuple( Message& msg
384 , std::index_sequence<_Is...> )
386 deserialize_pack(msg, std::get<_Is>(t)...);
390 template <
typename... _ValueTypes>
391 inline Message& operator>>(Message& msg, Struct<_ValueTypes...>& item)
393 auto structSignature = signature_of<Struct<_ValueTypes...>>::str();
395 auto structContentSignature = structSignature.substr(1, structSignature.size()-2);
397 if (!msg.enterStruct(structContentSignature))
400 detail::deserialize_tuple(msg, item, std::index_sequence_for<_ValueTypes...>{});
407 template <
typename... _ValueTypes>
408 inline Message& operator>>(Message& msg, std::tuple<_ValueTypes...>& item)
410 detail::deserialize_tuple(msg, item, std::index_sequence_for<_ValueTypes...>{});
Definition: Message.h:171
Definition: Message.h:199
Definition: Message.h:239
Definition: Message.h:229
Definition: Message.h:220
Definition: Message.h:209
Definition: TypeTraits.h:79
Definition: TypeTraits.h:76
Definition: TypeTraits.h:73
Definition: TypeTraits.h:88