blob: 6b807789574a14117b051bd42fb1478f43e6f74f [file] [log] [blame]
Gian Marco Iodice6c113ed2023-01-19 17:14:26 +00001/*
2 * Copyright (c) 2023 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 */
Viet-Hoa Do3389f532023-07-05 17:36:40 +010024#ifndef CKW_INCLUDE_CKW_ERROR_H
25#define CKW_INCLUDE_CKW_ERROR_H
Gian Marco Iodice6c113ed2023-01-19 17:14:26 +000026
Viet-Hoa Doce3c48c2023-07-03 13:44:43 +010027#include <stdexcept>
Nikolaj Jensenacea4072023-07-03 09:44:42 +010028#include <string>
Gian Marco Iodice6c113ed2023-01-19 17:14:26 +000029
30namespace ckw
31{
32/** Creates the error message
33 *
34 * @param[in] file File in which the error occurred.
35 * @param[in] func Function in which the error occurred.
36 * @param[in] line Line in which the error occurred.
37 * @param[in] msg Message to display before abandoning.
38 *
39 * @return status containing the error
40 */
Nikolaj Jensenacea4072023-07-03 09:44:42 +010041std::string
42create_error_msg(const std::string &file, const std::string &func, const std::string &line, const std::string &msg);
Gian Marco Iodice6c113ed2023-01-19 17:14:26 +000043
44/** Print the given message then throw an std::runtime_error.
45 *
46 * @param[in] msg Message to display.
47 */
Nikolaj Jensenacea4072023-07-03 09:44:42 +010048#define COMPUTE_KERNEL_WRITER_ERROR_ON_MSG(msg) \
49 do \
50 { \
51 const std::string arg0(__FILE__); \
52 const std::string arg1(__func__); \
53 const std::string arg2(std::to_string(__LINE__)); \
54 const std::string arg3(msg); \
Gian Marco Iodice6c113ed2023-01-19 17:14:26 +000055 std::runtime_error(create_error_msg(arg0, arg1, arg2, arg3)); \
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010056 } while (false)
Gian Marco Iodice6c113ed2023-01-19 17:14:26 +000057
Viet-Hoa Do3389f532023-07-05 17:36:40 +010058/** Mark the variables as unused.
59 *
60 * @param[in] ... Variables which are unused.
61 */
62#define CKW_UNUSED(...) ckw::ignore_unused(__VA_ARGS__) // NOLINT
63
64/** Mark the variables as unused.
65 *
66 * @param[in] ... Variables which are unused.
67 */
68template <typename... T>
69inline void ignore_unused(T &&...)
70{
71}
72
73/** Throw an std::runtime_error with the specified message.
74 *
75 * @param[in] msg The error message.
76 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010077#define CKW_THROW_MSG(msg) \
78 do \
79 { \
80 const std::string file(__FILE__); \
81 const std::string func(__func__); \
82 const std::string line(std::to_string(__LINE__)); \
83 const std::string message(msg); \
84 \
Viet-Hoa Do3389f532023-07-05 17:36:40 +010085 throw std::runtime_error(ckw::create_error_msg(file, func, line, message)); \
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010086 } while (false)
Viet-Hoa Do3389f532023-07-05 17:36:40 +010087
88#ifdef COMPUTE_KERNEL_WRITER_ASSERTS_ENABLED
89
Gian Marco Iodiceebfdb5a2023-07-07 11:25:57 +010090/** If the condition is not met, throw an std::runtime_error with the specified message if assertion is enabled.
Viet-Hoa Do3389f532023-07-05 17:36:40 +010091 *
92 * @param[in] cond The condition that is expected to be true.
93 * @param[in] msg The error message when the condition is not met.
94 */
95#define CKW_ASSERT_MSG(cond, msg) \
96 do \
97 { \
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010098 if (!(cond)) \
Viet-Hoa Do3389f532023-07-05 17:36:40 +010099 { \
100 CKW_THROW_MSG(msg); \
101 } \
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100102 } while (false)
Viet-Hoa Do3389f532023-07-05 17:36:40 +0100103
Gian Marco Iodiceebfdb5a2023-07-07 11:25:57 +0100104#else // COMPUTE_KERNEL_WRITER_ASSERTS_ENABLED
105
106#define CKW_ASSERT_MSG(cond, msg)
107
108#endif // COMPUTE_KERNEL_WRITER_ASSERTS_ENABLED
109
110/** If the condition is not met, throw an std::runtime_error if assertion is enabled.
Viet-Hoa Do3389f532023-07-05 17:36:40 +0100111 *
112 * @param[in] cond The condition that is expected to be true.
113 */
114#define CKW_ASSERT(cond) CKW_ASSERT_MSG(cond, #cond)
115
Viet-Hoa Doe1c3b462023-07-31 17:13:34 +0100116/** If the precondition is met but the condition is not met, throw an std::runtime_error if assertion is enabled.
117 *
118 * @param[in] precond The precondition that triggers the check.
119 * @param[in] cond The condition that is expected to be true if precondition is true.
120 */
121#define CKW_ASSERT_IF(precond, cond) CKW_ASSERT(!(precond) || (cond))
122
Gian Marco Iodiceebfdb5a2023-07-07 11:25:57 +0100123/** Throw an std::runtime_error with the specified message if assertion is enabled.
124 *
125 * @param[in] msg The error message when the condition is not met.
126 */
127#define CKW_ASSERT_FAILED_MSG(msg) CKW_ASSERT_MSG(false, msg)
Viet-Hoa Do3389f532023-07-05 17:36:40 +0100128
Gian Marco Iodice6c113ed2023-01-19 17:14:26 +0000129} // namespace ckw
130
Viet-Hoa Do3389f532023-07-05 17:36:40 +0100131#endif // CKW_INCLUDE_CKW_ERROR_H