Skip to content

Commit

Permalink
Define custom throw for nlohman::json when LDTK_NO_THROW is defined
Browse files Browse the repository at this point in the history
Nlohmann json library allows defining JSON_THROW_USER macro to override the default throw macro when JSON_NOEXCEPTION is defined.
  • Loading branch information
Madour committed Mar 16, 2024
1 parent d0112b0 commit 2ce2c36
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
8 changes: 5 additions & 3 deletions include/LDtkLoader/Utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@
#include "LDtkLoader/DataTypes.hpp"

#ifdef LDTK_NO_THROW
#include <exception>
#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

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);
}
1 change: 1 addition & 0 deletions src/Project.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <fstream>
#include <istream>

#include "LDtkLoader/Utils.hpp"
#include "LDtkLoader/World.hpp"
#include "LDtkLoader/generated/Version.hpp"
#include "json.hpp"
Expand Down
8 changes: 8 additions & 0 deletions src/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit 2ce2c36

Please sign in to comment.