Matt Spinler | c09b8c8 | 2021-08-05 16:29:47 -0500 | [diff] [blame] | 1 | /** |
Mike Capps | b2e9a4f | 2022-06-13 10:15:42 -0400 | [diff] [blame] | 2 | * Copyright © 2022 IBM Corporation |
Matt Spinler | c09b8c8 | 2021-08-05 16:29:47 -0500 | [diff] [blame] | 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" |
Matt Spinler | c272790 | 2022-02-02 11:13:09 -0600 | [diff] [blame] | 45 | * - "less_than" |
Matt Spinler | c09b8c8 | 2021-08-05 16:29:47 -0500 | [diff] [blame] | 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 | { |
Mike Capps | b2e9a4f | 2022-06-13 10:15:42 -0400 | [diff] [blame] | 58 | virtual ~BaseOperator() = default; |
| 59 | |
Matt Spinler | c09b8c8 | 2021-08-05 16:29:47 -0500 | [diff] [blame] | 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 | */ |
Matt Spinler | 9b06243 | 2023-01-26 14:38:50 -0600 | [diff] [blame] | 83 | explicit Modifier(const json& jsonObj); |
Matt Spinler | c09b8c8 | 2021-08-05 16:29:47 -0500 | [diff] [blame] | 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 | |
Matt Spinler | c09b8c8 | 2021-08-05 16:29:47 -0500 | [diff] [blame] | 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 |