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_LISTOPTION |
| 25 | #define ARM_COMPUTE_UTILS_LISTOPTION |
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 <initializer_list> |
| 29 | #include <sstream> |
| 30 | #include <stdexcept> |
| 31 | #include <string> |
| 32 | #include <vector> |
| 33 | |
| 34 | namespace arm_compute |
| 35 | { |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 36 | namespace utils |
Moritz Pflanzer | bb4a79b | 2017-07-05 11:02:46 +0100 | [diff] [blame] | 37 | { |
| 38 | /** Implementation of an option that accepts any number of values. */ |
| 39 | template <typename T> |
| 40 | class ListOption : public Option |
| 41 | { |
| 42 | public: |
| 43 | using Option::Option; |
| 44 | |
| 45 | /** Construct the option with the given default values. |
| 46 | * |
| 47 | * @param[in] name Name of the option. |
| 48 | * @param[in] default_values Default values. |
| 49 | */ |
| 50 | ListOption(std::string name, std::initializer_list<T> &&default_values); |
| 51 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 52 | bool parse(std::string value) override; |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 53 | std::string help() const override; |
| 54 | |
| 55 | /** Get the list of option values. |
| 56 | * |
| 57 | * @return get the list of option values. |
| 58 | */ |
Moritz Pflanzer | bb4a79b | 2017-07-05 11:02:46 +0100 | [diff] [blame] | 59 | const std::vector<T> &value() const; |
| 60 | |
| 61 | private: |
| 62 | std::vector<T> _values{}; |
| 63 | }; |
| 64 | |
| 65 | template <typename T> |
| 66 | inline ListOption<T>::ListOption(std::string name, std::initializer_list<T> &&default_values) |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 67 | : Option{std::move(name), false, true}, _values{std::forward<std::initializer_list<T>>(default_values)} |
Moritz Pflanzer | bb4a79b | 2017-07-05 11:02:46 +0100 | [diff] [blame] | 68 | { |
| 69 | } |
| 70 | |
| 71 | template <typename T> |
| 72 | bool ListOption<T>::parse(std::string value) |
| 73 | { |
Moritz Pflanzer | 014fb63 | 2017-09-01 12:56:06 +0100 | [diff] [blame] | 74 | _is_set = true; |
| 75 | |
Moritz Pflanzer | bb4a79b | 2017-07-05 11:02:46 +0100 | [diff] [blame] | 76 | try |
| 77 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 78 | std::stringstream stream{value}; |
Moritz Pflanzer | 014fb63 | 2017-09-01 12:56:06 +0100 | [diff] [blame] | 79 | std::string item; |
Moritz Pflanzer | bb4a79b | 2017-07-05 11:02:46 +0100 | [diff] [blame] | 80 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 81 | while (!std::getline(stream, item, ',').fail()) |
Moritz Pflanzer | bb4a79b | 2017-07-05 11:02:46 +0100 | [diff] [blame] | 82 | { |
Moritz Pflanzer | 014fb63 | 2017-09-01 12:56:06 +0100 | [diff] [blame] | 83 | std::stringstream item_stream(item); |
| 84 | T typed_value{}; |
Moritz Pflanzer | bb4a79b | 2017-07-05 11:02:46 +0100 | [diff] [blame] | 85 | |
Moritz Pflanzer | 014fb63 | 2017-09-01 12:56:06 +0100 | [diff] [blame] | 86 | item_stream >> typed_value; |
| 87 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 88 | if (!item_stream.fail()) |
Moritz Pflanzer | bb4a79b | 2017-07-05 11:02:46 +0100 | [diff] [blame] | 89 | { |
| 90 | _values.emplace_back(typed_value); |
| 91 | } |
| 92 | |
Moritz Pflanzer | 014fb63 | 2017-09-01 12:56:06 +0100 | [diff] [blame] | 93 | _is_set = _is_set && !item_stream.fail(); |
Moritz Pflanzer | bb4a79b | 2017-07-05 11:02:46 +0100 | [diff] [blame] | 94 | } |
| 95 | |
Moritz Pflanzer | bb4a79b | 2017-07-05 11:02:46 +0100 | [diff] [blame] | 96 | return _is_set; |
| 97 | } |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 98 | catch (const std::invalid_argument &) |
Moritz Pflanzer | bb4a79b | 2017-07-05 11:02:46 +0100 | [diff] [blame] | 99 | { |
| 100 | return false; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | template <typename T> |
| 105 | inline std::string ListOption<T>::help() const |
| 106 | { |
| 107 | return "--" + name() + "=VALUE[,VALUE[,...]] - " + _help; |
| 108 | } |
| 109 | |
| 110 | template <typename T> |
| 111 | inline const std::vector<T> &ListOption<T>::value() const |
| 112 | { |
| 113 | return _values; |
| 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_LISTOPTION */ |