Implemented better exceptions
continuous-integration/drone/push Build is failing Details

This commit is contained in:
Deukhoofd 2019-10-31 13:26:56 +01:00
parent ffba5fb24c
commit 65f814b006
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
9 changed files with 55 additions and 11 deletions

View File

@ -1,4 +1,5 @@
#include "TurnHandler.hpp"
#include "../../Core/Exceptions/NotImplementedException.hpp"
void CreatureLib::Battling::TurnHandler::RunTurn(CreatureLib::Battling::ChoiceQueue &queue) {
while (queue.HasNext()){
@ -14,7 +15,7 @@ void CreatureLib::Battling::TurnHandler::ExecuteChoice(const CreatureLib::Battli
case TurnChoiceKind::Item:
case TurnChoiceKind::Switch:
case TurnChoiceKind::RunAway:
throw "Not implemented";
throw NotImplementedException();
}
}

View File

@ -1,5 +1,6 @@
#include "BattleStatCalculator.hpp"
#include "../Models/Creature.hpp"
#include "../../Core/Exceptions/NotImplementedException.hpp"
using namespace CreatureLib;
@ -49,5 +50,5 @@ uint32_t Battling::BattleStatCalculator::CalculateFlatStat(Battling::Creature *c
}
uint32_t Battling::BattleStatCalculator::CalculateBoostedStat(Battling::Creature *creature, Core::Statistic stat) const{
throw "TODO";
throw NotImplementedException();
}

View File

@ -1,4 +1,5 @@
#include "BattleSide.hpp"
#include "../../Core/Exceptions/CreatureException.hpp"
#include <algorithm>
using namespace CreatureLib::Battling;
@ -25,7 +26,7 @@ const std::vector<const BaseTurnChoice *>& BattleSide::GetChoices() const{
void BattleSide::SetChoice(const BaseTurnChoice *choice) {
auto find = std::find(_creatures.begin(), _creatures.end(), choice->GetUser());
if (find ==_creatures.end())
throw "User not found";
throw CreatureException("User not found");
uint8_t index = std::distance(_creatures.begin(),find);
_choices[index] = choice;
}

View File

@ -1,4 +1,5 @@
#include "CreateCreature.hpp"
#include "../../Core/Exceptions/CreatureException.hpp"
#include <utility>
using namespace CreatureLib::Battling;
@ -67,8 +68,7 @@ CreateCreature *CreateCreature::WithGender(Library::Gender gender) {
CreateCreature *CreateCreature::WithAttack(const std::string& attackName, AttackLearnMethod learnMethod) {
if (_attacks.size() >= _library->GetSettings().GetMaximalMoves())
//TODO: Better exception
throw "";
throw CreatureException("You have already set the maximum amount of allowed moves.");
auto attackData = _library->GetAttackLibrary()->GetAttack(attackName);
_attacks.emplace_back(attackData, learnMethod);

View File

@ -0,0 +1,18 @@
#ifndef CREATURELIB_CREATUREEXCEPTION_HPP
#define CREATURELIB_CREATUREEXCEPTION_HPP
#include <exception>
#include <string>
class CreatureException : std::exception{
std::string _error;
public:
CreatureException(std::string error){}
const char *what() const noexcept override {
return _error.c_str();
}
};
#endif //CREATURELIB_CREATUREEXCEPTION_HPP

View File

@ -0,0 +1,12 @@
#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

View File

@ -0,0 +1,11 @@
#ifndef CREATURELIB_NOTREACHABLEEXCEPTION_HPP
#define CREATURELIB_NOTREACHABLEEXCEPTION_HPP
#include "CreatureException.hpp"
class NotReachableException : CreatureException{
public:
NotReachableException() : CreatureException("Not Reachable"){};
};
#endif //CREATURELIB_NOTREACHABLEEXCEPTION_HPP

View File

@ -4,6 +4,7 @@
#include <exception>
#include "Statistic.hpp"
#include "../GenericTemplates.cpp"
#include "Exceptions/NotReachableException.hpp"
namespace CreatureLib::Core{
template <class T>
@ -41,7 +42,7 @@ namespace CreatureLib::Core{
case MagicalDefense: return _magicalDefense;
case Speed: return _speed;
}
throw std::exception();
throw NotReachableException();
}
inline void SetStat(Statistic stat, T value){
@ -53,7 +54,7 @@ namespace CreatureLib::Core{
case MagicalDefense: _magicalDefense = value;
case Speed: _speed = value;
}
throw std::exception();
throw NotReachableException();
}
inline void IncreaseStatBy(Statistic stat, T amount){
@ -65,7 +66,7 @@ namespace CreatureLib::Core{
case MagicalDefense: _magicalDefense += amount;
case Speed: _speed += amount;
}
throw std::exception();
throw NotReachableException();
}
inline void DecreaseStatBy(Statistic stat, T amount){
switch (stat){
@ -76,7 +77,7 @@ namespace CreatureLib::Core{
case MagicalDefense: _magicalDefense -= amount;
case Speed: _speed -= amount;
}
throw std::exception();
throw NotReachableException();
}
};
}

View File

@ -34,8 +34,7 @@ int8_t CreatureLib::Library::SpeciesVariant::GetTalentIndex(std::string talent)
if (i != _secretTalents.end()){
return std::distance(_secretTalents.begin(), i);
}
//TODO: implement better exception.
throw;
throw CreatureException("The given talent is not a valid talent for this creature.");
}
int8_t CreatureLib::Library::SpeciesVariant::GetRandomTalent(CreatureLib::Core::Random *rand) const {