Brad Bishop | 0df00be | 2017-05-25 23:38:37 -0400 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include <experimental/tuple> |
| 4 | #include "callback.hpp" |
| 5 | |
| 6 | namespace phosphor |
| 7 | { |
| 8 | namespace dbus |
| 9 | { |
| 10 | namespace monitoring |
| 11 | { |
| 12 | namespace 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 Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 22 | template <typename DBusInterface, typename... MethodArgs> struct CallDBusMethod |
Brad Bishop | 0df00be | 2017-05-25 23:38:37 -0400 | [diff] [blame] | 23 | { |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 24 | static void op(const std::string& bus, const std::string& path, |
| 25 | const std::string& iface, const std::string& method, |
| 26 | MethodArgs&&... args) |
Brad Bishop | 0df00be | 2017-05-25 23:38:37 -0400 | [diff] [blame] | 27 | { |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 28 | DBusInterface::callMethodNoReply(bus, path, iface, method, |
| 29 | std::forward<MethodArgs>(args)...); |
Brad Bishop | 0df00be | 2017-05-25 23:38:37 -0400 | [diff] [blame] | 30 | } |
| 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 | */ |
| 39 | class MethodBase : public Callback |
| 40 | { |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 41 | 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 Bishop | 0df00be | 2017-05-25 23:38:37 -0400 | [diff] [blame] | 54 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 55 | /** @brief Callback interface implementation. */ |
| 56 | void operator()(Context ctx) override = 0; |
Brad Bishop | 0df00be | 2017-05-25 23:38:37 -0400 | [diff] [blame] | 57 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 58 | protected: |
| 59 | const std::string& bus; |
| 60 | const std::string& path; |
| 61 | const std::string& interface; |
| 62 | const std::string& method; |
Brad Bishop | 0df00be | 2017-05-25 23:38:37 -0400 | [diff] [blame] | 63 | }; |
| 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 Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 71 | template <typename DBusInterface, typename... MethodArgs> |
Brad Bishop | 0df00be | 2017-05-25 23:38:37 -0400 | [diff] [blame] | 72 | class Method : public MethodBase |
| 73 | { |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 74 | 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 Bishop | 0df00be | 2017-05-25 23:38:37 -0400 | [diff] [blame] | 88 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 89 | /** @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 Bishop | 0df00be | 2017-05-25 23:38:37 -0400 | [diff] [blame] | 98 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 99 | private: |
| 100 | std::tuple<MethodArgs...> args; |
Brad Bishop | 0df00be | 2017-05-25 23:38:37 -0400 | [diff] [blame] | 101 | }; |
| 102 | |
| 103 | /** @brief Argument type deduction for constructing Method instances. */ |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 104 | template <typename DBusInterface, typename... MethodArgs> |
| 105 | auto makeMethod(const std::string& bus, const std::string& path, |
| 106 | const std::string& iface, const std::string& method, |
| 107 | MethodArgs&&... arguments) |
Brad Bishop | 0df00be | 2017-05-25 23:38:37 -0400 | [diff] [blame] | 108 | { |
| 109 | return std::make_unique<Method<DBusInterface, MethodArgs...>>( |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 110 | bus, path, iface, method, std::forward<MethodArgs>(arguments)...); |
Brad Bishop | 0df00be | 2017-05-25 23:38:37 -0400 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | } // namespace monitoring |
| 114 | } // namespace dbus |
| 115 | } // namespace phosphor |