blob: 86326b61fb8a66ff51961db6da3802ef53d2cc42 [file] [log] [blame]
#pragma once
#include "callback.hpp"
#include <experimental/tuple>
namespace phosphor
{
namespace dbus
{
namespace monitoring
{
namespace detail
{
/** @class CallDBusMethod
* @brief Provide explicit call forwarding to
* DBusInterface::callMethodNoReply.
*
* @tparam DBusInterface - The DBus interface to use.
* @tparam MethodArgs - DBus method argument types.
*/
template <typename DBusInterface, typename... MethodArgs>
struct CallDBusMethod
{
static void op(const std::string& bus, const std::string& path,
const std::string& iface, const std::string& method,
MethodArgs&&... args)
{
DBusInterface::callMethodNoReply(bus, path, iface, method,
std::forward<MethodArgs>(args)...);
}
};
} // namespace detail
/** @class MethodBase
* @brief Invoke DBus method callback implementation.
*
* The method callback invokes the client supplied DBus method.
*/
class MethodBase : public Callback
{
public:
MethodBase() = delete;
MethodBase(const MethodBase&) = delete;
MethodBase(MethodBase&&) = default;
MethodBase& operator=(const MethodBase&) = delete;
MethodBase& operator=(MethodBase&&) = default;
virtual ~MethodBase() = default;
MethodBase(const std::string& b, const std::string& p, const std::string& i,
const std::string& m) :
Callback(),
bus(b), path(p), interface(i), method(m)
{
}
/** @brief Callback interface implementation. */
void operator()(Context ctx) override = 0;
protected:
const std::string& bus;
const std::string& path;
const std::string& interface;
const std::string& method;
};
/** @class Method
* @brief C++ type specific logic for the method callback.
*
* @tparam DBusInterface - The DBus interface to use to call the method.
* @tparam MethodArgs - DBus method argument types.
*/
template <typename DBusInterface, typename... MethodArgs>
class Method : public MethodBase
{
public:
Method() = delete;
Method(const Method&) = default;
Method(Method&&) = default;
Method& operator=(const Method&) = default;
Method& operator=(Method&&) = default;
~Method() = default;
Method(const std::string& bus, const std::string& path,
const std::string& iface, const std::string& method,
MethodArgs&&... arguments) :
MethodBase(bus, path, iface, method),
args(std::forward<MethodArgs>(arguments)...)
{
}
/** @brief Callback interface implementation. */
void operator()(Context ctx) override
{
std::experimental::apply(
detail::CallDBusMethod<DBusInterface, MethodArgs...>::op,
std::tuple_cat(std::make_tuple(bus), std::make_tuple(path),
std::make_tuple(interface), std::make_tuple(method),
args));
}
private:
std::tuple<MethodArgs...> args;
};
/** @brief Argument type deduction for constructing Method instances. */
template <typename DBusInterface, typename... MethodArgs>
auto makeMethod(const std::string& bus, const std::string& path,
const std::string& iface, const std::string& method,
MethodArgs&&... arguments)
{
return std::make_unique<Method<DBusInterface, MethodArgs...>>(
bus, path, iface, method, std::forward<MethodArgs>(arguments)...);
}
} // namespace monitoring
} // namespace dbus
} // namespace phosphor