2020-03-25 18:07:36 +00:00
|
|
|
#ifndef CREATURELIB_CORE_HPP
|
|
|
|
#define CREATURELIB_CORE_HPP
|
|
|
|
|
2020-08-15 13:10:48 +00:00
|
|
|
#include <Arbutils/Exception.hpp>
|
2020-03-25 18:07:36 +00:00
|
|
|
#include <cstring>
|
|
|
|
#include <exception>
|
2020-07-26 15:22:36 +00:00
|
|
|
#include <sstream>
|
2020-03-25 18:07:36 +00:00
|
|
|
#include <string>
|
2022-03-23 13:19:07 +00:00
|
|
|
#include "../src/Defines.hpp"
|
2022-04-02 10:33:26 +00:00
|
|
|
#define export_func extern "C" [[maybe_unused]]
|
2020-03-25 18:07:36 +00:00
|
|
|
|
2020-07-31 08:51:03 +00:00
|
|
|
#define CreatureLibException 2;
|
2020-04-14 13:43:30 +00:00
|
|
|
|
2020-03-25 18:07:36 +00:00
|
|
|
class ExceptionHandler {
|
2020-07-28 17:37:03 +00:00
|
|
|
static std::string _creatureLibLastException;
|
2020-08-15 14:34:52 +00:00
|
|
|
static std::string _creatureLibLastExceptionStacktrace;
|
2020-03-25 18:07:36 +00:00
|
|
|
|
|
|
|
public:
|
2020-08-15 15:47:01 +00:00
|
|
|
static void SetLastArbUtException(const std::string& function, const ArbUt::Exception& e, size_t depth = 6) {
|
2020-08-15 13:10:48 +00:00
|
|
|
std::stringstream ss;
|
2020-08-15 15:47:01 +00:00
|
|
|
ss << "[" << function << "] " << e.what();
|
2020-08-15 13:10:48 +00:00
|
|
|
_creatureLibLastException = ss.str();
|
2020-08-15 15:47:01 +00:00
|
|
|
_creatureLibLastExceptionStacktrace = e.GetStacktrace(depth, true);
|
2020-08-16 15:41:56 +00:00
|
|
|
if (_creatureLibLastExceptionStacktrace.empty())
|
|
|
|
_creatureLibLastExceptionStacktrace = "err";
|
2020-08-15 13:10:48 +00:00
|
|
|
}
|
2020-08-15 14:34:52 +00:00
|
|
|
static void SetLastException(const std::string& function, const std::exception& e) {
|
2020-07-26 15:22:36 +00:00
|
|
|
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();
|
2020-08-16 15:41:56 +00:00
|
|
|
_creatureLibLastExceptionStacktrace = "Exception did not have a stacktrace.";
|
2020-07-26 15:22:36 +00:00
|
|
|
}
|
2020-07-28 17:37:03 +00:00
|
|
|
static const char* GetLastException() { return _creatureLibLastException.c_str(); }
|
2020-08-15 14:34:52 +00:00
|
|
|
static const char* GetLastExceptionStacktrace() { return _creatureLibLastExceptionStacktrace.c_str(); }
|
2020-03-25 18:07:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#define Try(data) \
|
|
|
|
try { \
|
|
|
|
data; \
|
|
|
|
return 0; \
|
2020-08-15 13:10:48 +00:00
|
|
|
} catch (const ArbUt::Exception& e) { \
|
2020-08-15 14:34:52 +00:00
|
|
|
ExceptionHandler::SetLastArbUtException(__FUNCTION__, e); \
|
2020-08-15 13:10:48 +00:00
|
|
|
return CreatureLibException; \
|
2020-03-25 18:07:36 +00:00
|
|
|
} catch (const std::exception& e) { \
|
2020-07-26 15:29:06 +00:00
|
|
|
ExceptionHandler::SetLastException(__FUNCTION__, e); \
|
2020-07-31 08:51:03 +00:00
|
|
|
return CreatureLibException; \
|
2020-03-25 18:07:36 +00:00
|
|
|
}
|
|
|
|
|
2020-07-07 13:33:43 +00:00
|
|
|
#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(); }
|
2020-07-07 13:33:43 +00:00
|
|
|
#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(); }
|
2020-12-12 11:22:48 +00:00
|
|
|
#define OPTIONAL_GET_FUNC(type, name, returnType) \
|
2022-05-14 14:07:32 +00:00
|
|
|
export_func returnType CreatureLib_##type##_##name(type* p) { \
|
2020-12-12 11:22:48 +00:00
|
|
|
if (p->name().HasValue()) \
|
|
|
|
return p->name().GetValue(); \
|
|
|
|
return nullptr; \
|
|
|
|
}
|
2020-07-07 13:33:43 +00:00
|
|
|
#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(); }
|
2020-08-07 09:51:43 +00:00
|
|
|
#define DESTRUCTOR(type) \
|
2022-04-02 10:33:26 +00:00
|
|
|
export_func void CreatureLib_##type##_Destruct(const type* p) { delete p; }
|
2020-07-07 13:33:43 +00:00
|
|
|
|
2020-03-25 18:07:36 +00:00
|
|
|
#endif // CREATURELIB_CORE_HPP
|