blob: 0854f2c5274eddb3a5ac5052c4732548719d6d95 [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 **/
Georgios Pinitas631c41a2017-12-06 11:53:03 +000056 Status()
57 : _code(ErrorCode::OK), _error_description(" ")
Georgios Pinitas3faea252017-10-30 14:13:50 +000058 {
59 }
60 /** Default Constructor
61 *
62 * @param error_status Error status.
Georgios Pinitas631c41a2017-12-06 11:53:03 +000063 * @param error_description (Optional) Error description if error_status is not valid.
Georgios Pinitas3faea252017-10-30 14:13:50 +000064 */
Georgios Pinitas631c41a2017-12-06 11:53:03 +000065 explicit Status(ErrorCode error_status, std::string error_description = " ")
66 : _code(error_status), _error_description(error_description)
Georgios Pinitas3faea252017-10-30 14:13:50 +000067 {
68 }
69 /** Allow instances of this class to be copy constructed */
Georgios Pinitas631c41a2017-12-06 11:53:03 +000070 Status(const Status &) = default;
Georgios Pinitas3faea252017-10-30 14:13:50 +000071 /** Allow instances of this class to be move constructed */
Georgios Pinitas631c41a2017-12-06 11:53:03 +000072 Status(Status &&) = default;
Georgios Pinitas3faea252017-10-30 14:13:50 +000073 /** Allow instances of this class to be copy assigned */
Georgios Pinitas631c41a2017-12-06 11:53:03 +000074 Status &operator=(const Status &) = default;
Georgios Pinitas3faea252017-10-30 14:13:50 +000075 /** Allow instances of this class to be move assigned */
Georgios Pinitas631c41a2017-12-06 11:53:03 +000076 Status &operator=(Status &&) = default;
Georgios Pinitas3faea252017-10-30 14:13:50 +000077 /** Explicit bool conversion operator
78 *
Georgios Pinitas631c41a2017-12-06 11:53:03 +000079 * @return True if there is no error else false
Georgios Pinitas3faea252017-10-30 14:13:50 +000080 */
81 explicit operator bool() const noexcept
82 {
Georgios Pinitas631c41a2017-12-06 11:53:03 +000083 return _code == ErrorCode::OK;
Georgios Pinitas3faea252017-10-30 14:13:50 +000084 }
85 /** Gets error code
86 *
87 * @return Error code.
88 */
89 ErrorCode error_code() const
90 {
91 return _code;
92 }
93 /** Gets error description if any
94 *
95 * @return Error description.
96 */
Georgios Pinitas631c41a2017-12-06 11:53:03 +000097 std::string error_description() const
Georgios Pinitas3faea252017-10-30 14:13:50 +000098 {
Georgios Pinitas631c41a2017-12-06 11:53:03 +000099 return _error_description;
Georgios Pinitas3faea252017-10-30 14:13:50 +0000100 }
101 /** Throws a runtime exception in case it contains a valid error status */
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +0000102 void throw_if_error() const
Georgios Pinitas3faea252017-10-30 14:13:50 +0000103 {
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000104 if(!bool(*this))
Georgios Pinitas3faea252017-10-30 14:13:50 +0000105 {
106 internal_throw_on_error();
107 }
108 }
109
110private:
111 /** Internal throwing function */
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +0000112 [[noreturn]] void internal_throw_on_error() const;
Georgios Pinitas3faea252017-10-30 14:13:50 +0000113
114private:
115 ErrorCode _code;
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000116 std::string _error_description;
Georgios Pinitas3faea252017-10-30 14:13:50 +0000117};
118
119/** Creates an error containing the error message
120 *
121 * @param[in] error_code Error code
ramelg01b2eba7f2021-12-23 08:32:08 +0000122 * @param[in] msg Message to display before abandoning.
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000123 *
124 * @return status containing the error
Georgios Pinitas3faea252017-10-30 14:13:50 +0000125 */
Michalis Spyrou7c60c992019-10-10 14:33:47 +0100126Status create_error(ErrorCode error_code, std::string msg);
127
128/** Creates an error and the error message
Georgios Pinitas3faea252017-10-30 14:13:50 +0000129 *
Michalis Spyrou7c60c992019-10-10 14:33:47 +0100130 * @param[in] error_code Error code
131 * @param[in] func Function in which the error occurred.
132 * @param[in] file File in which the error occurred.
133 * @param[in] line Line in which the error occurred.
ramelg01b2eba7f2021-12-23 08:32:08 +0000134 * @param[in] msg Message to display before abandoning.
Michalis Spyrou7c60c992019-10-10 14:33:47 +0100135 *
136 * @return status containing the error
Georgios Pinitas3faea252017-10-30 14:13:50 +0000137 */
Michalis Spyrou7c60c992019-10-10 14:33:47 +0100138Status create_error_msg(ErrorCode error_code, const char *func, const char *file, int line, const char *msg);
139/** Throw an std::runtime_error
140 *
141 * @param[in] err Error status
142 */
143[[noreturn]] void throw_error(Status err);
Georgios Pinitas3faea252017-10-30 14:13:50 +0000144}
145/** To avoid unused variables warnings
146 *
147 * This is useful if for example a variable is only used
148 * in debug builds and generates a warning in release builds.
149 *
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000150 * @param[in] ... Variables which are unused.
Georgios Pinitas3faea252017-10-30 14:13:50 +0000151 */
Anthony Barbierc8e84b52018-07-17 16:48:42 +0100152#define ARM_COMPUTE_UNUSED(...) ::arm_compute::ignore_unused(__VA_ARGS__) // NOLINT
Georgios Pinitas3faea252017-10-30 14:13:50 +0000153
154/** Creates an error with a given message
155 *
156 * @param[in] error_code Error code.
Michalis Spyrou7c60c992019-10-10 14:33:47 +0100157 * @param[in] msg Message to encapsulate.
Georgios Pinitas3faea252017-10-30 14:13:50 +0000158 */
Michalis Spyrou7c60c992019-10-10 14:33:47 +0100159#define ARM_COMPUTE_CREATE_ERROR(error_code, msg) 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 */
Michalis Spyrou7c60c992019-10-10 14:33:47 +0100169#define ARM_COMPUTE_CREATE_ERROR_LOC(error_code, func, file, line, msg) arm_compute::create_error_msg(error_code, func, file, line, msg)
170
171/** Creates an error on location with a given message. Accepts a message format
172 * and a variable list of arguments matching the format description.
173 *
174 * @param[in] error_code Error code.
175 * @param[in] func Function in which the error occurred.
176 * @param[in] file File in which the error occurred.
177 * @param[in] line Line in which the error occurred.
178 * @param[in] msg Error description message format.
179 * @param[in] ... List of arguments matching the format description.
180 */
181#define ARM_COMPUTE_CREATE_ERROR_LOC_VAR(error_code, func, file, line, msg, ...) \
182 do \
183 { \
184 std::array<char, 512> out{ 0 }; \
185 int offset = snprintf(out.data(), out.size(), "in %s %s:%d: ", func, file, line); \
186 snprintf(out.data() + offset, out.size() - offset, msg, __VA_ARGS__); \
187 arm_compute::create_error(error_code, std::string(out.data())); \
188 } while(false)
Georgios Pinitas3faea252017-10-30 14:13:50 +0000189
Vidhya Sudhan Loganathan3ca97862018-04-23 08:20:04 +0100190/** An error is returned with the given description.
191 *
192 * @param[in] ... Error description message.
193 */
194#define ARM_COMPUTE_RETURN_ERROR_MSG(...) \
195 do \
196 { \
197 return ARM_COMPUTE_CREATE_ERROR(arm_compute::ErrorCode::RUNTIME_ERROR, __VA_ARGS__); \
198 } while(false)
199
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000200/** Checks if a status contains an error and returns it
Georgios Pinitas3faea252017-10-30 14:13:50 +0000201 *
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000202 * @param[in] status Status value to check
Georgios Pinitas3faea252017-10-30 14:13:50 +0000203 */
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000204#define ARM_COMPUTE_RETURN_ON_ERROR(status) \
205 do \
206 { \
Viet-Hoa Do246fe082023-08-16 10:29:00 +0100207 const auto s = status; \
208 if(!bool(s)) \
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000209 { \
Viet-Hoa Do246fe082023-08-16 10:29:00 +0100210 return s; \
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000211 } \
Georgios Pinitas3faea252017-10-30 14:13:50 +0000212 } while(false)
213
214/** Checks if an error value is valid if not throws an exception with the error
215 *
216 * @param[in] error Error value to check.
217 */
218#define ARM_COMPUTE_THROW_ON_ERROR(error) \
219 error.throw_if_error();
220
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 { \
231 if(cond) \
232 { \
233 std::array<char, 512> out{ 0 }; \
234 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 } \
238 } while(false)
239
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 */
Michalis Spyrou7c60c992019-10-10 14:33:47 +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__, msg); \
251 } \
Georgios Pinitas3faea252017-10-30 14:13:50 +0000252 } while(false)
253
Michalis Spyrou7c60c992019-10-10 14:33:47 +0100254/** If the condition is true, an error is thrown. Accepts a message format
255 * and a variable list of arguments matching the format description.
Georgios Pinitas3faea252017-10-30 14:13:50 +0000256 *
257 * @param[in] cond Condition to evaluate.
258 * @param[in] func Function in which the error occurred.
259 * @param[in] file File in which the error occurred.
260 * @param[in] line Line in which the error occurred.
Michalis Spyrou7c60c992019-10-10 14:33:47 +0100261 * @param[in] msg Error description message format.
262 * @param[in] ... List of arguments matching the format description.
Georgios Pinitas3faea252017-10-30 14:13:50 +0000263 */
Michalis Spyrou7c60c992019-10-10 14:33:47 +0100264#define ARM_COMPUTE_RETURN_ERROR_ON_LOC_MSG_VAR(cond, func, file, line, msg, ...) \
265 do \
266 { \
267 if(cond) \
268 { \
269 std::array<char, 512> out{ 0 }; \
270 int offset = snprintf(out.data(), out.size(), "in %s %s:%d: ", func, file, line); \
271 snprintf(out.data() + offset, out.size() - offset, msg, __VA_ARGS__); \
272 return arm_compute::create_error(ErrorCode::RUNTIME_ERROR, std::string(out.data())); \
273 } \
274 } while(false)
275
276/** If the condition is true, an error is thrown.
277 *
278 * @param[in] cond Condition to evaluate.
279 * @param[in] func Function in which the error occurred.
280 * @param[in] file File in which the error occurred.
281 * @param[in] line Line in which the error occurred.
282 * @param[in] msg Message to display.
283 */
284#define ARM_COMPUTE_RETURN_ERROR_ON_LOC_MSG(cond, func, file, line, msg) \
285 do \
286 { \
287 if(cond) \
288 { \
289 return arm_compute::create_error_msg(ErrorCode::RUNTIME_ERROR, func, file, line, msg); \
290 } \
Georgios Pinitas3faea252017-10-30 14:13:50 +0000291 } while(false)
292
293/** If the condition is true, an error is returned
294 *
295 * @param[in] cond Condition to evaluate
296 */
297#define ARM_COMPUTE_RETURN_ERROR_ON(cond) \
298 ARM_COMPUTE_RETURN_ERROR_ON_MSG(cond, #cond)
299
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 */
Michalis Spyrou7c60c992019-10-10 14:33:47 +0100317#define ARM_COMPUTE_THROW_ERROR(func, file, line, msg) \
318 do \
319 { \
320 arm_compute::throw_error(arm_compute::create_error_msg(arm_compute::ErrorCode::RUNTIME_ERROR, func, file, line, msg)); \
321 } while(false)
322
323/** Print the given message then throw an std::runtime_error. Accepts a message format
324 * and a variable list of arguments matching the format description.
325 *
326 * @param[in] func Function in which the error occurred.
327 * @param[in] file File in which the error occurred.
328 * @param[in] line Line in which the error occurred.
329 * @param[in] msg Error description message format.
330 * @param[in] ... List of arguments matching the format description.
331 */
332#define ARM_COMPUTE_THROW_ERROR_VAR(func, file, line, msg, ...) \
333 do \
334 { \
335 std::array<char, 512> out{ 0 }; \
336 int offset = snprintf(out.data(), out.size(), "in %s %s:%d: ", func, file, line); \
337 snprintf(out.data() + offset, out.size() - offset, msg, __VA_ARGS__); \
338 arm_compute::throw_error(arm_compute::Status(arm_compute::ErrorCode::RUNTIME_ERROR, std::string(out.data()))); \
339 } while(false)
340
341/** Print the given message then throw an std::runtime_error. Accepts a message format
342 * and a variable list of arguments matching the format description.
343 *
344 * @param[in] msg Error description message format.
345 * @param[in] ... List of arguments matching the format description.
346 */
347#define ARM_COMPUTE_ERROR_VAR(msg, ...) ARM_COMPUTE_THROW_ERROR_VAR(__func__, __FILE__, __LINE__, msg, __VA_ARGS__)
348
349/** Print the given message then throw an std::runtime_error.
350 *
351 * @param[in] msg Message to display.
352 */
353#define ARM_COMPUTE_ERROR(msg) ARM_COMPUTE_THROW_ERROR(__func__, __FILE__, __LINE__, msg)
354
355/** Print the given message then throw an std::runtime_error. Accepts a message format
356 * and a variable list of arguments matching the format description.
357 *
358 * @param[in] func Function in which the error occurred.
359 * @param[in] file File in which the error occurred.
360 * @param[in] line Line in which the error occurred.
361 * @param[in] msg Error description message format.
362 * @param[in] ... List of arguments matching the format description.
363 */
364#define ARM_COMPUTE_ERROR_LOC_VAR(func, file, line, msg, ...) ARM_COMPUTE_THROW_ERROR_VAR(func, file, line, msg, __VA_ARGS__) // NOLINT
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100365
366/** Print the given message then throw an std::runtime_error.
367 *
368 * @param[in] func Function in which the error occurred.
369 * @param[in] file File in which the error occurred.
370 * @param[in] line Line in which the error occurred.
Michalis Spyrou7c60c992019-10-10 14:33:47 +0100371 * @param[in] msg Message to display.
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100372 */
Michalis Spyrou7c60c992019-10-10 14:33:47 +0100373#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 +0100374
Georgios Pinitas6ed43b52018-07-12 17:34:22 +0100375/** If the condition is true, the given message is printed and program exits
376 *
377 * @param[in] cond Condition to evaluate.
Michalis Spyrou7c60c992019-10-10 14:33:47 +0100378 * @param[in] msg Message to display.
Georgios Pinitas6ed43b52018-07-12 17:34:22 +0100379 */
Michalis Spyrou7c60c992019-10-10 14:33:47 +0100380#define ARM_COMPUTE_EXIT_ON_MSG(cond, msg) \
381 do \
382 { \
383 if(cond) \
384 { \
385 ARM_COMPUTE_ERROR(msg); \
386 } \
387 } while(false)
388
389/** If the condition is true, the given message is printed and program exits. Accepts a message format
390 * and a variable list of arguments matching the format description.
391 *
392 * @param[in] cond Condition to evaluate.
393 * @param[in] msg Error description message format.
394 * @param[in] ... List of arguments matching the format description.
395 */
396#define ARM_COMPUTE_EXIT_ON_MSG_VAR(cond, msg, ...) \
397 do \
398 { \
399 if(cond) \
400 { \
401 ARM_COMPUTE_ERROR_VAR(msg, __VA_ARGS__); \
402 } \
Georgios Pinitas6ed43b52018-07-12 17:34:22 +0100403 } while(false)
404
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100405#ifdef ARM_COMPUTE_ASSERTS_ENABLED
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000406/** Checks if a status value is valid if not throws an exception with the error
Georgios Pinitas3faea252017-10-30 14:13:50 +0000407 *
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000408 * @param[in] status Status value to check.
Georgios Pinitas3faea252017-10-30 14:13:50 +0000409 */
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000410#define ARM_COMPUTE_ERROR_THROW_ON(status) \
411 status.throw_if_error()
Georgios Pinitas3faea252017-10-30 14:13:50 +0000412
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100413/** If the condition is true, the given message is printed and an exception is thrown
414 *
415 * @param[in] cond Condition to evaluate.
Michalis Spyrou7c60c992019-10-10 14:33:47 +0100416 * @param[in] msg Message to display.
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100417 */
Michalis Spyrou7c60c992019-10-10 14:33:47 +0100418#define ARM_COMPUTE_ERROR_ON_MSG(cond, msg) \
419 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 */
428#define ARM_COMPUTE_ERROR_ON_MSG_VAR(cond, msg, ...) \
429 ARM_COMPUTE_EXIT_ON_MSG_VAR(cond, msg, __VA_ARGS__)
430
431/** If the condition is true, the given message is printed and an exception is thrown.
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100432 *
433 * @param[in] cond Condition to evaluate.
434 * @param[in] func Function in which the error occurred.
435 * @param[in] file File in which the error occurred.
436 * @param[in] line Line in which the error occurred.
437 * @param[in] ... Message to print if cond is false.
438 */
Michalis Spyrou7c60c992019-10-10 14:33:47 +0100439#define ARM_COMPUTE_ERROR_ON_LOC_MSG(cond, func, file, line, ...) \
440 do \
441 { \
442 if(cond) \
443 { \
444 ARM_COMPUTE_ERROR_LOC_VAR(func, file, line, __VA_ARGS__); \
445 } \
Georgios Pinitas6ed43b52018-07-12 17:34:22 +0100446 } while(false)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100447
448/** If the condition is true, the given message is printed and an exception is thrown, otherwise value is returned
449 *
450 * @param[in] cond Condition to evaluate.
451 * @param[in] val Value to be returned.
452 * @param[in] msg Message to print if cond is false.
453 */
454#define ARM_COMPUTE_CONST_ON_ERROR(cond, val, msg) (cond) ? throw std::logic_error(msg) : val;
455#else /* ARM_COMPUTE_ASSERTS_ENABLED */
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000456#define ARM_COMPUTE_ERROR_THROW_ON(status)
Michalis Spyrou7c60c992019-10-10 14:33:47 +0100457#define ARM_COMPUTE_ERROR_ON_MSG(cond, msg)
458#define ARM_COMPUTE_ERROR_ON_MSG_VAR(cond, msg, ...)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100459#define ARM_COMPUTE_ERROR_ON_LOC_MSG(cond, func, file, line, ...)
460#define ARM_COMPUTE_CONST_ON_ERROR(cond, val, msg) val
461#endif /* ARM_COMPUTE_ASSERTS_ENABLED */
462
463/** If the condition is true then an error message is printed and an exception thrown
464 *
Georgios Pinitas3faea252017-10-30 14:13:50 +0000465 * @param[in] cond Condition to evaluate.
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100466 */
467#define ARM_COMPUTE_ERROR_ON(cond) \
468 ARM_COMPUTE_ERROR_ON_MSG(cond, #cond)
469
470/** If the condition is true then an error message is printed and an exception thrown
471 *
Georgios Pinitas3faea252017-10-30 14:13:50 +0000472 * @param[in] cond Condition to evaluate.
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100473 * @param[in] func Function in which the error occurred.
474 * @param[in] file File in which the error occurred.
475 * @param[in] line Line in which the error occurred.
476 */
477#define ARM_COMPUTE_ERROR_ON_LOC(cond, func, file, line) \
Michalis Spyrou7c60c992019-10-10 14:33:47 +0100478 ARM_COMPUTE_ERROR_ON_LOC_MSG(cond, func, file, line, "%s", #cond)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100479
Michalis Spyrou323ce0f2018-11-30 16:30:43 +0000480#ifndef ARM_COMPUTE_EXCEPTIONS_DISABLED
481#define ARM_COMPUTE_THROW(ex) throw(ex)
482#else /* ARM_COMPUTE_EXCEPTIONS_DISABLED */
483#define ARM_COMPUTE_THROW(ex) (ex), std::abort()
484#endif /* ARM_COMPUTE_EXCEPTIONS_DISABLED */
485
Michalis Spyrouf4643372019-11-29 16:17:13 +0000486#endif /* ARM_COMPUTE_ERROR_H */