blob: b290191e084bec561b385357f04dad55f98e984f [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"
28
29#include <initializer_list>
30#include <sstream>
31#include <stdexcept>
32#include <string>
33#include <vector>
34
35namespace arm_compute
36{
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010037namespace utils
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +010038{
39/** Implementation of an option that accepts any number of values. */
40template <typename T>
41class ListOption : public Option
42{
43public:
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;
Alex Gildayc357c472018-03-21 13:54:09 +000054 std::string help() const override;
55
56 /** Get the list of option values.
57 *
58 * @return get the list of option values.
59 */
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +010060 const std::vector<T> &value() const;
61
62private:
63 std::vector<T> _values{};
64};
65
66template <typename T>
67inline 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
72template <typename T>
73bool ListOption<T>::parse(std::string value)
74{
Moritz Pflanzer014fb632017-09-01 12:56:06 +010075 _is_set = true;
76
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +010077 try
78 {
79 std::stringstream stream{ value };
Moritz Pflanzer014fb632017-09-01 12:56:06 +010080 std::string item;
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +010081
Moritz Pflanzer014fb632017-09-01 12:56:06 +010082 while(!std::getline(stream, item, ',').fail())
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +010083 {
Moritz Pflanzer014fb632017-09-01 12:56:06 +010084 std::stringstream item_stream(item);
85 T typed_value{};
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +010086
Moritz Pflanzer014fb632017-09-01 12:56:06 +010087 item_stream >> typed_value;
88
89 if(!item_stream.fail())
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +010090 {
91 _values.emplace_back(typed_value);
92 }
93
Moritz Pflanzer014fb632017-09-01 12:56:06 +010094 _is_set = _is_set && !item_stream.fail();
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +010095 }
96
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +010097 return _is_set;
98 }
99 catch(const std::invalid_argument &)
100 {
101 return false;
102 }
103}
104
105template <typename T>
106inline std::string ListOption<T>::help() const
107{
108 return "--" + name() + "=VALUE[,VALUE[,...]] - " + _help;
109}
110
111template <typename T>
112inline const std::vector<T> &ListOption<T>::value() const
113{
114 return _values;
115}
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100116} // namespace utils
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +0100117} // namespace arm_compute
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100118#endif /* ARM_COMPUTE_UTILS_LISTOPTION */