diff --git a/src/Battling/Models/Battle.cpp b/src/Battling/Models/Battle.cpp index e40f888..2409dfd 100644 --- a/src/Battling/Models/Battle.cpp +++ b/src/Battling/Models/Battle.cpp @@ -137,9 +137,7 @@ void Battle::AddVolatileScript(const ArbUt::StringView& key) { } script = _library->LoadScript(ScriptCategory::Battle, key); if (script == nullptr) { - std::stringstream ss; - ss << "Invalid volatile script requested for battle: '" << key.c_str() << "'."; - throw CreatureException(ss.str()); + THROW_CREATURE("Invalid volatile script requested for battle: '" << key.c_str() << "'."); } return _volatile.Add(script.GetRaw()); } diff --git a/src/Battling/Models/BattleSide.cpp b/src/Battling/Models/BattleSide.cpp index 8b15930..e386753 100644 --- a/src/Battling/Models/BattleSide.cpp +++ b/src/Battling/Models/BattleSide.cpp @@ -30,7 +30,7 @@ void BattleSide::SetChoice(BaseTurnChoice* choice) { AssertNotNull(choice) auto find = std::find(_creatures.begin(), _creatures.end(), choice->GetUser()); if (find == _creatures.end()) - throw CreatureException("User not found"); + THROW_CREATURE("User not found"); uint8_t index = std::distance(_creatures.begin(), find); _choices[index] = std::shared_ptr(choice); _choicesSet++; diff --git a/src/Battling/Models/BattleSide.hpp b/src/Battling/Models/BattleSide.hpp index 49a4cb9..c76b5be 100644 --- a/src/Battling/Models/BattleSide.hpp +++ b/src/Battling/Models/BattleSide.hpp @@ -59,7 +59,7 @@ namespace CreatureLib::Battling { if (_creatures[i] == c) return i; } - throw CreatureException("Unable to find creature on field."); + THROW_CREATURE("Unable to find creature on field."); } void MarkSlotAsUnfillable(const ArbUt::BorrowedPtr& creature) noexcept { diff --git a/src/Battling/Models/CreateCreature.cpp b/src/Battling/Models/CreateCreature.cpp index 1eb5020..7cdf609 100644 --- a/src/Battling/Models/CreateCreature.cpp +++ b/src/Battling/Models/CreateCreature.cpp @@ -22,7 +22,7 @@ CreateCreature CreateCreature::WithGender(Library::Gender gender) { CreateCreature CreateCreature::WithAttack(const ArbUt::StringView& attackName, AttackLearnMethod learnMethod) { if (_attacks.Count() >= _library->GetSettings()->GetMaximalMoves()) - throw CreatureException("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()); _attacks.Append(std::tuple(attackData, learnMethod)); @@ -50,7 +50,7 @@ Creature* CreateCreature::Create() { ArbUt::BorrowedPtr heldItem; if (!this->_heldItem.Empty()) { if (!_library->GetItemLibrary()->TryGet(this->_heldItem.GetHash(), heldItem)) { - throw CreatureException("Invalid held item."); + THROW_CREATURE("Invalid held item '" << this->_heldItem.c_str() << "'."); } } auto experience = _library->GetGrowthRateLibrary()->CalculateExperience(species->GetGrowthRate(), _level); diff --git a/src/Battling/Models/Creature.cpp b/src/Battling/Models/Creature.cpp index 6dc53a0..cfc9971 100644 --- a/src/Battling/Models/Creature.cpp +++ b/src/Battling/Models/Creature.cpp @@ -227,14 +227,14 @@ ArbUt::BorrowedPtr Battling::Creature::GetDisplay void Battling::Creature::SetHeldItem(const ArbUt::BasicStringView& itemName) { ArbUt::BorrowedPtr item; if (!_library->GetItemLibrary()->TryGet(itemName.GetHash(), item)) { - throw CreatureException("Item not found."); + THROW_CREATURE("Item not found '" << itemName.c_str() << "'."); } _heldItem = item; } void Battling::Creature::SetHeldItem(uint32_t itemNameHash) { ArbUt::BorrowedPtr item; if (!_library->GetItemLibrary()->TryGet(itemNameHash, item)) { - throw CreatureException("Item not found."); + THROW_CREATURE("Item not found."); } _heldItem = item; } @@ -247,9 +247,7 @@ void Battling::Creature::AddVolatileScript(const ArbUt::StringView& name) { } script = this->_library->LoadScript(ScriptCategory::Creature, name); if (script == nullptr) { - std::stringstream ss; - ss << "Invalid volatile script requested for creature: '" << name.c_str() << "'."; - throw CreatureException(ss.str()); + THROW_CREATURE("Invalid volatile script requested for creature: '" << name.c_str() << "'."); } _volatile.Add(script.GetRaw()); } diff --git a/src/Battling/Models/ExecutingAttack.hpp b/src/Battling/Models/ExecutingAttack.hpp index 8cdf440..d2b6678 100644 --- a/src/Battling/Models/ExecutingAttack.hpp +++ b/src/Battling/Models/ExecutingAttack.hpp @@ -66,7 +66,7 @@ namespace CreatureLib::Battling { return _hits[i * _numberHits + hit]; } } - throw CreatureException("Invalid target requested."); + THROW_CREATURE("Invalid target requested."); } HitData* GetTargetIteratorBegin(Creature* creature) { @@ -75,7 +75,7 @@ namespace CreatureLib::Battling { return &_hits[i * _numberHits * sizeof(HitData)]; } } - throw CreatureException("Invalid target requested."); + THROW_CREATURE("Invalid target requested."); } bool IsCreatureTarget(Creature* creature) noexcept { diff --git a/src/Library/CreatureData/SpeciesVariant.cpp b/src/Library/CreatureData/SpeciesVariant.cpp index 2944a51..3f5c95c 100644 --- a/src/Library/CreatureData/SpeciesVariant.cpp +++ b/src/Library/CreatureData/SpeciesVariant.cpp @@ -13,5 +13,5 @@ CreatureLib::Library::SpeciesVariant::GetTalentIndex(const ArbUt::StringView& ta return TalentIndex(true, i); } } - throw CreatureException("The given talent is not a valid talent for this creature."); + THROW_CREATURE("The given talent is not a valid talent for this creature."); } \ No newline at end of file diff --git a/src/Library/EffectParameter.hpp b/src/Library/EffectParameter.hpp index ab233dd..fb12271 100644 --- a/src/Library/EffectParameter.hpp +++ b/src/Library/EffectParameter.hpp @@ -25,9 +25,7 @@ namespace CreatureLib::Library { inline EffectParameterType GetType() const noexcept { return _type; } bool AsBool() const { if (_type != EffectParameterType::Bool) { - std::stringstream ss; - ss << "Cast effect parameter to bool, but was " << EffectParameterTypeHelper::ToString(_type); - throw CreatureException(ss.str()); + THROW_CREATURE("Cast effect parameter to bool, but was " << EffectParameterTypeHelper::ToString(_type)); } return std::get(_value); } @@ -36,9 +34,7 @@ namespace CreatureLib::Library { if (_type == EffectParameterType::Float) { return static_cast(std::get(_value)); } - std::stringstream ss; - ss << "Cast effect parameter to int, but was " << EffectParameterTypeHelper::ToString(_type); - throw CreatureException(ss.str()); + THROW_CREATURE("Cast effect parameter to int, but was " << EffectParameterTypeHelper::ToString(_type)); } return std::get(_value); } @@ -47,17 +43,15 @@ namespace CreatureLib::Library { if (_type == EffectParameterType::Int) { return static_cast(std::get(_value)); } - std::stringstream ss; - ss << "Cast effect parameter to float, but was " << EffectParameterTypeHelper::ToString(_type); - throw CreatureException(ss.str()); + THROW_CREATURE("Cast effect parameter to float, but was " + << EffectParameterTypeHelper::ToString(_type)); } return std::get(_value); } const ArbUt::StringView& AsString() const { if (_type != EffectParameterType::String) { - std::stringstream ss; - ss << "Cast effect parameter to string, but was " << EffectParameterTypeHelper::ToString(_type); - throw CreatureException(ss.str()); + THROW_CREATURE("Cast effect parameter to string, but was " + << EffectParameterTypeHelper::ToString(_type)); } return std::get(_value); } diff --git a/src/Library/Exceptions/CreatureException.hpp b/src/Library/Exceptions/CreatureException.hpp index 7526a48..8753ee9 100644 --- a/src/Library/Exceptions/CreatureException.hpp +++ b/src/Library/Exceptions/CreatureException.hpp @@ -1,6 +1,7 @@ #ifndef CREATURELIB_CREATUREEXCEPTION_HPP #define CREATURELIB_CREATUREEXCEPTION_HPP +#include #include #include #define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__) @@ -12,7 +13,9 @@ public: }; #define THROW_CREATURE(message) \ - std::stringstream ss; \ - ss << "[" << __FILENAME__ << ", " << __LINE__ << "] " << message; \ - throw CreatureException(ss.str()); + { \ + std::stringstream ss; \ + ss << "[" << __FILENAME__ << ":" << __LINE__ << "] " << message; \ + throw CreatureException(ss.str()); \ + } #endif // CREATURELIB_CREATUREEXCEPTION_HPP diff --git a/src/Library/GrowthRates/GrowthRateLibrary.cpp b/src/Library/GrowthRates/GrowthRateLibrary.cpp index dbac82a..f07e0d1 100644 --- a/src/Library/GrowthRates/GrowthRateLibrary.cpp +++ b/src/Library/GrowthRates/GrowthRateLibrary.cpp @@ -5,7 +5,7 @@ uint8_t CreatureLib::Library::GrowthRateLibrary::CalculateLevel(const ArbUt::Bas uint32_t experience) const { auto find = _growthRates.find(growthRate); if (find == _growthRates.end()) { - throw CreatureException("Invalid growth rate was requested."); + THROW_CREATURE("Invalid growth rate was requested."); } return find->second->CalculateLevel(experience); } @@ -13,7 +13,7 @@ uint8_t CreatureLib::Library::GrowthRateLibrary::CalculateLevel(const ArbUt::Bas uint8_t CreatureLib::Library::GrowthRateLibrary::CalculateLevel(uint32_t hash, uint32_t experience) const { auto find = _growthRates.find(hash); if (find == _growthRates.end()) { - throw CreatureException("Invalid growth rate was requested."); + THROW_CREATURE("Invalid growth rate was requested."); } return find->second->CalculateLevel(experience); } @@ -22,7 +22,7 @@ uint32_t CreatureLib::Library::GrowthRateLibrary::CalculateExperience(const ArbU uint8_t level) const { auto find = _growthRates.find(growthRate); if (find == _growthRates.end()) { - throw CreatureException("Invalid growth rate was requested."); + THROW_CREATURE("Invalid growth rate was requested."); } return find->second->CalculateExperience(level); } @@ -30,7 +30,7 @@ uint32_t CreatureLib::Library::GrowthRateLibrary::CalculateExperience(const ArbU uint32_t CreatureLib::Library::GrowthRateLibrary::CalculateExperience(uint32_t hash, uint8_t level) const { auto find = _growthRates.find(hash); if (find == _growthRates.end()) { - throw CreatureException("Invalid growth rate was requested."); + THROW_CREATURE("Invalid growth rate was requested."); } return find->second->CalculateExperience(level); } diff --git a/tests/BattleTests/ScriptTests/ScriptAggregatorTests.cpp b/tests/BattleTests/ScriptTests/ScriptAggregatorTests.cpp index 4fa4440..fcb84ba 100644 --- a/tests/BattleTests/ScriptTests/ScriptAggregatorTests.cpp +++ b/tests/BattleTests/ScriptTests/ScriptAggregatorTests.cpp @@ -133,7 +133,7 @@ TEST_CASE("Script Aggregator properly iterates when empty.", "[Battling, Scripti auto vec = ArbUt::List{}; auto aggr = ScriptAggregator(vec); while (aggr.HasNext()) { - throw CreatureException("Aggregator returned a script, but should have been empty."); + THROW_CREATURE("Aggregator returned a script, but should have been empty."); } CHECK(ran == 0); } diff --git a/tests/ExceptionTests.cpp b/tests/ExceptionTests.cpp index f48fe7c..242010a 100644 --- a/tests/ExceptionTests.cpp +++ b/tests/ExceptionTests.cpp @@ -6,11 +6,11 @@ TEST_CASE("When throwing exception, what() is readable", "[Utilities]") { bool hasCaught = false; try { - throw CreatureException("foobar"); + THROW_CREATURE("foobar"); } catch (const std::exception& e) { hasCaught = true; INFO(e.what()); - REQUIRE(strcmp(e.what(), "foobar") == 0); + REQUIRE(std::string(e.what()) == "[ExceptionTests.cpp:9] foobar"); } REQUIRE(hasCaught); }