Moritz Pflanzer | bb4a79b | 2017-07-05 11:02:46 +0100 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d9eaf61 | 2020-07-08 11:12:57 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 Arm Limited. |
Moritz Pflanzer | bb4a79b | 2017-07-05 11:02:46 +0100 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: MIT |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to |
| 8 | * deal in the Software without restriction, including without limitation the |
| 9 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 10 | * sell copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in all |
| 14 | * copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | * SOFTWARE. |
| 23 | */ |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 24 | #ifndef ARM_COMPUTE_UTILS_SIMPLEOPTION |
| 25 | #define ARM_COMPUTE_UTILS_SIMPLEOPTION |
Moritz Pflanzer | bb4a79b | 2017-07-05 11:02:46 +0100 | [diff] [blame] | 26 | |
| 27 | #include "Option.h" |
Moritz Pflanzer | bb4a79b | 2017-07-05 11:02:46 +0100 | [diff] [blame] | 28 | #include <sstream> |
| 29 | #include <stdexcept> |
| 30 | #include <string> |
| 31 | |
| 32 | namespace arm_compute |
| 33 | { |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 34 | namespace utils |
Moritz Pflanzer | bb4a79b | 2017-07-05 11:02:46 +0100 | [diff] [blame] | 35 | { |
| 36 | /** Implementation of an option that accepts a single value. */ |
| 37 | template <typename T> |
| 38 | class SimpleOption : public Option |
| 39 | { |
| 40 | public: |
| 41 | using Option::Option; |
| 42 | |
| 43 | /** Construct the option with the given default value. |
| 44 | * |
| 45 | * @param[in] name Name of the option. |
| 46 | * @param[in] default_value Default value. |
| 47 | */ |
| 48 | SimpleOption(std::string name, T default_value); |
| 49 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 50 | /** Parses the given string. |
| 51 | * |
| 52 | * @param[in] value String representation as passed on the command line. |
| 53 | * |
| 54 | * @return True if the value could be parsed by the specific subclass. |
| 55 | */ |
Moritz Pflanzer | bb4a79b | 2017-07-05 11:02:46 +0100 | [diff] [blame] | 56 | bool parse(std::string value) override; |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 57 | |
| 58 | /** Help message for the option. |
| 59 | * |
| 60 | * @return String representing the help message for the specific subclass. |
| 61 | */ |
Moritz Pflanzer | bb4a79b | 2017-07-05 11:02:46 +0100 | [diff] [blame] | 62 | std::string help() const override; |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 63 | |
| 64 | /** Get the option value. |
| 65 | * |
| 66 | * @return the option value. |
| 67 | */ |
| 68 | const T &value() const; |
Moritz Pflanzer | bb4a79b | 2017-07-05 11:02:46 +0100 | [diff] [blame] | 69 | |
| 70 | protected: |
| 71 | T _value{}; |
| 72 | }; |
| 73 | |
| 74 | template <typename T> |
| 75 | inline SimpleOption<T>::SimpleOption(std::string name, T default_value) |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 76 | : Option{std::move(name), false, true}, _value{std::move(default_value)} |
Moritz Pflanzer | bb4a79b | 2017-07-05 11:02:46 +0100 | [diff] [blame] | 77 | { |
| 78 | } |
| 79 | |
| 80 | template <typename T> |
| 81 | bool SimpleOption<T>::parse(std::string value) |
| 82 | { |
| 83 | try |
| 84 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 85 | std::stringstream stream{std::move(value)}; |
Moritz Pflanzer | bb4a79b | 2017-07-05 11:02:46 +0100 | [diff] [blame] | 86 | stream >> _value; |
| 87 | _is_set = !stream.fail(); |
| 88 | return _is_set; |
| 89 | } |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 90 | catch (const std::invalid_argument &) |
Moritz Pflanzer | bb4a79b | 2017-07-05 11:02:46 +0100 | [diff] [blame] | 91 | { |
| 92 | return false; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | template <> |
| 97 | inline bool SimpleOption<std::string>::parse(std::string value) |
| 98 | { |
| 99 | _value = std::move(value); |
| 100 | _is_set = true; |
| 101 | return true; |
| 102 | } |
| 103 | |
| 104 | template <typename T> |
| 105 | inline std::string SimpleOption<T>::help() const |
| 106 | { |
| 107 | return "--" + name() + "=VALUE - " + _help; |
| 108 | } |
| 109 | |
| 110 | template <typename T> |
| 111 | inline const T &SimpleOption<T>::value() const |
| 112 | { |
| 113 | return _value; |
| 114 | } |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 115 | } // namespace utils |
Moritz Pflanzer | bb4a79b | 2017-07-05 11:02:46 +0100 | [diff] [blame] | 116 | } // namespace arm_compute |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 117 | #endif /* ARM_COMPUTE_UTILS_SIMPLEOPTION */ |