summaryrefslogtreecommitdiffstats
path: root/actions.hpp
blob: ff0801907b9d624e98dc039c74d5780194b37323 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#pragma once

#include <utility>
#include <memory>

namespace phosphor
{
namespace inventory
{
namespace manager
{
class Manager;

namespace actions
{
namespace details
{
namespace holder
{

/** @struct Base
 *  @brief Event action functor holder base.
 *
 *  Provides an un-templated holder for actionsof any type with the correct
 *  function call signature.
 */
struct Base
{
    Base() = default;
    virtual ~Base() = default;
    Base(const Base&) = delete;
    Base& operator=(const Base&) = delete;
    Base(Base&&) = default;
    Base& operator=(Base&&) = default;

    virtual void operator()(Manager &mgr) const = 0;
    virtual void operator()(Manager &mgr)
    {
        const_cast<const Base &>(*this)(mgr);
    }
};

/** @struct Holder
 *  @brief Event action functor holder.
 *
 *  Adapts a functor of any type (with the correct function call
 *  signature) to a non-templated type usable by the manager for
 *  actions.
 *
 *  @tparam T - The functor type.
 */
template <typename T>
struct Holder final : public Base
{
    Holder() = delete;
    ~Holder() = default;
    Holder(const Holder&) = delete;
    Holder & operator=(const Holder&) = delete;
    Holder(Holder&&) = default;
    Holder& operator=(Holder&&) = default;
    explicit Holder(T &&func) : _func(std::forward<T>(func)) {}

    virtual void operator()(Manager &mgr) const override
    {
        _func(mgr);
    }

    virtual void operator()(Manager &mgr) override
    {
        _func(mgr);
    }

    private:
    T _func;
};

} // namespace holder

/** @struct Wrapper
 *  @brief Provides implicit type conversion from action functors.
 *
 *  Converts action functors to ptr-to-holder.
 */
struct Wrapper
{
    template <typename T>
    Wrapper(T &&func) :
        _ptr(static_cast<std::shared_ptr<holder::Base>>(
                    std::make_shared<holder::Holder<T>>(
                        std::forward<T>(func)))) { }

    ~Wrapper() = default;
    Wrapper(const Wrapper&) = default;
    Wrapper& operator=(const Wrapper&) = delete;
    Wrapper(Wrapper&&) = default;
    Wrapper& operator=(Wrapper&&) = default;

    void operator()(Manager &mgr)
    {
        (*_ptr)(mgr);
    }
    void operator()(Manager &mgr) const
    {
        (*_ptr)(mgr);
    }

    private:
    std::shared_ptr<holder::Base> _ptr;
};

} // namespace details

/** @brief The default action.  */
inline void noop(Manager &mgr) noexcept { }

/** @brief Destroy an object action.  */
inline auto destroyObject(const char *path)
{
    return [path](auto &m){m.destroyObject(path);};
}
} // namespace actions
} // namespace manager
} // namespace inventory
} // namespace phosphor

// vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
OpenPOWER on IntegriCloud