blob: 13b1533fa2d9705c76290e5cff54e9efa767a319 [file] [log] [blame]
Matthew Barthefe01582019-09-09 15:22:37 -05001#pragma once
2
3#include "data_types.hpp"
4
5#include <algorithm>
6#include <functional>
7
8namespace phosphor
9{
10namespace dbus
11{
12namespace monitoring
13{
14
15/** @class Filters
16 * @brief Filter interface
17 *
18 * Filters of any type can be applied to property value changes.
19 */
20class Filters
21{
22 public:
23 Filters() = default;
24 Filters(const Filters&) = delete;
25 Filters(Filters&&) = default;
26 Filters& operator=(const Filters&) = delete;
27 Filters& operator=(Filters&&) = default;
28 virtual ~Filters() = default;
29
30 /** @brief Apply filter operations to a property value. */
Patrick Williams26dc0bc2022-06-16 17:06:18 -050031 virtual bool operator()(const std::any& value) = 0;
Matthew Barthefe01582019-09-09 15:22:37 -050032};
33
34/** @class OperandFilters
35 * @brief Filter property values utilzing operand based functions.
36 *
37 * When configured, an operand filter is applied to a property value each
38 * time it changes to determine if that property value should be filtered
39 * from being stored or used within a given callback function.
40 */
41template <typename T>
42class OperandFilters : public Filters
43{
44 public:
45 OperandFilters() = delete;
46 OperandFilters(const OperandFilters&) = delete;
47 OperandFilters(OperandFilters&&) = default;
48 OperandFilters& operator=(const OperandFilters&) = delete;
49 OperandFilters& operator=(OperandFilters&&) = default;
50 virtual ~OperandFilters() = default;
51 explicit OperandFilters(const std::vector<std::function<bool(T)>>& _ops) :
52 Filters(), ops(std::move(_ops))
53 {
54 }
55
Patrick Williams26dc0bc2022-06-16 17:06:18 -050056 bool operator()(const std::any& value) override
Matthew Barthefe01582019-09-09 15:22:37 -050057 {
58 for (const auto& filterOps : ops)
59 {
60 try
61 {
62 // Apply filter operand to property value
Patrick Williams26dc0bc2022-06-16 17:06:18 -050063 if (!filterOps(std::any_cast<T>(value)))
Matthew Barthefe01582019-09-09 15:22:37 -050064 {
65 // Property value should be filtered
66 return true;
67 }
68 }
Patrick Williams26dc0bc2022-06-16 17:06:18 -050069 catch (const std::bad_any_cast& bac)
Matthew Barthefe01582019-09-09 15:22:37 -050070 {
71 // Unable to cast property value to filter value type
72 // to check filter, continue to next filter op
73 continue;
74 }
75 }
76
77 // Property value should not be filtered
78 return false;
79 }
80
81 private:
82 /** @brief List of operand based filter functions. */
83 const std::vector<std::function<bool(T)>> ops;
84};
85
86} // namespace monitoring
87} // namespace dbus
88} // namespace phosphor