From 287b50fb2f6867cd0a1133cb905a1cfcbf49b448 Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Sun, 19 Jul 2020 11:08:05 +0200 Subject: [PATCH] Exception handling for the C Interface. --- CInterface/Core.cpp | 4 ++++ CInterface/Core.hpp | 28 ++++++++++++++++++++++++++++ CInterface/Random.cpp | 17 ++++++++++------- CMakeLists.txt | 2 +- 4 files changed, 43 insertions(+), 8 deletions(-) create mode 100644 CInterface/Core.cpp create mode 100644 CInterface/Core.hpp diff --git a/CInterface/Core.cpp b/CInterface/Core.cpp new file mode 100644 index 0000000..f38b4a6 --- /dev/null +++ b/CInterface/Core.cpp @@ -0,0 +1,4 @@ +#include "Core.hpp" + +std::string ExceptionHandler::_lastException = ""; +export const char* Arbutils_C_GetLastException() { return ExceptionHandler::GetLastException(); } diff --git a/CInterface/Core.hpp b/CInterface/Core.hpp new file mode 100644 index 0000000..c01e58c --- /dev/null +++ b/CInterface/Core.hpp @@ -0,0 +1,28 @@ +#ifndef ARBUTILS_CORE_HPP +#define ARBUTILS_CORE_HPP + +#include +#include +#include +#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 diff --git a/CInterface/Random.cpp b/CInterface/Random.cpp index b217ebb..6b0ae42 100644 --- a/CInterface/Random.cpp +++ b/CInterface/Random.cpp @@ -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(); } \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 27c592c..dddb60e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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)