Further work on better exceptions.
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2020-07-26 17:41:11 +02:00
parent 29eb7c603a
commit 36f1e5beeb
12 changed files with 30 additions and 37 deletions

View File

@@ -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.");
}

View File

@@ -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<bool>(_value);
}
@@ -36,9 +34,7 @@ namespace CreatureLib::Library {
if (_type == EffectParameterType::Float) {
return static_cast<int64_t>(std::get<float>(_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<int64_t>(_value);
}
@@ -47,17 +43,15 @@ namespace CreatureLib::Library {
if (_type == EffectParameterType::Int) {
return static_cast<float>(std::get<int64_t>(_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<float>(_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<ArbUt::StringView>(_value);
}

View File

@@ -1,6 +1,7 @@
#ifndef CREATURELIB_CREATUREEXCEPTION_HPP
#define CREATURELIB_CREATUREEXCEPTION_HPP
#include <sstream>
#include <stdexcept>
#include <string>
#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

View File

@@ -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);
}