blob: e6e80458400720871548cc5e932af104ea532636 [file] [log] [blame]
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +01001/*
2 * Copyright (c) 2017 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_TEST_SIMPLEOPTION
25#define ARM_COMPUTE_TEST_SIMPLEOPTION
26
27#include "Option.h"
28
29#include <sstream>
30#include <stdexcept>
31#include <string>
32
33namespace arm_compute
34{
35namespace test
36{
37namespace framework
38{
39/** Implementation of an option that accepts a single value. */
40template <typename T>
41class SimpleOption : public Option
42{
43public:
44 using Option::Option;
45
46 /** Construct the option with the given default value.
47 *
48 * @param[in] name Name of the option.
49 * @param[in] default_value Default value.
50 */
51 SimpleOption(std::string name, T default_value);
52
53 bool parse(std::string value) override;
54 std::string help() const override;
55 const T &value() const;
56
57protected:
58 T _value{};
59};
60
61template <typename T>
62inline SimpleOption<T>::SimpleOption(std::string name, T default_value)
63 : Option{ std::move(name), false, true }, _value{ std::move(default_value) }
64{
65}
66
67template <typename T>
68bool SimpleOption<T>::parse(std::string value)
69{
70 try
71 {
72 std::stringstream stream{ std::move(value) };
73 stream >> _value;
74 _is_set = !stream.fail();
75 return _is_set;
76 }
77 catch(const std::invalid_argument &)
78 {
79 return false;
80 }
81}
82
83template <>
84inline bool SimpleOption<std::string>::parse(std::string value)
85{
86 _value = std::move(value);
87 _is_set = true;
88 return true;
89}
90
91template <typename T>
92inline std::string SimpleOption<T>::help() const
93{
94 return "--" + name() + "=VALUE - " + _help;
95}
96
97template <typename T>
98inline const T &SimpleOption<T>::value() const
99{
100 return _value;
101}
102} // namespace framework
103} // namespace test
104} // namespace arm_compute
105#endif /* ARM_COMPUTE_TEST_SIMPLEOPTION */