blob: 7c6f69b28ef3a05313d87fa700996c0ca39f038f [file] [log] [blame]
Brad Bishopc1283ae2017-05-20 21:42:38 -04001#pragma once
2
3#include "data_types.hpp"
4
5namespace phosphor
6{
7namespace dbus
8{
9namespace monitoring
10{
11
12/** @class Callback
13 * @brief Callback interface.
14 *
15 * Callbacks of any type can be run.
16 */
17class 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 */
34class 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
Brad Bishop49e66172017-05-23 19:16:21 -040055/** @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 */
65template <typename CallbackAccess>
66class 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
Brad Bishopc1283ae2017-05-20 21:42:38 -040093} // namespace monitoring
94} // namespace dbus
95} // namespace phosphor