blob: 2fea212c2dadfd213d332f78b940010b25f69d27 [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 Bishopeb68a682017-01-22 00:58:54 -05006#include "types.hpp"
Brad Bishopc038e012016-10-19 13:02:24 -04007
8namespace phosphor
9{
10namespace inventory
11{
12namespace manager
13{
Brad Bishop65ffffa2016-11-29 12:31:31 -050014
Brad Bishopc0eae112016-10-19 21:59:47 -040015class Manager;
Brad Bishop65ffffa2016-11-29 12:31:31 -050016namespace details
17{
Brad Bishop23719002017-01-24 21:08:46 -050018using ActionBase = holder::CallableBase<void, sdbusplus::bus::bus&, Manager&>;
Brad Bishop65ffffa2016-11-29 12:31:31 -050019using ActionBasePtr = std::shared_ptr<ActionBase>;
20template <typename T>
Brad Bishop23719002017-01-24 21:08:46 -050021using Action = holder::CallableHolder<T, void, sdbusplus::bus::bus&, Manager&>;
Brad Bishop65ffffa2016-11-29 12:31:31 -050022
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 */
32template <typename T>
33auto make_action(T&& action)
34{
35 return Action<T>::template make_shared<Action<T>>(
36 std::forward<T>(action));
37}
38} // namespace details
Brad Bishopc0eae112016-10-19 21:59:47 -040039
Brad Bishopc038e012016-10-19 13:02:24 -040040namespace actions
41{
Brad Bishopc038e012016-10-19 13:02:24 -040042
Brad Bishop7b7e7122017-01-21 21:21:46 -050043/** @brief Destroy objects action. */
Brad Bishopa8d3a082017-02-02 22:42:31 -050044inline auto destroyObjects(std::vector<const char*>&& paths)
Brad Bishop656a7d02016-10-19 22:20:02 -040045{
Brad Bishopa8d3a082017-02-02 22:42:31 -050046 return [=](auto&, auto & m)
Brad Bishop7b337772017-01-12 16:11:24 -050047 {
Brad Bishop7b7e7122017-01-21 21:21:46 -050048 m.destroyObjects(paths);
Brad Bishop7b337772017-01-12 16:11:24 -050049 };
Brad Bishop656a7d02016-10-19 22:20:02 -040050}
Brad Bishopda649b12016-11-30 14:35:02 -050051
Brad Bishopeb68a682017-01-22 00:58:54 -050052/** @brief Create objects action. */
53inline auto createObjects(
Brad Bishopa8d3a082017-02-02 22:42:31 -050054 std::map<sdbusplus::message::object_path, Object>&& objs)
Brad Bishopeb68a682017-01-22 00:58:54 -050055{
Brad Bishopa8d3a082017-02-02 22:42:31 -050056 return [=](auto&, auto & m)
Brad Bishopeb68a682017-01-22 00:58:54 -050057 {
58 m.createObjects(objs);
59 };
60}
61
Brad Bishopda649b12016-11-30 14:35:02 -050062/** @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 */
81template <typename T, typename U, typename V>
Brad Bishop5dd1c1e2017-01-12 16:14:27 -050082auto setProperty(
Brad Bishop7b337772017-01-12 16:11:24 -050083 const char* path, const char* iface,
84 U&& member, V&& value)
Brad Bishopda649b12016-11-30 14:35:02 -050085{
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 Bishop23719002017-01-24 21:08:46 -050091 value = std::forward<V>(value)](auto&, auto & m)
Brad Bishopda649b12016-11-30 14:35:02 -050092 {
93 m.template invokeMethod<T>(
94 path, iface, member, value);
95 };
96}
97
Brad Bishopc038e012016-10-19 13:02:24 -040098} // namespace actions
99} // namespace manager
100} // namespace inventory
101} // namespace phosphor
102
103// vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4