blob: 86326b61fb8a66ff51961db6da3802ef53d2cc42 [file] [log] [blame]
Brad Bishop0df00be2017-05-25 23:38:37 -04001#pragma once
2
Brad Bishop0df00be2017-05-25 23:38:37 -04003#include "callback.hpp"
4
Patrick Venture3d6d3182018-08-31 09:33:09 -07005#include <experimental/tuple>
6
Brad Bishop0df00be2017-05-25 23:38:37 -04007namespace phosphor
8{
9namespace dbus
10{
11namespace monitoring
12{
13namespace detail
14{
15
16/** @class CallDBusMethod
17 * @brief Provide explicit call forwarding to
18 * DBusInterface::callMethodNoReply.
19 *
20 * @tparam DBusInterface - The DBus interface to use.
21 * @tparam MethodArgs - DBus method argument types.
22 */
Patrick Venture3d6d3182018-08-31 09:33:09 -070023template <typename DBusInterface, typename... MethodArgs>
24struct CallDBusMethod
Brad Bishop0df00be2017-05-25 23:38:37 -040025{
Brad Bishopd1eac882018-03-29 10:34:05 -040026 static void op(const std::string& bus, const std::string& path,
27 const std::string& iface, const std::string& method,
28 MethodArgs&&... args)
Brad Bishop0df00be2017-05-25 23:38:37 -040029 {
Brad Bishopd1eac882018-03-29 10:34:05 -040030 DBusInterface::callMethodNoReply(bus, path, iface, method,
31 std::forward<MethodArgs>(args)...);
Brad Bishop0df00be2017-05-25 23:38:37 -040032 }
33};
34} // namespace detail
35
36/** @class MethodBase
37 * @brief Invoke DBus method callback implementation.
38 *
39 * The method callback invokes the client supplied DBus method.
40 */
41class MethodBase : public Callback
42{
Brad Bishopd1eac882018-03-29 10:34:05 -040043 public:
44 MethodBase() = delete;
45 MethodBase(const MethodBase&) = delete;
46 MethodBase(MethodBase&&) = default;
47 MethodBase& operator=(const MethodBase&) = delete;
48 MethodBase& operator=(MethodBase&&) = default;
49 virtual ~MethodBase() = default;
50 MethodBase(const std::string& b, const std::string& p, const std::string& i,
51 const std::string& m) :
52 Callback(),
53 bus(b), path(p), interface(i), method(m)
54 {
55 }
Brad Bishop0df00be2017-05-25 23:38:37 -040056
Brad Bishopd1eac882018-03-29 10:34:05 -040057 /** @brief Callback interface implementation. */
58 void operator()(Context ctx) override = 0;
Brad Bishop0df00be2017-05-25 23:38:37 -040059
Brad Bishopd1eac882018-03-29 10:34:05 -040060 protected:
61 const std::string& bus;
62 const std::string& path;
63 const std::string& interface;
64 const std::string& method;
Brad Bishop0df00be2017-05-25 23:38:37 -040065};
66
67/** @class Method
68 * @brief C++ type specific logic for the method callback.
69 *
70 * @tparam DBusInterface - The DBus interface to use to call the method.
71 * @tparam MethodArgs - DBus method argument types.
72 */
Brad Bishopd1eac882018-03-29 10:34:05 -040073template <typename DBusInterface, typename... MethodArgs>
Brad Bishop0df00be2017-05-25 23:38:37 -040074class Method : public MethodBase
75{
Brad Bishopd1eac882018-03-29 10:34:05 -040076 public:
77 Method() = delete;
78 Method(const Method&) = default;
79 Method(Method&&) = default;
80 Method& operator=(const Method&) = default;
81 Method& operator=(Method&&) = default;
82 ~Method() = default;
83 Method(const std::string& bus, const std::string& path,
84 const std::string& iface, const std::string& method,
85 MethodArgs&&... arguments) :
86 MethodBase(bus, path, iface, method),
87 args(std::forward<MethodArgs>(arguments)...)
88 {
89 }
Brad Bishop0df00be2017-05-25 23:38:37 -040090
Brad Bishopd1eac882018-03-29 10:34:05 -040091 /** @brief Callback interface implementation. */
92 void operator()(Context ctx) override
93 {
94 std::experimental::apply(
95 detail::CallDBusMethod<DBusInterface, MethodArgs...>::op,
96 std::tuple_cat(std::make_tuple(bus), std::make_tuple(path),
97 std::make_tuple(interface), std::make_tuple(method),
98 args));
99 }
Brad Bishop0df00be2017-05-25 23:38:37 -0400100
Brad Bishopd1eac882018-03-29 10:34:05 -0400101 private:
102 std::tuple<MethodArgs...> args;
Brad Bishop0df00be2017-05-25 23:38:37 -0400103};
104
105/** @brief Argument type deduction for constructing Method instances. */
Brad Bishopd1eac882018-03-29 10:34:05 -0400106template <typename DBusInterface, typename... MethodArgs>
107auto makeMethod(const std::string& bus, const std::string& path,
108 const std::string& iface, const std::string& method,
109 MethodArgs&&... arguments)
Brad Bishop0df00be2017-05-25 23:38:37 -0400110{
111 return std::make_unique<Method<DBusInterface, MethodArgs...>>(
Brad Bishopd1eac882018-03-29 10:34:05 -0400112 bus, path, iface, method, std::forward<MethodArgs>(arguments)...);
Brad Bishop0df00be2017-05-25 23:38:37 -0400113}
114
115} // namespace monitoring
116} // namespace dbus
117} // namespace phosphor