blob: 543759259aa2140d78931011c15a5fe09d491b63 [file] [log] [blame]
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +01001/*
Alex Gildayc357c472018-03-21 13:54:09 +00002 * 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"
28
29#include <sstream>
30#include <stdexcept>
31#include <string>
32
33namespace arm_compute
34{
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010035namespace utils
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +010036{
37/** Implementation of an option that accepts a single value. */
38template <typename T>
39class SimpleOption : public Option
40{
41public:
42 using Option::Option;
43
44 /** Construct the option with the given default value.
45 *
46 * @param[in] name Name of the option.
47 * @param[in] default_value Default value.
48 */
49 SimpleOption(std::string name, T default_value);
50
Alex Gildayc357c472018-03-21 13:54:09 +000051 /** Parses the given string.
52 *
53 * @param[in] value String representation as passed on the command line.
54 *
55 * @return True if the value could be parsed by the specific subclass.
56 */
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +010057 bool parse(std::string value) override;
Alex Gildayc357c472018-03-21 13:54:09 +000058
59 /** Help message for the option.
60 *
61 * @return String representing the help message for the specific subclass.
62 */
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +010063 std::string help() const override;
Alex Gildayc357c472018-03-21 13:54:09 +000064
65 /** Get the option value.
66 *
67 * @return the option value.
68 */
69 const T &value() const;
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +010070
71protected:
72 T _value{};
73};
74
75template <typename T>
76inline SimpleOption<T>::SimpleOption(std::string name, T default_value)
77 : Option{ std::move(name), false, true }, _value{ std::move(default_value) }
78{
79}
80
81template <typename T>
82bool SimpleOption<T>::parse(std::string value)
83{
84 try
85 {
86 std::stringstream stream{ std::move(value) };
87 stream >> _value;
88 _is_set = !stream.fail();
89 return _is_set;
90 }
91 catch(const std::invalid_argument &)
92 {
93 return false;
94 }
95}
96
97template <>
98inline bool SimpleOption<std::string>::parse(std::string value)
99{
100 _value = std::move(value);
101 _is_set = true;
102 return true;
103}
104
105template <typename T>
106inline std::string SimpleOption<T>::help() const
107{
108 return "--" + name() + "=VALUE - " + _help;
109}
110
111template <typename T>
112inline const T &SimpleOption<T>::value() const
113{
114 return _value;
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_SIMPLEOPTION */