Refactoring: Changed all check parameters starting with a 'p' to the new rulespec...
[check_mk.git] / livestatus / src / IntFilter.cc
blobfce79a437cf6694edc3e7b62fd0cbdc48775c858
1 // +------------------------------------------------------------------+
2 // | ____ _ _ __ __ _ __ |
3 // | / ___| |__ ___ ___| | __ | \/ | |/ / |
4 // | | | | '_ \ / _ \/ __| |/ / | |\/| | ' / |
5 // | | |___| | | | __/ (__| < | | | | . \ |
6 // | \____|_| |_|\___|\___|_|\_\___|_| |_|_|\_\ |
7 // | |
8 // | Copyright Mathias Kettner 2014 mk@mathias-kettner.de |
9 // +------------------------------------------------------------------+
11 // This file is part of Check_MK.
12 // The official homepage is at http://mathias-kettner.de/check_mk.
14 // check_mk is free software; you can redistribute it and/or modify it
15 // under the terms of the GNU General Public License as published by
16 // the Free Software Foundation in version 2. check_mk is distributed
17 // in the hope that it will be useful, but WITHOUT ANY WARRANTY; with-
18 // out even the implied warranty of MERCHANTABILITY or FITNESS FOR A
19 // PARTICULAR PURPOSE. See the GNU General Public License for more de-
20 // tails. You should have received a copy of the GNU General Public
21 // License along with GNU Make; see the file COPYING. If not, write
22 // to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 // Boston, MA 02110-1301 USA.
25 #include "IntFilter.h"
26 #include <cstdlib>
27 #include "Filter.h"
28 #include "IntColumn.h"
29 #include "Row.h"
31 IntFilter::IntFilter(Kind kind, const IntColumn &column,
32 RelationalOperator relOp, const std::string &value)
33 : ColumnFilter(kind, column, relOp, value)
34 , _column(column)
35 , _ref_value(atoi(value.c_str())) {}
37 namespace {
38 bool eval(int32_t x, RelationalOperator op, int32_t y) {
39 switch (op) {
40 case RelationalOperator::equal:
41 return x == y;
42 case RelationalOperator::not_equal:
43 return x != y;
44 case RelationalOperator::matches: // superset
45 return (x & y) == y;
46 case RelationalOperator::doesnt_match: // not superset
47 return (x & y) != y;
48 case RelationalOperator::equal_icase: // subset
49 return (x & y) == x;
50 case RelationalOperator::not_equal_icase: // not subset
51 return (x & y) != x;
52 case RelationalOperator::matches_icase: // contains any
53 return (x & y) != 0;
54 case RelationalOperator::doesnt_match_icase: // contains none of
55 return (x & y) == 0;
56 case RelationalOperator::less:
57 return x < y;
58 case RelationalOperator::greater_or_equal:
59 return x >= y;
60 case RelationalOperator::greater:
61 return x > y;
62 case RelationalOperator::less_or_equal:
63 return x <= y;
65 return false;
67 } // namespace
69 bool IntFilter::accepts(Row row, const contact *auth_user,
70 std::chrono::seconds /*timezone_offset*/) const {
71 return eval(_column.getValue(row, auth_user), oper(), _ref_value);
74 std::optional<int32_t> IntFilter::greatestLowerBoundFor(
75 const std::string &column_name,
76 std::chrono::seconds /* timezone_offset */) const {
77 if (column_name != columnName()) {
78 return {}; // wrong column
80 switch (oper()) {
81 case RelationalOperator::equal:
82 case RelationalOperator::greater_or_equal:
83 return {_ref_value};
84 case RelationalOperator::greater:
85 return {_ref_value + 1};
86 case RelationalOperator::not_equal:
87 case RelationalOperator::matches: // superset
88 case RelationalOperator::doesnt_match: // not superset
89 case RelationalOperator::equal_icase: // subset
90 case RelationalOperator::not_equal_icase: // not subset
91 case RelationalOperator::matches_icase: // contains any
92 case RelationalOperator::doesnt_match_icase: // contains none of
93 case RelationalOperator::less:
94 case RelationalOperator::less_or_equal:
95 // NOTE: If we use the equivalent 'return {}' here and the other
96 // std::nullopt occurences below, we run into g++/libstdc++ bug
97 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=86465. :-/
98 return std::nullopt;
100 return std::nullopt; // unreachable
103 std::optional<int32_t> IntFilter::leastUpperBoundFor(
104 const std::string &column_name,
105 std::chrono::seconds /* timezone_offset */) const {
106 if (column_name != columnName()) {
107 return {}; // wrong column
109 switch (oper()) {
110 case RelationalOperator::equal:
111 case RelationalOperator::less_or_equal:
112 return {_ref_value};
113 case RelationalOperator::less:
114 return {_ref_value - 1};
115 case RelationalOperator::not_equal:
116 case RelationalOperator::matches: // superset
117 case RelationalOperator::doesnt_match: // not superset
118 case RelationalOperator::equal_icase: // subset
119 case RelationalOperator::not_equal_icase: // not subset
120 case RelationalOperator::matches_icase: // contains any
121 case RelationalOperator::doesnt_match_icase: // contains none of
122 case RelationalOperator::greater_or_equal:
123 case RelationalOperator::greater:
124 return std::nullopt;
126 return std::nullopt; // unreachable
129 std::optional<std::bitset<32>> IntFilter::valueSetLeastUpperBoundFor(
130 const std::string &column_name,
131 std::chrono::seconds /* timezone_offset */) const {
132 if (column_name != columnName()) {
133 return {}; // wrong column
135 std::bitset<32> result;
136 for (int32_t bit = 0; bit < 32; ++bit) {
137 result[bit] = eval(bit, oper(), _ref_value);
139 return {result};
142 std::unique_ptr<Filter> IntFilter::copy() const {
143 return std::make_unique<IntFilter>(*this);
146 std::unique_ptr<Filter> IntFilter::negate() const {
147 return std::make_unique<IntFilter>(
148 kind(), _column, negateRelationalOperator(oper()), value());