blob: 3f59246105fbe5e11297bb328b1fcd1f1f2439e6 [file] [log] [blame]
Matt Spinlerc09b8c82021-08-05 16:29:47 -05001/**
2 * Copyright © 2021 IBM Corporation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#pragma once
17
18#include "config_base.hpp"
19
20#include <string>
21#include <vector>
22
23namespace phosphor::fan::control::json
24{
25
26using json = nlohmann::json;
27
28/**
29 * @class Modifier
30 *
31 * This class provides a doOp() function to modify a PropertyVariantType value
32 * based on a JSON config passed into its constructor.
33 *
34 * For example, with the JSON:
35 * {
36 * "operator": "minus",
37 * "value": 3
38 * }
39 *
40 * When doOp() is called, it will subtract 3 from the value passed
41 * into doOp() and return the result.
42 *
43 * The valid operators are:
44 * - "minus"
45 *
46 * To add a new operator, derive a new class from BaseOperator and
47 * then create it accordingly in setOperator.
48 */
49class Modifier
50{
51 public:
52 /**
53 * @brief Base class for operators
54 */
55 struct BaseOperator
56 {
57 virtual PropertyVariantType operator()(double val) = 0;
58
59 virtual PropertyVariantType operator()(int32_t val) = 0;
60
61 virtual PropertyVariantType operator()(int64_t val) = 0;
62
63 virtual PropertyVariantType operator()(const std::string& val) = 0;
64
65 virtual PropertyVariantType operator()(bool val) = 0;
66 };
67
68 Modifier() = delete;
69 ~Modifier() = default;
70 Modifier(const Modifier&) = delete;
71 Modifier& operator=(const Modifier&) = delete;
72 Modifier(Modifier&&) = delete;
73 Modifier& operator=(Modifier&&) = delete;
74
75 /**
76 * @brief Constructor
77 *
78 * @param[in] jsonObj - The JSON config object
79 */
80 Modifier(const json& jsonObj);
81
82 /**
83 * @brief Performs the operation
84 *
85 * @param[in] value - The variant to do the operation on
86 *
87 * @return PropertyVariantType - The result
88 */
89 PropertyVariantType doOp(const PropertyVariantType& value);
90
91 private:
92 /**
93 * @brief Parse and set the value
94 *
95 * @param[in] jsonObj - The JSON config object
96 */
97 void setValue(const json& jsonObj);
98
99 /**
100 * @brief Parse and set the operator
101 *
102 * @param[in] jsonObj - The JSON config object
103 */
104 void setOperator(const json& jsonObj);
105
106 /**
107 * @brief Subtracts _value from value
108 *
109 * @param[in] value - The value to subtract from
110 */
111 PropertyVariantType minus(const PropertyVariantType& value);
112
113 /** @brief The value used by the operator */
114 PropertyVariantType _value;
115
116 /** @brief The operator that will be used */
117 std::unique_ptr<BaseOperator> _operator;
118};
119
120} // namespace phosphor::fan::control::json