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