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_ENUMLISTOPTION |
| 25 | #define ARM_COMPUTE_UTILS_ENUMLISTOPTION |
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 <set> |
| 30 | #include <sstream> |
| 31 | #include <stdexcept> |
| 32 | #include <string> |
| 33 | #include <vector> |
| 34 | |
| 35 | namespace arm_compute |
| 36 | { |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 37 | namespace utils |
Moritz Pflanzer | bb4a79b | 2017-07-05 11:02:46 +0100 | [diff] [blame] | 38 | { |
| 39 | /** Implementation of an option that accepts any number of values from a fixed set. */ |
| 40 | template <typename T> |
| 41 | class EnumListOption : public Option |
| 42 | { |
| 43 | public: |
| 44 | /** Construct option with allowed values. |
| 45 | * |
| 46 | * @param[in] name Name of the option. |
| 47 | * @param[in] allowed_values Set of allowed values for the option. |
| 48 | */ |
| 49 | EnumListOption(std::string name, std::set<T> allowed_values); |
| 50 | |
| 51 | /** Construct option with allowed values, a fixed number of accepted values and default values for the option. |
| 52 | * |
| 53 | * @param[in] name Name of the option. |
| 54 | * @param[in] allowed_values Set of allowed values for the option. |
| 55 | * @param[in] default_values Default values. |
| 56 | */ |
| 57 | EnumListOption(std::string name, std::set<T> allowed_values, std::initializer_list<T> &&default_values); |
| 58 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 59 | bool parse(std::string value) override; |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 60 | std::string help() const override; |
| 61 | |
| 62 | /** Get the values of the option. |
| 63 | * |
| 64 | * @return a list of the selected option values. |
| 65 | */ |
Moritz Pflanzer | bb4a79b | 2017-07-05 11:02:46 +0100 | [diff] [blame] | 66 | const std::vector<T> &value() const; |
| 67 | |
| 68 | private: |
| 69 | std::vector<T> _values{}; |
| 70 | std::set<T> _allowed_values{}; |
| 71 | }; |
| 72 | |
| 73 | template <typename T> |
| 74 | inline EnumListOption<T>::EnumListOption(std::string name, std::set<T> allowed_values) |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 75 | : Option{std::move(name)}, _allowed_values{std::move(allowed_values)} |
Moritz Pflanzer | bb4a79b | 2017-07-05 11:02:46 +0100 | [diff] [blame] | 76 | { |
| 77 | } |
| 78 | |
| 79 | template <typename T> |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 80 | inline EnumListOption<T>::EnumListOption(std::string name, |
| 81 | std::set<T> allowed_values, |
| 82 | std::initializer_list<T> &&default_values) |
| 83 | : Option{std::move(name), false, true}, |
| 84 | _values{std::forward<std::initializer_list<T>>(default_values)}, |
| 85 | _allowed_values{std::move(allowed_values)} |
Moritz Pflanzer | bb4a79b | 2017-07-05 11:02:46 +0100 | [diff] [blame] | 86 | { |
| 87 | } |
| 88 | |
| 89 | template <typename T> |
| 90 | bool EnumListOption<T>::parse(std::string value) |
| 91 | { |
| 92 | // Remove default values |
| 93 | _values.clear(); |
Moritz Pflanzer | 014fb63 | 2017-09-01 12:56:06 +0100 | [diff] [blame] | 94 | _is_set = true; |
Moritz Pflanzer | bb4a79b | 2017-07-05 11:02:46 +0100 | [diff] [blame] | 95 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 96 | std::stringstream stream{value}; |
Anthony Barbier | 4a4a164 | 2017-11-09 19:40:42 +0000 | [diff] [blame] | 97 | std::string item; |
Moritz Pflanzer | bb4a79b | 2017-07-05 11:02:46 +0100 | [diff] [blame] | 98 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 99 | while (!std::getline(stream, item, ',').fail()) |
Anthony Barbier | 4a4a164 | 2017-11-09 19:40:42 +0000 | [diff] [blame] | 100 | { |
| 101 | try |
Moritz Pflanzer | bb4a79b | 2017-07-05 11:02:46 +0100 | [diff] [blame] | 102 | { |
Moritz Pflanzer | 014fb63 | 2017-09-01 12:56:06 +0100 | [diff] [blame] | 103 | std::stringstream item_stream(item); |
| 104 | T typed_value{}; |
Moritz Pflanzer | bb4a79b | 2017-07-05 11:02:46 +0100 | [diff] [blame] | 105 | |
Moritz Pflanzer | 014fb63 | 2017-09-01 12:56:06 +0100 | [diff] [blame] | 106 | item_stream >> typed_value; |
| 107 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 108 | if (!item_stream.fail()) |
Moritz Pflanzer | bb4a79b | 2017-07-05 11:02:46 +0100 | [diff] [blame] | 109 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 110 | if (_allowed_values.count(typed_value) == 0) |
Moritz Pflanzer | bb4a79b | 2017-07-05 11:02:46 +0100 | [diff] [blame] | 111 | { |
Anthony Barbier | 4a4a164 | 2017-11-09 19:40:42 +0000 | [diff] [blame] | 112 | _is_set = false; |
| 113 | continue; |
Moritz Pflanzer | bb4a79b | 2017-07-05 11:02:46 +0100 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | _values.emplace_back(typed_value); |
| 117 | } |
| 118 | |
Moritz Pflanzer | 014fb63 | 2017-09-01 12:56:06 +0100 | [diff] [blame] | 119 | _is_set = _is_set && !item_stream.fail(); |
Moritz Pflanzer | bb4a79b | 2017-07-05 11:02:46 +0100 | [diff] [blame] | 120 | } |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 121 | catch (const std::invalid_argument &) |
Anthony Barbier | 4a4a164 | 2017-11-09 19:40:42 +0000 | [diff] [blame] | 122 | { |
| 123 | _is_set = false; |
| 124 | } |
| 125 | } |
Moritz Pflanzer | bb4a79b | 2017-07-05 11:02:46 +0100 | [diff] [blame] | 126 | |
Anthony Barbier | 4a4a164 | 2017-11-09 19:40:42 +0000 | [diff] [blame] | 127 | return _is_set; |
Moritz Pflanzer | bb4a79b | 2017-07-05 11:02:46 +0100 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | template <typename T> |
| 131 | std::string EnumListOption<T>::help() const |
| 132 | { |
| 133 | std::stringstream msg; |
| 134 | msg << "--" + name() + "={"; |
| 135 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 136 | for (const auto &value : _allowed_values) |
Moritz Pflanzer | bb4a79b | 2017-07-05 11:02:46 +0100 | [diff] [blame] | 137 | { |
| 138 | msg << value << ","; |
| 139 | } |
| 140 | |
Moritz Pflanzer | 014fb63 | 2017-09-01 12:56:06 +0100 | [diff] [blame] | 141 | msg << "}[,{...}[,...]] - " << _help; |
Moritz Pflanzer | bb4a79b | 2017-07-05 11:02:46 +0100 | [diff] [blame] | 142 | |
| 143 | return msg.str(); |
| 144 | } |
| 145 | |
| 146 | template <typename T> |
| 147 | inline const std::vector<T> &EnumListOption<T>::value() const |
| 148 | { |
| 149 | return _values; |
| 150 | } |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 151 | } // namespace utils |
Moritz Pflanzer | bb4a79b | 2017-07-05 11:02:46 +0100 | [diff] [blame] | 152 | } // namespace arm_compute |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 153 | #endif /* ARM_COMPUTE_UTILS_ENUMLISTOPTION */ |