CreatureLib/CInterface/Core.hpp

42 lines
2.2 KiB
C++

#ifndef CREATURELIB_CORE_HPP
#define CREATURELIB_CORE_HPP
#include <cstring>
#include <exception>
#include <sstream>
#include <string>
#define export extern "C" [[maybe_unused]]
#define CreatureLibException 1;
#define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
class ExceptionHandler {
static std::string _lastException;
public:
static void SetLastException(std::string file, std::string function, int line, const std::exception& e) {
std::stringstream ss;
ss << "[" << file << ", " << function << "-" << line << "] " << e.what();
_lastException = ss.str();
}
static const char* GetLastException() { return _lastException.c_str(); }
};
#define Try(data) \
try { \
data; \
return 0; \
} catch (const std::exception& e) { \
ExceptionHandler::SetLastException(__FILENAME__, __FUNCTION__, __LINE__, e); \
return CreatureLibException; \
}
#define SIMPLE_GET_FUNC(type, name, returnType) \
export returnType CreatureLib_##type##_##name(const type* p) { return p->name(); }
#define BORROWED_GET_FUNC(type, name, returnType) \
export returnType CreatureLib_##type##_##name(const type* p) { return p->name().GetRaw(); }
#define SMART_GET_FUNC(type, name, returnType) \
export returnType CreatureLib_##type##_##name(const type* p) { return p->name().get(); }
#endif // CREATURELIB_CORE_HPP