(svn r12590) [NoAI] -Add: EnforcePrecondition macro to make code much better readable. noai
authorrubidium
Sun, 06 Apr 2008 14:12:19 +0000
branchnoai
changeset 9868 3998f2e73dda
parent 9867 b7d9ffe24f81
child 9869 6404afe43575
(svn r12590) [NoAI] -Add: EnforcePrecondition macro to make code much better readable.
src/ai/api/ai_airport.cpp
src/ai/api/ai_bridge.cpp
src/ai/api/ai_company.cpp
src/ai/api/ai_error.hpp
--- a/src/ai/api/ai_airport.cpp	Sun Apr 06 12:26:40 2008 +0000
+++ b/src/ai/api/ai_airport.cpp	Sun Apr 06 14:12:19 2008 +0000
@@ -50,21 +50,15 @@
 
 /* static */ bool AIAirport::BuildAirport(TileIndex tile, AirportType type)
 {
-	if (!::IsValidTile(tile) || type > AT_HELISTATION) {
-		AIObject::SetLastError(AIError::ERR_PRECONDITION_FAILED);
-		return false;
-	}
-
+	EnforcePrecondition(false, ::IsValidTile(tile));
+	EnforcePrecondition(false, type <= AT_HELISTATION);
 	return AIObject::DoCommand(tile, type, 0, CMD_BUILD_AIRPORT);
 }
 
 /* static */ bool AIAirport::RemoveAirport(TileIndex tile)
 {
-	if (!::IsValidTile(tile) || !(IsAirportTile(tile) || IsHangarTile(tile))) {
-		AIObject::SetLastError(AIError::ERR_PRECONDITION_FAILED);
-		return false;
-	}
-
+	EnforcePrecondition(false, ::IsValidTile(tile))
+	EnforcePrecondition(false, IsAirportTile(tile) || IsHangarTile(tile));
 	return AIObject::DoCommand(tile, 0, 0, CMD_LANDSCAPE_CLEAR);
 }
 
--- a/src/ai/api/ai_bridge.cpp	Sun Apr 06 12:26:40 2008 +0000
+++ b/src/ai/api/ai_bridge.cpp	Sun Apr 06 14:12:19 2008 +0000
@@ -24,15 +24,10 @@
 
 /* static */ bool AIBridge::BuildBridge(AIVehicle::VehicleType vehicle_type, BridgeID bridge_id, TileIndex start, TileIndex end)
 {
-	if (start == end ||
-			!::IsValidTile(start) ||
-			!::IsValidTile(end) ||
-			/* Not on one line */
-			(TileX(start) != TileX(end) && TileY(start) != TileY(end)) ||
-			(vehicle_type != AIVehicle::VEHICLE_ROAD && vehicle_type != AIVehicle::VEHICLE_RAIL)) {
-		AIObject::SetLastError(AIError::ERR_PRECONDITION_FAILED);
-		return false;
-	}
+	EnforcePrecondition(false, start != end);
+	EnforcePrecondition(false, ::IsValidTile(start) && ::IsValidTile(end));
+	EnforcePrecondition(false, TileX(start) == TileX(end) || TileY(start) == TileY(end));
+	EnforcePrecondition(false, vehicle_type == AIVehicle::VEHICLE_ROAD || vehicle_type == AIVehicle::VEHICLE_RAIL);
 
 	uint type = 0;
 	if (vehicle_type == AIVehicle::VEHICLE_ROAD) {
@@ -48,11 +43,7 @@
 
 /* static */ bool AIBridge::RemoveBridge(TileIndex tile)
 {
-	if (!IsBridgeTile(tile)) {
-		AIObject::SetLastError(AIError::ERR_PRECONDITION_FAILED);
-		return false;
-	}
-
+	EnforcePrecondition(false, IsBridgeTile(tile));
 	return AIObject::DoCommand(tile, 0, 0, CMD_LANDSCAPE_CLEAR);
 }
 
--- a/src/ai/api/ai_company.cpp	Sun Apr 06 12:26:40 2008 +0000
+++ b/src/ai/api/ai_company.cpp	Sun Apr 06 14:12:19 2008 +0000
@@ -24,10 +24,7 @@
 
 /* static */ bool AICompany::SetCompanyName(const char *name)
 {
-	if (::StrEmpty(name)) {
-		AIObject::SetLastError(AIError::ERR_PRECONDITION_FAILED);
-		return false;
-	}
+	EnforcePrecondition(false, !::StrEmpty(name));
 
 	_cmd_text = name;
 	return AIObject::DoCommand(0, 0, 0, CMD_CHANGE_COMPANY_NAME);
@@ -48,10 +45,7 @@
 
 /* static */ bool AICompany::SetPresidentName(const char *name)
 {
-	if (::StrEmpty(name)) {
-		AIObject::SetLastError(AIError::ERR_PRECONDITION_FAILED);
-		return false;
-	}
+	EnforcePrecondition(false, !::StrEmpty(name));
 
 	_cmd_text = name;
 	return AIObject::DoCommand(0, 0, 0, CMD_CHANGE_PRESIDENT_NAME);
@@ -106,13 +100,10 @@
 
 /* static */ bool AICompany::SetLoanAmount(int32 loan)
 {
-	if (loan < 0 ||
-			(loan % GetLoanInterval()) != 0 ||
-			loan > GetMaxLoanAmount() ||
-			(loan - GetLoanAmount() + GetBankBalance(MY_COMPANY)) < 0) {
-		AIObject::SetLastError(AIError::ERR_PRECONDITION_FAILED);
-		return false;
-	}
+	EnforcePrecondition(false, loan >= 0);
+	EnforcePrecondition(false, (loan % GetLoanInterval()) == 0);
+	EnforcePrecondition(false, loan <= GetMaxLoanAmount());
+	EnforcePrecondition(false, (loan - GetLoanAmount() + GetBankBalance(MY_COMPANY)) >= 0);
 
 	if (loan == GetLoanAmount()) return true;
 
@@ -123,18 +114,12 @@
 
 /* static */ bool AICompany::SetMinimumLoanAmount(int32 loan)
 {
-	if (loan < 0) {
-		AIObject::SetLastError(AIError::ERR_PRECONDITION_FAILED);
-		return false;
-	}
+	EnforcePrecondition(false, loan >= 0);
 
 	int32 over_interval = loan % GetLoanInterval();
 	if (over_interval != 0) loan += GetLoanInterval() - over_interval;
 
-	if (loan > GetMaxLoanAmount()) {
-		AIObject::SetLastError(AIError::ERR_PRECONDITION_FAILED);
-		return false;
-	}
+	EnforcePrecondition(false, loan <= GetMaxLoanAmount());
 
 	SetLoanAmount(loan);
 
@@ -143,10 +128,7 @@
 
 /* static */ bool AICompany::BuildCompanyHQ(TileIndex tile)
 {
-	if (!::IsValidTile(tile)) {
-		AIObject::SetLastError(AIError::ERR_PRECONDITION_FAILED);
-		return false;
-	}
+	EnforcePrecondition(false, ::IsValidTile(tile));
 
 	return AIObject::DoCommand(tile, 0, 0, CMD_BUILD_COMPANY_HQ);
 }
--- a/src/ai/api/ai_error.hpp	Sun Apr 06 12:26:40 2008 +0000
+++ b/src/ai/api/ai_error.hpp	Sun Apr 06 14:12:19 2008 +0000
@@ -9,6 +9,17 @@
 #include <map>
 
 /**
+ * Helper to write precondition enforcers for the AI API in an abbreviated manner.
+ * @param returnval The value to return on failure.
+ * @param condition The condition that must be obeyed.
+ */
+#define EnforcePrecondition(returnval, condition)               \
+	if (!(condition)) {                                           \
+		AIObject::SetLastError(AIError::ERR_PRECONDITION_FAILED);   \
+		return returnval;                                           \
+	}
+
+/**
  * Class that handles all error related functions.
  */
 class AIError : public AIObject {