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