blob: 815da04810254b07347560f75bce4a73cc86b150 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +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_USER_CONFIGURATION_H__
25#define __ARM_COMPUTE_TEST_USER_CONFIGURATION_H__
26
27#include "arm_compute/core/Error.h"
28#include "arm_compute/core/Types.h"
29
30#include <random>
31#include <string>
32
33namespace arm_compute
34{
35namespace test
36{
37class ProgramOptions;
38
39/** Container providing easy access to runtime options provided by the user. */
40struct UserConfiguration
41{
42protected:
43 /** Wrapper around options to store if an option has been set. */
44 template <typename T>
45 class Option
46 {
47 public:
48 /** Initialise the option to its default (C++) value and mark it as 'not set'. */
49 Option();
50
51 /** Initialise the option to the given @p value and mark it as 'set'. */
52 Option(const T &value);
53
54 /** Assign the given @p value and mark it as 'set'. */
55 Option<T> &operator=(const T &value);
56
57 /** Query if the option has been set. */
58 constexpr bool is_set() const;
59
60 /** Return the underlying value as constant. */
61 T get() const;
62
63 /** Return the underlying value. */
64 T &get();
65
66 /** Implicitly return the underlying value. */
67 operator T() const;
68
69 private:
70 T _value;
71 bool _is_set;
72 };
73
74public:
75 UserConfiguration() = default;
76
77 /** Initialise the configuration according to the program options.
78 *
79 * @param[in] options Parsed command line options.
80 */
81 UserConfiguration(const ProgramOptions &options);
82
83 Option<std::string> path{};
84 Option<std::random_device::result_type> seed{};
85 Option<unsigned int> threads{};
86};
87
88template <typename T>
89UserConfiguration::Option<T>::Option()
90 : _value{}, _is_set{ false }
91{
92}
93
94template <typename T>
95UserConfiguration::Option<T>::Option(const T &value)
96 : _value{ value }, _is_set{ true }
97{
98}
99
100template <typename T>
101UserConfiguration::Option<T> &UserConfiguration::Option<T>::operator=(const T &value)
102{
103 _value = value;
104 _is_set = true;
105
106 return *this;
107}
108
109template <typename T>
110constexpr bool UserConfiguration::Option<T>::is_set() const
111{
112 return _is_set;
113}
114
115template <typename T>
116T UserConfiguration::Option<T>::get() const
117{
118 ARM_COMPUTE_ERROR_ON(!is_set());
119 return _value;
120}
121
122template <typename T>
123T &UserConfiguration::Option<T>::get()
124{
125 return _value;
126}
127
128template <typename T>
129UserConfiguration::Option<T>::operator T() const
130{
131 ARM_COMPUTE_ERROR_ON(!is_set());
132 return _value;
133}
134} // namespace test
135} // namespace arm_compute
Anthony Barbierac69aa12017-07-03 17:39:37 +0100136#endif /* __ARM_COMPUTE_TEST_USER_CONFIGURATION_H__ */