blob: f867ef804cdfbed16ced20496f4e810fb1c17fbd [file] [log] [blame]
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +01001/*
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
35namespace arm_compute
36{
37namespace test
38{
39namespace framework
40{
41/** Implementation of an option that accepts any number of values. */
42template <typename T>
43class ListOption : public Option
44{
45public:
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
59private:
60 std::vector<T> _values{};
61};
62
63template <typename T>
64inline 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
69template <typename T>
70bool ListOption<T>::parse(std::string value)
71{
72 try
73 {
74 std::stringstream stream{ value };
75 T typed_value{};
76
77 while(stream.good())
78 {
79 stream >> typed_value;
80
81 if(!stream.fail())
82 {
83 _values.emplace_back(typed_value);
84 }
85
86 if(!stream.eof())
87 {
88 stream.ignore(1, ',');
89 }
90 }
91
92 _is_set = !stream.fail();
93
94 return _is_set;
95 }
96 catch(const std::invalid_argument &)
97 {
98 return false;
99 }
100}
101
102template <typename T>
103inline std::string ListOption<T>::help() const
104{
105 return "--" + name() + "=VALUE[,VALUE[,...]] - " + _help;
106}
107
108template <typename T>
109inline 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 */