| 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_ENUMOPTION |
| 25 |
#define ARM_COMPUTE_UTILS_ENUMOPTION |
| 26 |
|
| 27 |
#include "SimpleOption.h" |
| 28 |
#include <set> |
| 29 |
#include <sstream> |
| 30 |
#include <stdexcept> |
| 31 |
#include <string> |
| 32 |
|
| 33 |
namespace arm_compute |
| 34 |
{ |
| 35 |
namespace utils |
| 36 |
{ |
| 37 |
/** Implementation of a simple option that accepts a value from a fixed set. */ |
| 38 |
template <typename T> |
| 39 |
class EnumOption : public SimpleOption<T> |
| 40 |
{ |
| 41 |
public: |
| 42 |
/** Construct option with allowed values. |
| 43 |
* |
| 44 |
* @param[in] name Name of the option. |
| 45 |
* @param[in] allowed_values Set of allowed values for the option. |
| 46 |
*/ |
| 47 |
EnumOption(std::string name, std::set<T> allowed_values); |
| 48 |
|
| 49 |
/** Construct option with allowed values, a fixed number of accepted values and default values for the option. |
| 50 |
* |
| 51 |
* @param[in] name Name of the option. |
| 52 |
* @param[in] allowed_values Set of allowed values for the option. |
| 53 |
* @param[in] default_value Default value. |
| 54 |
*/ |
| 55 |
EnumOption(std::string name, std::set<T> allowed_values, T default_value); |
| 56 |
|
| 57 |
bool parse(std::string value) override; |
| 58 |
std::string help() const override; |
| 59 |
|
| 60 |
/** Get the selected value. |
| 61 |
* |
| 62 |
* @return get the selected enum value. |
| 63 |
*/ |
| 64 |
const T &value() const; |
| 65 |
|
| 66 |
private: |
| 67 |
std::set<T> _allowed_values{}; |
| 68 |
}; |
| 69 |
|
| 70 |
template <typename T> |
| 71 |
inline EnumOption<T>::EnumOption(std::string name, std::set<T> allowed_values) |
| 72 |
: SimpleOption<T>{std::move(name)}, _allowed_values{std::move(allowed_values)} |
| 73 |
{ |
| 74 |
} |
| 75 |
|
| 76 |
template <typename T> |
| 77 |
inline EnumOption<T>::EnumOption(std::string name, std::set<T> allowed_values, T default_value) |
| 78 |
: SimpleOption<T>{std::move(name), std::move(default_value)}, _allowed_values{std::move(allowed_values)} |
| 79 |
{ |
| 80 |
} |
| 81 |
|
| 82 |
template <typename T> |
| 83 |
bool EnumOption<T>::parse(std::string value) |
| 84 |
{ |
| 85 |
try |
| 86 |
{ |
| 87 |
std::stringstream stream{value}; |
| 88 |
T typed_value{}; |
| 89 |
|
| 90 |
stream >> typed_value; |
| 91 |
|
| 92 |
if (!stream.fail()) |
| 93 |
{ |
| 94 |
if (_allowed_values.count(typed_value) == 0) |
| 95 |
{ |
| 96 |
return false; |
| 97 |
} |
| 98 |
|
| 99 |
this->_value = std::move(typed_value); |
| 100 |
this->_is_set = true; |
| 101 |
return true; |
| 102 |
} |
| 103 |
|
| 104 |
return false; |
| 105 |
} |
| 106 |
catch (const std::invalid_argument &) |
| 107 |
{ |
| 108 |
return false; |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
template <typename T> |
| 113 |
std::string EnumOption<T>::help() const |
| 114 |
{ |
| 115 |
std::stringstream msg; |
| 116 |
msg << "--" + this->name() + "={"; |
| 117 |
|
| 118 |
for (const auto &value : _allowed_values) |
| 119 |
{ |
| 120 |
msg << value << ","; |
| 121 |
} |
| 122 |
|
| 123 |
msg << "} - " << this->_help; |
| 124 |
|
| 125 |
return msg.str(); |
| 126 |
} |
| 127 |
|
| 128 |
template <typename T> |
| 129 |
inline const T &EnumOption<T>::value() const |
| 130 |
{ |
| 131 |
return this->_value; |
| 132 |
} |
| 133 |
} // namespace utils |
| 134 |
} // namespace arm_compute |
| 135 |
#endif /* ARM_COMPUTE_UTILS_ENUMOPTION */ |