From 2ab9445b71edc125f2fbef9238a7a4ea3cdbbecc Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Tue, 14 Apr 2020 16:36:54 +0200 Subject: [PATCH] Implements CInterface core, C Interface for LibrarySettings. --- CInterface/Core.cpp | 4 ++++ CInterface/Core.hpp | 29 ++++++++++++++++++++++++++ CInterface/Library/LibrarySettings.cpp | 16 ++++++++++++++ CMakeLists.txt | 9 +++++++- src/Library/LibrarySettings.hpp | 4 ++-- src/Library/PokemonLibrary.cpp | 1 - 6 files changed, 59 insertions(+), 4 deletions(-) create mode 100644 CInterface/Core.cpp create mode 100644 CInterface/Core.hpp create mode 100644 CInterface/Library/LibrarySettings.cpp delete mode 100644 src/Library/PokemonLibrary.cpp diff --git a/CInterface/Core.cpp b/CInterface/Core.cpp new file mode 100644 index 0000000..0145855 --- /dev/null +++ b/CInterface/Core.cpp @@ -0,0 +1,4 @@ +#include "Core.hpp" + +std::string ExceptionHandler::_lastException = ""; +export const char* PkmnLib_GetLastException() { return ExceptionHandler::GetLastException(); } \ No newline at end of file diff --git a/CInterface/Core.hpp b/CInterface/Core.hpp new file mode 100644 index 0000000..2ff53d9 --- /dev/null +++ b/CInterface/Core.hpp @@ -0,0 +1,29 @@ +#ifndef PKMNLIB_CORE_HPP +#define PKMNLIB_CORE_HPP + +#include +#include +#include +#define export extern "C" + +// CreatureLibException is 1, so use 2. +#define PkmnLibException 2 + +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 PkmnLibException; \ + } + +#endif // PKMNLIB_CORE_HPP diff --git a/CInterface/Library/LibrarySettings.cpp b/CInterface/Library/LibrarySettings.cpp new file mode 100644 index 0000000..bf76761 --- /dev/null +++ b/CInterface/Library/LibrarySettings.cpp @@ -0,0 +1,16 @@ +#include "../../src/Library/LibrarySettings.hpp" +#include "../Core.hpp" +using namespace PkmnLib::Library; + +export const LibrarySettings* PkmnLib_LibrarySettings_Construct(uint8_t maximalLevel, uint8_t maximalMoves, uint16_t shinyRate) { + return new LibrarySettings(maximalLevel, maximalMoves, shinyRate); +} + +export void PkmnLib_LibrarySettings_Destruct(const LibrarySettings* p) { delete p; } + +#define SIMPLE_GET_FUNC(type, name, returnType) \ + export returnType PkmnLib_##type##_##name(const PkmnLib::Library::type* p) { return p->name(); } + +SIMPLE_GET_FUNC(LibrarySettings, GetShinyRate, uint16_t); + +#undef SIMPLE_GET_FUNC \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index e8f356d..b9789d7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -56,6 +56,11 @@ SET(FILE_SOURCE "src/Battling/*.hpp" "src/Library/*.cpp" "src/Library/*.hpp" + "CInterface/Core.*" + "CInterface/Library/*.cpp" + "CInterface/Library/*.hpp" + "CInterface/Battling/*.cpp" + "CInterface/Battling/*.hpp" ) if (SCRIPT_PROVIDER STREQUAL "angelscript") SET(FILE_SOURCE ${FILE_SOURCE} @@ -63,7 +68,9 @@ if (SCRIPT_PROVIDER STREQUAL "angelscript") "src/ScriptResolving/AngelScript/*.hpp" "extern/angelscript_addons/*.cpp" "extern/angelscript_addons/*.h" - ) + "CInterface/AngelScript/*.cpp" + "CInterface/AngelScript/*.hpp" + ) ADD_DEFINITIONS(-D AS_USE_ACCESSORS=1) endif() message(STATUS "${FILE_SOURCE}") diff --git a/src/Library/LibrarySettings.hpp b/src/Library/LibrarySettings.hpp index 01e90da..631c35c 100644 --- a/src/Library/LibrarySettings.hpp +++ b/src/Library/LibrarySettings.hpp @@ -8,10 +8,10 @@ namespace PkmnLib::Library { uint16_t _shinyRate; public: - LibrarySettings(uint8_t maximalLevel, uint8_t maximalMoves, uint16_t shinyRate) + LibrarySettings(uint8_t maximalLevel, uint8_t maximalMoves, uint16_t shinyRate) noexcept : CreatureLib::Library::LibrarySettings(maximalLevel, maximalMoves), _shinyRate(shinyRate) {} - uint16_t GetShinyRate() const { return _shinyRate; } + uint16_t GetShinyRate() const noexcept { return _shinyRate; } }; } diff --git a/src/Library/PokemonLibrary.cpp b/src/Library/PokemonLibrary.cpp deleted file mode 100644 index 8c8ed63..0000000 --- a/src/Library/PokemonLibrary.cpp +++ /dev/null @@ -1 +0,0 @@ -#include "PokemonLibrary.hpp"