/home/runner/work/HiCR/HiCR/include/hicr/core/exceptions.hpp Source File

HiCR: /home/runner/work/HiCR/HiCR/include/hicr/core/exceptions.hpp Source File
HiCR
exceptions.hpp
Go to the documentation of this file.
1/*
2 * Copyright 2025 Huawei Technologies Co., Ltd.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
24#pragma once
25
26#include <cstdlib>
27#include <cstdarg>
28#include <stdexcept>
29#include <string>
30#include <cstdio>
31#include <hicr/core/definitions.hpp>
32
33namespace HiCR
34{
35
36namespace exceptions
37{
38
59
60} // namespace exceptions
61
67#define HICR_THROW_LOGIC(...) [[unlikely]] HiCR::throwException(HiCR::exceptions::exception_t::logic, __FILE__, __LINE__, __VA_ARGS__)
68
74#define HICR_THROW_RUNTIME(...) [[unlikely]] HiCR::throwException(HiCR::exceptions::exception_t::runtime, __FILE__, __LINE__, __VA_ARGS__)
75
81#define HICR_THROW_FATAL(...) [[unlikely]] HiCR::throwException(HiCR::exceptions::exception_t::fatal, __FILE__, __LINE__, __VA_ARGS__)
82
95class LogicException : public std::logic_error
96{
97 public:
98
104 __INLINE__ LogicException(const char *const message)
105 : logic_error(message)
106 {}
107};
108
121class RuntimeException : public std::runtime_error
122{
123 public:
124
130 __INLINE__ RuntimeException(const char *const message)
131 : runtime_error(message)
132 {}
133};
134
141class FatalException : public std::runtime_error
142{
143 public:
144
150 __INLINE__ FatalException(const char *const message)
151 : runtime_error(message)
152 {}
153};
154
164__INLINE__ static void throwException [[noreturn]] (const exceptions::exception_t type, const char *fileName, const int lineNumber, const char *format, ...)
165{
166 char *outstr = nullptr;
167 va_list ap;
168 va_start(ap, format);
169 auto res = vasprintf(&outstr, format, ap);
170
171 if (res < 0)
172 {
173 auto errorMsg = std::string("Error in exceptions.hpp, throwLogic() function\n");
174
175#ifdef HICR_EXCEPTION_USE_STDEXCEPTION
176 throw std::runtime_error(errorMsg.c_str());
177#endif
178
179#ifdef HICR_EXCEPTION_USE_ABORT
180 fprintf(stderr, "%s", errorMsg.c_str());
181 fflush(stderr);
182 std::abort();
183#endif
184 }
185
186 std::string typeString = "Undefined";
187 switch (type)
188 {
189 case exceptions::exception_t::logic: typeString = "Logic"; break;
190 case exceptions::exception_t::runtime: typeString = "Runtime"; break;
191 case exceptions::exception_t::fatal: typeString = "Fatal"; break;
192 default: break;
193 }
194 std::string outString = std::string("[HiCR] ") + typeString + std::string(" Exception: ") + std::string(outstr);
195 free(outstr);
196
197 const size_t bufferSize = 1024;
198 char info[bufferSize];
199 snprintf(info, sizeof(info) - 1, " + From %s:%d\n", fileName, lineNumber);
200 outString += info;
201
202#ifdef HICR_EXCEPTION_USE_STDEXCEPTION
203
204 switch (type)
205 {
206 case exceptions::exception_t::logic: throw LogicException(outString.c_str()); break;
207 case exceptions::exception_t::runtime: throw RuntimeException(outString.c_str()); break;
208 case exceptions::exception_t::fatal: throw FatalException(outString.c_str()); break;
209 default: break;
210 }
211
212 throw std::runtime_error(outString.c_str());
213
214#endif
215
216 // #ifdef HICR_EXCEPTION_USE_ABORT
217
218 fprintf(stderr, "%s", outString.c_str());
219 fflush(stderr);
220 std::abort();
221
222 // #endif
223}
224
225} // namespace HiCR
Definition exceptions.hpp:142
__INLINE__ FatalException(const char *const message)
Definition exceptions.hpp:150
Definition exceptions.hpp:96
__INLINE__ LogicException(const char *const message)
Definition exceptions.hpp:104
Definition exceptions.hpp:122
__INLINE__ RuntimeException(const char *const message)
Definition exceptions.hpp:130
exception_t
Definition exceptions.hpp:43
@ fatal
Definition exceptions.hpp:57
@ logic
Definition exceptions.hpp:47
@ runtime
Definition exceptions.hpp:52