1 /** 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 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 PropertyVariantType operator()(double val) = 0; 59 60 virtual PropertyVariantType operator()(int32_t val) = 0; 61 62 virtual PropertyVariantType operator()(int64_t val) = 0; 63 64 virtual PropertyVariantType operator()(const std::string& val) = 0; 65 66 virtual PropertyVariantType operator()(bool val) = 0; 67 }; 68 69 Modifier() = delete; 70 ~Modifier() = default; 71 Modifier(const Modifier&) = delete; 72 Modifier& operator=(const Modifier&) = delete; 73 Modifier(Modifier&&) = delete; 74 Modifier& operator=(Modifier&&) = delete; 75 76 /** 77 * @brief Constructor 78 * 79 * @param[in] jsonObj - The JSON config object 80 */ 81 Modifier(const json& jsonObj); 82 83 /** 84 * @brief Performs the operation 85 * 86 * @param[in] value - The variant to do the operation on 87 * 88 * @return PropertyVariantType - The result 89 */ 90 PropertyVariantType doOp(const PropertyVariantType& value); 91 92 private: 93 /** 94 * @brief Parse and set the value 95 * 96 * @param[in] jsonObj - The JSON config object 97 */ 98 void setValue(const json& jsonObj); 99 100 /** 101 * @brief Parse and set the operator 102 * 103 * @param[in] jsonObj - The JSON config object 104 */ 105 void setOperator(const json& jsonObj); 106 107 /** @brief The value used by the operator */ 108 PropertyVariantType _value; 109 110 /** @brief The operator that will be used */ 111 std::unique_ptr<BaseOperator> _operator; 112 }; 113 114 } // namespace phosphor::fan::control::json 115