Redesign inheritance from Message (#62)

... so that the code is more idiomatic, clear and expressive about its intended use
This commit is contained in:
Stanislav Angelovič
2019-06-10 21:38:30 +02:00
committed by GitHub
parent 57c840637c
commit dcad208ffe
12 changed files with 136 additions and 62 deletions
+6 -6
View File
@@ -253,10 +253,10 @@ namespace sdbus {
if (propertySignature_.empty())
propertySignature_ = signature_of_function_output_arguments<_Function>::str();
getter_ = [callback = std::forward<_Function>(callback)](Message& msg)
getter_ = [callback = std::forward<_Function>(callback)](PropertyGetReply& reply)
{
// Get the propety value and serialize it into the message
msg << callback();
// Get the propety value and serialize it into the pre-constructed reply message
reply << callback();
};
return *this;
@@ -271,14 +271,14 @@ namespace sdbus {
if (propertySignature_.empty())
propertySignature_ = signature_of_function_input_arguments<_Function>::str();
setter_ = [callback = std::forward<_Function>(callback)](Message& msg)
setter_ = [callback = std::forward<_Function>(callback)](PropertySetCall& call)
{
// Default-construct property value
using property_type = function_argument_t<_Function, 0>;
std::decay_t<property_type> property;
// Deserialize property value from the message
msg >> property;
// Deserialize property value from the incoming call message
call >> property;
// Invoke setter with the value
callback(property);
+53 -15
View File
@@ -77,16 +77,6 @@ namespace sdbus {
class Message
{
public:
Message() = default;
Message(internal::ISdBus* sdbus) noexcept;
Message(void *msg, internal::ISdBus* sdbus) noexcept;
Message(void *msg, internal::ISdBus* sdbus, adopt_message_t) noexcept;
Message(const Message&) noexcept;
Message& operator=(const Message&) noexcept;
Message(Message&& other) noexcept;
Message& operator=(Message&& other) noexcept;
~Message();
Message& operator<<(bool item);
Message& operator<<(int16_t item);
Message& operator<<(int32_t item);
@@ -150,6 +140,23 @@ namespace sdbus {
void seal();
void rewind(bool complete);
class Factory;
protected:
Message() = default;
Message(internal::ISdBus* sdbus) noexcept;
Message(void *msg, internal::ISdBus* sdbus) noexcept;
Message(void *msg, internal::ISdBus* sdbus, adopt_message_t) noexcept;
Message(const Message&) noexcept;
Message& operator=(const Message&) noexcept;
Message(Message&& other) noexcept;
Message& operator=(Message&& other) noexcept;
~Message();
friend Factory;
protected:
void* msg_{};
internal::ISdBus* sdbus_{};
@@ -158,8 +165,10 @@ namespace sdbus {
class MethodCall : public Message
{
public:
using Message::Message;
public:
MethodCall() = default;
MethodReply send() const;
MethodReply createReply() const;
MethodReply createErrorReply(const sdbus::Error& error) const;
@@ -173,29 +182,58 @@ namespace sdbus {
class AsyncMethodCall : public Message
{
using Message::Message;
public:
using Slot = std::unique_ptr<void, std::function<void(void*)>>;
using Message::Message;
AsyncMethodCall() = default; // Fixes gcc 6.3 error (default c-tor is not imported in above using declaration)
AsyncMethodCall() = default;
AsyncMethodCall(MethodCall&& call) noexcept;
Slot send(void* callback, void* userData) const;
};
class MethodReply : public Message
{
public:
using Message::Message;
public:
MethodReply() = default;
void send() const;
};
class Signal : public Message
{
public:
using Message::Message;
public:
Signal() = default;
void send() const;
};
class PropertySetCall : public Message
{
using Message::Message;
public:
PropertySetCall() = default;
};
class PropertyGetReply : public Message
{
using Message::Message;
public:
PropertyGetReply() = default;
};
class PlainMessage : public Message
{
using Message::Message;
public:
PlainMessage() = default;
};
template <typename _Element>
inline Message& operator<<(Message& msg, const std::vector<_Element>& items)
{
+4 -3
View File
@@ -41,10 +41,11 @@ namespace sdbus {
class ObjectPath;
class Signature;
struct UnixFd;
class Message;
class MethodCall;
class MethodReply;
class Signal;
class PropertySetCall;
class PropertyGetReply;
template <typename... _Results> class Result;
class Error;
}
@@ -54,8 +55,8 @@ namespace sdbus {
using method_callback = std::function<void(MethodCall msg)>;
using async_reply_handler = std::function<void(MethodReply& reply, const Error* error)>;
using signal_handler = std::function<void(Signal& signal)>;
using property_set_callback = std::function<void(Message& msg)>;
using property_get_callback = std::function<void(Message& reply)>;
using property_set_callback = std::function<void(PropertySetCall& msg)>;
using property_get_callback = std::function<void(PropertyGetReply& reply)>;
template <typename _T>
struct signature_of
+1 -1
View File
@@ -94,7 +94,7 @@ namespace sdbus {
std::string peekValueType() const;
private:
mutable Message msg_{};
mutable PlainMessage msg_{};
};
/********************************************//**