blob: c7a5bcff7bd925ba1374c8d8cc2628c6dc24d88f [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. */
31 virtual bool operator()(const any_ns::any& value) = 0;
32};
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
56 bool operator()(const any_ns::any& value) override
57 {
58 for (const auto& filterOps : ops)
59 {
60 try
61 {
62 // Apply filter operand to property value
63 if (!filterOps(any_ns::any_cast<T>(value)))
64 {
65 // Property value should be filtered
66 return true;
67 }
68 }
69 catch (const any_ns::bad_any_cast& bac)
70 {
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