Debian Codegraph @

utils/command_line/ListOption.h

raw
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_LISTOPTION
25 #define ARM_COMPUTE_UTILS_LISTOPTION
26
27 #include "Option.h"
28 #include <initializer_list>
29 #include <sstream>
30 #include <stdexcept>
31 #include <string>
32 #include <vector>
33
34 namespace arm_compute
35 {
36 namespace utils
37 {
38 /** Implementation of an option that accepts any number of values. */
39 template <typename T>
40 class ListOption : public Option
41 {
42 public:
43 using Option::Option;
44
45 /** Construct the option with the given default values.
46 *
47 * @param[in] name Name of the option.
48 * @param[in] default_values Default values.
49 */
50 ListOption(std::string name, std::initializer_list<T> &&default_values);
51
52 bool parse(std::string value) override;
53 std::string help() const override;
54
55 /** Get the list of option values.
56 *
57 * @return get the list of option values.
58 */
59 const std::vector<T> &value() const;
60
61 private:
62 std::vector<T> _values{};
63 };
64
65 template <typename T>
66 inline ListOption<T>::ListOption(std::string name, std::initializer_list<T> &&default_values)
67 : Option{std::move(name), false, true}, _values{std::forward<std::initializer_list<T>>(default_values)}
68 {
69 }
70
71 template <typename T>
72 bool ListOption<T>::parse(std::string value)
73 {
74 _is_set = true;
75
76 try
77 {
78 std::stringstream stream{value};
79 std::string item;
80
81 while (!std::getline(stream, item, ',').fail())
82 {
83 std::stringstream item_stream(item);
84 T typed_value{};
85
86 item_stream >> typed_value;
87
88 if (!item_stream.fail())
89 {
90 _values.emplace_back(typed_value);
91 }
92
93 _is_set = _is_set && !item_stream.fail();
94 }
95
96 return _is_set;
97 }
98 catch (const std::invalid_argument &)
99 {
100 return false;
101 }
102 }
103
104 template <typename T>
105 inline std::string ListOption<T>::help() const
106 {
107 return "--" + name() + "=VALUE[,VALUE[,...]] - " + _help;
108 }
109
110 template <typename T>
111 inline const std::vector<T> &ListOption<T>::value() const
112 {
113 return _values;
114 }
115 } // namespace utils
116 } // namespace arm_compute
117 #endif /* ARM_COMPUTE_UTILS_LISTOPTION */