blob: 7a7033805a1d29c10723ba13248bdffbc5fe1547 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Viet-Hoa Do246fe082023-08-16 10:29:00 +01002 * Copyright (c) 2016-2019, 2021, 2023 Arm Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
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 */
Michalis Spyrouf4643372019-11-29 16:17:13 +000024#ifndef ARM_COMPUTE_ERROR_H
25#define ARM_COMPUTE_ERROR_H
Anthony Barbier6ff3b192017-09-04 18:44:23 +010026
Michalis Spyrou7c60c992019-10-10 14:33:47 +010027#include <array>
Georgios Pinitas3faea252017-10-30 14:13:50 +000028#include <string>
29
30namespace arm_compute
31{
Georgios Pinitasd8734b52017-12-22 15:27:52 +000032/** Ignores unused arguments
33 *
34 * @tparam T Argument types
Alex Gildayc357c472018-03-21 13:54:09 +000035 *
36 * @param[in] ... Ignored arguments
Georgios Pinitasd8734b52017-12-22 15:27:52 +000037 */
38template <typename... T>
39inline void ignore_unused(T &&...)
40{
41}
42
43/** Available error codes */
Georgios Pinitas3faea252017-10-30 14:13:50 +000044enum class ErrorCode
45{
Vidhya Sudhan Loganathan76c85642018-05-25 13:53:02 +010046 OK, /**< No error */
47 RUNTIME_ERROR, /**< Generic runtime error */
48 UNSUPPORTED_EXTENSION_USE /**< Unsupported extension used*/
Georgios Pinitas3faea252017-10-30 14:13:50 +000049};
50
Georgios Pinitas631c41a2017-12-06 11:53:03 +000051/** Status class */
52class Status
Georgios Pinitas3faea252017-10-30 14:13:50 +000053{
54public:
55 /** Default Constructor **/
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010056 Status() : _code(ErrorCode::OK), _error_description(" ")
Georgios Pinitas3faea252017-10-30 14:13:50 +000057 {
58 }
59 /** Default Constructor
60 *
61 * @param error_status Error status.
Georgios Pinitas631c41a2017-12-06 11:53:03 +000062 * @param error_description (Optional) Error description if error_status is not valid.
Georgios Pinitas3faea252017-10-30 14:13:50 +000063 */
Georgios Pinitas631c41a2017-12-06 11:53:03 +000064 explicit Status(ErrorCode error_status, std::string error_description = " ")
65 : _code(error_status), _error_description(error_description)
Georgios Pinitas3faea252017-10-30 14:13:50 +000066 {
67 }
68 /** Allow instances of this class to be copy constructed */
Georgios Pinitas631c41a2017-12-06 11:53:03 +000069 Status(const Status &) = default;
Georgios Pinitas3faea252017-10-30 14:13:50 +000070 /** Allow instances of this class to be move constructed */
Georgios Pinitas631c41a2017-12-06 11:53:03 +000071 Status(Status &&) = default;
Georgios Pinitas3faea252017-10-30 14:13:50 +000072 /** Allow instances of this class to be copy assigned */
Georgios Pinitas631c41a2017-12-06 11:53:03 +000073 Status &operator=(const Status &) = default;
Georgios Pinitas3faea252017-10-30 14:13:50 +000074 /** Allow instances of this class to be move assigned */
Georgios Pinitas631c41a2017-12-06 11:53:03 +000075 Status &operator=(Status &&) = default;
Georgios Pinitas3faea252017-10-30 14:13:50 +000076 /** Explicit bool conversion operator
77 *
Georgios Pinitas631c41a2017-12-06 11:53:03 +000078 * @return True if there is no error else false
Georgios Pinitas3faea252017-10-30 14:13:50 +000079 */
80 explicit operator bool() const noexcept
81 {
Georgios Pinitas631c41a2017-12-06 11:53:03 +000082 return _code == ErrorCode::OK;
Georgios Pinitas3faea252017-10-30 14:13:50 +000083 }
84 /** Gets error code
85 *
86 * @return Error code.
87 */
88 ErrorCode error_code() const
89 {
90 return _code;
91 }
92 /** Gets error description if any
93 *
94 * @return Error description.
95 */
Georgios Pinitas631c41a2017-12-06 11:53:03 +000096 std::string error_description() const
Georgios Pinitas3faea252017-10-30 14:13:50 +000097 {
Georgios Pinitas631c41a2017-12-06 11:53:03 +000098 return _error_description;
Georgios Pinitas3faea252017-10-30 14:13:50 +000099 }
100 /** Throws a runtime exception in case it contains a valid error status */
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +0000101 void throw_if_error() const
Georgios Pinitas3faea252017-10-30 14:13:50 +0000102 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100103 if (!bool(*this))
Georgios Pinitas3faea252017-10-30 14:13:50 +0000104 {
105 internal_throw_on_error();
106 }
107 }
108
109private:
110 /** Internal throwing function */
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +0000111 [[noreturn]] void internal_throw_on_error() const;
Georgios Pinitas3faea252017-10-30 14:13:50 +0000112
113private:
114 ErrorCode _code;
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000115 std::string _error_description;
Georgios Pinitas3faea252017-10-30 14:13:50 +0000116};
117
118/** Creates an error containing the error message
119 *
120 * @param[in] error_code Error code
ramelg01b2eba7f2021-12-23 08:32:08 +0000121 * @param[in] msg Message to display before abandoning.
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000122 *
123 * @return status containing the error
Georgios Pinitas3faea252017-10-30 14:13:50 +0000124 */
Michalis Spyrou7c60c992019-10-10 14:33:47 +0100125Status create_error(ErrorCode error_code, std::string msg);
126
127/** Creates an error and the error message
Georgios Pinitas3faea252017-10-30 14:13:50 +0000128 *
Michalis Spyrou7c60c992019-10-10 14:33:47 +0100129 * @param[in] error_code Error code
130 * @param[in] func Function in which the error occurred.
131 * @param[in] file File in which the error occurred.
132 * @param[in] line Line in which the error occurred.
ramelg01b2eba7f2021-12-23 08:32:08 +0000133 * @param[in] msg Message to display before abandoning.
Michalis Spyrou7c60c992019-10-10 14:33:47 +0100134 *
135 * @return status containing the error
Georgios Pinitas3faea252017-10-30 14:13:50 +0000136 */
Michalis Spyrou7c60c992019-10-10 14:33:47 +0100137Status create_error_msg(ErrorCode error_code, const char *func, const char *file, int line, const char *msg);
138/** Throw an std::runtime_error
139 *
140 * @param[in] err Error status
141 */
142[[noreturn]] void throw_error(Status err);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100143} // namespace arm_compute
Georgios Pinitas3faea252017-10-30 14:13:50 +0000144/** To avoid unused variables warnings
145 *
146 * This is useful if for example a variable is only used
147 * in debug builds and generates a warning in release builds.
148 *
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000149 * @param[in] ... Variables which are unused.
Georgios Pinitas3faea252017-10-30 14:13:50 +0000150 */
Anthony Barbierc8e84b52018-07-17 16:48:42 +0100151#define ARM_COMPUTE_UNUSED(...) ::arm_compute::ignore_unused(__VA_ARGS__) // NOLINT
Georgios Pinitas3faea252017-10-30 14:13:50 +0000152
153/** Creates an error with a given message
154 *
155 * @param[in] error_code Error code.
Michalis Spyrou7c60c992019-10-10 14:33:47 +0100156 * @param[in] msg Message to encapsulate.
Georgios Pinitas3faea252017-10-30 14:13:50 +0000157 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100158#define ARM_COMPUTE_CREATE_ERROR(error_code, msg) \
159 arm_compute::create_error_msg(error_code, __func__, __FILE__, __LINE__, msg)
Georgios Pinitas3faea252017-10-30 14:13:50 +0000160
161/** Creates an error on location with a given message
162 *
163 * @param[in] error_code Error code.
164 * @param[in] func Function in which the error occurred.
165 * @param[in] file File in which the error occurred.
166 * @param[in] line Line in which the error occurred.
ramelg01b2eba7f2021-12-23 08:32:08 +0000167 * @param[in] msg Message to display before abandoning.
Georgios Pinitas3faea252017-10-30 14:13:50 +0000168 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100169#define ARM_COMPUTE_CREATE_ERROR_LOC(error_code, func, file, line, msg) \
170 arm_compute::create_error_msg(error_code, func, file, line, msg)
Michalis Spyrou7c60c992019-10-10 14:33:47 +0100171
172/** Creates an error on location with a given message. Accepts a message format
173 * and a variable list of arguments matching the format description.
174 *
175 * @param[in] error_code Error code.
176 * @param[in] func Function in which the error occurred.
177 * @param[in] file File in which the error occurred.
178 * @param[in] line Line in which the error occurred.
179 * @param[in] msg Error description message format.
180 * @param[in] ... List of arguments matching the format description.
181 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100182#define ARM_COMPUTE_CREATE_ERROR_LOC_VAR(error_code, func, file, line, msg, ...) \
183 do \
184 { \
185 std::array<char, 512> out{0}; \
186 int offset = snprintf(out.data(), out.size(), "in %s %s:%d: ", func, file, line); \
187 snprintf(out.data() + offset, out.size() - offset, msg, __VA_ARGS__); \
188 arm_compute::create_error(error_code, std::string(out.data())); \
189 } while (false)
Georgios Pinitas3faea252017-10-30 14:13:50 +0000190
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100191/** An error is returned with the given description.
192 *
193 * @param[in] ... Error description message.
194 */
195#define ARM_COMPUTE_RETURN_ERROR_MSG(...) \
196 do \
197 { \
198 return ARM_COMPUTE_CREATE_ERROR(arm_compute::ErrorCode::RUNTIME_ERROR, __VA_ARGS__); \
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100199 } while (false)
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100200
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000201/** Checks if a status contains an error and returns it
Georgios Pinitas3faea252017-10-30 14:13:50 +0000202 *
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000203 * @param[in] status Status value to check
Georgios Pinitas3faea252017-10-30 14:13:50 +0000204 */
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000205#define ARM_COMPUTE_RETURN_ON_ERROR(status) \
206 do \
207 { \
Viet-Hoa Do246fe082023-08-16 10:29:00 +0100208 const auto s = status; \
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100209 if (!bool(s)) \
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000210 { \
Viet-Hoa Do246fe082023-08-16 10:29:00 +0100211 return s; \
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000212 } \
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100213 } while (false)
Georgios Pinitas3faea252017-10-30 14:13:50 +0000214
215/** Checks if an error value is valid if not throws an exception with the error
216 *
217 * @param[in] error Error value to check.
218 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100219#define ARM_COMPUTE_THROW_ON_ERROR(error) error.throw_if_error();
Georgios Pinitas3faea252017-10-30 14:13:50 +0000220
Michalis Spyrou7c60c992019-10-10 14:33:47 +0100221/** If the condition is true, an error is returned. Accepts a message format
222 * and a variable list of arguments matching the format description.
223 *
224 * @param[in] cond Condition to evaluate.
225 * @param[in] msg Error description message format.
226 * @param[in] ... List of arguments matching the format description.
227 */
228#define ARM_COMPUTE_RETURN_ERROR_ON_MSG_VAR(cond, msg, ...) \
229 do \
230 { \
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100231 if (cond) \
Michalis Spyrou7c60c992019-10-10 14:33:47 +0100232 { \
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100233 std::array<char, 512> out{0}; \
Michalis Spyrou7c60c992019-10-10 14:33:47 +0100234 int offset = snprintf(out.data(), out.size(), "in %s %s:%d: ", __func__, __FILE__, __LINE__); \
235 snprintf(out.data() + offset, out.size() - offset, msg, __VA_ARGS__); \
236 return arm_compute::create_error(arm_compute::ErrorCode::RUNTIME_ERROR, std::string(out.data())); \
237 } \
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100238 } while (false)
Michalis Spyrou7c60c992019-10-10 14:33:47 +0100239
Georgios Pinitas3faea252017-10-30 14:13:50 +0000240/** If the condition is true, an error is returned
241 *
242 * @param[in] cond Condition to evaluate.
Michalis Spyrou7c60c992019-10-10 14:33:47 +0100243 * @param[in] msg Error description message
Georgios Pinitas3faea252017-10-30 14:13:50 +0000244 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100245#define ARM_COMPUTE_RETURN_ERROR_ON_MSG(cond, msg) \
246 do \
247 { \
248 if (cond) \
249 { \
250 return arm_compute::create_error_msg(arm_compute::ErrorCode::RUNTIME_ERROR, __func__, __FILE__, __LINE__, \
251 msg); \
252 } \
253 } while (false)
Georgios Pinitas3faea252017-10-30 14:13:50 +0000254
Michalis Spyrou7c60c992019-10-10 14:33:47 +0100255/** If the condition is true, an error is thrown. Accepts a message format
256 * and a variable list of arguments matching the format description.
Georgios Pinitas3faea252017-10-30 14:13:50 +0000257 *
258 * @param[in] cond Condition to evaluate.
259 * @param[in] func Function in which the error occurred.
260 * @param[in] file File in which the error occurred.
261 * @param[in] line Line in which the error occurred.
Michalis Spyrou7c60c992019-10-10 14:33:47 +0100262 * @param[in] msg Error description message format.
263 * @param[in] ... List of arguments matching the format description.
Georgios Pinitas3faea252017-10-30 14:13:50 +0000264 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100265#define ARM_COMPUTE_RETURN_ERROR_ON_LOC_MSG_VAR(cond, func, file, line, msg, ...) \
266 do \
267 { \
268 if (cond) \
269 { \
270 std::array<char, 512> out{0}; \
271 int offset = snprintf(out.data(), out.size(), "in %s %s:%d: ", func, file, line); \
272 snprintf(out.data() + offset, out.size() - offset, msg, __VA_ARGS__); \
273 return arm_compute::create_error(ErrorCode::RUNTIME_ERROR, std::string(out.data())); \
274 } \
275 } while (false)
Michalis Spyrou7c60c992019-10-10 14:33:47 +0100276
277/** If the condition is true, an error is thrown.
278 *
279 * @param[in] cond Condition to evaluate.
280 * @param[in] func Function in which the error occurred.
281 * @param[in] file File in which the error occurred.
282 * @param[in] line Line in which the error occurred.
283 * @param[in] msg Message to display.
284 */
285#define ARM_COMPUTE_RETURN_ERROR_ON_LOC_MSG(cond, func, file, line, msg) \
286 do \
287 { \
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100288 if (cond) \
Michalis Spyrou7c60c992019-10-10 14:33:47 +0100289 { \
290 return arm_compute::create_error_msg(ErrorCode::RUNTIME_ERROR, func, file, line, msg); \
291 } \
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100292 } while (false)
Georgios Pinitas3faea252017-10-30 14:13:50 +0000293
294/** If the condition is true, an error is returned
295 *
296 * @param[in] cond Condition to evaluate
297 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100298#define ARM_COMPUTE_RETURN_ERROR_ON(cond) ARM_COMPUTE_RETURN_ERROR_ON_MSG(cond, #cond)
Georgios Pinitas3faea252017-10-30 14:13:50 +0000299
300/** If the condition is true, an error is returned
301 *
302 * @param[in] cond Condition to evaluate.
303 * @param[in] func Function in which the error occurred.
304 * @param[in] file File in which the error occurred.
305 * @param[in] line Line in which the error occurred.
306 */
307#define ARM_COMPUTE_RETURN_ERROR_ON_LOC(cond, func, file, line) \
308 ARM_COMPUTE_RETURN_ERROR_ON_LOC_MSG(cond, func, file, line, #cond)
309
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100310/** Print the given message then throw an std::runtime_error.
311 *
Michalis Spyrou7c60c992019-10-10 14:33:47 +0100312 * @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 * @param[in] msg Message to display.
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100316 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100317#define ARM_COMPUTE_THROW_ERROR(func, file, line, msg) \
318 do \
319 { \
320 arm_compute::throw_error( \
321 arm_compute::create_error_msg(arm_compute::ErrorCode::RUNTIME_ERROR, func, file, line, msg)); \
322 } while (false)
Michalis Spyrou7c60c992019-10-10 14:33:47 +0100323
324/** Print the given message then throw an std::runtime_error. Accepts a message format
325 * and a variable list of arguments matching the format description.
326 *
327 * @param[in] func Function in which the error occurred.
328 * @param[in] file File in which the error occurred.
329 * @param[in] line Line in which the error occurred.
330 * @param[in] msg Error description message format.
331 * @param[in] ... List of arguments matching the format description.
332 */
333#define ARM_COMPUTE_THROW_ERROR_VAR(func, file, line, msg, ...) \
334 do \
335 { \
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100336 std::array<char, 512> out{0}; \
337 int offset = snprintf(out.data(), out.size(), "in %s %s:%d: ", func, file, line); \
Michalis Spyrou7c60c992019-10-10 14:33:47 +0100338 snprintf(out.data() + offset, out.size() - offset, msg, __VA_ARGS__); \
339 arm_compute::throw_error(arm_compute::Status(arm_compute::ErrorCode::RUNTIME_ERROR, std::string(out.data()))); \
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100340 } while (false)
Michalis Spyrou7c60c992019-10-10 14:33:47 +0100341
342/** Print the given message then throw an std::runtime_error. Accepts a message format
343 * and a variable list of arguments matching the format description.
344 *
345 * @param[in] msg Error description message format.
346 * @param[in] ... List of arguments matching the format description.
347 */
348#define ARM_COMPUTE_ERROR_VAR(msg, ...) ARM_COMPUTE_THROW_ERROR_VAR(__func__, __FILE__, __LINE__, msg, __VA_ARGS__)
349
350/** Print the given message then throw an std::runtime_error.
351 *
352 * @param[in] msg Message to display.
353 */
354#define ARM_COMPUTE_ERROR(msg) ARM_COMPUTE_THROW_ERROR(__func__, __FILE__, __LINE__, msg)
355
356/** Print the given message then throw an std::runtime_error. Accepts a message format
357 * and a variable list of arguments matching the format description.
358 *
359 * @param[in] func Function in which the error occurred.
360 * @param[in] file File in which the error occurred.
361 * @param[in] line Line in which the error occurred.
362 * @param[in] msg Error description message format.
363 * @param[in] ... List of arguments matching the format description.
364 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100365#define ARM_COMPUTE_ERROR_LOC_VAR(func, file, line, msg, ...) \
366 ARM_COMPUTE_THROW_ERROR_VAR(func, file, line, msg, __VA_ARGS__) // NOLINT
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100367
368/** Print the given message then throw an std::runtime_error.
369 *
370 * @param[in] func Function in which the error occurred.
371 * @param[in] file File in which the error occurred.
372 * @param[in] line Line in which the error occurred.
Michalis Spyrou7c60c992019-10-10 14:33:47 +0100373 * @param[in] msg Message to display.
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100374 */
Michalis Spyrou7c60c992019-10-10 14:33:47 +0100375#define ARM_COMPUTE_ERROR_LOC(func, file, line, msg) ARM_COMPUTE_THROW_ERROR(func, file, line, msg) // NOLINT
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100376
Georgios Pinitas6ed43b52018-07-12 17:34:22 +0100377/** If the condition is true, the given message is printed and program exits
378 *
379 * @param[in] cond Condition to evaluate.
Michalis Spyrou7c60c992019-10-10 14:33:47 +0100380 * @param[in] msg Message to display.
Georgios Pinitas6ed43b52018-07-12 17:34:22 +0100381 */
Michalis Spyrou7c60c992019-10-10 14:33:47 +0100382#define ARM_COMPUTE_EXIT_ON_MSG(cond, msg) \
383 do \
384 { \
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100385 if (cond) \
Michalis Spyrou7c60c992019-10-10 14:33:47 +0100386 { \
387 ARM_COMPUTE_ERROR(msg); \
388 } \
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100389 } while (false)
Michalis Spyrou7c60c992019-10-10 14:33:47 +0100390
391/** If the condition is true, the given message is printed and program exits. Accepts a message format
392 * and a variable list of arguments matching the format description.
393 *
394 * @param[in] cond Condition to evaluate.
395 * @param[in] msg Error description message format.
396 * @param[in] ... List of arguments matching the format description.
397 */
398#define ARM_COMPUTE_EXIT_ON_MSG_VAR(cond, msg, ...) \
399 do \
400 { \
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100401 if (cond) \
Michalis Spyrou7c60c992019-10-10 14:33:47 +0100402 { \
403 ARM_COMPUTE_ERROR_VAR(msg, __VA_ARGS__); \
404 } \
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100405 } while (false)
Georgios Pinitas6ed43b52018-07-12 17:34:22 +0100406
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100407#ifdef ARM_COMPUTE_ASSERTS_ENABLED
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000408/** Checks if a status value is valid if not throws an exception with the error
Georgios Pinitas3faea252017-10-30 14:13:50 +0000409 *
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000410 * @param[in] status Status value to check.
Georgios Pinitas3faea252017-10-30 14:13:50 +0000411 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100412#define ARM_COMPUTE_ERROR_THROW_ON(status) status.throw_if_error()
Georgios Pinitas3faea252017-10-30 14:13:50 +0000413
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100414/** If the condition is true, the given message is printed and an exception is thrown
415 *
416 * @param[in] cond Condition to evaluate.
Michalis Spyrou7c60c992019-10-10 14:33:47 +0100417 * @param[in] msg Message to display.
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100418 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100419#define ARM_COMPUTE_ERROR_ON_MSG(cond, msg) ARM_COMPUTE_EXIT_ON_MSG(cond, msg)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100420
Michalis Spyrou7c60c992019-10-10 14:33:47 +0100421/** If the condition is true, the given message is printed and an exception is thrown. Accepts a message format
422 * and a variable list of arguments matching the format description.
423 *
424 * @param[in] cond Condition to evaluate.
425 * @param[in] msg Error description message format.
426 * @param[in] ... List of arguments matching the format description.
427 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100428#define ARM_COMPUTE_ERROR_ON_MSG_VAR(cond, msg, ...) ARM_COMPUTE_EXIT_ON_MSG_VAR(cond, msg, __VA_ARGS__)
Michalis Spyrou7c60c992019-10-10 14:33:47 +0100429
430/** If the condition is true, the given message is printed and an exception is thrown.
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100431 *
432 * @param[in] cond Condition to evaluate.
433 * @param[in] func Function in which the error occurred.
434 * @param[in] file File in which the error occurred.
435 * @param[in] line Line in which the error occurred.
436 * @param[in] ... Message to print if cond is false.
437 */
Michalis Spyrou7c60c992019-10-10 14:33:47 +0100438#define ARM_COMPUTE_ERROR_ON_LOC_MSG(cond, func, file, line, ...) \
439 do \
440 { \
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100441 if (cond) \
Michalis Spyrou7c60c992019-10-10 14:33:47 +0100442 { \
443 ARM_COMPUTE_ERROR_LOC_VAR(func, file, line, __VA_ARGS__); \
444 } \
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100445 } while (false)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100446
447/** If the condition is true, the given message is printed and an exception is thrown, otherwise value is returned
448 *
449 * @param[in] cond Condition to evaluate.
450 * @param[in] val Value to be returned.
451 * @param[in] msg Message to print if cond is false.
452 */
453#define ARM_COMPUTE_CONST_ON_ERROR(cond, val, msg) (cond) ? throw std::logic_error(msg) : val;
454#else /* ARM_COMPUTE_ASSERTS_ENABLED */
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000455#define ARM_COMPUTE_ERROR_THROW_ON(status)
Michalis Spyrou7c60c992019-10-10 14:33:47 +0100456#define ARM_COMPUTE_ERROR_ON_MSG(cond, msg)
457#define ARM_COMPUTE_ERROR_ON_MSG_VAR(cond, msg, ...)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100458#define ARM_COMPUTE_ERROR_ON_LOC_MSG(cond, func, file, line, ...)
459#define ARM_COMPUTE_CONST_ON_ERROR(cond, val, msg) val
460#endif /* ARM_COMPUTE_ASSERTS_ENABLED */
461
462/** If the condition is true then an error message is printed and an exception thrown
463 *
Georgios Pinitas3faea252017-10-30 14:13:50 +0000464 * @param[in] cond Condition to evaluate.
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100465 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100466#define ARM_COMPUTE_ERROR_ON(cond) ARM_COMPUTE_ERROR_ON_MSG(cond, #cond)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100467
468/** If the condition is true then an error message is printed and an exception thrown
469 *
Georgios Pinitas3faea252017-10-30 14:13:50 +0000470 * @param[in] cond Condition to evaluate.
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100471 * @param[in] func Function in which the error occurred.
472 * @param[in] file File in which the error occurred.
473 * @param[in] line Line in which the error occurred.
474 */
475#define ARM_COMPUTE_ERROR_ON_LOC(cond, func, file, line) \
Michalis Spyrou7c60c992019-10-10 14:33:47 +0100476 ARM_COMPUTE_ERROR_ON_LOC_MSG(cond, func, file, line, "%s", #cond)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100477
Michalis Spyrou323ce0f2018-11-30 16:30:43 +0000478#ifndef ARM_COMPUTE_EXCEPTIONS_DISABLED
479#define ARM_COMPUTE_THROW(ex) throw(ex)
480#else /* ARM_COMPUTE_EXCEPTIONS_DISABLED */
481#define ARM_COMPUTE_THROW(ex) (ex), std::abort()
482#endif /* ARM_COMPUTE_EXCEPTIONS_DISABLED */
483
Michalis Spyrouf4643372019-11-29 16:17:13 +0000484#endif /* ARM_COMPUTE_ERROR_H */