blob: 380df8b7102772bdd87d995a62c1c0c8ea54bfe1 [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
42/** @brief The default action. */
Brad Bishop7b337772017-01-12 16:11:24 -050043inline void noop(Manager& mgr) noexcept { }
Brad Bishopc038e012016-10-19 13:02:24 -040044
Brad Bishop656a7d02016-10-19 22:20:02 -040045/** @brief Destroy an object action. */
Brad Bishop7b337772017-01-12 16:11:24 -050046inline auto destroyObject(const char* path)
Brad Bishop656a7d02016-10-19 22:20:02 -040047{
Brad Bishop7b337772017-01-12 16:11:24 -050048 return [path](auto & m)
49 {
50 m.destroyObject(path);
51 };
Brad Bishop656a7d02016-10-19 22:20:02 -040052}
Brad Bishopda649b12016-11-30 14:35:02 -050053
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 */
73template <typename T, typename U, typename V>
Brad Bishop5dd1c1e2017-01-12 16:14:27 -050074auto setProperty(
Brad Bishop7b337772017-01-12 16:11:24 -050075 const char* path, const char* iface,
76 U&& member, V&& value)
Brad Bishopda649b12016-11-30 14:35:02 -050077{
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 Bishop7b337772017-01-12 16:11:24 -050083 value = std::forward<V>(value)](auto & m)
Brad Bishopda649b12016-11-30 14:35:02 -050084 {
85 m.template invokeMethod<T>(
86 path, iface, member, value);
87 };
88}
89
Brad Bishopc038e012016-10-19 13:02:24 -040090} // namespace actions
91} // namespace manager
92} // namespace inventory
93} // namespace phosphor
94
95// vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4