Brad Bishop | c038e01 | 2016-10-19 13:02:24 -0400 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include <utility> |
| 4 | #include <memory> |
| 5 | |
| 6 | namespace phosphor |
| 7 | { |
| 8 | namespace inventory |
| 9 | { |
| 10 | namespace manager |
| 11 | { |
Brad Bishop | c0eae11 | 2016-10-19 21:59:47 -0400 | [diff] [blame] | 12 | class Manager; |
| 13 | |
Brad Bishop | c038e01 | 2016-10-19 13:02:24 -0400 | [diff] [blame] | 14 | namespace actions |
| 15 | { |
| 16 | namespace details |
| 17 | { |
| 18 | namespace 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 | */ |
| 27 | struct 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 Bishop | c0eae11 | 2016-10-19 21:59:47 -0400 | [diff] [blame] | 36 | virtual void operator()(Manager &mgr) const = 0; |
| 37 | virtual void operator()(Manager &mgr) |
Brad Bishop | c038e01 | 2016-10-19 13:02:24 -0400 | [diff] [blame] | 38 | { |
Brad Bishop | c0eae11 | 2016-10-19 21:59:47 -0400 | [diff] [blame] | 39 | const_cast<const Base &>(*this)(mgr); |
Brad Bishop | c038e01 | 2016-10-19 13:02:24 -0400 | [diff] [blame] | 40 | } |
| 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 | */ |
| 52 | template <typename T> |
| 53 | struct 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 Bishop | c0eae11 | 2016-10-19 21:59:47 -0400 | [diff] [blame] | 63 | virtual void operator()(Manager &mgr) const override |
Brad Bishop | c038e01 | 2016-10-19 13:02:24 -0400 | [diff] [blame] | 64 | { |
Brad Bishop | c0eae11 | 2016-10-19 21:59:47 -0400 | [diff] [blame] | 65 | _func(mgr); |
Brad Bishop | c038e01 | 2016-10-19 13:02:24 -0400 | [diff] [blame] | 66 | } |
| 67 | |
Brad Bishop | c0eae11 | 2016-10-19 21:59:47 -0400 | [diff] [blame] | 68 | virtual void operator()(Manager &mgr) override |
Brad Bishop | c038e01 | 2016-10-19 13:02:24 -0400 | [diff] [blame] | 69 | { |
Brad Bishop | c0eae11 | 2016-10-19 21:59:47 -0400 | [diff] [blame] | 70 | _func(mgr); |
Brad Bishop | c038e01 | 2016-10-19 13:02:24 -0400 | [diff] [blame] | 71 | } |
| 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 | */ |
| 84 | struct 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 Bishop | c0eae11 | 2016-10-19 21:59:47 -0400 | [diff] [blame] | 98 | void operator()(Manager &mgr) |
Brad Bishop | c038e01 | 2016-10-19 13:02:24 -0400 | [diff] [blame] | 99 | { |
Brad Bishop | c0eae11 | 2016-10-19 21:59:47 -0400 | [diff] [blame] | 100 | (*_ptr)(mgr); |
Brad Bishop | c038e01 | 2016-10-19 13:02:24 -0400 | [diff] [blame] | 101 | } |
Brad Bishop | c0eae11 | 2016-10-19 21:59:47 -0400 | [diff] [blame] | 102 | void operator()(Manager &mgr) const |
Brad Bishop | c038e01 | 2016-10-19 13:02:24 -0400 | [diff] [blame] | 103 | { |
Brad Bishop | c0eae11 | 2016-10-19 21:59:47 -0400 | [diff] [blame] | 104 | (*_ptr)(mgr); |
Brad Bishop | c038e01 | 2016-10-19 13:02:24 -0400 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | private: |
| 108 | std::shared_ptr<holder::Base> _ptr; |
| 109 | }; |
| 110 | |
| 111 | } // namespace details |
| 112 | |
| 113 | /** @brief The default action. */ |
Brad Bishop | c0eae11 | 2016-10-19 21:59:47 -0400 | [diff] [blame] | 114 | inline void noop(Manager &mgr) noexcept { } |
Brad Bishop | c038e01 | 2016-10-19 13:02:24 -0400 | [diff] [blame] | 115 | |
Brad Bishop | 656a7d0 | 2016-10-19 22:20:02 -0400 | [diff] [blame] | 116 | /** @brief Destroy an object action. */ |
| 117 | inline auto destroyObject(const char *path) |
| 118 | { |
| 119 | return [path](auto &m){m.destroyObject(path);}; |
| 120 | } |
Brad Bishop | c038e01 | 2016-10-19 13:02:24 -0400 | [diff] [blame] | 121 | } // namespace actions |
| 122 | } // namespace manager |
| 123 | } // namespace inventory |
| 124 | } // namespace phosphor |
| 125 | |
| 126 | // vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 |