blob: ea7f2475915f3751f06a4615470585bd0d0bfb78 [file] [log] [blame]
Brad Bishopc038e012016-10-19 13:02:24 -04001#pragma once
2
3#include <utility>
4#include <memory>
Brad Bishop65ffffa2016-11-29 12:31:31 -05005#include "utils.hpp"
Brad Bishopc038e012016-10-19 13:02:24 -04006
7namespace phosphor
8{
9namespace inventory
10{
11namespace manager
12{
Brad Bishop65ffffa2016-11-29 12:31:31 -050013
Brad Bishopc0eae112016-10-19 21:59:47 -040014class Manager;
Brad Bishop65ffffa2016-11-29 12:31:31 -050015namespace details
16{
17using ActionBase = holder::CallableBase<void, Manager&>;
18using ActionBasePtr = std::shared_ptr<ActionBase>;
19template <typename T>
20using 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 */
31template <typename T>
32auto make_action(T&& action)
33{
34 return Action<T>::template make_shared<Action<T>>(
35 std::forward<T>(action));
36}
37} // namespace details
Brad Bishopc0eae112016-10-19 21:59:47 -040038
Brad Bishopc038e012016-10-19 13:02:24 -040039namespace actions
40{
Brad Bishopc038e012016-10-19 13:02:24 -040041
Brad Bishop7b7e7122017-01-21 21:21:46 -050042/** @brief Destroy objects action. */
43inline auto destroyObjects(std::vector<const char*> paths)
Brad Bishop656a7d02016-10-19 22:20:02 -040044{
Brad Bishop7b7e7122017-01-21 21:21:46 -050045 return [paths = std::move(paths)](auto & m)
Brad Bishop7b337772017-01-12 16:11:24 -050046 {
Brad Bishop7b7e7122017-01-21 21:21:46 -050047 m.destroyObjects(paths);
Brad Bishop7b337772017-01-12 16:11:24 -050048 };
Brad Bishop656a7d02016-10-19 22:20:02 -040049}
Brad Bishopda649b12016-11-30 14:35:02 -050050
51/** @brief Set a property action.
52 *
53 * Invoke the requested method with a reference to the requested
54 * sdbusplus server binding interface as a parameter.
55 *
56 * @tparam T - The sdbusplus server binding interface type.
57 * @tparam U - The type of the sdbusplus server binding member
58 * function that sets the property.
59 * @tparam V - The property value type.
60 *
61 * @param[in] path - The DBus path on which the property should
62 * be set.
63 * @param[in] iface - The DBus interface hosting the property.
64 * @param[in] member - Pointer to sdbusplus server binding member.
65 * @param[in] value - The value the property should be set to.
66 *
67 * @returns - A function object that sets the requested property
68 * to the requested value.
69 */
70template <typename T, typename U, typename V>
Brad Bishop5dd1c1e2017-01-12 16:14:27 -050071auto setProperty(
Brad Bishop7b337772017-01-12 16:11:24 -050072 const char* path, const char* iface,
73 U&& member, V&& value)
Brad Bishopda649b12016-11-30 14:35:02 -050074{
75 // The manager is the only parameter passed to actions.
76 // Bind the path, interface, interface member function pointer,
77 // and value to a lambda. When it is called, forward the
78 // path, interface and value on to the manager member function.
79 return [path, iface, member,
Brad Bishop7b337772017-01-12 16:11:24 -050080 value = std::forward<V>(value)](auto & m)
Brad Bishopda649b12016-11-30 14:35:02 -050081 {
82 m.template invokeMethod<T>(
83 path, iface, member, value);
84 };
85}
86
Brad Bishopc038e012016-10-19 13:02:24 -040087} // namespace actions
88} // namespace manager
89} // namespace inventory
90} // namespace phosphor
91
92// vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4