blob: 1abba77b4b44271c91f2f24fbcec4a2ece6a4819 [file] [log] [blame]
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +01001/*
2 * Copyright (c) 2017 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_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;
62 const T &value() const;
63
64private:
65 std::set<T> _allowed_values{};
66};
67
68template <typename T>
69inline EnumOption<T>::EnumOption(std::string name, std::set<T> allowed_values)
70 : SimpleOption<T>{ std::move(name) }, _allowed_values{ std::move(allowed_values) }
71{
72}
73
74template <typename T>
75inline EnumOption<T>::EnumOption(std::string name, std::set<T> allowed_values, T default_value)
76 : SimpleOption<T>{ std::move(name), std::move(default_value) }, _allowed_values{ std::move(allowed_values) }
77{
78}
79
80template <typename T>
81bool EnumOption<T>::parse(std::string value)
82{
83 try
84 {
85 std::stringstream stream{ value };
86 T typed_value{};
87
88 stream >> typed_value;
89
90 if(!stream.fail())
91 {
92 if(_allowed_values.count(typed_value) == 0)
93 {
94 return false;
95 }
96
97 this->_value = std::move(typed_value);
98 this->_is_set = true;
99 return true;
100 }
101
102 return false;
103 }
104 catch(const std::invalid_argument &)
105 {
106 return false;
107 }
108}
109
110template <typename T>
111std::string EnumOption<T>::help() const
112{
113 std::stringstream msg;
114 msg << "--" + this->name() + "={";
115
116 for(const auto &value : _allowed_values)
117 {
118 msg << value << ",";
119 }
120
121 msg << "} - " << this->_help;
122
123 return msg.str();
124}
125
126template <typename T>
127inline const T &EnumOption<T>::value() const
128{
129 return this->_value;
130}
131} // namespace framework
132} // namespace test
133} // namespace arm_compute
134#endif /* ARM_COMPUTE_TEST_ENUMOPTION */