blob: 403dca1cebdbaab762db32b1411410bf669796d1 [file] [log] [blame]
Brad Bishopc038e012016-10-19 13:02:24 -04001#pragma once
2
3#include <utility>
4#include <memory>
Brad Bishop07934a62017-02-08 23:34:59 -05005#include <functional>
Brad Bishop65ffffa2016-11-29 12:31:31 -05006#include "utils.hpp"
Brad Bishopeb68a682017-01-22 00:58:54 -05007#include "types.hpp"
Brad Bishopc038e012016-10-19 13:02:24 -04008
9namespace phosphor
10{
11namespace inventory
12{
13namespace manager
14{
Brad Bishop65ffffa2016-11-29 12:31:31 -050015
Brad Bishopc0eae112016-10-19 21:59:47 -040016class Manager;
Brad Bishop65ffffa2016-11-29 12:31:31 -050017namespace details
18{
Brad Bishop07934a62017-02-08 23:34:59 -050019using Action = std::function<void (sdbusplus::bus::bus&, Manager&)>;
Brad Bishop65ffffa2016-11-29 12:31:31 -050020
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 */
30template <typename T>
31auto make_action(T&& action)
32{
Brad Bishop07934a62017-02-08 23:34:59 -050033 return Action(std::forward<T>(action));
Brad Bishop65ffffa2016-11-29 12:31:31 -050034}
35} // namespace details
Brad Bishopc0eae112016-10-19 21:59:47 -040036
Brad Bishopc038e012016-10-19 13:02:24 -040037namespace actions
38{
Brad Bishopc038e012016-10-19 13:02:24 -040039
Brad Bishop7b7e7122017-01-21 21:21:46 -050040/** @brief Destroy objects action. */
Brad Bishopa8d3a082017-02-02 22:42:31 -050041inline auto destroyObjects(std::vector<const char*>&& paths)
Brad Bishop656a7d02016-10-19 22:20:02 -040042{
Brad Bishop07934a62017-02-08 23:34:59 -050043 return [ = ](auto&, auto & m)
Brad Bishop7b337772017-01-12 16:11:24 -050044 {
Brad Bishop7b7e7122017-01-21 21:21:46 -050045 m.destroyObjects(paths);
Brad Bishop7b337772017-01-12 16:11:24 -050046 };
Brad Bishop656a7d02016-10-19 22:20:02 -040047}
Brad Bishopda649b12016-11-30 14:35:02 -050048
Brad Bishopeb68a682017-01-22 00:58:54 -050049/** @brief Create objects action. */
50inline auto createObjects(
Brad Bishopa8d3a082017-02-02 22:42:31 -050051 std::map<sdbusplus::message::object_path, Object>&& objs)
Brad Bishopeb68a682017-01-22 00:58:54 -050052{
Brad Bishop07934a62017-02-08 23:34:59 -050053 return [ = ](auto&, auto & m)
Brad Bishopeb68a682017-01-22 00:58:54 -050054 {
55 m.createObjects(objs);
56 };
57}
58
Brad Bishopda649b12016-11-30 14:35:02 -050059/** @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 */
78template <typename T, typename U, typename V>
Brad Bishop5dd1c1e2017-01-12 16:14:27 -050079auto setProperty(
Brad Bishop7b337772017-01-12 16:11:24 -050080 const char* path, const char* iface,
81 U&& member, V&& value)
Brad Bishopda649b12016-11-30 14:35:02 -050082{
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 Bishop23719002017-01-24 21:08:46 -050088 value = std::forward<V>(value)](auto&, auto & m)
Brad Bishopda649b12016-11-30 14:35:02 -050089 {
90 m.template invokeMethod<T>(
91 path, iface, member, value);
92 };
93}
94
Brad Bishopc038e012016-10-19 13:02:24 -040095} // namespace actions
96} // namespace manager
97} // namespace inventory
98} // namespace phosphor
99
100// vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4