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