xref: /openbmc/phosphor-dbus-monitor/src/callback.hpp (revision 731171a12c6d52ead2d93303867b6d731d4d6728)
1 #pragma once
2 
3 #include "data_types.hpp"
4 
5 namespace phosphor
6 {
7 namespace dbus
8 {
9 namespace monitoring
10 {
11 
12 /** @class Callback
13  *  @brief Callback interface.
14  *
15  *  Callbacks of any type can be run.
16  */
17 class Callback
18 {
19     public:
20         Callback() = default;
21         Callback(const Callback&) = delete;
22         Callback(Callback&&) = default;
23         Callback& operator=(const Callback&) = delete;
24         Callback& operator=(Callback&&) = default;
25         virtual ~Callback() = default;
26 
27         /** @brief Run the callback. */
28         virtual void operator()() = 0;
29 };
30 
31 /** @class IndexedCallback
32  *  @brief Callback with an index.
33  */
34 class IndexedCallback : public Callback
35 {
36     public:
37         IndexedCallback() = delete;
38         IndexedCallback(const IndexedCallback&) = delete;
39         IndexedCallback(IndexedCallback&&) = default;
40         IndexedCallback& operator=(const IndexedCallback&) = delete;
41         IndexedCallback& operator=(IndexedCallback&&) = default;
42         virtual ~IndexedCallback() = default;
43         explicit IndexedCallback(const PropertyIndex& callbackIndex)
44             : Callback(), index(callbackIndex) {}
45 
46         /** @brief Run the callback. */
47         virtual void operator()() override = 0;
48 
49     protected:
50 
51         /** @brief Property names and their associated storage. */
52         const PropertyIndex& index;
53 };
54 
55 /** @class GroupOfCallbacks
56  *  @brief Invoke multiple callbacks.
57  *
58  *  A group of callbacks is implemented as a vector of array indicies
59  *  into an external array  of callbacks.  The group function call
60  *  operator traverses the vector of indicies, invoking each
61  *  callback.
62  *
63  *  @tparam CallbackAccess - Access to the array of callbacks.
64  */
65 template <typename CallbackAccess>
66 class GroupOfCallbacks : public Callback
67 {
68     public:
69         GroupOfCallbacks() = delete;
70         GroupOfCallbacks(const GroupOfCallbacks&) = delete;
71         GroupOfCallbacks(GroupOfCallbacks&&) = default;
72         GroupOfCallbacks& operator=(const GroupOfCallbacks&) = delete;
73         GroupOfCallbacks& operator=(GroupOfCallbacks&&) = default;
74         ~GroupOfCallbacks() = default;
75         explicit GroupOfCallbacks(
76             const std::vector<size_t>& graphEntry)
77             : graph(graphEntry) {}
78 
79         /** @brief Run the callbacks. */
80         void operator()() override
81         {
82             for (auto e : graph)
83             {
84                 (*CallbackAccess::get()[e])();
85             }
86         }
87 
88     private:
89         /** @brief The offsets of the callbacks in the group. */
90         const std::vector<size_t>& graph;
91 };
92 
93 } // namespace monitoring
94 } // namespace dbus
95 } // namespace phosphor
96