blob: 6bcfe5f14e50364ce5214828a756394646fd73e5 [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_ENUMOPTION
25#define ARM_COMPUTE_UTILS_ENUMOPTION
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +010026
27#include "SimpleOption.h"
28
29#include <set>
30#include <sstream>
31#include <stdexcept>
32#include <string>
33
34namespace arm_compute
35{
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010036namespace utils
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +010037{
38/** Implementation of a simple option that accepts a value from a fixed set. */
39template <typename T>
40class EnumOption : public SimpleOption<T>
41{
42public:
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;
Alex Gildayc357c472018-03-21 13:54:09 +000060
61 /** Get the selected value.
62 *
63 * @return get the selected enum value.
64 */
65 const T &value() const;
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +010066
67private:
68 std::set<T> _allowed_values{};
69};
70
71template <typename T>
72inline 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
77template <typename T>
78inline 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
83template <typename T>
84bool 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
113template <typename T>
114std::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
129template <typename T>
130inline const T &EnumOption<T>::value() const
131{
132 return this->_value;
133}
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100134} // namespace utils
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +0100135} // namespace arm_compute
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100136#endif /* ARM_COMPUTE_UTILS_ENUMOPTION */