From 93a763ab78c08565a25370470ce0b9d53ca89371 Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Sat, 15 Aug 2020 16:34:52 +0200 Subject: [PATCH] Rework exceptions. Signed-off-by: Deukhoofd --- CInterface/Core.cpp | 1 + CInterface/Core.hpp | 11 +++++++---- src/Battling/Flow/TurnHandler.cpp | 9 ++++----- src/Battling/Library/BattleStatCalculator.cpp | 1 - src/Battling/Models/Battle.cpp | 4 ++-- src/Battling/ScriptHandling/ScriptAggregator.hpp | 1 - src/Battling/ScriptHandling/ScriptMacros.hpp | 2 +- src/Library/ClampedStatisticSet.hpp | 10 +++++----- src/Library/Exceptions/CreatureException.hpp | 12 ++++-------- src/Library/Exceptions/NotImplementedException.hpp | 12 ------------ src/Library/Exceptions/NotReachableException.hpp | 11 ----------- src/Library/StatisticSet.hpp | 10 +++++----- 12 files changed, 29 insertions(+), 55 deletions(-) delete mode 100644 src/Library/Exceptions/NotImplementedException.hpp delete mode 100644 src/Library/Exceptions/NotReachableException.hpp diff --git a/CInterface/Core.cpp b/CInterface/Core.cpp index 786aae0..a364279 100644 --- a/CInterface/Core.cpp +++ b/CInterface/Core.cpp @@ -3,3 +3,4 @@ std::string ExceptionHandler::_creatureLibLastException = ""; export const char* CreatureLib_C_GetLastException() { return ExceptionHandler::GetLastException(); } +export const char* CreatureLib_C_GetLastExceptionStacktrace() { return ExceptionHandler::GetLastExceptionStacktrace(); } diff --git a/CInterface/Core.hpp b/CInterface/Core.hpp index 20784c0..8bde751 100644 --- a/CInterface/Core.hpp +++ b/CInterface/Core.hpp @@ -13,20 +13,23 @@ class ExceptionHandler { static std::string _creatureLibLastException; + static std::string _creatureLibLastExceptionStacktrace; public: - static void SetLastException(std::string function, const ArbUt::Exception& e) { + static void SetLastArbUtException(const std::string& function, const ArbUt::Exception& e) { std::stringstream ss; ss << "[" << function << "] " << e.what() << std::endl; - ss << e.GetStacktrace(); _creatureLibLastException = ss.str(); + _creatureLibLastExceptionStacktrace = e.GetStacktrace(); } - static void SetLastException(std::string function, const std::exception& e) { + static void SetLastException(const std::string& function, const std::exception& e) { std::stringstream ss; ss << "[" << function << "] " << e.what(); _creatureLibLastException = ss.str(); + _creatureLibLastExceptionStacktrace = ""; } static const char* GetLastException() { return _creatureLibLastException.c_str(); } + static const char* GetLastExceptionStacktrace() { return _creatureLibLastExceptionStacktrace.c_str(); } }; #define Try(data) \ @@ -34,7 +37,7 @@ public: data; \ return 0; \ } catch (const ArbUt::Exception& e) { \ - ExceptionHandler::SetLastException(__FUNCTION__, e); \ + ExceptionHandler::SetLastArbUtException(__FUNCTION__, e); \ return CreatureLibException; \ } catch (const std::exception& e) { \ ExceptionHandler::SetLastException(__FUNCTION__, e); \ diff --git a/src/Battling/Flow/TurnHandler.cpp b/src/Battling/Flow/TurnHandler.cpp index 028b622..212ed81 100644 --- a/src/Battling/Flow/TurnHandler.cpp +++ b/src/Battling/Flow/TurnHandler.cpp @@ -1,7 +1,6 @@ #include "TurnHandler.hpp" #include #include -#include "../../Library/Exceptions/NotImplementedException.hpp" #include "../EventHooks/EventDataClasses.hpp" #include "../History/HistoryElements/AttackUseHistory.hpp" #include "../ScriptHandling/ScriptMacros.hpp" @@ -58,14 +57,14 @@ void TurnHandler::ExecuteChoice(ArbUt::BorrowedPtr choice) { } switch (choiceKind) { - case TurnChoiceKind::Pass: throw NotReachableException(); + case TurnChoiceKind::Pass: return; case TurnChoiceKind::Attack: try_creature(return ExecuteAttackChoice(choice.ForceAs()), "Encountered exception during attack choice execution"); case TurnChoiceKind::Switch: return ExecuteSwitchChoice(choice.ForceAs()); case TurnChoiceKind::Flee: return ExecuteFleeChoice(choice.ForceAs()); - case TurnChoiceKind::Item: throw NotImplementedException(); + case TurnChoiceKind::Item: NOT_IMPLEMENTED; } } @@ -201,7 +200,7 @@ void TurnHandler::HandleAttackForTarget(ExecutingAttack* attack, const ArbUt::Bo if (hasSecondaryEffect) { HOOK(OnSecondaryEffect, user, attack, target.GetRaw(), hitIndex); } - } catch (const CreatureException& e) { + } catch (const ArbUt::Exception& e) { throw e; } catch (const std::exception& e) { THROW_CREATURE("Exception during status attack effect handling: " << e.what() << "."); @@ -233,7 +232,7 @@ void TurnHandler::HandleAttackForTarget(ExecutingAttack* attack, const ArbUt::Bo if (hasSecondaryEffect) { HOOK(OnSecondaryEffect, user, attack, target.GetRaw(), hitIndex); } - } catch (const CreatureException& e) { + } catch (const ArbUt::Exception& e) { throw e; } catch (const std::exception& e) { THROW_CREATURE("Exception during offensive attack secondary effect handling: " << e.what() diff --git a/src/Battling/Library/BattleStatCalculator.cpp b/src/Battling/Library/BattleStatCalculator.cpp index fa417b3..ceff789 100644 --- a/src/Battling/Library/BattleStatCalculator.cpp +++ b/src/Battling/Library/BattleStatCalculator.cpp @@ -1,6 +1,5 @@ #include "BattleStatCalculator.hpp" #include -#include "../../Library/Exceptions/NotImplementedException.hpp" #include "../Models/Creature.hpp" using namespace CreatureLib; diff --git a/src/Battling/Models/Battle.cpp b/src/Battling/Models/Battle.cpp index ee2e7c3..69d7036 100644 --- a/src/Battling/Models/Battle.cpp +++ b/src/Battling/Models/Battle.cpp @@ -40,7 +40,7 @@ void Battle::CheckChoicesSetAndRun() { return; } } - } catch (const CreatureException& e) { + } catch (const ArbUt::Exception& e) { throw e; } catch (const std::exception& e) { THROW_CREATURE("Exception during choices set validation: '" << e.what() << "'.") @@ -81,7 +81,7 @@ void Battle::CheckChoicesSetAndRun() { TriggerEventListener(); try { TurnHandler::RunTurn(this->_currentTurnQueue); - } catch (const CreatureException& e) { + } catch (const ArbUt::Exception& e) { throw e; } catch (const std::exception& e) { THROW_CREATURE("Error during running a turn: '" << e.what() << "'."); diff --git a/src/Battling/ScriptHandling/ScriptAggregator.hpp b/src/Battling/ScriptHandling/ScriptAggregator.hpp index 6ddb334..fdf9680 100644 --- a/src/Battling/ScriptHandling/ScriptAggregator.hpp +++ b/src/Battling/ScriptHandling/ScriptAggregator.hpp @@ -2,7 +2,6 @@ #define CREATURELIB_SCRIPTAGGREGATOR_HPP #include -#include "../../Library/Exceptions/NotReachableException.hpp" #include "Script.hpp" #include "ScriptSet.hpp" #include "ScriptWrapper.hpp" diff --git a/src/Battling/ScriptHandling/ScriptMacros.hpp b/src/Battling/ScriptHandling/ScriptMacros.hpp index 8d81d32..59c8169 100644 --- a/src/Battling/ScriptHandling/ScriptMacros.hpp +++ b/src/Battling/ScriptHandling/ScriptMacros.hpp @@ -12,7 +12,7 @@ THROW_CREATURE("Exception running script hook '" #hookName "': " << e.what()) \ } \ } \ - } catch (const CreatureException& e) { \ + } catch (const ArbUt::Exception& e) { \ throw e; \ } catch (const std::exception& e) { \ THROW_CREATURE("Exception setting up script hook '" #hookName "': " << e.what()) \ diff --git a/src/Library/ClampedStatisticSet.hpp b/src/Library/ClampedStatisticSet.hpp index 51581a4..fe21844 100644 --- a/src/Library/ClampedStatisticSet.hpp +++ b/src/Library/ClampedStatisticSet.hpp @@ -1,7 +1,7 @@ #ifndef CREATURELIB_CLAMPEDSTATISTICSET_HPP #define CREATURELIB_CLAMPEDSTATISTICSET_HPP -#include "Exceptions/NotReachableException.hpp" +#include "Exceptions/CreatureException.hpp" #include "Statistic.hpp" namespace CreatureLib::Library { template class ClampedStatisticSet { @@ -36,7 +36,7 @@ namespace CreatureLib::Library { case CreatureLib::Library::Statistic::MagicalAttack: return _magicalAttack; case CreatureLib::Library::Statistic::MagicalDefense: return _magicalDefense; case CreatureLib::Library::Statistic::Speed: return _speed; - default: throw NotReachableException(); + default: NOT_REACHABLE; } } @@ -52,7 +52,7 @@ namespace CreatureLib::Library { case CreatureLib::Library::Statistic::MagicalAttack: _magicalAttack = value; break; case CreatureLib::Library::Statistic::MagicalDefense: _magicalDefense = value; break; case CreatureLib::Library::Statistic::Speed: _speed = value; break; - default: throw NotReachableException(); + default: NOT_REACHABLE; } } @@ -78,7 +78,7 @@ namespace CreatureLib::Library { case CreatureLib::Library::Statistic::MagicalAttack: ModifyStat(_magicalAttack, +) break; case CreatureLib::Library::Statistic::MagicalDefense: ModifyStat(_magicalDefense, +) break; case CreatureLib::Library::Statistic::Speed: ModifyStat(_speed, +) break; - default: throw NotReachableException(); + default: NOT_REACHABLE; } } inline bool DecreaseStatBy(Statistic stat, T amount) { @@ -89,7 +89,7 @@ namespace CreatureLib::Library { case CreatureLib::Library::Statistic::MagicalAttack: ModifyStat(_magicalAttack, -) break; case CreatureLib::Library::Statistic::MagicalDefense: ModifyStat(_magicalDefense, -) break; case CreatureLib::Library::Statistic::Speed: ModifyStat(_speed, -) break; - default: throw NotReachableException(); + default: NOT_REACHABLE; } } }; diff --git a/src/Library/Exceptions/CreatureException.hpp b/src/Library/Exceptions/CreatureException.hpp index e7d66aa..ca89b09 100644 --- a/src/Library/Exceptions/CreatureException.hpp +++ b/src/Library/Exceptions/CreatureException.hpp @@ -8,21 +8,17 @@ #include #define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__) -class CreatureException : public ArbUt::Exception { -public: - explicit CreatureException(const std::string& error) : ArbUt::Exception(error) {} - virtual ~CreatureException() = default; -}; - #define THROW_CREATURE(message) \ std::stringstream ___ss; \ ___ss << "[" << __FILENAME__ << ":" << __LINE__ << "] " << message; \ - throw CreatureException(___ss.str()); + throw ArbUt::Exception(___ss.str()); +#define NOT_REACHABLE THROW_CREATURE("Not reachable"); +#define NOT_IMPLEMENTED THROW_CREATURE("Not implemented"); #define try_creature(data, msg) \ try { \ data; \ - } catch (const CreatureException& e) { \ + } catch (const ArbUt::Exception& e) { \ throw e; \ } catch (const std::exception& e) { \ THROW_CREATURE(msg << ": '" << e.what() << "'."); \ diff --git a/src/Library/Exceptions/NotImplementedException.hpp b/src/Library/Exceptions/NotImplementedException.hpp deleted file mode 100644 index 31ea3a8..0000000 --- a/src/Library/Exceptions/NotImplementedException.hpp +++ /dev/null @@ -1,12 +0,0 @@ -#ifndef CREATURELIB_NOTIMPLEMENTEDEXCEPTION_HPP -#define CREATURELIB_NOTIMPLEMENTEDEXCEPTION_HPP - -#include "CreatureException.hpp" - -class NotImplementedException : CreatureException { -public: - NotImplementedException(std::string error) : CreatureException(error) {} - NotImplementedException() : CreatureException("Not Implemented") {} -}; - -#endif // CREATURELIB_NOTIMPLEMENTEDEXCEPTION_HPP diff --git a/src/Library/Exceptions/NotReachableException.hpp b/src/Library/Exceptions/NotReachableException.hpp deleted file mode 100644 index 6148ff6..0000000 --- a/src/Library/Exceptions/NotReachableException.hpp +++ /dev/null @@ -1,11 +0,0 @@ -#ifndef CREATURELIB_NOTREACHABLEEXCEPTION_HPP -#define CREATURELIB_NOTREACHABLEEXCEPTION_HPP - -#include "CreatureException.hpp" - -class NotReachableException : CreatureException { -public: - NotReachableException() : CreatureException("Not Reachable"){}; -}; - -#endif // CREATURELIB_NOTREACHABLEEXCEPTION_HPP diff --git a/src/Library/StatisticSet.hpp b/src/Library/StatisticSet.hpp index e9eb3bd..8174020 100644 --- a/src/Library/StatisticSet.hpp +++ b/src/Library/StatisticSet.hpp @@ -2,7 +2,7 @@ #define CREATURELIB_STATISTICSET_HPP #include #include -#include "Exceptions/NotReachableException.hpp" +#include "Exceptions/CreatureException.hpp" #include "Statistic.hpp" namespace CreatureLib::Library { @@ -37,7 +37,7 @@ namespace CreatureLib::Library { case CreatureLib::Library::Statistic::MagicalAttack: return _magicalAttack; case CreatureLib::Library::Statistic::MagicalDefense: return _magicalDefense; case CreatureLib::Library::Statistic::Speed: return _speed; - default: throw NotReachableException(); + default: NOT_REACHABLE; } } @@ -49,7 +49,7 @@ namespace CreatureLib::Library { case CreatureLib::Library::Statistic::MagicalAttack: _magicalAttack = value; break; case CreatureLib::Library::Statistic::MagicalDefense: _magicalDefense = value; break; case CreatureLib::Library::Statistic::Speed: _speed = value; break; - default: throw NotReachableException(); + default: NOT_REACHABLE; } } @@ -61,7 +61,7 @@ namespace CreatureLib::Library { case CreatureLib::Library::Statistic::MagicalAttack: _magicalAttack += amount; break; case CreatureLib::Library::Statistic::MagicalDefense: _magicalDefense += amount; break; case CreatureLib::Library::Statistic::Speed: _speed += amount; break; - default: throw NotReachableException(); + default: NOT_REACHABLE; } } inline void DecreaseStatBy(Statistic stat, T amount) { @@ -72,7 +72,7 @@ namespace CreatureLib::Library { case CreatureLib::Library::Statistic::MagicalAttack: _magicalAttack -= amount; break; case CreatureLib::Library::Statistic::MagicalDefense: _magicalDefense -= amount; break; case CreatureLib::Library::Statistic::Speed: _speed -= amount; break; - default: throw NotReachableException(); + default: NOT_REACHABLE; } } };