Tweaks for exception handling.
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2020-07-28 19:37:03 +02:00
parent e2e706693b
commit 13c4609cdd
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
5 changed files with 12 additions and 10 deletions

View File

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

View File

@ -11,15 +11,15 @@
#define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__) #define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
class ExceptionHandler { class ExceptionHandler {
static std::string _lastException; static std::string _creatureLibLastException;
public: public:
static void SetLastException(std::string function, const std::exception& e) { static void SetLastException(std::string function, const std::exception& e) {
std::stringstream ss; std::stringstream ss;
ss << "[" << function << "] " << e.what(); ss << "[" << function << "] " << e.what();
_lastException = ss.str(); _creatureLibLastException = ss.str();
} }
static const char* GetLastException() { return _lastException.c_str(); } static const char* GetLastException() { return _creatureLibLastException.c_str(); }
}; };
#define Try(data) \ #define Try(data) \

View File

@ -19,7 +19,7 @@ bool Battle::CanUse(const ArbUt::BorrowedPtr<BaseTurnChoice>& choice) {
} }
bool Battle::TrySetChoice(BaseTurnChoice* choice) { bool Battle::TrySetChoice(BaseTurnChoice* choice) {
AssertNotNull(choice) AssertNotNull(choice);
if (!CanUse(choice)) if (!CanUse(choice))
return false; return false;
choice->GetUser()->GetBattleSide()->SetChoice(choice); choice->GetUser()->GetBattleSide()->SetChoice(choice);

View File

@ -1,5 +1,6 @@
#include "CreateCreature.hpp" #include "CreateCreature.hpp"
#include <sstream>
#include <utility> #include <utility>
#include "../Library/BattleLibrary.hpp" #include "../Library/BattleLibrary.hpp"
@ -21,8 +22,9 @@ CreateCreature CreateCreature::WithGender(Library::Gender gender) {
} }
CreateCreature CreateCreature::WithAttack(const ArbUt::StringView& attackName, AttackLearnMethod learnMethod) { CreateCreature CreateCreature::WithAttack(const ArbUt::StringView& attackName, AttackLearnMethod learnMethod) {
if (_attacks.Count() >= _library->GetSettings()->GetMaximalMoves()) if (_attacks.Count() >= _library->GetSettings()->GetMaximalMoves()) {
THROW_CREATURE("You have already set the maximum amount of allowed moves."); THROW_CREATURE("You have already set the maximum amount of allowed moves.");
}
auto attackData = _library->GetAttackLibrary()->Get(attackName.GetHash()); auto attackData = _library->GetAttackLibrary()->Get(attackName.GetHash());
_attacks.Append(std::tuple(attackData, learnMethod)); _attacks.Append(std::tuple(attackData, learnMethod));

View File

@ -13,9 +13,8 @@ public:
}; };
#define THROW_CREATURE(message) \ #define THROW_CREATURE(message) \
{ \ std::stringstream ___ss; \
std::stringstream ss; \ ___ss << "[" << __FILENAME__ << ":" << __LINE__ << "] " << message; \
ss << "[" << __FILENAME__ << ":" << __LINE__ << "] " << message; \ throw CreatureException(___ss.str());
throw CreatureException(ss.str()); \
}
#endif // CREATURELIB_CREATUREEXCEPTION_HPP #endif // CREATURELIB_CREATUREEXCEPTION_HPP