Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016, 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_ERROR_H__ |
| 25 | #define __ARM_COMPUTE_ERROR_H__ |
| 26 | |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 27 | #include <stdarg.h> |
Georgios Pinitas | 3faea25 | 2017-10-30 14:13:50 +0000 | [diff] [blame] | 28 | #include <stdexcept> |
| 29 | #include <string> |
| 30 | |
| 31 | namespace arm_compute |
| 32 | { |
| 33 | enum class ErrorCode |
| 34 | { |
| 35 | OK, /**< No error */ |
| 36 | RUNTIME_ERROR /**< Generic runtime error */ |
| 37 | }; |
| 38 | |
| 39 | /** Error class */ |
| 40 | class Error |
| 41 | { |
| 42 | public: |
| 43 | /** Default Constructor **/ |
| 44 | Error() |
| 45 | : _code(ErrorCode::OK), _description(" ") |
| 46 | { |
| 47 | } |
| 48 | /** Default Constructor |
| 49 | * |
| 50 | * @param error_status Error status. |
| 51 | * @param error_description Error description if error_status is not valid. |
| 52 | */ |
| 53 | explicit Error(ErrorCode error_status, std::string error_description = " ") |
| 54 | : _code(error_status), _description(error_description) |
| 55 | { |
| 56 | } |
| 57 | /** Allow instances of this class to be copy constructed */ |
| 58 | Error(const Error &) = default; |
| 59 | /** Allow instances of this class to be move constructed */ |
| 60 | Error(Error &&) = default; |
| 61 | /** Allow instances of this class to be copy assigned */ |
| 62 | Error &operator=(const Error &) = default; |
| 63 | /** Allow instances of this class to be move assigned */ |
| 64 | Error &operator=(Error &&) = default; |
| 65 | /** Explicit bool conversion operator |
| 66 | * |
| 67 | * @return True if there is a valid error else false if status is OK. |
| 68 | */ |
| 69 | explicit operator bool() const noexcept |
| 70 | { |
| 71 | return _code != ErrorCode::OK; |
| 72 | } |
| 73 | /** Gets error code |
| 74 | * |
| 75 | * @return Error code. |
| 76 | */ |
| 77 | ErrorCode error_code() const |
| 78 | { |
| 79 | return _code; |
| 80 | } |
| 81 | /** Gets error description if any |
| 82 | * |
| 83 | * @return Error description. |
| 84 | */ |
| 85 | std::string description() const |
| 86 | { |
| 87 | return _description; |
| 88 | } |
| 89 | /** Throws a runtime exception in case it contains a valid error status */ |
| 90 | void throw_if_error() |
| 91 | { |
| 92 | if(bool(*this)) |
| 93 | { |
| 94 | internal_throw_on_error(); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | private: |
| 99 | /** Internal throwing function */ |
| 100 | [[noreturn]] void internal_throw_on_error() |
| 101 | { |
| 102 | throw std::runtime_error(_description); |
| 103 | } |
| 104 | |
| 105 | private: |
| 106 | ErrorCode _code; |
| 107 | std::string _description; |
| 108 | }; |
| 109 | |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 110 | /** Creates an error containing the error message from variable argument list |
| 111 | * |
| 112 | * @param[in] error_code Error code |
| 113 | * @param[in] function Function in which the error occurred. |
| 114 | * @param[in] file Name of the file where the error occurred. |
| 115 | * @param[in] line Line on which the error occurred. |
| 116 | * @param[in] msg Message to display before aborting. |
| 117 | * @param[in] args Variable argument list of the message. |
| 118 | */ |
| 119 | Error create_error_va_list(ErrorCode error_code, const char *function, const char *file, const int line, const char *msg, va_list args); |
Georgios Pinitas | 3faea25 | 2017-10-30 14:13:50 +0000 | [diff] [blame] | 120 | /** Creates an error containing the error message |
| 121 | * |
| 122 | * @param[in] error_code Error code |
| 123 | * @param[in] function Function in which the error occurred. |
| 124 | * @param[in] file Name of the file where the error occurred. |
| 125 | * @param[in] line Line on which the error occurred. |
| 126 | * @param[in] msg Message to display before aborting. |
| 127 | * @param[in] ... Variable number of arguments of the message. |
| 128 | */ |
| 129 | Error create_error(ErrorCode error_code, const char *function, const char *file, const int line, const char *msg, ...); |
| 130 | /** Print an error message then throw an std::runtime_error |
| 131 | * |
| 132 | * @param[in] function Function in which the error occurred. |
| 133 | * @param[in] file Name of the file where the error occurred. |
| 134 | * @param[in] line Line on which the error occurred. |
| 135 | * @param[in] msg Message to display before aborting. |
| 136 | * @param[in] ... Variable number of arguments of the message. |
| 137 | */ |
| 138 | [[noreturn]] void error(const char *function, const char *file, const int line, const char *msg, ...); |
| 139 | } |
| 140 | /** To avoid unused variables warnings |
| 141 | * |
| 142 | * This is useful if for example a variable is only used |
| 143 | * in debug builds and generates a warning in release builds. |
| 144 | * |
| 145 | * @param[in] var Variable which is unused. |
| 146 | */ |
| 147 | #define ARM_COMPUTE_UNUSED(var) (void)(var) |
| 148 | |
| 149 | /** Creates an error with a given message |
| 150 | * |
| 151 | * @param[in] error_code Error code. |
| 152 | * @param[in] ... Message to encapsulate. |
| 153 | */ |
| 154 | #define ARM_COMPUTE_CREATE_ERROR(error_code, ...) ::arm_compute::create_error(error_code, __func__, __FILE__, __LINE__, __VA_ARGS__) // NOLINT |
| 155 | |
| 156 | /** Creates an error on location with a given message |
| 157 | * |
| 158 | * @param[in] error_code Error code. |
| 159 | * @param[in] func Function in which the error occurred. |
| 160 | * @param[in] file File in which the error occurred. |
| 161 | * @param[in] line Line in which the error occurred. |
| 162 | * @param[in] ... Message to display before aborting. |
| 163 | */ |
| 164 | #define ARM_COMPUTE_CREATE_ERROR_LOC(error_code, func, file, line, ...) ::arm_compute::create_error(error_code, func, file, line, __VA_ARGS__) // NOLINT |
| 165 | |
| 166 | /** Checks if an error value is valid if not returns |
| 167 | * |
| 168 | * @param[in] error Error value to check |
| 169 | */ |
| 170 | #define ARM_COMPUTE_RETURN_ON_ERROR(error) \ |
| 171 | do \ |
| 172 | { \ |
| 173 | if(bool(error)) \ |
| 174 | { \ |
| 175 | return error; \ |
| 176 | } \ |
| 177 | } while(false) |
| 178 | |
| 179 | /** Checks if an error value is valid if not throws an exception with the error |
| 180 | * |
| 181 | * @param[in] error Error value to check. |
| 182 | */ |
| 183 | #define ARM_COMPUTE_THROW_ON_ERROR(error) \ |
| 184 | error.throw_if_error(); |
| 185 | |
| 186 | /** If the condition is true, an error is returned |
| 187 | * |
| 188 | * @param[in] cond Condition to evaluate. |
| 189 | * @param[in] ... Error description message |
| 190 | */ |
| 191 | #define ARM_COMPUTE_RETURN_ERROR_ON_MSG(cond, ...) \ |
| 192 | do \ |
| 193 | { \ |
| 194 | if(cond) \ |
| 195 | { \ |
| 196 | return ARM_COMPUTE_CREATE_ERROR(arm_compute::ErrorCode::RUNTIME_ERROR, __VA_ARGS__); \ |
| 197 | } \ |
| 198 | } while(false) |
| 199 | |
| 200 | /** If the condition is true, an error is thrown |
| 201 | * |
| 202 | * @param[in] cond Condition to evaluate. |
| 203 | * @param[in] func Function in which the error occurred. |
| 204 | * @param[in] file File in which the error occurred. |
| 205 | * @param[in] line Line in which the error occurred. |
| 206 | * @param[in] ... Error description message. |
| 207 | */ |
| 208 | #define ARM_COMPUTE_RETURN_ERROR_ON_LOC_MSG(cond, func, file, line, ...) \ |
| 209 | do \ |
| 210 | { \ |
| 211 | if(cond) \ |
| 212 | { \ |
| 213 | return ARM_COMPUTE_CREATE_ERROR_LOC(arm_compute::ErrorCode::RUNTIME_ERROR, func, file, line, __VA_ARGS__); \ |
| 214 | } \ |
| 215 | } while(false) |
| 216 | |
| 217 | /** If the condition is true, an error is returned |
| 218 | * |
| 219 | * @param[in] cond Condition to evaluate |
| 220 | */ |
| 221 | #define ARM_COMPUTE_RETURN_ERROR_ON(cond) \ |
| 222 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(cond, #cond) |
| 223 | |
| 224 | /** If the condition is true, an error is returned |
| 225 | * |
| 226 | * @param[in] cond Condition to evaluate. |
| 227 | * @param[in] func Function in which the error occurred. |
| 228 | * @param[in] file File in which the error occurred. |
| 229 | * @param[in] line Line in which the error occurred. |
| 230 | */ |
| 231 | #define ARM_COMPUTE_RETURN_ERROR_ON_LOC(cond, func, file, line) \ |
| 232 | ARM_COMPUTE_RETURN_ERROR_ON_LOC_MSG(cond, func, file, line, #cond) |
| 233 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 234 | /** Print the given message then throw an std::runtime_error. |
| 235 | * |
| 236 | * @param[in] ... Message to display before aborting. |
| 237 | */ |
| 238 | #define ARM_COMPUTE_ERROR(...) ::arm_compute::error(__func__, __FILE__, __LINE__, __VA_ARGS__) // NOLINT |
| 239 | |
| 240 | /** Print the given message then throw an std::runtime_error. |
| 241 | * |
| 242 | * @param[in] func Function in which the error occurred. |
| 243 | * @param[in] file File in which the error occurred. |
| 244 | * @param[in] line Line in which the error occurred. |
| 245 | * @param[in] ... Message to display before aborting. |
| 246 | */ |
| 247 | #define ARM_COMPUTE_ERROR_LOC(func, file, line, ...) ::arm_compute::error(func, file, line, __VA_ARGS__) // NOLINT |
| 248 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 249 | #ifdef ARM_COMPUTE_ASSERTS_ENABLED |
Georgios Pinitas | 3faea25 | 2017-10-30 14:13:50 +0000 | [diff] [blame] | 250 | /** Checks if an error value is valid if not throws an exception with the error |
| 251 | * |
| 252 | * @param[in] error Error value to check. |
| 253 | */ |
Georgios Pinitas | f9d3a0a | 2017-11-03 19:01:44 +0000 | [diff] [blame] | 254 | #define ARM_COMPUTE_ERROR_THROW_ON(error) \ |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 255 | error.throw_if_error() |
Georgios Pinitas | 3faea25 | 2017-10-30 14:13:50 +0000 | [diff] [blame] | 256 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 257 | /** If the condition is true, the given message is printed and an exception is thrown |
| 258 | * |
| 259 | * @param[in] cond Condition to evaluate. |
| 260 | * @param[in] ... Message to print if cond is false. |
| 261 | */ |
| 262 | #define ARM_COMPUTE_ERROR_ON_MSG(cond, ...) \ |
| 263 | do \ |
| 264 | { \ |
| 265 | if(cond) \ |
| 266 | { \ |
| 267 | ARM_COMPUTE_ERROR(__VA_ARGS__); \ |
| 268 | } \ |
| 269 | } while(0) |
| 270 | |
| 271 | /** If the condition is true, the given message is printed and an exception is thrown |
| 272 | * |
| 273 | * @param[in] cond Condition to evaluate. |
| 274 | * @param[in] func Function in which the error occurred. |
| 275 | * @param[in] file File in which the error occurred. |
| 276 | * @param[in] line Line in which the error occurred. |
| 277 | * @param[in] ... Message to print if cond is false. |
| 278 | */ |
| 279 | #define ARM_COMPUTE_ERROR_ON_LOC_MSG(cond, func, file, line, ...) \ |
| 280 | do \ |
| 281 | { \ |
| 282 | if(cond) \ |
| 283 | { \ |
| 284 | ARM_COMPUTE_ERROR_LOC(func, file, line, __VA_ARGS__); \ |
| 285 | } \ |
| 286 | } while(0) |
| 287 | |
| 288 | /** If the condition is true, the given message is printed and an exception is thrown, otherwise value is returned |
| 289 | * |
| 290 | * @param[in] cond Condition to evaluate. |
| 291 | * @param[in] val Value to be returned. |
| 292 | * @param[in] msg Message to print if cond is false. |
| 293 | */ |
| 294 | #define ARM_COMPUTE_CONST_ON_ERROR(cond, val, msg) (cond) ? throw std::logic_error(msg) : val; |
| 295 | #else /* ARM_COMPUTE_ASSERTS_ENABLED */ |
Georgios Pinitas | f9d3a0a | 2017-11-03 19:01:44 +0000 | [diff] [blame] | 296 | #define ARM_COMPUTE_ERROR_THROW_ON(error) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 297 | #define ARM_COMPUTE_ERROR_ON_MSG(cond, ...) |
| 298 | #define ARM_COMPUTE_ERROR_ON_LOC_MSG(cond, func, file, line, ...) |
| 299 | #define ARM_COMPUTE_CONST_ON_ERROR(cond, val, msg) val |
| 300 | #endif /* ARM_COMPUTE_ASSERTS_ENABLED */ |
| 301 | |
| 302 | /** If the condition is true then an error message is printed and an exception thrown |
| 303 | * |
Georgios Pinitas | 3faea25 | 2017-10-30 14:13:50 +0000 | [diff] [blame] | 304 | * @param[in] cond Condition to evaluate. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 305 | */ |
| 306 | #define ARM_COMPUTE_ERROR_ON(cond) \ |
| 307 | ARM_COMPUTE_ERROR_ON_MSG(cond, #cond) |
| 308 | |
| 309 | /** If the condition is true then an error message is printed and an exception thrown |
| 310 | * |
Georgios Pinitas | 3faea25 | 2017-10-30 14:13:50 +0000 | [diff] [blame] | 311 | * @param[in] cond Condition to evaluate. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 312 | * @param[in] func Function in which the error occurred. |
| 313 | * @param[in] file File in which the error occurred. |
| 314 | * @param[in] line Line in which the error occurred. |
| 315 | */ |
| 316 | #define ARM_COMPUTE_ERROR_ON_LOC(cond, func, file, line) \ |
| 317 | ARM_COMPUTE_ERROR_ON_LOC_MSG(cond, func, file, line, #cond) |
| 318 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 319 | #endif /* __ARM_COMPUTE_ERROR_H__ */ |