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