blob: f318e1646a3e9afd281ea2e29cff105ec83f5519 [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_LISTOPTION
25#define ARM_COMPUTE_UTILS_LISTOPTION
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +010026
27#include "Option.h"
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +010028#include <initializer_list>
29#include <sstream>
30#include <stdexcept>
31#include <string>
32#include <vector>
33
34namespace arm_compute
35{
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010036namespace utils
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +010037{
38/** Implementation of an option that accepts any number of values. */
39template <typename T>
40class ListOption : public Option
41{
42public:
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
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010052 bool parse(std::string value) override;
Alex Gildayc357c472018-03-21 13:54:09 +000053 std::string help() const override;
54
55 /** Get the list of option values.
56 *
57 * @return get the list of option values.
58 */
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +010059 const std::vector<T> &value() const;
60
61private:
62 std::vector<T> _values{};
63};
64
65template <typename T>
66inline ListOption<T>::ListOption(std::string name, std::initializer_list<T> &&default_values)
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010067 : Option{std::move(name), false, true}, _values{std::forward<std::initializer_list<T>>(default_values)}
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +010068{
69}
70
71template <typename T>
72bool ListOption<T>::parse(std::string value)
73{
Moritz Pflanzer014fb632017-09-01 12:56:06 +010074 _is_set = true;
75
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +010076 try
77 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010078 std::stringstream stream{value};
Moritz Pflanzer014fb632017-09-01 12:56:06 +010079 std::string item;
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +010080
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010081 while (!std::getline(stream, item, ',').fail())
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +010082 {
Moritz Pflanzer014fb632017-09-01 12:56:06 +010083 std::stringstream item_stream(item);
84 T typed_value{};
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +010085
Moritz Pflanzer014fb632017-09-01 12:56:06 +010086 item_stream >> typed_value;
87
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010088 if (!item_stream.fail())
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +010089 {
90 _values.emplace_back(typed_value);
91 }
92
Moritz Pflanzer014fb632017-09-01 12:56:06 +010093 _is_set = _is_set && !item_stream.fail();
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +010094 }
95
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +010096 return _is_set;
97 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010098 catch (const std::invalid_argument &)
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +010099 {
100 return false;
101 }
102}
103
104template <typename T>
105inline std::string ListOption<T>::help() const
106{
107 return "--" + name() + "=VALUE[,VALUE[,...]] - " + _help;
108}
109
110template <typename T>
111inline const std::vector<T> &ListOption<T>::value() const
112{
113 return _values;
114}
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100115} // namespace utils
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +0100116} // namespace arm_compute
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100117#endif /* ARM_COMPUTE_UTILS_LISTOPTION */