blob: ff0801907b9d624e98dc039c74d5780194b37323 [file] [log] [blame]
Brad Bishopc038e012016-10-19 13:02:24 -04001#pragma once
2
3#include <utility>
4#include <memory>
5
6namespace phosphor
7{
8namespace inventory
9{
10namespace manager
11{
Brad Bishopc0eae112016-10-19 21:59:47 -040012class Manager;
13
Brad Bishopc038e012016-10-19 13:02:24 -040014namespace actions
15{
16namespace details
17{
18namespace holder
19{
20
21/** @struct Base
22 * @brief Event action functor holder base.
23 *
24 * Provides an un-templated holder for actionsof any type with the correct
25 * function call signature.
26 */
27struct Base
28{
29 Base() = default;
30 virtual ~Base() = default;
31 Base(const Base&) = delete;
32 Base& operator=(const Base&) = delete;
33 Base(Base&&) = default;
34 Base& operator=(Base&&) = default;
35
Brad Bishopc0eae112016-10-19 21:59:47 -040036 virtual void operator()(Manager &mgr) const = 0;
37 virtual void operator()(Manager &mgr)
Brad Bishopc038e012016-10-19 13:02:24 -040038 {
Brad Bishopc0eae112016-10-19 21:59:47 -040039 const_cast<const Base &>(*this)(mgr);
Brad Bishopc038e012016-10-19 13:02:24 -040040 }
41};
42
43/** @struct Holder
44 * @brief Event action functor holder.
45 *
46 * Adapts a functor of any type (with the correct function call
47 * signature) to a non-templated type usable by the manager for
48 * actions.
49 *
50 * @tparam T - The functor type.
51 */
52template <typename T>
53struct Holder final : public Base
54{
55 Holder() = delete;
56 ~Holder() = default;
57 Holder(const Holder&) = delete;
58 Holder & operator=(const Holder&) = delete;
59 Holder(Holder&&) = default;
60 Holder& operator=(Holder&&) = default;
61 explicit Holder(T &&func) : _func(std::forward<T>(func)) {}
62
Brad Bishopc0eae112016-10-19 21:59:47 -040063 virtual void operator()(Manager &mgr) const override
Brad Bishopc038e012016-10-19 13:02:24 -040064 {
Brad Bishopc0eae112016-10-19 21:59:47 -040065 _func(mgr);
Brad Bishopc038e012016-10-19 13:02:24 -040066 }
67
Brad Bishopc0eae112016-10-19 21:59:47 -040068 virtual void operator()(Manager &mgr) override
Brad Bishopc038e012016-10-19 13:02:24 -040069 {
Brad Bishopc0eae112016-10-19 21:59:47 -040070 _func(mgr);
Brad Bishopc038e012016-10-19 13:02:24 -040071 }
72
73 private:
74 T _func;
75};
76
77} // namespace holder
78
79/** @struct Wrapper
80 * @brief Provides implicit type conversion from action functors.
81 *
82 * Converts action functors to ptr-to-holder.
83 */
84struct Wrapper
85{
86 template <typename T>
87 Wrapper(T &&func) :
88 _ptr(static_cast<std::shared_ptr<holder::Base>>(
89 std::make_shared<holder::Holder<T>>(
90 std::forward<T>(func)))) { }
91
92 ~Wrapper() = default;
93 Wrapper(const Wrapper&) = default;
94 Wrapper& operator=(const Wrapper&) = delete;
95 Wrapper(Wrapper&&) = default;
96 Wrapper& operator=(Wrapper&&) = default;
97
Brad Bishopc0eae112016-10-19 21:59:47 -040098 void operator()(Manager &mgr)
Brad Bishopc038e012016-10-19 13:02:24 -040099 {
Brad Bishopc0eae112016-10-19 21:59:47 -0400100 (*_ptr)(mgr);
Brad Bishopc038e012016-10-19 13:02:24 -0400101 }
Brad Bishopc0eae112016-10-19 21:59:47 -0400102 void operator()(Manager &mgr) const
Brad Bishopc038e012016-10-19 13:02:24 -0400103 {
Brad Bishopc0eae112016-10-19 21:59:47 -0400104 (*_ptr)(mgr);
Brad Bishopc038e012016-10-19 13:02:24 -0400105 }
106
107 private:
108 std::shared_ptr<holder::Base> _ptr;
109};
110
111} // namespace details
112
113/** @brief The default action. */
Brad Bishopc0eae112016-10-19 21:59:47 -0400114inline void noop(Manager &mgr) noexcept { }
Brad Bishopc038e012016-10-19 13:02:24 -0400115
Brad Bishop656a7d02016-10-19 22:20:02 -0400116/** @brief Destroy an object action. */
117inline auto destroyObject(const char *path)
118{
119 return [path](auto &m){m.destroyObject(path);};
120}
Brad Bishopc038e012016-10-19 13:02:24 -0400121} // namespace actions
122} // namespace manager
123} // namespace inventory
124} // namespace phosphor
125
126// vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4