blob: 57a2f6f027a67e4f553967edf476707b45be6576 [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{
12namespace actions
13{
14namespace details
15{
16namespace holder
17{
18
19/** @struct Base
20 * @brief Event action functor holder base.
21 *
22 * Provides an un-templated holder for actionsof any type with the correct
23 * function call signature.
24 */
25struct Base
26{
27 Base() = default;
28 virtual ~Base() = default;
29 Base(const Base&) = delete;
30 Base& operator=(const Base&) = delete;
31 Base(Base&&) = default;
32 Base& operator=(Base&&) = default;
33
34 virtual void operator()() const = 0;
35 virtual void operator()()
36 {
37 const_cast<const Base &>(*this)();
38 }
39};
40
41/** @struct Holder
42 * @brief Event action functor holder.
43 *
44 * Adapts a functor of any type (with the correct function call
45 * signature) to a non-templated type usable by the manager for
46 * actions.
47 *
48 * @tparam T - The functor type.
49 */
50template <typename T>
51struct Holder final : public Base
52{
53 Holder() = delete;
54 ~Holder() = default;
55 Holder(const Holder&) = delete;
56 Holder & operator=(const Holder&) = delete;
57 Holder(Holder&&) = default;
58 Holder& operator=(Holder&&) = default;
59 explicit Holder(T &&func) : _func(std::forward<T>(func)) {}
60
61 virtual void operator()() const override
62 {
63 _func();
64 }
65
66 virtual void operator()() override
67 {
68 _func();
69 }
70
71 private:
72 T _func;
73};
74
75} // namespace holder
76
77/** @struct Wrapper
78 * @brief Provides implicit type conversion from action functors.
79 *
80 * Converts action functors to ptr-to-holder.
81 */
82struct Wrapper
83{
84 template <typename T>
85 Wrapper(T &&func) :
86 _ptr(static_cast<std::shared_ptr<holder::Base>>(
87 std::make_shared<holder::Holder<T>>(
88 std::forward<T>(func)))) { }
89
90 ~Wrapper() = default;
91 Wrapper(const Wrapper&) = default;
92 Wrapper& operator=(const Wrapper&) = delete;
93 Wrapper(Wrapper&&) = default;
94 Wrapper& operator=(Wrapper&&) = default;
95
96 void operator()()
97 {
98 (*_ptr)();
99 }
100 void operator()() const
101 {
102 (*_ptr)();
103 }
104
105 private:
106 std::shared_ptr<holder::Base> _ptr;
107};
108
109} // namespace details
110
111/** @brief The default action. */
112inline void noop() noexcept { }
113
114} // namespace actions
115} // namespace manager
116} // namespace inventory
117} // namespace phosphor
118
119// vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4