PkmnLibAI/CInterface/Core.hpp

56 lines
3.1 KiB
C++

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