1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-24 22:14:36 +02:00

- Replaced exceptions with assert for internal-API code

This commit is contained in:
beegee1 2013-04-22 16:04:17 +00:00
parent 8d82c49a99
commit 7a3f5dc23b
5 changed files with 28 additions and 91 deletions

View File

@ -345,7 +345,7 @@ CColorMapping::CColorMapping()
void CColorMapping::setColorFor(const CLoggerDomain & domain, ELogLevel::ELogLevel level, EConsoleTextColor::EConsoleTextColor color) void CColorMapping::setColorFor(const CLoggerDomain & domain, ELogLevel::ELogLevel level, EConsoleTextColor::EConsoleTextColor color)
{ {
if(level == ELogLevel::NOT_SET) throw std::runtime_error("Log level NOT_SET not allowed for configuring the color mapping."); assert(level != ELogLevel::NOT_SET);
map[domain.getName()][level] = color; map[domain.getName()][level] = color;
} }
@ -375,7 +375,7 @@ EConsoleTextColor::EConsoleTextColor CColorMapping::getColorFor(const CLoggerDom
break; break;
} }
} }
throw std::runtime_error("No color mapping found. Should not happen."); assert(0);
} }
CLogConsoleTarget::CLogConsoleTarget(CConsoleHandler * console) : console(console), threshold(ELogLevel::INFO), coloredOutputEnabled(true) CLogConsoleTarget::CLogConsoleTarget(CConsoleHandler * console) : console(console), threshold(ELogLevel::INFO), coloredOutputEnabled(true)

View File

@ -244,23 +244,15 @@ bool CMap::isInTheMap(const int3 & pos) const
} }
} }
void CMap::getTileRangeCheck(const int3 & tile) const
{
if(!isInTheMap(tile))
{
throw std::runtime_error(boost::str(boost::format("Cannot get map tile for position x '%d', y '%d', z '%d'.") % tile.x % tile.y % tile.z));
}
}
TerrainTile & CMap::getTile(const int3 & tile) TerrainTile & CMap::getTile(const int3 & tile)
{ {
getTileRangeCheck(tile); assert(isInTheMap(tile));
return terrain[tile.x][tile.y][tile.z]; return terrain[tile.x][tile.y][tile.z];
} }
const TerrainTile & CMap::getTile(const int3 & tile) const const TerrainTile & CMap::getTile(const int3 & tile) const
{ {
getTileRangeCheck(tile); assert(isInTheMap(tile));
return terrain[tile.x][tile.y][tile.z]; return terrain[tile.x][tile.y][tile.z];
} }

View File

@ -367,8 +367,6 @@ public:
unique_ptr<CMapEditManager> editManager; unique_ptr<CMapEditManager> editManager;
private: private:
void getTileRangeCheck(const int3 & tile) const;
TerrainTile*** terrain; TerrainTile*** terrain;
public: public:

View File

@ -114,7 +114,7 @@ int CMapUndoManager::getUndoRedoLimit() const
void CMapUndoManager::setUndoRedoLimit(int value) void CMapUndoManager::setUndoRedoLimit(int value)
{ {
if(value < 0) throw std::runtime_error("Cannot set a negative value for the undo redo limit."); assert(value >= 0);
undoStack.resize(std::min(undoStack.size(), static_cast<TStack::size_type>(value))); undoStack.resize(std::min(undoStack.size(), static_cast<TStack::size_type>(value)));
redoStack.resize(std::min(redoStack.size(), static_cast<TStack::size_type>(value))); redoStack.resize(std::min(redoStack.size(), static_cast<TStack::size_type>(value)));
} }

View File

@ -24,14 +24,8 @@ si32 CMapGenOptions::getWidth() const
void CMapGenOptions::setWidth(si32 value) void CMapGenOptions::setWidth(si32 value)
{ {
if(value > 0) assert(value >= 1);
{ width = value;
width = value;
}
else
{
throw std::runtime_error("A map width lower than 1 is not allowed.");
}
} }
si32 CMapGenOptions::getHeight() const si32 CMapGenOptions::getHeight() const
@ -41,14 +35,8 @@ si32 CMapGenOptions::getHeight() const
void CMapGenOptions::setHeight(si32 value) void CMapGenOptions::setHeight(si32 value)
{ {
if(value > 0) assert(value >= 1);
{ height = value;
height = value;
}
else
{
throw std::runtime_error("A map height lower than 1 is not allowed.");
}
} }
bool CMapGenOptions::getHasTwoLevels() const bool CMapGenOptions::getHasTwoLevels() const
@ -68,16 +56,9 @@ si8 CMapGenOptions::getPlayersCnt() const
void CMapGenOptions::setPlayersCnt(si8 value) void CMapGenOptions::setPlayersCnt(si8 value)
{ {
if((value >= 1 && value <= PlayerColor::PLAYER_LIMIT_I) || value == RANDOM_SIZE) assert((value >= 1 && value <= PlayerColor::PLAYER_LIMIT_I) || value == RANDOM_SIZE);
{ playersCnt = value;
playersCnt = value; resetPlayersMap();
resetPlayersMap();
}
else
{
throw std::runtime_error("Players count of RMG options should be between 1 and " +
boost::lexical_cast<std::string>(PlayerColor::PLAYER_LIMIT_I) + " or CMapGenOptions::RANDOM_SIZE for random.");
}
} }
si8 CMapGenOptions::getTeamsCnt() const si8 CMapGenOptions::getTeamsCnt() const
@ -87,15 +68,8 @@ si8 CMapGenOptions::getTeamsCnt() const
void CMapGenOptions::setTeamsCnt(si8 value) void CMapGenOptions::setTeamsCnt(si8 value)
{ {
if(playersCnt == RANDOM_SIZE || (value >= 0 && value < playersCnt) || value == RANDOM_SIZE) assert(playersCnt == RANDOM_SIZE || (value >= 0 && value < playersCnt) || value == RANDOM_SIZE);
{ teamsCnt = value;
teamsCnt = value;
}
else
{
throw std::runtime_error("Teams count of RMG options should be between 0 and <" +
boost::lexical_cast<std::string>(playersCnt) + "(players count) - 1> or CMapGenOptions::RANDOM_SIZE for random.");
}
} }
si8 CMapGenOptions::getCompOnlyPlayersCnt() const si8 CMapGenOptions::getCompOnlyPlayersCnt() const
@ -105,17 +79,9 @@ si8 CMapGenOptions::getCompOnlyPlayersCnt() const
void CMapGenOptions::setCompOnlyPlayersCnt(si8 value) void CMapGenOptions::setCompOnlyPlayersCnt(si8 value)
{ {
if(value == RANDOM_SIZE || (value >= 0 && value <= PlayerColor::PLAYER_LIMIT_I - playersCnt)) assert(value == RANDOM_SIZE || (value >= 0 && value <= PlayerColor::PLAYER_LIMIT_I - playersCnt));
{ compOnlyPlayersCnt = value;
compOnlyPlayersCnt = value; resetPlayersMap();
resetPlayersMap();
}
else
{
throw std::runtime_error(std::string("Computer only players count of RMG options should be ") +
"between 0 and <PlayerColor::PLAYER_LIMIT - " + boost::lexical_cast<std::string>(playersCnt) +
"(playersCount)> or CMapGenOptions::RANDOM_SIZE for random.");
}
} }
si8 CMapGenOptions::getCompOnlyTeamsCnt() const si8 CMapGenOptions::getCompOnlyTeamsCnt() const
@ -125,16 +91,8 @@ si8 CMapGenOptions::getCompOnlyTeamsCnt() const
void CMapGenOptions::setCompOnlyTeamsCnt(si8 value) void CMapGenOptions::setCompOnlyTeamsCnt(si8 value)
{ {
if(value == RANDOM_SIZE || compOnlyPlayersCnt == RANDOM_SIZE || (value >= 0 && value <= std::max(compOnlyPlayersCnt - 1, 0))) assert(value == RANDOM_SIZE || compOnlyPlayersCnt == RANDOM_SIZE || (value >= 0 && value <= std::max(compOnlyPlayersCnt - 1, 0)));
{ compOnlyTeamsCnt = value;
compOnlyTeamsCnt = value;
}
else
{
throw std::runtime_error(std::string("Computer only teams count of RMG options should be ") +
"between 0 and <" + boost::lexical_cast<std::string>(compOnlyPlayersCnt) +
"(compOnlyPlayersCnt) - 1> or CMapGenOptions::RANDOM_SIZE for random.");
}
} }
EWaterContent::EWaterContent CMapGenOptions::getWaterContent() const EWaterContent::EWaterContent CMapGenOptions::getWaterContent() const
@ -179,15 +137,15 @@ const std::map<PlayerColor, CMapGenOptions::CPlayerSettings> & CMapGenOptions::g
void CMapGenOptions::setStartingTownForPlayer(PlayerColor color, si32 town) void CMapGenOptions::setStartingTownForPlayer(PlayerColor color, si32 town)
{ {
auto it = players.find(color); auto it = players.find(color);
if(it == players.end()) throw std::runtime_error(boost::str(boost::format("Cannot set starting town for the player with the color '%s'.") % color)); if(it == players.end()) assert(0);
it->second.setStartingTown(town); it->second.setStartingTown(town);
} }
void CMapGenOptions::setPlayerTypeForStandardPlayer(PlayerColor color, EPlayerType::EPlayerType playerType) void CMapGenOptions::setPlayerTypeForStandardPlayer(PlayerColor color, EPlayerType::EPlayerType playerType)
{ {
assert(playerType != EPlayerType::COMP_ONLY);
auto it = players.find(color); auto it = players.find(color);
if(it == players.end()) throw std::runtime_error(boost::str(boost::format("Cannot set player type for the player with the color '%s'.") % color)); if(it == players.end()) assert(0);
if(playerType == EPlayerType::COMP_ONLY) throw std::runtime_error("Cannot set player type computer only to a standard player.");
it->second.setPlayerType(playerType); it->second.setPlayerType(playerType);
} }
@ -305,7 +263,8 @@ PlayerColor CMapGenOptions::getNextPlayerColor() const
return i; return i;
} }
} }
throw std::runtime_error("Shouldn't happen. No free player color exists."); assert(0);
return PlayerColor(0);
} }
CMapGenOptions::CPlayerSettings::CPlayerSettings() : color(0), startingTown(RANDOM_TOWN), playerType(EPlayerType::AI) CMapGenOptions::CPlayerSettings::CPlayerSettings() : color(0), startingTown(RANDOM_TOWN), playerType(EPlayerType::AI)
@ -320,14 +279,8 @@ PlayerColor CMapGenOptions::CPlayerSettings::getColor() const
void CMapGenOptions::CPlayerSettings::setColor(PlayerColor value) void CMapGenOptions::CPlayerSettings::setColor(PlayerColor value)
{ {
if(value >= PlayerColor(0) && value < PlayerColor::PLAYER_LIMIT) assert(value >= PlayerColor(0) && value < PlayerColor::PLAYER_LIMIT);
{ color = value;
color = value;
}
else
{
throw std::runtime_error("The color of the player is not in a valid range.");
}
} }
si32 CMapGenOptions::CPlayerSettings::getStartingTown() const si32 CMapGenOptions::CPlayerSettings::getStartingTown() const
@ -337,14 +290,8 @@ si32 CMapGenOptions::CPlayerSettings::getStartingTown() const
void CMapGenOptions::CPlayerSettings::setStartingTown(si32 value) void CMapGenOptions::CPlayerSettings::setStartingTown(si32 value)
{ {
if(value >= -1 && value < static_cast<int>(VLC->townh->factions.size())) assert(value >= -1 && value < static_cast<int>(VLC->townh->factions.size()));
{ startingTown = value;
startingTown = value;
}
else
{
throw std::runtime_error("The starting town of the player is not in a valid range.");
}
} }
EPlayerType::EPlayerType CMapGenOptions::CPlayerSettings::getPlayerType() const EPlayerType::EPlayerType CMapGenOptions::CPlayerSettings::getPlayerType() const