blob: 39006d86b993c4b1560624bb68bc93701e237623 [file] [log] [blame]
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +01001/*
Alex Gildayc357c472018-03-21 13:54:09 +00002 * Copyright (c) 2017-2018 ARM Limited.
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +01003 *
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;
Alex Gildayc357c472018-03-21 13:54:09 +000063 std::string help() const override;
64
65 /** Get the values of the option.
66 *
67 * @return a list of the selected option values.
68 */
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +010069 const std::vector<T> &value() const;
70
71private:
72 std::vector<T> _values{};
73 std::set<T> _allowed_values{};
74};
75
76template <typename T>
77inline EnumListOption<T>::EnumListOption(std::string name, std::set<T> allowed_values)
78 : Option{ std::move(name) }, _allowed_values{ std::move(allowed_values) }
79{
80}
81
82template <typename T>
83inline EnumListOption<T>::EnumListOption(std::string name, std::set<T> allowed_values, std::initializer_list<T> &&default_values)
84 : Option{ std::move(name), false, true }, _values{ std::forward<std::initializer_list<T>>(default_values) }, _allowed_values{ std::move(allowed_values) }
85{
86}
87
88template <typename T>
89bool EnumListOption<T>::parse(std::string value)
90{
91 // Remove default values
92 _values.clear();
Moritz Pflanzer014fb632017-09-01 12:56:06 +010093 _is_set = true;
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +010094
Anthony Barbier4a4a1642017-11-09 19:40:42 +000095 std::stringstream stream{ value };
96 std::string item;
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +010097
Anthony Barbier4a4a1642017-11-09 19:40:42 +000098 while(!std::getline(stream, item, ',').fail())
99 {
100 try
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +0100101 {
Moritz Pflanzer014fb632017-09-01 12:56:06 +0100102 std::stringstream item_stream(item);
103 T typed_value{};
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +0100104
Moritz Pflanzer014fb632017-09-01 12:56:06 +0100105 item_stream >> typed_value;
106
107 if(!item_stream.fail())
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +0100108 {
109 if(_allowed_values.count(typed_value) == 0)
110 {
Anthony Barbier4a4a1642017-11-09 19:40:42 +0000111 _is_set = false;
112 continue;
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +0100113 }
114
115 _values.emplace_back(typed_value);
116 }
117
Moritz Pflanzer014fb632017-09-01 12:56:06 +0100118 _is_set = _is_set && !item_stream.fail();
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +0100119 }
Anthony Barbier4a4a1642017-11-09 19:40:42 +0000120 catch(const std::invalid_argument &)
121 {
122 _is_set = false;
123 }
124 }
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +0100125
Anthony Barbier4a4a1642017-11-09 19:40:42 +0000126 return _is_set;
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +0100127}
128
129template <typename T>
130std::string EnumListOption<T>::help() const
131{
132 std::stringstream msg;
133 msg << "--" + name() + "={";
134
135 for(const auto &value : _allowed_values)
136 {
137 msg << value << ",";
138 }
139
Moritz Pflanzer014fb632017-09-01 12:56:06 +0100140 msg << "}[,{...}[,...]] - " << _help;
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +0100141
142 return msg.str();
143}
144
145template <typename T>
146inline const std::vector<T> &EnumListOption<T>::value() const
147{
148 return _values;
149}
150} // namespace framework
151} // namespace test
152} // namespace arm_compute
153#endif /* ARM_COMPUTE_TEST_ENUMLISTOPTION */