PkmnLib/CInterface/Core.hpp

56 lines
3.1 KiB
C++

#ifndef PKMNLIB_CORE_HPP
#define PKMNLIB_CORE_HPP
#include <Arbutils/Exception.hpp>
#include <cstring>
#include <exception>
#include <sstream>
#include <string>
#define export_func extern "C" [[maybe_unused]]
#define PkmnLibException 4
class ExceptionHandler {
static std::string _pkmnLibLastException;
static std::string _pkmnLibLastExceptionStacktrace;
public:
static void SetLastArbUtException(const std::string& function, const ArbUt::Exception& e, size_t depth = 6) {
std::stringstream ss;
ss << "[" << function << "] " << e.what();
_pkmnLibLastException = ss.str();
_pkmnLibLastExceptionStacktrace = e.GetStacktrace(depth, true);
if (_pkmnLibLastExceptionStacktrace.empty())
_pkmnLibLastExceptionStacktrace = "err";
}
static void SetLastException(const std::string& function, const std::exception& e) {
std::stringstream ss;
ss << "[" << function << "] " << e.what();
_pkmnLibLastException = ss.str();
_pkmnLibLastExceptionStacktrace = "Exception did not have a stacktrace.";
}
static const char* GetLastException() { return _pkmnLibLastException.c_str(); }
static const char* GetLastExceptionStacktrace() { return _pkmnLibLastExceptionStacktrace.c_str(); }
};
#define Try(data) \
try { \
data; \
return 0; \
} catch (const ArbUt::Exception& e) { \
ExceptionHandler::SetLastArbUtException(__FUNCTION__, e); \
return PkmnLibException; \
} catch (const std::exception& e) { \
ExceptionHandler::SetLastException(__FUNCTION__, e); \
return PkmnLibException; \
}
#define SIMPLE_GET_FUNC(type, name, returnType) \
export_func returnType PkmnLib_##type##_##name(const type* p) { return p->name(); }
#define SIMPLE_GET_FUNC_SMART_PTR(type, name, returnType) \
export_func returnType PkmnLib_##type##_##name(const type* p) { return p->name().GetRaw(); }
#define DESTRUCTOR(type) \
export_func void PkmnLib_##type##_Destruct(const type* p) { delete p; }
#endif // PKMNLIB_CORE_HPP