blob: 7c29205aa86c676129dc2577e27bf9e7e54490ba [file] [log] [blame]
Brad Bishop0df00be2017-05-25 23:38:37 -04001#pragma once
2
3#include <experimental/tuple>
4#include "callback.hpp"
5
6namespace phosphor
7{
8namespace dbus
9{
10namespace monitoring
11{
12namespace detail
13{
14
15/** @class CallDBusMethod
16 * @brief Provide explicit call forwarding to
17 * DBusInterface::callMethodNoReply.
18 *
19 * @tparam DBusInterface - The DBus interface to use.
20 * @tparam MethodArgs - DBus method argument types.
21 */
Brad Bishopd1eac882018-03-29 10:34:05 -040022template <typename DBusInterface, typename... MethodArgs> struct CallDBusMethod
Brad Bishop0df00be2017-05-25 23:38:37 -040023{
Brad Bishopd1eac882018-03-29 10:34:05 -040024 static void op(const std::string& bus, const std::string& path,
25 const std::string& iface, const std::string& method,
26 MethodArgs&&... args)
Brad Bishop0df00be2017-05-25 23:38:37 -040027 {
Brad Bishopd1eac882018-03-29 10:34:05 -040028 DBusInterface::callMethodNoReply(bus, path, iface, method,
29 std::forward<MethodArgs>(args)...);
Brad Bishop0df00be2017-05-25 23:38:37 -040030 }
31};
32} // namespace detail
33
34/** @class MethodBase
35 * @brief Invoke DBus method callback implementation.
36 *
37 * The method callback invokes the client supplied DBus method.
38 */
39class MethodBase : public Callback
40{
Brad Bishopd1eac882018-03-29 10:34:05 -040041 public:
42 MethodBase() = delete;
43 MethodBase(const MethodBase&) = delete;
44 MethodBase(MethodBase&&) = default;
45 MethodBase& operator=(const MethodBase&) = delete;
46 MethodBase& operator=(MethodBase&&) = default;
47 virtual ~MethodBase() = default;
48 MethodBase(const std::string& b, const std::string& p, const std::string& i,
49 const std::string& m) :
50 Callback(),
51 bus(b), path(p), interface(i), method(m)
52 {
53 }
Brad Bishop0df00be2017-05-25 23:38:37 -040054
Brad Bishopd1eac882018-03-29 10:34:05 -040055 /** @brief Callback interface implementation. */
56 void operator()(Context ctx) override = 0;
Brad Bishop0df00be2017-05-25 23:38:37 -040057
Brad Bishopd1eac882018-03-29 10:34:05 -040058 protected:
59 const std::string& bus;
60 const std::string& path;
61 const std::string& interface;
62 const std::string& method;
Brad Bishop0df00be2017-05-25 23:38:37 -040063};
64
65/** @class Method
66 * @brief C++ type specific logic for the method callback.
67 *
68 * @tparam DBusInterface - The DBus interface to use to call the method.
69 * @tparam MethodArgs - DBus method argument types.
70 */
Brad Bishopd1eac882018-03-29 10:34:05 -040071template <typename DBusInterface, typename... MethodArgs>
Brad Bishop0df00be2017-05-25 23:38:37 -040072class Method : public MethodBase
73{
Brad Bishopd1eac882018-03-29 10:34:05 -040074 public:
75 Method() = delete;
76 Method(const Method&) = default;
77 Method(Method&&) = default;
78 Method& operator=(const Method&) = default;
79 Method& operator=(Method&&) = default;
80 ~Method() = default;
81 Method(const std::string& bus, const std::string& path,
82 const std::string& iface, const std::string& method,
83 MethodArgs&&... arguments) :
84 MethodBase(bus, path, iface, method),
85 args(std::forward<MethodArgs>(arguments)...)
86 {
87 }
Brad Bishop0df00be2017-05-25 23:38:37 -040088
Brad Bishopd1eac882018-03-29 10:34:05 -040089 /** @brief Callback interface implementation. */
90 void operator()(Context ctx) override
91 {
92 std::experimental::apply(
93 detail::CallDBusMethod<DBusInterface, MethodArgs...>::op,
94 std::tuple_cat(std::make_tuple(bus), std::make_tuple(path),
95 std::make_tuple(interface), std::make_tuple(method),
96 args));
97 }
Brad Bishop0df00be2017-05-25 23:38:37 -040098
Brad Bishopd1eac882018-03-29 10:34:05 -040099 private:
100 std::tuple<MethodArgs...> args;
Brad Bishop0df00be2017-05-25 23:38:37 -0400101};
102
103/** @brief Argument type deduction for constructing Method instances. */
Brad Bishopd1eac882018-03-29 10:34:05 -0400104template <typename DBusInterface, typename... MethodArgs>
105auto makeMethod(const std::string& bus, const std::string& path,
106 const std::string& iface, const std::string& method,
107 MethodArgs&&... arguments)
Brad Bishop0df00be2017-05-25 23:38:37 -0400108{
109 return std::make_unique<Method<DBusInterface, MethodArgs...>>(
Brad Bishopd1eac882018-03-29 10:34:05 -0400110 bus, path, iface, method, std::forward<MethodArgs>(arguments)...);
Brad Bishop0df00be2017-05-25 23:38:37 -0400111}
112
113} // namespace monitoring
114} // namespace dbus
115} // namespace phosphor