blob: f7b809f810613f46ccb590fc96fb96677e4c40ad [file] [log] [blame]
Brad Bishop4041d722017-05-21 10:06:07 -04001#pragma once
2
3#include "callback.hpp"
4#include "data_types.hpp"
5
Ratan Gupta882d7412018-02-19 23:43:44 +05306#include <algorithm>
7
Brad Bishop4041d722017-05-21 10:06:07 -04008namespace phosphor
9{
10namespace dbus
11{
12namespace monitoring
13{
14
15/** @class CountCondition
16 * @brief Count properties that satisfy a condition.
17 *
18 * When invoked, a count class instance performs its condition
19 * test in two passes.
20 *
21 * In pass one, apply a C++ relational operator to the value of
22 * each property in the index and a value provided by the
23 * configuration file.
24 *
25 * Count the number of properties that pass the test in pass
26 * one. In pass two, apply a second C++ relational operator
27 * to the number of properties that pass the test from pass one
28 * to a count provided by the configuration file.
29 */
30template <typename T>
31class CountCondition : public IndexedConditional
32{
33 public:
34 CountCondition() = delete;
35 CountCondition(const CountCondition&) = default;
36 CountCondition(CountCondition&&) = default;
37 CountCondition& operator=(const CountCondition&) = default;
38 CountCondition& operator=(CountCondition&&) = default;
39 ~CountCondition() = default;
40
41 CountCondition(
42 const PropertyIndex& conditionIndex,
43 const std::function<bool(size_t)>& _countOp,
44 const std::function<bool(T)>& _propertyOp) :
45 IndexedConditional(conditionIndex),
46 countOp(_countOp),
47 propertyOp(_propertyOp) {}
48
49 bool operator()() override
50 {
51 // Count the number of properties in the index that
52 // pass the condition specified in the config file.
53 auto count = std::count_if(
54 index.cbegin(),
55 index.cend(),
56 [this](const auto & item)
57 // *INDENT-OFF*
58 {
59 const auto& storage = std::get<2>(
60 item.second);
61 // Don't count properties that don't exist.
62 if (storage.get().empty())
63 {
64 return false;
65 }
66 const auto& value = any_ns::any_cast<T>(
67 storage);
68 return propertyOp(value);
69 });
70 // *INDENT-ON*
71
72 // Now apply the count condition to the count.
73 return countOp(count);
74 }
75
76 private:
77 /** @brief The comparison to perform on the count. */
78 std::function<bool(size_t)> countOp;
79 /** @brief The comparison to perform on each property. */
80 std::function<bool(T)> propertyOp;
81};
82} // namespace monitoring
83} // namespace dbus
84} // namespace phosphor