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