blob: d19bfbdc0f429581c05e5721c36dd4f76d0654c0 [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_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
36namespace arm_compute
37{
38namespace test
39{
40namespace framework
41{
42/** Implementation of an option that accepts any number of values from a fixed set. */
43template <typename T>
44class EnumListOption : public Option
45{
46public:
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
66private:
67 std::vector<T> _values{};
68 std::set<T> _allowed_values{};
69};
70
71template <typename T>
72inline 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
77template <typename T>
78inline 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
83template <typename T>
84bool EnumListOption<T>::parse(std::string value)
85{
86 // Remove default values
87 _values.clear();
Moritz Pflanzer014fb632017-09-01 12:56:06 +010088 _is_set = true;
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +010089
90 try
91 {
92 std::stringstream stream{ value };
Moritz Pflanzer014fb632017-09-01 12:56:06 +010093 std::string item;
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +010094
Moritz Pflanzer014fb632017-09-01 12:56:06 +010095 while(!std::getline(stream, item, ',').fail())
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +010096 {
Moritz Pflanzer014fb632017-09-01 12:56:06 +010097 std::stringstream item_stream(item);
98 T typed_value{};
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +010099
Moritz Pflanzer014fb632017-09-01 12:56:06 +0100100 item_stream >> typed_value;
101
102 if(!item_stream.fail())
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +0100103 {
104 if(_allowed_values.count(typed_value) == 0)
105 {
106 _values.clear();
107 return false;
108 }
109
110 _values.emplace_back(typed_value);
111 }
112
Moritz Pflanzer014fb632017-09-01 12:56:06 +0100113 _is_set = _is_set && !item_stream.fail();
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +0100114 }
115
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +0100116 return _is_set;
117 }
118 catch(const std::invalid_argument &)
119 {
120 return false;
121 }
122}
123
124template <typename T>
125std::string EnumListOption<T>::help() const
126{
127 std::stringstream msg;
128 msg << "--" + name() + "={";
129
130 for(const auto &value : _allowed_values)
131 {
132 msg << value << ",";
133 }
134
Moritz Pflanzer014fb632017-09-01 12:56:06 +0100135 msg << "}[,{...}[,...]] - " << _help;
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +0100136
137 return msg.str();
138}
139
140template <typename T>
141inline const std::vector<T> &EnumListOption<T>::value() const
142{
143 return _values;
144}
145} // namespace framework
146} // namespace test
147} // namespace arm_compute
148#endif /* ARM_COMPUTE_TEST_ENUMLISTOPTION */