Debian Codegraph @

utils/command_line/SimpleOption.h

raw
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_SIMPLEOPTION
25 #define ARM_COMPUTE_UTILS_SIMPLEOPTION
26
27 #include "Option.h"
28 #include <sstream>
29 #include <stdexcept>
30 #include <string>
31
32 namespace arm_compute
33 {
34 namespace utils
35 {
36 /** Implementation of an option that accepts a single value. */
37 template <typename T>
38 class SimpleOption : public Option
39 {
40 public:
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
50 /** 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 */
56 bool parse(std::string value) override;
57
58 /** Help message for the option.
59 *
60 * @return String representing the help message for the specific subclass.
61 */
62 std::string help() const override;
63
64 /** Get the option value.
65 *
66 * @return the option value.
67 */
68 const T &value() const;
69
70 protected:
71 T _value{};
72 };
73
74 template <typename T>
75 inline SimpleOption<T>::SimpleOption(std::string name, T default_value)
76 : Option{std::move(name), false, true}, _value{std::move(default_value)}
77 {
78 }
79
80 template <typename T>
81 bool SimpleOption<T>::parse(std::string value)
82 {
83 try
84 {
85 std::stringstream stream{std::move(value)};
86 stream >> _value;
87 _is_set = !stream.fail();
88 return _is_set;
89 }
90 catch (const std::invalid_argument &)
91 {
92 return false;
93 }
94 }
95
96 template <>
97 inline bool SimpleOption<std::string>::parse(std::string value)
98 {
99 _value = std::move(value);
100 _is_set = true;
101 return true;
102 }
103
104 template <typename T>
105 inline std::string SimpleOption<T>::help() const
106 {
107 return "--" + name() + "=VALUE - " + _help;
108 }
109
110 template <typename T>
111 inline const T &SimpleOption<T>::value() const
112 {
113 return _value;
114 }
115 } // namespace utils
116 } // namespace arm_compute
117 #endif /* ARM_COMPUTE_UTILS_SIMPLEOPTION */