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
29 #include <sstream>
30 #include <stdexcept>
31 #include <string>
32
33 namespace arm_compute
34 {
35 namespace utils
36 {
37 /** Implementation of an option that accepts a single value. */
38 template <typename T>
39 class SimpleOption : public Option
40 {
41 public:
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
51 /** 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 */
57 bool parse(std::string value) override;
58
59 /** Help message for the option.
60 *
61 * @return String representing the help message for the specific subclass.
62 */
63 std::string help() const override;
64
65 /** Get the option value.
66 *
67 * @return the option value.
68 */
69 const T &value() const;
70
71 protected:
72 T _value{};
73 };
74
75 template <typename T>
SimpleOption(std::string name,T default_value)76 inline SimpleOption<T>::SimpleOption(std::string name, T default_value)
77 : Option{ std::move(name), false, true }, _value{ std::move(default_value) }
78 {
79 }
80
81 template <typename T>
parse(std::string value)82 bool 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
97 template <>
parse(std::string value)98 inline bool SimpleOption<std::string>::parse(std::string value)
99 {
100 _value = std::move(value);
101 _is_set = true;
102 return true;
103 }
104
105 template <typename T>
help()106 inline std::string SimpleOption<T>::help() const
107 {
108 return "--" + name() + "=VALUE - " + _help;
109 }
110
111 template <typename T>
value()112 inline const T &SimpleOption<T>::value() const
113 {
114 return _value;
115 }
116 } // namespace utils
117 } // namespace arm_compute
118 #endif /* ARM_COMPUTE_UTILS_SIMPLEOPTION */
119