Gian Marco Iodice | 6c113ed | 2023-01-19 17:14:26 +0000 | [diff] [blame] | 1 | /* |
| 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 Do | 3389f53 | 2023-07-05 17:36:40 +0100 | [diff] [blame] | 24 | #ifndef CKW_INCLUDE_CKW_ERROR_H |
| 25 | #define CKW_INCLUDE_CKW_ERROR_H |
Gian Marco Iodice | 6c113ed | 2023-01-19 17:14:26 +0000 | [diff] [blame] | 26 | |
Viet-Hoa Do | ce3c48c | 2023-07-03 13:44:43 +0100 | [diff] [blame] | 27 | #include <stdexcept> |
Nikolaj Jensen | acea407 | 2023-07-03 09:44:42 +0100 | [diff] [blame] | 28 | #include <string> |
Gian Marco Iodice | 6c113ed | 2023-01-19 17:14:26 +0000 | [diff] [blame] | 29 | |
| 30 | namespace 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 Jensen | acea407 | 2023-07-03 09:44:42 +0100 | [diff] [blame] | 41 | std::string |
| 42 | create_error_msg(const std::string &file, const std::string &func, const std::string &line, const std::string &msg); |
Gian Marco Iodice | 6c113ed | 2023-01-19 17:14:26 +0000 | [diff] [blame] | 43 | |
| 44 | /** Print the given message then throw an std::runtime_error. |
| 45 | * |
| 46 | * @param[in] msg Message to display. |
| 47 | */ |
Nikolaj Jensen | acea407 | 2023-07-03 09:44:42 +0100 | [diff] [blame] | 48 | #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 Iodice | 6c113ed | 2023-01-19 17:14:26 +0000 | [diff] [blame] | 55 | std::runtime_error(create_error_msg(arg0, arg1, arg2, arg3)); \ |
| 56 | } while(false) |
| 57 | |
Viet-Hoa Do | 3389f53 | 2023-07-05 17:36:40 +0100 | [diff] [blame] | 58 | /** 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 | */ |
| 68 | template <typename... T> |
| 69 | inline 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 | */ |
| 77 | #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 | \ |
| 85 | throw std::runtime_error(ckw::create_error_msg(file, func, line, message)); \ |
| 86 | } while(false) |
| 87 | |
| 88 | #ifdef COMPUTE_KERNEL_WRITER_ASSERTS_ENABLED |
| 89 | |
Gian Marco Iodice | ebfdb5a | 2023-07-07 11:25:57 +0100 | [diff] [blame] | 90 | /** If the condition is not met, throw an std::runtime_error with the specified message if assertion is enabled. |
Viet-Hoa Do | 3389f53 | 2023-07-05 17:36:40 +0100 | [diff] [blame] | 91 | * |
| 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 | { \ |
| 98 | if(!(cond)) \ |
| 99 | { \ |
| 100 | CKW_THROW_MSG(msg); \ |
| 101 | } \ |
| 102 | } while(false) |
| 103 | |
Gian Marco Iodice | ebfdb5a | 2023-07-07 11:25:57 +0100 | [diff] [blame] | 104 | #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 Do | 3389f53 | 2023-07-05 17:36:40 +0100 | [diff] [blame] | 111 | * |
| 112 | * @param[in] cond The condition that is expected to be true. |
| 113 | */ |
| 114 | #define CKW_ASSERT(cond) CKW_ASSERT_MSG(cond, #cond) |
| 115 | |
Gian Marco Iodice | ebfdb5a | 2023-07-07 11:25:57 +0100 | [diff] [blame] | 116 | /** Throw an std::runtime_error with the specified message if assertion is enabled. |
| 117 | * |
| 118 | * @param[in] msg The error message when the condition is not met. |
| 119 | */ |
| 120 | #define CKW_ASSERT_FAILED_MSG(msg) CKW_ASSERT_MSG(false, msg) |
Viet-Hoa Do | 3389f53 | 2023-07-05 17:36:40 +0100 | [diff] [blame] | 121 | |
Gian Marco Iodice | 6c113ed | 2023-01-19 17:14:26 +0000 | [diff] [blame] | 122 | } // namespace ckw |
| 123 | |
Viet-Hoa Do | 3389f53 | 2023-07-05 17:36:40 +0100 | [diff] [blame] | 124 | #endif // CKW_INCLUDE_CKW_ERROR_H |