Преглед изворни кода

- Replaced exceptions with assert for internal-API code

beegee1 пре 12 година
родитељ
комит
7a3f5dc23b
5 измењених фајлова са 28 додато и 91 уклоњено
  1. 2 2
      lib/logging/CLogger.cpp
  2. 2 10
      lib/mapping/CMap.cpp
  3. 0 2
      lib/mapping/CMap.h
  4. 1 1
      lib/mapping/CMapEditManager.cpp
  5. 23 76
      lib/rmg/CMapGenerator.cpp

+ 2 - 2
lib/logging/CLogger.cpp

@@ -345,7 +345,7 @@ CColorMapping::CColorMapping()
 
 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;
 }
 
@@ -375,7 +375,7 @@ EConsoleTextColor::EConsoleTextColor CColorMapping::getColorFor(const CLoggerDom
 			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)

+ 2 - 10
lib/mapping/CMap.cpp

@@ -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)
 {
-	getTileRangeCheck(tile);
+	assert(isInTheMap(tile));
 	return terrain[tile.x][tile.y][tile.z];
 }
 
 const TerrainTile & CMap::getTile(const int3 & tile) const
 {
-	getTileRangeCheck(tile);
+	assert(isInTheMap(tile));
 	return terrain[tile.x][tile.y][tile.z];
 }
 

+ 0 - 2
lib/mapping/CMap.h

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

+ 1 - 1
lib/mapping/CMapEditManager.cpp

@@ -114,7 +114,7 @@ int CMapUndoManager::getUndoRedoLimit() const
 
 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)));
 	redoStack.resize(std::min(redoStack.size(), static_cast<TStack::size_type>(value)));
 }

+ 23 - 76
lib/rmg/CMapGenerator.cpp

@@ -24,14 +24,8 @@ si32 CMapGenOptions::getWidth() const
 
 void CMapGenOptions::setWidth(si32 value)
 {
-	if(value > 0)
-	{
-		width = value;
-	}
-	else
-	{
-		throw std::runtime_error("A map width lower than 1 is not allowed.");
-	}
+	assert(value >= 1);
+	width = value;
 }
 
 si32 CMapGenOptions::getHeight() const
@@ -41,14 +35,8 @@ si32 CMapGenOptions::getHeight() const
 
 void CMapGenOptions::setHeight(si32 value)
 {
-	if(value > 0)
-	{
-		height = value;
-	}
-	else
-	{
-		throw std::runtime_error("A map height lower than 1 is not allowed.");
-	}
+	assert(value >= 1);
+	height = value;
 }
 
 bool CMapGenOptions::getHasTwoLevels() const
@@ -68,16 +56,9 @@ si8 CMapGenOptions::getPlayersCnt() const
 
 void CMapGenOptions::setPlayersCnt(si8 value)
 {
-	if((value >= 1 && value <= PlayerColor::PLAYER_LIMIT_I) || value == RANDOM_SIZE)
-	{
-		playersCnt = value;
-		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.");
-	}
+	assert((value >= 1 && value <= PlayerColor::PLAYER_LIMIT_I) || value == RANDOM_SIZE);
+	playersCnt = value;
+	resetPlayersMap();
 }
 
 si8 CMapGenOptions::getTeamsCnt() const
@@ -87,15 +68,8 @@ si8 CMapGenOptions::getTeamsCnt() const
 
 void CMapGenOptions::setTeamsCnt(si8 value)
 {
-	if(playersCnt == RANDOM_SIZE || (value >= 0 && value < playersCnt) || value == RANDOM_SIZE)
-	{
-		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.");
-	}
+	assert(playersCnt == RANDOM_SIZE || (value >= 0 && value < playersCnt) || value == RANDOM_SIZE);
+	teamsCnt = value;
 }
 
 si8 CMapGenOptions::getCompOnlyPlayersCnt() const
@@ -105,17 +79,9 @@ si8 CMapGenOptions::getCompOnlyPlayersCnt() const
 
 void CMapGenOptions::setCompOnlyPlayersCnt(si8 value)
 {
-	if(value == RANDOM_SIZE || (value >= 0 && value <= PlayerColor::PLAYER_LIMIT_I - playersCnt))
-	{
-		compOnlyPlayersCnt = value;
-		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.");
-	}
+	assert(value == RANDOM_SIZE || (value >= 0 && value <= PlayerColor::PLAYER_LIMIT_I - playersCnt));
+	compOnlyPlayersCnt = value;
+	resetPlayersMap();
 }
 
 si8 CMapGenOptions::getCompOnlyTeamsCnt() const
@@ -125,16 +91,8 @@ si8 CMapGenOptions::getCompOnlyTeamsCnt() const
 
 void CMapGenOptions::setCompOnlyTeamsCnt(si8 value)
 {
-	if(value == RANDOM_SIZE || compOnlyPlayersCnt == RANDOM_SIZE || (value >= 0 && value <= std::max(compOnlyPlayersCnt - 1, 0)))
-	{
-		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.");
-	}
+	assert(value == RANDOM_SIZE || compOnlyPlayersCnt == RANDOM_SIZE || (value >= 0 && value <= std::max(compOnlyPlayersCnt - 1, 0)));
+	compOnlyTeamsCnt = value;
 }
 
 EWaterContent::EWaterContent CMapGenOptions::getWaterContent() const
@@ -179,15 +137,15 @@ const std::map<PlayerColor, CMapGenOptions::CPlayerSettings> & CMapGenOptions::g
 void CMapGenOptions::setStartingTownForPlayer(PlayerColor color, si32 town)
 {
 	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);
 }
 
 void CMapGenOptions::setPlayerTypeForStandardPlayer(PlayerColor color, EPlayerType::EPlayerType playerType)
 {
+	assert(playerType != EPlayerType::COMP_ONLY);
 	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(playerType == EPlayerType::COMP_ONLY) throw std::runtime_error("Cannot set player type computer only to a standard player.");
+	if(it == players.end()) assert(0);
 	it->second.setPlayerType(playerType);
 }
 
@@ -305,7 +263,8 @@ PlayerColor CMapGenOptions::getNextPlayerColor() const
 			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)
@@ -320,14 +279,8 @@ PlayerColor CMapGenOptions::CPlayerSettings::getColor() const
 
 void CMapGenOptions::CPlayerSettings::setColor(PlayerColor value)
 {
-	if(value >= PlayerColor(0) && value < PlayerColor::PLAYER_LIMIT)
-	{
-		color = value;
-	}
-	else
-	{
-		throw std::runtime_error("The color of the player is not in a valid range.");
-	}
+	assert(value >= PlayerColor(0) && value < PlayerColor::PLAYER_LIMIT);
+	color = value;
 }
 
 si32 CMapGenOptions::CPlayerSettings::getStartingTown() const
@@ -337,14 +290,8 @@ si32 CMapGenOptions::CPlayerSettings::getStartingTown() const
 
 void CMapGenOptions::CPlayerSettings::setStartingTown(si32 value)
 {
-	if(value >= -1 && value < static_cast<int>(VLC->townh->factions.size()))
-	{
-		startingTown = value;
-	}
-	else
-	{
-		throw std::runtime_error("The starting town of the player is not in a valid range.");
-	}
+	assert(value >= -1 && value < static_cast<int>(VLC->townh->factions.size()));
+	startingTown = value;
 }
 
 EPlayerType::EPlayerType CMapGenOptions::CPlayerSettings::getPlayerType() const