blob: f4ee2835286dec5f72330ab75b266ee3a8a5aa4f [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"
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{
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010038namespace utils
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +010039{
40/** Implementation of an option that accepts any number of values from a fixed set. */
41template <typename T>
42class EnumListOption : public Option
43{
44public:
45 /** Construct option with allowed values.
46 *
47 * @param[in] name Name of the option.
48 * @param[in] allowed_values Set of allowed values for the option.
49 */
50 EnumListOption(std::string name, std::set<T> allowed_values);
51
52 /** Construct option with allowed values, a fixed number of accepted values and default values for the option.
53 *
54 * @param[in] name Name of the option.
55 * @param[in] allowed_values Set of allowed values for the option.
56 * @param[in] default_values Default values.
57 */
58 EnumListOption(std::string name, std::set<T> allowed_values, std::initializer_list<T> &&default_values);
59
60 bool parse(std::string value) override;
Alex Gildayc357c472018-03-21 13:54:09 +000061 std::string help() const override;
62
63 /** Get the values of the option.
64 *
65 * @return a list of the selected option values.
66 */
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +010067 const std::vector<T> &value() const;
68
69private:
70 std::vector<T> _values{};
71 std::set<T> _allowed_values{};
72};
73
74template <typename T>
75inline EnumListOption<T>::EnumListOption(std::string name, std::set<T> allowed_values)
76 : Option{ std::move(name) }, _allowed_values{ std::move(allowed_values) }
77{
78}
79
80template <typename T>
81inline EnumListOption<T>::EnumListOption(std::string name, std::set<T> allowed_values, std::initializer_list<T> &&default_values)
82 : Option{ std::move(name), false, true }, _values{ std::forward<std::initializer_list<T>>(default_values) }, _allowed_values{ std::move(allowed_values) }
83{
84}
85
86template <typename T>
87bool EnumListOption<T>::parse(std::string value)
88{
89 // Remove default values
90 _values.clear();
Moritz Pflanzer014fb632017-09-01 12:56:06 +010091 _is_set = true;
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +010092
Anthony Barbier4a4a1642017-11-09 19:40:42 +000093 std::stringstream stream{ value };
94 std::string item;
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +010095
Anthony Barbier4a4a1642017-11-09 19:40:42 +000096 while(!std::getline(stream, item, ',').fail())
97 {
98 try
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +010099 {
Moritz Pflanzer014fb632017-09-01 12:56:06 +0100100 std::stringstream item_stream(item);
101 T typed_value{};
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +0100102
Moritz Pflanzer014fb632017-09-01 12:56:06 +0100103 item_stream >> typed_value;
104
105 if(!item_stream.fail())
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +0100106 {
107 if(_allowed_values.count(typed_value) == 0)
108 {
Anthony Barbier4a4a1642017-11-09 19:40:42 +0000109 _is_set = false;
110 continue;
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +0100111 }
112
113 _values.emplace_back(typed_value);
114 }
115
Moritz Pflanzer014fb632017-09-01 12:56:06 +0100116 _is_set = _is_set && !item_stream.fail();
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +0100117 }
Anthony Barbier4a4a1642017-11-09 19:40:42 +0000118 catch(const std::invalid_argument &)
119 {
120 _is_set = false;
121 }
122 }
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +0100123
Anthony Barbier4a4a1642017-11-09 19:40:42 +0000124 return _is_set;
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +0100125}
126
127template <typename T>
128std::string EnumListOption<T>::help() const
129{
130 std::stringstream msg;
131 msg << "--" + name() + "={";
132
133 for(const auto &value : _allowed_values)
134 {
135 msg << value << ",";
136 }
137
Moritz Pflanzer014fb632017-09-01 12:56:06 +0100138 msg << "}[,{...}[,...]] - " << _help;
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +0100139
140 return msg.str();
141}
142
143template <typename T>
144inline const std::vector<T> &EnumListOption<T>::value() const
145{
146 return _values;
147}
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100148} // namespace utils
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +0100149} // namespace arm_compute
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100150#endif /* ARM_COMPUTE_UTILS_ENUMLISTOPTION */