blob: eb43b6c54e4c235f8e5dfba03b223151bc28a2cf [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"
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +010028#include <set>
29#include <sstream>
30#include <stdexcept>
31#include <string>
32
33namespace arm_compute
34{
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010035namespace utils
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +010036{
37/** Implementation of a simple option that accepts a value from a fixed set. */
38template <typename T>
39class EnumOption : public SimpleOption<T>
40{
41public:
42 /** Construct option with allowed values.
43 *
44 * @param[in] name Name of the option.
45 * @param[in] allowed_values Set of allowed values for the option.
46 */
47 EnumOption(std::string name, std::set<T> allowed_values);
48
49 /** Construct option with allowed values, a fixed number of accepted values and default values for the option.
50 *
51 * @param[in] name Name of the option.
52 * @param[in] allowed_values Set of allowed values for the option.
53 * @param[in] default_value Default value.
54 */
55 EnumOption(std::string name, std::set<T> allowed_values, T default_value);
56
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010057 bool parse(std::string value) override;
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +010058 std::string help() const override;
Alex Gildayc357c472018-03-21 13:54:09 +000059
60 /** Get the selected value.
61 *
62 * @return get the selected enum value.
63 */
64 const T &value() const;
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +010065
66private:
67 std::set<T> _allowed_values{};
68};
69
70template <typename T>
71inline EnumOption<T>::EnumOption(std::string name, std::set<T> allowed_values)
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010072 : SimpleOption<T>{std::move(name)}, _allowed_values{std::move(allowed_values)}
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +010073{
74}
75
76template <typename T>
77inline EnumOption<T>::EnumOption(std::string name, std::set<T> allowed_values, T default_value)
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010078 : SimpleOption<T>{std::move(name), std::move(default_value)}, _allowed_values{std::move(allowed_values)}
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +010079{
80}
81
82template <typename T>
83bool EnumOption<T>::parse(std::string value)
84{
85 try
86 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010087 std::stringstream stream{value};
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +010088 T typed_value{};
89
90 stream >> typed_value;
91
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010092 if (!stream.fail())
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +010093 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010094 if (_allowed_values.count(typed_value) == 0)
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +010095 {
96 return false;
97 }
98
99 this->_value = std::move(typed_value);
100 this->_is_set = true;
101 return true;
102 }
103
104 return false;
105 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100106 catch (const std::invalid_argument &)
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +0100107 {
108 return false;
109 }
110}
111
112template <typename T>
113std::string EnumOption<T>::help() const
114{
115 std::stringstream msg;
116 msg << "--" + this->name() + "={";
117
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100118 for (const auto &value : _allowed_values)
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +0100119 {
120 msg << value << ",";
121 }
122
123 msg << "} - " << this->_help;
124
125 return msg.str();
126}
127
128template <typename T>
129inline const T &EnumOption<T>::value() const
130{
131 return this->_value;
132}
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100133} // namespace utils
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +0100134} // namespace arm_compute
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100135#endif /* ARM_COMPUTE_UTILS_ENUMOPTION */