Brad Bishop | c038e01 | 2016-10-19 13:02:24 -0400 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include <utility> |
| 4 | #include <memory> |
Brad Bishop | 65ffffa | 2016-11-29 12:31:31 -0500 | [diff] [blame] | 5 | #include "utils.hpp" |
Brad Bishop | c038e01 | 2016-10-19 13:02:24 -0400 | [diff] [blame] | 6 | |
| 7 | namespace phosphor |
| 8 | { |
| 9 | namespace inventory |
| 10 | { |
| 11 | namespace manager |
| 12 | { |
Brad Bishop | 65ffffa | 2016-11-29 12:31:31 -0500 | [diff] [blame] | 13 | |
Brad Bishop | c0eae11 | 2016-10-19 21:59:47 -0400 | [diff] [blame] | 14 | class Manager; |
Brad Bishop | 65ffffa | 2016-11-29 12:31:31 -0500 | [diff] [blame] | 15 | namespace details |
| 16 | { |
| 17 | using ActionBase = holder::CallableBase<void, Manager&>; |
| 18 | using ActionBasePtr = std::shared_ptr<ActionBase>; |
| 19 | template <typename T> |
| 20 | using Action = holder::CallableHolder<T, void, Manager&>; |
| 21 | |
| 22 | /** @brief make_action |
| 23 | * |
| 24 | * Adapt an action function object. |
| 25 | * |
| 26 | * @param[in] action - The action being adapted. |
| 27 | * @returns - The adapted action. |
| 28 | * |
| 29 | * @tparam T - The type of the action being adapted. |
| 30 | */ |
| 31 | template <typename T> |
| 32 | auto make_action(T&& action) |
| 33 | { |
| 34 | return Action<T>::template make_shared<Action<T>>( |
| 35 | std::forward<T>(action)); |
| 36 | } |
| 37 | } // namespace details |
Brad Bishop | c0eae11 | 2016-10-19 21:59:47 -0400 | [diff] [blame] | 38 | |
Brad Bishop | c038e01 | 2016-10-19 13:02:24 -0400 | [diff] [blame] | 39 | namespace actions |
| 40 | { |
Brad Bishop | c038e01 | 2016-10-19 13:02:24 -0400 | [diff] [blame] | 41 | |
| 42 | /** @brief The default action. */ |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 43 | inline void noop(Manager& mgr) noexcept { } |
Brad Bishop | c038e01 | 2016-10-19 13:02:24 -0400 | [diff] [blame] | 44 | |
Brad Bishop | 656a7d0 | 2016-10-19 22:20:02 -0400 | [diff] [blame] | 45 | /** @brief Destroy an object action. */ |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 46 | inline auto destroyObject(const char* path) |
Brad Bishop | 656a7d0 | 2016-10-19 22:20:02 -0400 | [diff] [blame] | 47 | { |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 48 | return [path](auto & m) |
| 49 | { |
| 50 | m.destroyObject(path); |
| 51 | }; |
Brad Bishop | 656a7d0 | 2016-10-19 22:20:02 -0400 | [diff] [blame] | 52 | } |
Brad Bishop | da649b1 | 2016-11-30 14:35:02 -0500 | [diff] [blame] | 53 | |
| 54 | /** @brief Set a property action. |
| 55 | * |
| 56 | * Invoke the requested method with a reference to the requested |
| 57 | * sdbusplus server binding interface as a parameter. |
| 58 | * |
| 59 | * @tparam T - The sdbusplus server binding interface type. |
| 60 | * @tparam U - The type of the sdbusplus server binding member |
| 61 | * function that sets the property. |
| 62 | * @tparam V - The property value type. |
| 63 | * |
| 64 | * @param[in] path - The DBus path on which the property should |
| 65 | * be set. |
| 66 | * @param[in] iface - The DBus interface hosting the property. |
| 67 | * @param[in] member - Pointer to sdbusplus server binding member. |
| 68 | * @param[in] value - The value the property should be set to. |
| 69 | * |
| 70 | * @returns - A function object that sets the requested property |
| 71 | * to the requested value. |
| 72 | */ |
| 73 | template <typename T, typename U, typename V> |
Brad Bishop | 5dd1c1e | 2017-01-12 16:14:27 -0500 | [diff] [blame] | 74 | auto setProperty( |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 75 | const char* path, const char* iface, |
| 76 | U&& member, V&& value) |
Brad Bishop | da649b1 | 2016-11-30 14:35:02 -0500 | [diff] [blame] | 77 | { |
| 78 | // The manager is the only parameter passed to actions. |
| 79 | // Bind the path, interface, interface member function pointer, |
| 80 | // and value to a lambda. When it is called, forward the |
| 81 | // path, interface and value on to the manager member function. |
| 82 | return [path, iface, member, |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 83 | value = std::forward<V>(value)](auto & m) |
Brad Bishop | da649b1 | 2016-11-30 14:35:02 -0500 | [diff] [blame] | 84 | { |
| 85 | m.template invokeMethod<T>( |
| 86 | path, iface, member, value); |
| 87 | }; |
| 88 | } |
| 89 | |
Brad Bishop | c038e01 | 2016-10-19 13:02:24 -0400 | [diff] [blame] | 90 | } // namespace actions |
| 91 | } // namespace manager |
| 92 | } // namespace inventory |
| 93 | } // namespace phosphor |
| 94 | |
| 95 | // vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 |