blob: f6329c1790a8dee60435141f9116b5321fc321e8 [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_SIMPLEOPTION
25#define ARM_COMPUTE_UTILS_SIMPLEOPTION
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +010026
27#include "Option.h"
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +010028#include <sstream>
29#include <stdexcept>
30#include <string>
31
32namespace arm_compute
33{
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010034namespace utils
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +010035{
36/** Implementation of an option that accepts a single value. */
37template <typename T>
38class SimpleOption : public Option
39{
40public:
41 using Option::Option;
42
43 /** Construct the option with the given default value.
44 *
45 * @param[in] name Name of the option.
46 * @param[in] default_value Default value.
47 */
48 SimpleOption(std::string name, T default_value);
49
Alex Gildayc357c472018-03-21 13:54:09 +000050 /** Parses the given string.
51 *
52 * @param[in] value String representation as passed on the command line.
53 *
54 * @return True if the value could be parsed by the specific subclass.
55 */
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +010056 bool parse(std::string value) override;
Alex Gildayc357c472018-03-21 13:54:09 +000057
58 /** Help message for the option.
59 *
60 * @return String representing the help message for the specific subclass.
61 */
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +010062 std::string help() const override;
Alex Gildayc357c472018-03-21 13:54:09 +000063
64 /** Get the option value.
65 *
66 * @return the option value.
67 */
68 const T &value() const;
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +010069
70protected:
71 T _value{};
72};
73
74template <typename T>
75inline SimpleOption<T>::SimpleOption(std::string name, T default_value)
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010076 : Option{std::move(name), false, true}, _value{std::move(default_value)}
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +010077{
78}
79
80template <typename T>
81bool SimpleOption<T>::parse(std::string value)
82{
83 try
84 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010085 std::stringstream stream{std::move(value)};
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +010086 stream >> _value;
87 _is_set = !stream.fail();
88 return _is_set;
89 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010090 catch (const std::invalid_argument &)
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +010091 {
92 return false;
93 }
94}
95
96template <>
97inline bool SimpleOption<std::string>::parse(std::string value)
98{
99 _value = std::move(value);
100 _is_set = true;
101 return true;
102}
103
104template <typename T>
105inline std::string SimpleOption<T>::help() const
106{
107 return "--" + name() + "=VALUE - " + _help;
108}
109
110template <typename T>
111inline const T &SimpleOption<T>::value() const
112{
113 return _value;
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_SIMPLEOPTION */