changeset 17184:07b9f8352bbd draft

(svn r21923) -Codechange: Unify articulated vehicle iteration functions.
author terkhen <terkhen@openttd.org>
date Sat, 29 Jan 2011 17:27:32 +0000
parents 935a970bf17f
children ccebafd2a9ba
files src/train.h src/vehicle_base.h
diffstat 2 files changed, 112 insertions(+), 70 deletions(-) [+]
line wrap: on
line diff
--- a/src/train.h
+++ b/src/train.h
@@ -136,76 +136,6 @@
 	int GetCurrentMaxSpeed() const;
 
 	/**
-	 * Get the next part of a multi-part engine.
-	 * Will only work on a multi-part engine (this->EngineHasArticPart() == true),
-	 * Result is undefined for normal engine.
-	 * @return next part of articulated engine
-	 */
-	FORCEINLINE Train *GetNextArticulatedPart() const
-	{
-		assert(this->HasArticulatedPart());
-		return this->Next();
-	}
-
-	/**
-	 * Get the first part of a multi-part engine.
-	 * @return First part of the engine.
-	 */
-	FORCEINLINE Train *GetFirstEnginePart()
-	{
-		Train *v = this;
-		while (v->IsArticulatedPart()) v = v->Previous();
-		return v;
-	}
-
-	/**
-	 * Get the first part of a multi-part engine.
-	 * @return First part of the engine.
-	 */
-	FORCEINLINE const Train *GetFirstEnginePart() const
-	{
-		const Train *v = this;
-		while (v->IsArticulatedPart()) v = v->Previous();
-		return v;
-	}
-
-	/**
-	 * Get the last part of a multi-part engine.
-	 * @return Last part of the engine.
-	 */
-	FORCEINLINE Train *GetLastEnginePart()
-	{
-		Train *v = this;
-		while (v->HasArticulatedPart()) v = v->GetNextArticulatedPart();
-		return v;
-	}
-
-	/**
-	 * Get the next real (non-articulated part) vehicle in the consist.
-	 * @return Next vehicle in the consist.
-	 */
-	FORCEINLINE Train *GetNextVehicle() const
-	{
-		const Train *v = this;
-		while (v->HasArticulatedPart()) v = v->GetNextArticulatedPart();
-
-		/* v now contains the last artic part in the engine */
-		return v->Next();
-	}
-
-	/**
-	 * Get the previous real (non-articulated part) vehicle in the consist.
-	 * @return Previous vehicle in the consist.
-	 */
-	FORCEINLINE Train *GetPrevVehicle() const
-	{
-		Train *v = this->Previous();
-		while (v != NULL && v->IsArticulatedPart()) v = v->Previous();
-
-		return v;
-	}
-
-	/**
 	 * Get the next real (non-articulated part and non rear part of dualheaded engine) vehicle in the consist.
 	 * @return Next vehicle in the consist.
 	 */
--- a/src/vehicle_base.h
+++ b/src/vehicle_base.h
@@ -671,6 +671,75 @@
 	{
 		return this->Next() != NULL && this->Next()->IsArticulatedPart();
 	}
+
+	/**
+	 * Get the next part of an articulated engine.
+	 * @return Next part of the articulated engine.
+	 * @pre The vehicle is an articulated engine.
+	 */
+	FORCEINLINE Vehicle *GetNextArticulatedPart() const
+	{
+		assert(this->HasArticulatedPart());
+		return this->Next();
+	}
+
+	/**
+	 * Get the first part of an articulated engine.
+	 * @return First part of the engine.
+	 */
+	FORCEINLINE Vehicle *GetFirstEnginePart()
+	{
+		Vehicle *v = this;
+		while (v->IsArticulatedPart()) v = v->Previous();
+		return v;
+	}
+
+	/**
+	 * Get the first part of an articulated engine.
+	 * @return First part of the engine.
+	 */
+	FORCEINLINE const Vehicle *GetFirstEnginePart() const
+	{
+		const Vehicle *v = this;
+		while (v->IsArticulatedPart()) v = v->Previous();
+		return v;
+	}
+
+	/**
+	 * Get the last part of an articulated engine.
+	 * @return Last part of the engine.
+	 */
+	FORCEINLINE Vehicle *GetLastEnginePart()
+	{
+		Vehicle *v = this;
+		while (v->HasArticulatedPart()) v = v->GetNextArticulatedPart();
+		return v;
+	}
+
+	/**
+	 * Get the next real (non-articulated part) vehicle in the consist.
+	 * @return Next vehicle in the consist.
+	 */
+	FORCEINLINE Vehicle *GetNextVehicle() const
+	{
+		const Vehicle *v = this;
+		while (v->HasArticulatedPart()) v = v->GetNextArticulatedPart();
+
+		/* v now contains the last articulated part in the engine */
+		return v->Next();
+	}
+
+	/**
+	 * Get the previous real (non-articulated part) vehicle in the consist.
+	 * @return Previous vehicle in the consist.
+	 */
+	FORCEINLINE Vehicle *GetPrevVehicle() const
+	{
+		Vehicle *v = this->Previous();
+		while (v != NULL && v->IsArticulatedPart()) v = v->Previous();
+
+		return v;
+	}
 };
 
 #define FOR_ALL_VEHICLES_FROM(var, start) FOR_ALL_ITEMS_FROM(Vehicle, vehicle_index, var, start)
@@ -721,6 +790,49 @@
 	 */
 	FORCEINLINE T *Previous() const { return (T *)this->Vehicle::Previous(); }
 
+	/**
+	 * Get the next part of an articulated engine.
+	 * @return Next part of the articulated engine.
+	 * @pre The vehicle is an articulated engine.
+	 */
+	FORCEINLINE T *GetNextArticulatedPart() { return (T *)this->Vehicle::GetNextArticulatedPart(); }
+
+	/**
+	 * Get the next part of an articulated engine.
+	 * @return Next part of the articulated engine.
+	 * @pre The vehicle is an articulated engine.
+	 */
+	FORCEINLINE T *GetNextArticulatedPart() const { return (T *)this->Vehicle::GetNextArticulatedPart(); }
+
+	/**
+	 * Get the first part of an articulated engine.
+	 * @return First part of the engine.
+	 */
+	FORCEINLINE T *GetFirstEnginePart() { return (T *)this->Vehicle::GetFirstEnginePart(); }
+
+	/**
+	 * Get the first part of an articulated engine.
+	 * @return First part of the engine.
+	 */
+	FORCEINLINE const T *GetFirstEnginePart() const { return (const T *)this->Vehicle::GetFirstEnginePart(); }
+
+	/**
+	 * Get the last part of an articulated engine.
+	 * @return Last part of the engine.
+	 */
+	FORCEINLINE T *GetLastEnginePart() { return (T *)this->Vehicle::GetLastEnginePart(); }
+
+	/**
+	 * Get the next real (non-articulated part) vehicle in the consist.
+	 * @return Next vehicle in the consist.
+	 */
+	FORCEINLINE T *GetNextVehicle() const { return (T *)this->Vehicle::GetNextVehicle(); }
+
+	/**
+	 * Get the previous real (non-articulated part) vehicle in the consist.
+	 * @return Previous vehicle in the consist.
+	 */
+	FORCEINLINE T *GetPrevVehicle() const { return (T *)this->Vehicle::GetPrevVehicle(); }
 
 	/**
 	 * Tests whether given index is a valid index for vehicle of this type