2020-07-19 09:08:05 +00:00
|
|
|
#ifndef ARBUTILS_CORE_HPP
|
|
|
|
#define ARBUTILS_CORE_HPP
|
|
|
|
|
|
|
|
#include <exception>
|
|
|
|
#include <string>
|
2020-08-15 12:52:15 +00:00
|
|
|
#include "../src/Exception.hpp"
|
2022-04-01 10:05:48 +00:00
|
|
|
#define export_func extern "C" [[maybe_unused]]
|
2020-07-19 09:08:05 +00:00
|
|
|
|
|
|
|
#define ArbutilsException 3;
|
|
|
|
|
2020-09-25 10:31:37 +00:00
|
|
|
/// @brief Handler class for storing C++ exceptions, and passing them over a C interface.
|
2020-07-19 09:08:05 +00:00
|
|
|
class ExceptionHandler {
|
2020-07-28 17:24:35 +00:00
|
|
|
static std::string _ArbutilsLastException;
|
2020-07-19 09:08:05 +00:00
|
|
|
|
|
|
|
public:
|
2020-09-25 10:31:37 +00:00
|
|
|
/// @brief Stores the last found exception. Overrides previously last found exception.
|
|
|
|
/// @param e The last found exception as Arbutils exception.
|
2020-08-15 12:52:15 +00:00
|
|
|
static void SetLastException(const ArbUt::Exception& e) {
|
|
|
|
_ArbutilsLastException = std::string(e.what()) + "\n" + e.GetStacktrace();
|
|
|
|
}
|
2020-09-25 10:31:37 +00:00
|
|
|
/// @brief Stores the last found exception. Overrides previously last found exception.
|
|
|
|
/// @param e The last found exception as std exception.
|
2020-07-28 16:35:42 +00:00
|
|
|
static void SetLastException(const std::exception& e) { _ArbutilsLastException = std::string(e.what()); }
|
2020-09-25 10:31:37 +00:00
|
|
|
/// @brief Returns the last found exception as null-terminated C string.
|
|
|
|
/// @return A null-terminated c string detailing the last found exception.
|
2020-07-28 16:35:42 +00:00
|
|
|
static const char* GetLastException() { return _ArbutilsLastException.c_str(); }
|
2020-07-19 09:08:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#define Try(data) \
|
|
|
|
try { \
|
|
|
|
data; \
|
|
|
|
return 0; \
|
2020-08-15 12:52:15 +00:00
|
|
|
} catch (const ArbUt::Exception& e) { \
|
|
|
|
ExceptionHandler::SetLastException(e); \
|
|
|
|
return ArbutilsException; \
|
2020-07-19 09:08:05 +00:00
|
|
|
} catch (const std::exception& e) { \
|
|
|
|
ExceptionHandler::SetLastException(e); \
|
|
|
|
return ArbutilsException; \
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // ARBUTILS_CORE_HPP
|