Compute Library
 23.11
EnumListOption.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-2018 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_UTILS_ENUMLISTOPTION
25 #define ARM_COMPUTE_UTILS_ENUMLISTOPTION
26 
27 #include "Option.h"
28 #include <initializer_list>
29 #include <set>
30 #include <sstream>
31 #include <stdexcept>
32 #include <string>
33 #include <vector>
34 
35 namespace arm_compute
36 {
37 namespace utils
38 {
39 /** Implementation of an option that accepts any number of values from a fixed set. */
40 template <typename T>
41 class EnumListOption : public Option
42 {
43 public:
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 
59  bool parse(std::string value) override;
60  std::string help() const override;
61 
62  /** Get the values of the option.
63  *
64  * @return a list of the selected option values.
65  */
66  const std::vector<T> &value() const;
67 
68 private:
69  std::vector<T> _values{};
70  std::set<T> _allowed_values{};
71 };
72 
73 template <typename T>
74 inline EnumListOption<T>::EnumListOption(std::string name, std::set<T> allowed_values)
75  : Option{std::move(name)}, _allowed_values{std::move(allowed_values)}
76 {
77 }
78 
79 template <typename T>
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)}
86 {
87 }
88 
89 template <typename T>
90 bool EnumListOption<T>::parse(std::string value)
91 {
92  // Remove default values
93  _values.clear();
94  _is_set = true;
95 
96  std::stringstream stream{value};
97  std::string item;
98 
99  while (!std::getline(stream, item, ',').fail())
100  {
101  try
102  {
103  std::stringstream item_stream(item);
104  T typed_value{};
105 
106  item_stream >> typed_value;
107 
108  if (!item_stream.fail())
109  {
110  if (_allowed_values.count(typed_value) == 0)
111  {
112  _is_set = false;
113  continue;
114  }
115 
116  _values.emplace_back(typed_value);
117  }
118 
119  _is_set = _is_set && !item_stream.fail();
120  }
121  catch (const std::invalid_argument &)
122  {
123  _is_set = false;
124  }
125  }
126 
127  return _is_set;
128 }
129 
130 template <typename T>
131 std::string EnumListOption<T>::help() const
132 {
133  std::stringstream msg;
134  msg << "--" + name() + "={";
135 
136  for (const auto &value : _allowed_values)
137  {
138  msg << value << ",";
139  }
140 
141  msg << "}[,{...}[,...]] - " << _help;
142 
143  return msg.str();
144 }
145 
146 template <typename T>
147 inline const std::vector<T> &EnumListOption<T>::value() const
148 {
149  return _values;
150 }
151 } // namespace utils
152 } // namespace arm_compute
153 #endif /* ARM_COMPUTE_UTILS_ENUMLISTOPTION */
Option.h
arm_compute::utils::EnumListOption::EnumListOption
EnumListOption(std::string name, std::set< T > allowed_values)
Construct option with allowed values.
Definition: EnumListOption.h:74
arm_compute::utils::EnumListOption::value
const std::vector< T > & value() const
Get the values of the option.
Definition: EnumListOption.h:147
arm_compute::utils::EnumListOption::parse
bool parse(std::string value) override
Parses the given string.
Definition: EnumListOption.h:90
name
const char * name
Definition: NEBatchNormalizationLayerKernel.cpp:66
arm_compute::utils::Option::name
std::string name() const
Name of the option.
Definition: Option.h:114
arm_compute::utils::EnumListOption
Implementation of an option that accepts any number of values from a fixed set.
Definition: EnumListOption.h:41
arm_compute
Copyright (c) 2017-2023 Arm Limited.
Definition: introduction.dox:24
arm_compute::utils::EnumListOption::help
std::string help() const override
Help message for the option.
Definition: EnumListOption.h:131
arm_compute::utils::Option
Abstract base class for a command line option.
Definition: Option.h:34
GemmTuner.help
help
Definition: GemmTuner.py:648