Exception handling for the C Interface.
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2020-07-19 11:08:05 +02:00
parent 0295bb949a
commit 287b50fb2f
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
4 changed files with 43 additions and 8 deletions

4
CInterface/Core.cpp Normal file
View File

@ -0,0 +1,4 @@
#include "Core.hpp"
std::string ExceptionHandler::_lastException = "";
export const char* Arbutils_C_GetLastException() { return ExceptionHandler::GetLastException(); }

28
CInterface/Core.hpp Normal file
View File

@ -0,0 +1,28 @@
#ifndef ARBUTILS_CORE_HPP
#define ARBUTILS_CORE_HPP
#include <cstring>
#include <exception>
#include <string>
#define export extern "C"
#define ArbutilsException 3;
class ExceptionHandler {
static std::string _lastException;
public:
static void SetLastException(const std::exception& e) { _lastException = std::string(e.what()); }
static const char* GetLastException() { return _lastException.c_str(); }
};
#define Try(data) \
try { \
data; \
return 0; \
} catch (const std::exception& e) { \
ExceptionHandler::SetLastException(e); \
return ArbutilsException; \
}
#endif // ARBUTILS_CORE_HPP

View File

@ -1,6 +1,5 @@
#include "../src/Random.hpp"
#define export extern "C"
#include "Core.hpp"
export ArbUt::Random* Arbutils_Random_Construct() { return new ArbUt::Random(); }
export ArbUt::Random* Arbutils_Random_ConstructWithSeed(uint_fast32_t seed) { return new ArbUt::Random(seed); }
@ -10,13 +9,17 @@ export void Arbutils_Random_Destruct(ArbUt::Random* p) { delete p; }
export float Arbutils_Random_GetFloat(ArbUt::Random* p) { return p->GetFloat(); }
export double Arbutils_Random_GetDouble(ArbUt::Random* p) { return p->GetDouble(); }
export int32_t Arbutils_Random_Get(ArbUt::Random* p) { return p->Get(); }
export int32_t Arbutils_Random_GetWithMax(ArbUt::Random* p, int32_t max) { return p->Get(max); }
export int32_t Arbutils_Random_GetInLimits(ArbUt::Random* p, int32_t min, int32_t max) { return p->Get(min, max); }
export uint8_t Arbutils_Random_GetWithMax(ArbUt::Random* p, int32_t max, int32_t& out) { Try(out = p->Get(max);) }
export uint8_t Arbutils_Random_GetInLimits(ArbUt::Random* p, int32_t min, int32_t max, int32_t& out) {
Try(out = p->Get(min, max);)
}
export uint32_t Arbutils_Random_GetUnsigned(ArbUt::Random* p) { return p->GetUnsigned(); }
export uint32_t Arbutils_Random_GetUnsignedWithMax(ArbUt::Random* p, uint32_t max) { return p->GetUnsigned(max); }
export uint32_t Arbutils_Random_GetUnsignedInLimits(ArbUt::Random* p, uint32_t min, uint32_t max) {
return p->GetUnsigned(min, max);
export uint8_t Arbutils_Random_GetUnsignedWithMax(ArbUt::Random* p, uint32_t max, uint32_t& out) {
Try(out = p->GetUnsigned(max);)
}
export uint8_t Arbutils_Random_GetUnsignedInLimits(ArbUt::Random* p, uint32_t min, uint32_t max, uint32_t& out) {
Try(out = p->GetUnsigned(min, max);)
}
export uint_fast32_t Arbutils_Random_GetSeed(ArbUt::Random* p) { return p->GetSeed(); }

View File

@ -18,7 +18,7 @@ if (SHARED)
endif (SHARED)
# Grab all cpp and hpp files in our source directories.
file(GLOB_RECURSE SRC_FILES "src/*.cpp" "src/*.hpp" "CInterface/*.cpp")
file(GLOB_RECURSE SRC_FILES "src/*.cpp" "src/*.hpp" "CInterface/*.cpp" "CInterface/*.hpp")
add_library(Arbutils ${LIBTYPE} ${SRC_FILES})
if (TESTS)