blob: 6c4146fa7544462f55ff48c74b59c5dfd659446b [file] [log] [blame]
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * 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 */
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010024#ifndef ARM_COMPUTE_UTILS_ENUMLISTOPTION
25#define ARM_COMPUTE_UTILS_ENUMLISTOPTION
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +010026
27#include "Option.h"
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +010028#include <initializer_list>
29#include <set>
30#include <sstream>
31#include <stdexcept>
32#include <string>
33#include <vector>
34
35namespace arm_compute
36{
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010037namespace utils
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +010038{
39/** Implementation of an option that accepts any number of values from a fixed set. */
40template <typename T>
41class EnumListOption : public Option
42{
43public:
44 /** Construct option with allowed values.
45 *
46 * @param[in] name Name of the option.
47 * @param[in] allowed_values Set of allowed values for the option.
48 */
49 EnumListOption(std::string name, std::set<T> allowed_values);
50
51 /** Construct option with allowed values, a fixed number of accepted values and default values for the option.
52 *
53 * @param[in] name Name of the option.
54 * @param[in] allowed_values Set of allowed values for the option.
55 * @param[in] default_values Default values.
56 */
57 EnumListOption(std::string name, std::set<T> allowed_values, std::initializer_list<T> &&default_values);
58
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010059 bool parse(std::string value) override;
Alex Gildayc357c472018-03-21 13:54:09 +000060 std::string help() const override;
61
62 /** Get the values of the option.
63 *
64 * @return a list of the selected option values.
65 */
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +010066 const std::vector<T> &value() const;
67
68private:
69 std::vector<T> _values{};
70 std::set<T> _allowed_values{};
71};
72
73template <typename T>
74inline EnumListOption<T>::EnumListOption(std::string name, std::set<T> allowed_values)
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010075 : Option{std::move(name)}, _allowed_values{std::move(allowed_values)}
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +010076{
77}
78
79template <typename T>
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010080inline EnumListOption<T>::EnumListOption(std::string name,
81 std::set<T> allowed_values,
82 std::initializer_list<T> &&default_values)
83 : Option{std::move(name), false, true},
84 _values{std::forward<std::initializer_list<T>>(default_values)},
85 _allowed_values{std::move(allowed_values)}
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +010086{
87}
88
89template <typename T>
90bool EnumListOption<T>::parse(std::string value)
91{
92 // Remove default values
93 _values.clear();
Moritz Pflanzer014fb632017-09-01 12:56:06 +010094 _is_set = true;
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +010095
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010096 std::stringstream stream{value};
Anthony Barbier4a4a1642017-11-09 19:40:42 +000097 std::string item;
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +010098
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010099 while (!std::getline(stream, item, ',').fail())
Anthony Barbier4a4a1642017-11-09 19:40:42 +0000100 {
101 try
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +0100102 {
Moritz Pflanzer014fb632017-09-01 12:56:06 +0100103 std::stringstream item_stream(item);
104 T typed_value{};
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +0100105
Moritz Pflanzer014fb632017-09-01 12:56:06 +0100106 item_stream >> typed_value;
107
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100108 if (!item_stream.fail())
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +0100109 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100110 if (_allowed_values.count(typed_value) == 0)
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +0100111 {
Anthony Barbier4a4a1642017-11-09 19:40:42 +0000112 _is_set = false;
113 continue;
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +0100114 }
115
116 _values.emplace_back(typed_value);
117 }
118
Moritz Pflanzer014fb632017-09-01 12:56:06 +0100119 _is_set = _is_set && !item_stream.fail();
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +0100120 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100121 catch (const std::invalid_argument &)
Anthony Barbier4a4a1642017-11-09 19:40:42 +0000122 {
123 _is_set = false;
124 }
125 }
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +0100126
Anthony Barbier4a4a1642017-11-09 19:40:42 +0000127 return _is_set;
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +0100128}
129
130template <typename T>
131std::string EnumListOption<T>::help() const
132{
133 std::stringstream msg;
134 msg << "--" + name() + "={";
135
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100136 for (const auto &value : _allowed_values)
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +0100137 {
138 msg << value << ",";
139 }
140
Moritz Pflanzer014fb632017-09-01 12:56:06 +0100141 msg << "}[,{...}[,...]] - " << _help;
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +0100142
143 return msg.str();
144}
145
146template <typename T>
147inline const std::vector<T> &EnumListOption<T>::value() const
148{
149 return _values;
150}
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100151} // namespace utils
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +0100152} // namespace arm_compute
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100153#endif /* ARM_COMPUTE_UTILS_ENUMLISTOPTION */