PkmnLib/CInterface/Core.hpp

56 lines
3.1 KiB
C++
Raw Normal View History

#ifndef PKMNLIB_CORE_HPP
#define PKMNLIB_CORE_HPP
#include <Arbutils/Exception.hpp>
#include <cstring>
#include <exception>
2021-10-30 14:30:57 +00:00
#include <sstream>
#include <string>
2022-04-02 11:03:11 +00:00
#define export_func extern "C" [[maybe_unused]]
2020-07-30 18:06:41 +00:00
#define PkmnLibException 4
class ExceptionHandler {
2020-07-30 18:06:41 +00:00
static std::string _pkmnLibLastException;
2021-01-22 13:11:03 +00:00
static std::string _pkmnLibLastExceptionStacktrace;
public:
2021-01-22 13:11:03 +00:00
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.";
}
2020-07-30 18:06:41 +00:00
static const char* GetLastException() { return _pkmnLibLastException.c_str(); }
2021-01-22 13:11:03 +00:00
static const char* GetLastExceptionStacktrace() { return _pkmnLibLastExceptionStacktrace.c_str(); }
};
#define Try(data) \
try { \
data; \
return 0; \
2021-01-22 13:11:03 +00:00
} catch (const ArbUt::Exception& e) { \
ExceptionHandler::SetLastArbUtException(__FUNCTION__, e); \
2021-03-26 18:14:22 +00:00
return PkmnLibException; \
} catch (const std::exception& e) { \
2021-01-22 13:11:03 +00:00
ExceptionHandler::SetLastException(__FUNCTION__, e); \
2021-03-26 18:14:22 +00:00
return PkmnLibException; \
}
#define SIMPLE_GET_FUNC(type, name, returnType) \
2022-04-02 11:03:11 +00:00
export_func returnType PkmnLib_##type##_##name(const type* p) { return p->name(); }
#define SIMPLE_GET_FUNC_SMART_PTR(type, name, returnType) \
2022-04-02 11:03:11 +00:00
export_func returnType PkmnLib_##type##_##name(const type* p) { return p->name().GetRaw(); }
2020-08-11 17:36:18 +00:00
#define DESTRUCTOR(type) \
2022-04-02 11:03:11 +00:00
export_func void PkmnLib_##type##_Destruct(const type* p) { delete p; }
#endif // PKMNLIB_CORE_HPP