Moritz Pflanzer | bb4a79b | 2017-07-05 11:02:46 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 ARM Limited. |
| 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 | */ |
| 24 | #ifndef ARM_COMPUTE_TEST_ENUMLISTOPTION |
| 25 | #define ARM_COMPUTE_TEST_ENUMLISTOPTION |
| 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 | { |
| 38 | namespace test |
| 39 | { |
| 40 | namespace framework |
| 41 | { |
| 42 | /** Implementation of an option that accepts any number of values from a fixed set. */ |
| 43 | template <typename T> |
| 44 | class EnumListOption : public Option |
| 45 | { |
| 46 | public: |
| 47 | /** Construct option with allowed values. |
| 48 | * |
| 49 | * @param[in] name Name of the option. |
| 50 | * @param[in] allowed_values Set of allowed values for the option. |
| 51 | */ |
| 52 | EnumListOption(std::string name, std::set<T> allowed_values); |
| 53 | |
| 54 | /** Construct option with allowed values, a fixed number of accepted values and default values for the option. |
| 55 | * |
| 56 | * @param[in] name Name of the option. |
| 57 | * @param[in] allowed_values Set of allowed values for the option. |
| 58 | * @param[in] default_values Default values. |
| 59 | */ |
| 60 | EnumListOption(std::string name, std::set<T> allowed_values, std::initializer_list<T> &&default_values); |
| 61 | |
| 62 | bool parse(std::string value) override; |
| 63 | std::string help() const override; |
| 64 | const std::vector<T> &value() const; |
| 65 | |
| 66 | private: |
| 67 | std::vector<T> _values{}; |
| 68 | std::set<T> _allowed_values{}; |
| 69 | }; |
| 70 | |
| 71 | template <typename T> |
| 72 | inline EnumListOption<T>::EnumListOption(std::string name, std::set<T> allowed_values) |
| 73 | : Option{ std::move(name) }, _allowed_values{ std::move(allowed_values) } |
| 74 | { |
| 75 | } |
| 76 | |
| 77 | template <typename T> |
| 78 | inline EnumListOption<T>::EnumListOption(std::string name, std::set<T> allowed_values, std::initializer_list<T> &&default_values) |
| 79 | : Option{ std::move(name), false, true }, _values{ std::forward<std::initializer_list<T>>(default_values) }, _allowed_values{ std::move(allowed_values) } |
| 80 | { |
| 81 | } |
| 82 | |
| 83 | template <typename T> |
| 84 | bool EnumListOption<T>::parse(std::string value) |
| 85 | { |
| 86 | // Remove default values |
| 87 | _values.clear(); |
| 88 | |
| 89 | try |
| 90 | { |
| 91 | std::stringstream stream{ value }; |
| 92 | T typed_value{}; |
| 93 | |
| 94 | while(stream.good()) |
| 95 | { |
| 96 | stream >> typed_value; |
| 97 | |
| 98 | if(!stream.fail()) |
| 99 | { |
| 100 | if(_allowed_values.count(typed_value) == 0) |
| 101 | { |
| 102 | _values.clear(); |
| 103 | return false; |
| 104 | } |
| 105 | |
| 106 | _values.emplace_back(typed_value); |
| 107 | } |
| 108 | |
| 109 | if(!stream.eof()) |
| 110 | { |
| 111 | stream.ignore(1, ','); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | _is_set = !stream.fail(); |
| 116 | |
| 117 | return _is_set; |
| 118 | } |
| 119 | catch(const std::invalid_argument &) |
| 120 | { |
| 121 | return false; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | template <typename T> |
| 126 | std::string EnumListOption<T>::help() const |
| 127 | { |
| 128 | std::stringstream msg; |
| 129 | msg << "--" + name() + "={"; |
| 130 | |
| 131 | for(const auto &value : _allowed_values) |
| 132 | { |
| 133 | msg << value << ","; |
| 134 | } |
| 135 | |
| 136 | msg << "} - " << _help; |
| 137 | |
| 138 | return msg.str(); |
| 139 | } |
| 140 | |
| 141 | template <typename T> |
| 142 | inline const std::vector<T> &EnumListOption<T>::value() const |
| 143 | { |
| 144 | return _values; |
| 145 | } |
| 146 | } // namespace framework |
| 147 | } // namespace test |
| 148 | } // namespace arm_compute |
| 149 | #endif /* ARM_COMPUTE_TEST_ENUMLISTOPTION */ |