Compute Library
 22.05
EnumOption.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_ENUMOPTION
25 #define ARM_COMPUTE_UTILS_ENUMOPTION
26 
27 #include "SimpleOption.h"
28 
29 #include <set>
30 #include <sstream>
31 #include <stdexcept>
32 #include <string>
33 
34 namespace arm_compute
35 {
36 namespace utils
37 {
38 /** Implementation of a simple option that accepts a value from a fixed set. */
39 template <typename T>
40 class EnumOption : public SimpleOption<T>
41 {
42 public:
43  /** Construct option with allowed values.
44  *
45  * @param[in] name Name of the option.
46  * @param[in] allowed_values Set of allowed values for the option.
47  */
48  EnumOption(std::string name, std::set<T> allowed_values);
49 
50  /** Construct option with allowed values, a fixed number of accepted values and default values for the option.
51  *
52  * @param[in] name Name of the option.
53  * @param[in] allowed_values Set of allowed values for the option.
54  * @param[in] default_value Default value.
55  */
56  EnumOption(std::string name, std::set<T> allowed_values, T default_value);
57 
58  bool parse(std::string value) override;
59  std::string help() const override;
60 
61  /** Get the selected value.
62  *
63  * @return get the selected enum value.
64  */
65  const T &value() const;
66 
67 private:
68  std::set<T> _allowed_values{};
69 };
70 
71 template <typename T>
72 inline EnumOption<T>::EnumOption(std::string name, std::set<T> allowed_values)
73  : SimpleOption<T>{ std::move(name) }, _allowed_values{ std::move(allowed_values) }
74 {
75 }
76 
77 template <typename T>
78 inline EnumOption<T>::EnumOption(std::string name, std::set<T> allowed_values, T default_value)
79  : SimpleOption<T>{ std::move(name), std::move(default_value) }, _allowed_values{ std::move(allowed_values) }
80 {
81 }
82 
83 template <typename T>
84 bool EnumOption<T>::parse(std::string value)
85 {
86  try
87  {
88  std::stringstream stream{ value };
89  T typed_value{};
90 
91  stream >> typed_value;
92 
93  if(!stream.fail())
94  {
95  if(_allowed_values.count(typed_value) == 0)
96  {
97  return false;
98  }
99 
100  this->_value = std::move(typed_value);
101  this->_is_set = true;
102  return true;
103  }
104 
105  return false;
106  }
107  catch(const std::invalid_argument &)
108  {
109  return false;
110  }
111 }
112 
113 template <typename T>
114 std::string EnumOption<T>::help() const
115 {
116  std::stringstream msg;
117  msg << "--" + this->name() + "={";
118 
119  for(const auto &value : _allowed_values)
120  {
121  msg << value << ",";
122  }
123 
124  msg << "} - " << this->_help;
125 
126  return msg.str();
127 }
128 
129 template <typename T>
130 inline const T &EnumOption<T>::value() const
131 {
132  return this->_value;
133 }
134 } // namespace utils
135 } // namespace arm_compute
136 #endif /* ARM_COMPUTE_UTILS_ENUMOPTION */
std::string help() const override
Help message for the option.
bool parse(std::string value) override
Parses the given string.
Copyright (c) 2017-2022 Arm Limited.
std::string name() const
Name of the option.
Definition: Option.h:115
EnumOption(std::string name, std::set< T > allowed_values)
Construct option with allowed values.
Definition: EnumOption.h:72
Implementation of a simple option that accepts a value from a fixed set.
Definition: EnumOption.h:40
const T & value() const
Get the selected value.
Implementation of an option that accepts a single value.
Definition: SimpleOption.h:39