From 2ce2c365ed3631999ae3ffcc5d8e266b37ff3ced Mon Sep 17 00:00:00 2001 From: Modar Nasser Date: Sat, 16 Mar 2024 18:42:36 +0100 Subject: [PATCH] Define custom throw for nlohman::json when LDTK_NO_THROW is defined Nlohmann json library allows defining JSON_THROW_USER macro to override the default throw macro when JSON_NOEXCEPTION is defined. --- include/LDtkLoader/Utils.hpp | 8 +++++--- src/Project.cpp | 1 + src/Utils.cpp | 8 ++++++++ 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/include/LDtkLoader/Utils.hpp b/include/LDtkLoader/Utils.hpp index b48cdcd..753ff97 100644 --- a/include/LDtkLoader/Utils.hpp +++ b/include/LDtkLoader/Utils.hpp @@ -8,7 +8,10 @@ #include "LDtkLoader/DataTypes.hpp" #ifdef LDTK_NO_THROW + #include #define ldtk_error(msg) do{ldtk::print_error(__FUNCTION__, msg);exit(EXIT_FAILURE);}while(0) + #define ldtk_json_error(ex) do{ldtk::print_json_error(ex.what());exit(EXIT_FAILURE);}while(0) + #define JSON_THROW_USER ldtk_json_error // override nlohmann::json throw macro #else #define ldtk_error(msg) throw std::invalid_argument("LDtkLoader exception (in "+std::string(__FUNCTION__)+") : "+std::string(msg)) #endif @@ -16,7 +19,6 @@ namespace ldtk { auto getLayerTypeFromString(const std::string& type_name) -> LayerType; - inline void print_error(const std::string& fn, const std::string& msg) { - std::cerr << "LDtkLoader Error (in " << fn << ") : " << msg << std::endl; - } + void print_error(const std::string& fn, const std::string& msg); + void print_json_error(const std::string& msg); } diff --git a/src/Project.cpp b/src/Project.cpp index d97e524..74f4045 100644 --- a/src/Project.cpp +++ b/src/Project.cpp @@ -5,6 +5,7 @@ #include #include +#include "LDtkLoader/Utils.hpp" #include "LDtkLoader/World.hpp" #include "LDtkLoader/generated/Version.hpp" #include "json.hpp" diff --git a/src/Utils.cpp b/src/Utils.cpp index 23488cc..537e095 100644 --- a/src/Utils.cpp +++ b/src/Utils.cpp @@ -14,3 +14,11 @@ auto ldtk::getLayerTypeFromString(const std::string& type_name) -> ldtk::LayerTy return LayerType::AutoLayer; return LayerType::Tiles; } + +void ldtk::print_error(const std::string& fn, const std::string& msg) { + std::cerr << "LDtkLoader Error (in " << fn << ") : " << msg << std::endl; +} + +void ldtk::print_json_error(const std::string& msg) { + std::cerr << "LDtkLoader Json Error : " << msg << std::endl; +}