Arbutils/CInterface/Core.hpp

42 lines
2.4 KiB
C++

#ifndef ARBUTILS_CORE_HPP
#define ARBUTILS_CORE_HPP
#include <exception>
#include <string>
#include "../src/Exception.hpp"
#define export extern "C" [[maybe_unused]]
#define ArbutilsException 3;
/// @brief Handler class for storing C++ exceptions, and passing them over a C interface.
class ExceptionHandler {
static std::string _ArbutilsLastException;
public:
/// @brief Stores the last found exception. Overrides previously last found exception.
/// @param e The last found exception as Arbutils exception.
static void SetLastException(const ArbUt::Exception& e) {
_ArbutilsLastException = std::string(e.what()) + "\n" + e.GetStacktrace();
}
/// @brief Stores the last found exception. Overrides previously last found exception.
/// @param e The last found exception as std exception.
static void SetLastException(const std::exception& e) { _ArbutilsLastException = std::string(e.what()); }
/// @brief Returns the last found exception as null-terminated C string.
/// @return A null-terminated c string detailing the last found exception.
static const char* GetLastException() { return _ArbutilsLastException.c_str(); }
};
#define Try(data) \
try { \
data; \
return 0; \
} catch (const ArbUt::Exception& e) { \
ExceptionHandler::SetLastException(e); \
return ArbutilsException; \
} catch (const std::exception& e) { \
ExceptionHandler::SetLastException(e); \
return ArbutilsException; \
}
#endif // ARBUTILS_CORE_HPP