1 /** 2 * Copyright © 2022 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 23 namespace phosphor::fan::control::json 24 { 25 26 using 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 * - "less_than" 46 * 47 * To add a new operator, derive a new class from BaseOperator and 48 * then create it accordingly in setOperator. 49 */ 50 class Modifier 51 { 52 public: 53 /** 54 * @brief Base class for operators 55 */ 56 struct BaseOperator 57 { 58 virtual ~BaseOperator() = default; 59 60 virtual PropertyVariantType operator()(double val) = 0; 61 62 virtual PropertyVariantType operator()(int32_t val) = 0; 63 64 virtual PropertyVariantType operator()(int64_t val) = 0; 65 66 virtual PropertyVariantType operator()(const std::string& val) = 0; 67 68 virtual PropertyVariantType operator()(bool val) = 0; 69 }; 70 71 Modifier() = delete; 72 ~Modifier() = default; 73 Modifier(const Modifier&) = delete; 74 Modifier& operator=(const Modifier&) = delete; 75 Modifier(Modifier&&) = delete; 76 Modifier& operator=(Modifier&&) = delete; 77 78 /** 79 * @brief Constructor 80 * 81 * @param[in] jsonObj - The JSON config object 82 */ 83 explicit Modifier(const json& jsonObj); 84 85 /** 86 * @brief Performs the operation 87 * 88 * @param[in] value - The variant to do the operation on 89 * 90 * @return PropertyVariantType - The result 91 */ 92 PropertyVariantType doOp(const PropertyVariantType& value); 93 94 private: 95 /** 96 * @brief Parse and set the value 97 * 98 * @param[in] jsonObj - The JSON config object 99 */ 100 void setValue(const json& jsonObj); 101 102 /** 103 * @brief Parse and set the operator 104 * 105 * @param[in] jsonObj - The JSON config object 106 */ 107 void setOperator(const json& jsonObj); 108 109 /** @brief The value used by the operator */ 110 PropertyVariantType _value; 111 112 /** @brief The operator that will be used */ 113 std::unique_ptr<BaseOperator> _operator; 114 }; 115 116 } // namespace phosphor::fan::control::json 117