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_ENUMOPTION |
| 25 | #define ARM_COMPUTE_TEST_ENUMOPTION |
| 26 | |
| 27 | #include "SimpleOption.h" |
| 28 | |
| 29 | #include <set> |
| 30 | #include <sstream> |
| 31 | #include <stdexcept> |
| 32 | #include <string> |
| 33 | |
| 34 | namespace arm_compute |
| 35 | { |
| 36 | namespace test |
| 37 | { |
| 38 | namespace framework |
| 39 | { |
| 40 | /** Implementation of a simple option that accepts a value from a fixed set. */ |
| 41 | template <typename T> |
| 42 | class EnumOption : public SimpleOption<T> |
| 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 | EnumOption(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_value Default value. |
| 57 | */ |
| 58 | EnumOption(std::string name, std::set<T> allowed_values, T default_value); |
| 59 | |
| 60 | bool parse(std::string value) override; |
| 61 | std::string help() const override; |
| 62 | const T &value() const; |
| 63 | |
| 64 | private: |
| 65 | std::set<T> _allowed_values{}; |
| 66 | }; |
| 67 | |
| 68 | template <typename T> |
| 69 | inline EnumOption<T>::EnumOption(std::string name, std::set<T> allowed_values) |
| 70 | : SimpleOption<T>{ std::move(name) }, _allowed_values{ std::move(allowed_values) } |
| 71 | { |
| 72 | } |
| 73 | |
| 74 | template <typename T> |
| 75 | inline EnumOption<T>::EnumOption(std::string name, std::set<T> allowed_values, T default_value) |
| 76 | : SimpleOption<T>{ std::move(name), std::move(default_value) }, _allowed_values{ std::move(allowed_values) } |
| 77 | { |
| 78 | } |
| 79 | |
| 80 | template <typename T> |
| 81 | bool EnumOption<T>::parse(std::string value) |
| 82 | { |
| 83 | try |
| 84 | { |
| 85 | std::stringstream stream{ value }; |
| 86 | T typed_value{}; |
| 87 | |
| 88 | stream >> typed_value; |
| 89 | |
| 90 | if(!stream.fail()) |
| 91 | { |
| 92 | if(_allowed_values.count(typed_value) == 0) |
| 93 | { |
| 94 | return false; |
| 95 | } |
| 96 | |
| 97 | this->_value = std::move(typed_value); |
| 98 | this->_is_set = true; |
| 99 | return true; |
| 100 | } |
| 101 | |
| 102 | return false; |
| 103 | } |
| 104 | catch(const std::invalid_argument &) |
| 105 | { |
| 106 | return false; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | template <typename T> |
| 111 | std::string EnumOption<T>::help() const |
| 112 | { |
| 113 | std::stringstream msg; |
| 114 | msg << "--" + this->name() + "={"; |
| 115 | |
| 116 | for(const auto &value : _allowed_values) |
| 117 | { |
| 118 | msg << value << ","; |
| 119 | } |
| 120 | |
| 121 | msg << "} - " << this->_help; |
| 122 | |
| 123 | return msg.str(); |
| 124 | } |
| 125 | |
| 126 | template <typename T> |
| 127 | inline const T &EnumOption<T>::value() const |
| 128 | { |
| 129 | return this->_value; |
| 130 | } |
| 131 | } // namespace framework |
| 132 | } // namespace test |
| 133 | } // namespace arm_compute |
| 134 | #endif /* ARM_COMPUTE_TEST_ENUMOPTION */ |