CreatureLib/CInterface/Core.hpp

44 lines
2.4 KiB
C++
Raw Normal View History

#ifndef CREATURELIB_CORE_HPP
#define CREATURELIB_CORE_HPP
#include <cstring>
#include <exception>
#include <sstream>
#include <string>
#define export extern "C" [[maybe_unused]]
#define CreatureLibException 2;
#define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
class ExceptionHandler {
2020-07-28 17:37:03 +00:00
static std::string _creatureLibLastException;
public:
2020-07-26 15:29:06 +00:00
static void SetLastException(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();
}
2020-07-28 17:37:03 +00:00
static const char* GetLastException() { return _creatureLibLastException.c_str(); }
};
#define Try(data) \
try { \
data; \
return 0; \
} 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) \
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(); }
#define DESTRUCTOR(type) \
export void CreatureLib_##type##_Destruct(const type* p) { delete p; }
#endif // CREATURELIB_CORE_HPP