Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
| 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 | |
| 33 | namespace arm_compute |
| 34 | { |
| 35 | namespace test |
| 36 | { |
| 37 | class ProgramOptions; |
| 38 | |
| 39 | /** Container providing easy access to runtime options provided by the user. */ |
| 40 | struct UserConfiguration |
| 41 | { |
| 42 | protected: |
| 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 | |
| 74 | public: |
| 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 | |
| 88 | template <typename T> |
| 89 | UserConfiguration::Option<T>::Option() |
| 90 | : _value{}, _is_set{ false } |
| 91 | { |
| 92 | } |
| 93 | |
| 94 | template <typename T> |
| 95 | UserConfiguration::Option<T>::Option(const T &value) |
| 96 | : _value{ value }, _is_set{ true } |
| 97 | { |
| 98 | } |
| 99 | |
| 100 | template <typename T> |
| 101 | UserConfiguration::Option<T> &UserConfiguration::Option<T>::operator=(const T &value) |
| 102 | { |
| 103 | _value = value; |
| 104 | _is_set = true; |
| 105 | |
| 106 | return *this; |
| 107 | } |
| 108 | |
| 109 | template <typename T> |
| 110 | constexpr bool UserConfiguration::Option<T>::is_set() const |
| 111 | { |
| 112 | return _is_set; |
| 113 | } |
| 114 | |
| 115 | template <typename T> |
| 116 | T UserConfiguration::Option<T>::get() const |
| 117 | { |
| 118 | ARM_COMPUTE_ERROR_ON(!is_set()); |
| 119 | return _value; |
| 120 | } |
| 121 | |
| 122 | template <typename T> |
| 123 | T &UserConfiguration::Option<T>::get() |
| 124 | { |
| 125 | return _value; |
| 126 | } |
| 127 | |
| 128 | template <typename T> |
| 129 | UserConfiguration::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 Barbier | ac69aa1 | 2017-07-03 17:39:37 +0100 | [diff] [blame] | 136 | #endif /* __ARM_COMPUTE_TEST_USER_CONFIGURATION_H__ */ |