blob: 14d61859ae3ab55d6eb086c78641f4a95547a80f [file] [log] [blame]
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +01001/*
Alex Gildayc357c472018-03-21 13:54:09 +00002 * 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 */
24#ifndef ARM_COMPUTE_TEST_ENUMOPTION
25#define ARM_COMPUTE_TEST_ENUMOPTION
26
27#include "SimpleOption.h"
28
29#include <set>
30#include <sstream>
31#include <stdexcept>
32#include <string>
33
34namespace arm_compute
35{
36namespace test
37{
38namespace framework
39{
40/** Implementation of a simple option that accepts a value from a fixed set. */
41template <typename T>
42class EnumOption : public SimpleOption<T>
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 EnumOption(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_value Default value.
57 */
58 EnumOption(std::string name, std::set<T> allowed_values, T default_value);
59
60 bool parse(std::string value) override;
61 std::string help() const override;
Alex Gildayc357c472018-03-21 13:54:09 +000062
63 /** Get the selected value.
64 *
65 * @return get the selected enum value.
66 */
67 const T &value() const;
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +010068
69private:
70 std::set<T> _allowed_values{};
71};
72
73template <typename T>
74inline EnumOption<T>::EnumOption(std::string name, std::set<T> allowed_values)
75 : SimpleOption<T>{ std::move(name) }, _allowed_values{ std::move(allowed_values) }
76{
77}
78
79template <typename T>
80inline EnumOption<T>::EnumOption(std::string name, std::set<T> allowed_values, T default_value)
81 : SimpleOption<T>{ std::move(name), std::move(default_value) }, _allowed_values{ std::move(allowed_values) }
82{
83}
84
85template <typename T>
86bool EnumOption<T>::parse(std::string value)
87{
88 try
89 {
90 std::stringstream stream{ value };
91 T typed_value{};
92
93 stream >> typed_value;
94
95 if(!stream.fail())
96 {
97 if(_allowed_values.count(typed_value) == 0)
98 {
99 return false;
100 }
101
102 this->_value = std::move(typed_value);
103 this->_is_set = true;
104 return true;
105 }
106
107 return false;
108 }
109 catch(const std::invalid_argument &)
110 {
111 return false;
112 }
113}
114
115template <typename T>
116std::string EnumOption<T>::help() const
117{
118 std::stringstream msg;
119 msg << "--" + this->name() + "={";
120
121 for(const auto &value : _allowed_values)
122 {
123 msg << value << ",";
124 }
125
126 msg << "} - " << this->_help;
127
128 return msg.str();
129}
130
131template <typename T>
132inline const T &EnumOption<T>::value() const
133{
134 return this->_value;
135}
136} // namespace framework
137} // namespace test
138} // namespace arm_compute
139#endif /* ARM_COMPUTE_TEST_ENUMOPTION */