Re-design sdbus-c++ approach to connections (#47)

Fixes #33 , among others
This commit is contained in:
Stanislav Angelovič
2019-03-25 16:28:31 +01:00
committed by GitHub
parent 26c6ea8730
commit fd7be39dd4
29 changed files with 526 additions and 452 deletions

View File

@@ -183,7 +183,6 @@ namespace sdbus {
AsyncMethodInvoker(IObjectProxy& objectProxy, const std::string& methodName);
AsyncMethodInvoker& onInterface(const std::string& interfaceName);
template <typename... _Args> AsyncMethodInvoker& withArguments(_Args&&... args);
//template <typename... _OutputArgs> void uponReplyInvoke(std::function<void(const Error*, _OutputArgs...)> callback);
template <typename _Function> void uponReplyInvoke(_Function&& callback);
private:

View File

@@ -113,7 +113,7 @@ namespace sdbus {
{
inputSignature_ = signature_of_function_input_arguments<_Function>::str();
outputSignature_ = signature_of_function_output_arguments<_Function>::str();
asyncCallback_ = [callback = std::forward<_Function>(callback)](MethodCall& msg, MethodResult result)
asyncCallback_ = [callback = std::forward<_Function>(callback)](MethodCall msg, MethodResult&& result)
{
// Create a tuple of callback input arguments' types, which will be used
// as a storage for the argument values deserialized from the message.
@@ -123,7 +123,7 @@ namespace sdbus {
msg >> inputArgs;
// Invoke callback with input arguments from the tuple.
sdbus::apply(callback, std::move(result), inputArgs); // TODO: Use std::apply when switching to full C++17 support
sdbus::apply(callback, std::move(result), std::move(inputArgs)); // TODO: Use std::apply when switching to full C++17 support
};
return *this;

View File

@@ -70,12 +70,12 @@ namespace sdbus {
sdbus::Error createError(int errNo, const std::string& customMsg);
}
#define SDBUS_THROW_ERROR(_MSG, _ERRNO) \
throw sdbus::createError((_ERRNO), (_MSG)) \
#define SDBUS_THROW_ERROR(_MSG, _ERRNO) \
throw sdbus::createError((_ERRNO), (_MSG)) \
/**/
#define SDBUS_THROW_ERROR_IF(_COND, _MSG, _ERRNO) \
if (_COND) SDBUS_THROW_ERROR((_MSG), (_ERRNO)) \
#define SDBUS_THROW_ERROR_IF(_COND, _MSG, _ERRNO) \
if (!(_COND)) ; else SDBUS_THROW_ERROR((_MSG), (_ERRNO)) \
/**/
#endif /* SDBUS_CXX_ERROR_H_ */

View File

@@ -50,10 +50,22 @@ namespace sdbus {
class MethodReply;
class Signal;
template <typename... _Results> class Result;
namespace internal {
class ISdBus;
}
}
namespace sdbus {
// Assume the caller has already obtained message ownership
struct adopt_message_t { explicit adopt_message_t() = default; };
#ifdef __cpp_inline_variables
inline constexpr adopt_message_t adopt_message{};
#else
constexpr adopt_message_t adopt_message{};
#endif
/********************************************//**
* @class Message
*
@@ -72,7 +84,9 @@ namespace sdbus {
{
public:
Message() = default;
Message(void *msg) noexcept;
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;
@@ -141,10 +155,8 @@ namespace sdbus {
void rewind(bool complete);
protected:
void* getMsg() const;
private:
void* msg_{};
internal::ISdBus* sdbus_{};
mutable bool ok_{true};
};
@@ -167,6 +179,7 @@ namespace sdbus {
{
public:
using Message::Message;
AsyncMethodCall() = default; // Fixes gcc 6.3 error (default c-tor is not imported in above using declaration)
AsyncMethodCall(MethodCall&& call) noexcept;
void send(void* callback, void* userData) const;
};

View File

@@ -1,7 +1,7 @@
/**
* (C) 2017 KISTLER INSTRUMENTE AG, Winterthur, Switzerland
*
* @file ConvenienceClasses.h
* @file MethodResult.h
*
* Created on: Nov 8, 2016
* Project: sdbus-c++
@@ -51,40 +51,23 @@ namespace sdbus {
{
protected:
friend sdbus::internal::Object;
MethodResult() = default;
MethodResult(const MethodCall& msg, sdbus::internal::Object& object);
MethodResult(MethodCall msg);
MethodResult(const MethodResult&) = delete;
MethodResult& operator=(const MethodResult&) = delete;
MethodResult(MethodResult&& other) = default;
MethodResult& operator=(MethodResult&& other) = default;
template <typename... _Results> void returnResults(const _Results&... results) const;
void returnError(const Error& error) const;
private:
void send(const MethodReply& reply) const;
private:
MethodCall call_;
sdbus::internal::Object* object_{};
};
template <typename... _Results>
inline void MethodResult::returnResults(const _Results&... results) const
{
assert(call_.isValid());
auto reply = call_.createReply();
#ifdef __cpp_fold_expressions
(reply << ... << results);
#else
using _ = std::initializer_list<int>;
(void)_{(void(reply << results), 0)...};
#endif
send(reply);
}
inline void MethodResult::returnError(const Error& error) const
{
auto reply = call_.createErrorReply(error);
send(reply);
}
/********************************************//**
* @class Result
*
@@ -98,29 +81,14 @@ namespace sdbus {
{
public:
Result() = default;
Result(MethodResult result);
Result(MethodResult&& result);
void returnResults(const _Results&... results) const;
void returnError(const Error& error) const;
};
template <typename... _Results>
inline Result<_Results...>::Result(MethodResult result)
: MethodResult(std::move(result))
{
}
template <typename... _Results>
inline void Result<_Results...>::returnResults(const _Results&... results) const
{
MethodResult::returnResults(results...);
}
template <typename... _Results>
inline void Result<_Results...>::returnError(const Error& error) const
{
MethodResult::returnError(error);
}
}
#include <sdbus-c++/MethodResult.inl>
#endif /* SDBUS_CXX_METHODRESULT_H_ */

View File

@@ -0,0 +1,79 @@
/**
* (C) 2017 KISTLER INSTRUMENTE AG, Winterthur, Switzerland
*
* @file MethodResult.inl
*
* Created on: Mar 21, 2019
* Project: sdbus-c++
* Description: High-level D-Bus IPC C++ library based on sd-bus
*
* This file is part of sdbus-c++.
*
* sdbus-c++ is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 2.1 of the License, or
* (at your option) any later version.
*
* sdbus-c++ is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with sdbus-c++. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef SDBUS_CXX_METHODRESULT_INL_
#define SDBUS_CXX_METHODRESULT_INL_
#include <sdbus-c++/MethodResult.h>
#include <cassert>
namespace sdbus {
inline MethodResult::MethodResult(MethodCall msg)
: call_(std::move(msg))
{
}
template <typename... _Results>
inline void MethodResult::returnResults(const _Results&... results) const
{
assert(call_.isValid());
auto reply = call_.createReply();
#ifdef __cpp_fold_expressions
(reply << ... << results);
#else
using _ = std::initializer_list<int>;
(void)_{(void(reply << results), 0)...};
#endif
reply.send();
}
inline void MethodResult::returnError(const Error& error) const
{
auto reply = call_.createErrorReply(error);
reply.send();
}
template <typename... _Results>
inline Result<_Results...>::Result(MethodResult&& result)
: MethodResult(std::move(result))
{
}
template <typename... _Results>
inline void Result<_Results...>::returnResults(const _Results&... results) const
{
MethodResult::returnResults(results...);
}
template <typename... _Results>
inline void Result<_Results...>::returnError(const Error& error) const
{
MethodResult::returnError(error);
}
}
#endif /* SDBUS_CXX_METHODRESULT_INL_ */

View File

@@ -52,7 +52,7 @@ namespace sdbus {
namespace sdbus {
using method_callback = std::function<void(MethodCall& msg, MethodReply& reply)>;
using async_method_callback = std::function<void(MethodCall& msg, MethodResult result)>;
using async_method_callback = std::function<void(MethodCall msg, MethodResult&& result)>;
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)>;
@@ -383,6 +383,13 @@ namespace sdbus {
static constexpr bool is_async = true;
};
template <typename... _Args, typename... _Results>
struct function_traits<void(Result<_Results...>&&, _Args...)>
: public function_traits_base<std::tuple<_Results...>, _Args...>
{
static constexpr bool is_async = true;
};
template <typename _ReturnType, typename... _Args>
struct function_traits<_ReturnType(*)(_Args...)>
: public function_traits<_ReturnType(_Args...)>

View File

@@ -139,20 +139,22 @@ namespace sdbus {
{
public:
using std::string::string;
using std::string::operator=;
ObjectPath() = default; // Fixes gcc 6.3 error (default c-tor is not imported in above using declaration)
ObjectPath(std::string path)
: std::string(std::move(path))
{}
using std::string::operator=;
};
class Signature : public std::string
{
public:
using std::string::string;
using std::string::operator=;
Signature() = default; // Fixes gcc 6.3 error (default c-tor is not imported in above using declaration)
Signature(std::string path)
: std::string(std::move(path))
{}
using std::string::operator=;
};
}