(svn r8276) -Fix
authortron
Fri, 19 Jan 2007 11:47:48 +0000
changeset 5733 388bb9dcb79b
parent 5732 20d674f37c75
child 5734 a2d4355c3a58
(svn r8276) -Fix

Change the signature of Swap() to be less error prone, i.e. pass the variables to be swapped by reference instead of passing pointers to the variables.
Just do Swap(x, y) instead of Swap(&x, &y). This prevents accidents when the variables are pointers.
src/ai/default/default.cpp
src/helpers.hpp
src/oldloader.cpp
src/pathfind.cpp
src/train_cmd.cpp
src/viewport.cpp
--- a/src/ai/default/default.cpp	Fri Jan 19 06:08:04 2007 +0000
+++ b/src/ai/default/default.cpp	Fri Jan 19 11:47:48 2007 +0000
@@ -2291,10 +2291,10 @@
 		AiBuildRail(p);
 
 		// Alternate between edges
-		SwapT(&p->ai.start_tile_a, &p->ai.start_tile_b);
-		SwapT(&p->ai.cur_tile_a, &p->ai.cur_tile_b);
-		SwapT(&p->ai.start_dir_a, &p->ai.start_dir_b);
-		SwapT(&p->ai.cur_dir_a, &p->ai.cur_dir_b);
+		Swap(p->ai.start_tile_a, p->ai.start_tile_b);
+		Swap(p->ai.cur_tile_a,   p->ai.cur_tile_b);
+		Swap(p->ai.start_dir_a,  p->ai.start_dir_b);
+		Swap(p->ai.cur_dir_a,    p->ai.cur_dir_b);
 		return;
 	}
 
@@ -3112,10 +3112,10 @@
 		AiBuildRoad(p);
 
 		// Alternate between edges
-		SwapT(&p->ai.start_tile_a, &p->ai.start_tile_b);
-		SwapT(&p->ai.cur_tile_a, &p->ai.cur_tile_b);
-		SwapT(&p->ai.start_dir_a, &p->ai.start_dir_b);
-		SwapT(&p->ai.cur_dir_a, &p->ai.cur_dir_b);
+		Swap(p->ai.start_tile_a, p->ai.start_tile_b);
+		Swap(p->ai.cur_tile_a,   p->ai.cur_tile_b);
+		Swap(p->ai.start_dir_a,  p->ai.start_dir_b);
+		Swap(p->ai.cur_dir_a,    p->ai.cur_dir_b);
 
 		return;
 	}
--- a/src/helpers.hpp	Fri Jan 19 06:08:04 2007 +0000
+++ b/src/helpers.hpp	Fri Jan 19 11:47:48 2007 +0000
@@ -30,14 +30,13 @@
 	return t_ptr;
 }
 
-/** type safe swap operation */
-template <typename T> void SwapT(T *a, T *b);
 
-template <typename T> FORCEINLINE void SwapT(T *a, T *b)
+/** type safe swap operation */
+template<typename T> void Swap(T& a, T& b)
 {
-	T t = *a;
-	*a = *b;
-	*b = t;
+	T t = a;
+	a = b;
+	b = t;
 }
 
 
@@ -141,11 +140,6 @@
 	}
 };
 
-template <typename Tenum_t> FORCEINLINE void SwapT(TinyEnumT<Tenum_t> *a, TinyEnumT<Tenum_t> *b)
-{
-	SwapT(&a->m_val, &b->m_val);
-}
-
 template <typename T> FORCEINLINE T ClrBitT(T t, int bit_index)
 {
 	int val = t;
--- a/src/oldloader.cpp	Fri Jan 19 06:08:04 2007 +0000
+++ b/src/oldloader.cpp	Fri Jan 19 11:47:48 2007 +0000
@@ -310,7 +310,7 @@
 	FOR_ALL_STATIONS(st) {
 		/* Check if we need to swap width and height for the station */
 		if (st->train_tile != 0 && GetRailStationAxis(st->train_tile) != AXIS_X) {
-			SwapT(&st->trainst_w, &st->trainst_h);
+			Swap(st->trainst_w, st->trainst_h);
 		}
 
 		/* Check if there is a bus or truck station, and convert to new format */
--- a/src/pathfind.cpp	Fri Jan 19 06:08:04 2007 +0000
+++ b/src/pathfind.cpp	Fri Jan 19 11:47:48 2007 +0000
@@ -932,11 +932,11 @@
 			assert(tpf->nstack == 1 || tpf->nstack == 2 || tpf->nstack == 3);
 			if (tpf->nstack != 1) {
 				uint32 r = Random();
-				if (r&1) SwapT(&tpf->stack[0].track, &tpf->stack[1].track);
+				if (r & 1) Swap(tpf->stack[0].track, tpf->stack[1].track);
 				if (tpf->nstack != 2) {
 					TrackdirByte t = tpf->stack[2].track;
-					if (r&2) SwapT(&tpf->stack[0].track, &t);
-					if (r&4) SwapT(&tpf->stack[1].track, &t);
+					if (r & 2) Swap(tpf->stack[0].track, t);
+					if (r & 4) Swap(tpf->stack[1].track, t);
 					tpf->stack[2].first_track = tpf->stack[2].track = t;
 				}
 				tpf->stack[0].first_track = tpf->stack[0].track;
--- a/src/train_cmd.cpp	Fri Jan 19 06:08:04 2007 +0000
+++ b/src/train_cmd.cpp	Fri Jan 19 11:47:48 2007 +0000
@@ -1594,19 +1594,17 @@
 			a->vehstatus = tmp;
 		}
 
-		/* swap variables */
-		SwapT(&a->u.rail.track, &b->u.rail.track);
-		SwapT(&a->direction, &b->direction);
+		Swap(a->u.rail.track, b->u.rail.track);
+		Swap(a->direction,    b->direction);
 
 		/* toggle direction */
 		if (!(a->u.rail.track & 0x80)) a->direction = ReverseDir(a->direction);
 		if (!(b->u.rail.track & 0x80)) b->direction = ReverseDir(b->direction);
 
-		/* swap more variables */
-		SwapT(&a->x_pos, &b->x_pos);
-		SwapT(&a->y_pos, &b->y_pos);
-		SwapT(&a->tile, &b->tile);
-		SwapT(&a->z_pos, &b->z_pos);
+		Swap(a->x_pos, b->x_pos);
+		Swap(a->y_pos, b->y_pos);
+		Swap(a->tile,  b->tile);
+		Swap(a->z_pos, b->z_pos);
 
 		SwapTrainFlags(&a->u.rail.flags, &b->u.rail.flags);
 
--- a/src/viewport.cpp	Fri Jan 19 06:08:04 2007 +0000
+++ b/src/viewport.cpp	Fri Jan 19 11:47:48 2007 +0000
@@ -2065,7 +2065,7 @@
 	uint h0, h1, ht; // start heigth, end height, and temp variable
 
 	if (start_tile == end_tile) return 0;
-	if (swap) SwapT(&start_tile, &end_tile);
+	if (swap) Swap(start_tile, end_tile);
 
 	switch (style & HT_DRAG_MASK) {
 		case HT_RECT: {
@@ -2125,7 +2125,7 @@
 		} break;
 	}
 
-	if (swap) SwapT(&h0, &h1);
+	if (swap) Swap(h0, h1);
 	/* Minimap shows height in intervals of 50 meters, let's do the same */
 	return (int)(h1 - h0) * 50;
 }