CreatureLib/CInterface/Core.hpp

65 lines
4.0 KiB
C++
Raw Normal View History

#ifndef CREATURELIB_CORE_HPP
#define CREATURELIB_CORE_HPP
#include <Arbutils/Exception.hpp>
#include <cstring>
#include <exception>
#include <sstream>
#include <string>
#include "../src/Defines.hpp"
2022-04-02 10:33:26 +00:00
#define export_func extern "C" [[maybe_unused]]
#define CreatureLibException 2;
class ExceptionHandler {
2020-07-28 17:37:03 +00:00
static std::string _creatureLibLastException;
static std::string _creatureLibLastExceptionStacktrace;
public:
static void SetLastArbUtException(const std::string& function, const ArbUt::Exception& e, size_t depth = 6) {
std::stringstream ss;
ss << "[" << function << "] " << e.what();
_creatureLibLastException = ss.str();
_creatureLibLastExceptionStacktrace = e.GetStacktrace(depth, true);
if (_creatureLibLastExceptionStacktrace.empty())
_creatureLibLastExceptionStacktrace = "err";
}
static void SetLastException(const std::string& function, const std::exception& e) {
std::stringstream ss;
2020-07-26 15:29:06 +00:00
ss << "[" << function << "] " << e.what();
2020-07-28 17:37:03 +00:00
_creatureLibLastException = ss.str();
_creatureLibLastExceptionStacktrace = "Exception did not have a stacktrace.";
}
2020-07-28 17:37:03 +00:00
static const char* GetLastException() { return _creatureLibLastException.c_str(); }
static const char* GetLastExceptionStacktrace() { return _creatureLibLastExceptionStacktrace.c_str(); }
};
#define Try(data) \
try { \
data; \
return 0; \
} catch (const ArbUt::Exception& e) { \
ExceptionHandler::SetLastArbUtException(__FUNCTION__, e); \
return CreatureLibException; \
} catch (const std::exception& e) { \
2020-07-26 15:29:06 +00:00
ExceptionHandler::SetLastException(__FUNCTION__, e); \
return CreatureLibException; \
}
#define SIMPLE_GET_FUNC(type, name, returnType) \
2022-04-02 10:33:26 +00:00
export_func returnType CreatureLib_##type##_##name(const type* p) { return p->name(); }
#define BORROWED_GET_FUNC(type, name, returnType) \
2022-04-02 10:33:26 +00:00
export_func returnType CreatureLib_##type##_##name(const type* p) { return p->name().GetRaw(); }
#define OPTIONAL_GET_FUNC(type, name, returnType) \
export_func returnType CreatureLib_##type##_##name(type* p) { \
if (p->name().HasValue()) \
return p->name().GetValue(); \
return nullptr; \
}
#define SMART_GET_FUNC(type, name, returnType) \
2022-04-02 10:33:26 +00:00
export_func returnType CreatureLib_##type##_##name(const type* p) { return p->name().get(); }
#define DESTRUCTOR(type) \
2022-04-02 10:33:26 +00:00
export_func void CreatureLib_##type##_Destruct(const type* p) { delete p; }
#endif // CREATURELIB_CORE_HPP