# HG changeset patch # User tron # Date 1169207268 0 # Node ID fbef81292ff9550969caf569fc8a042e8f488634 # Parent 8fb0a46d60b82c893fd8361ded1441a8fb33a513 (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. diff -r 8fb0a46d60b8 -r fbef81292ff9 src/ai/default/default.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; } diff -r 8fb0a46d60b8 -r fbef81292ff9 src/helpers.hpp --- 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 void SwapT(T *a, T *b); -template FORCEINLINE void SwapT(T *a, T *b) +/** type safe swap operation */ +template 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 FORCEINLINE void SwapT(TinyEnumT *a, TinyEnumT *b) -{ - SwapT(&a->m_val, &b->m_val); -} - template FORCEINLINE T ClrBitT(T t, int bit_index) { int val = t; diff -r 8fb0a46d60b8 -r fbef81292ff9 src/oldloader.cpp --- 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 */ diff -r 8fb0a46d60b8 -r fbef81292ff9 src/pathfind.cpp --- 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; diff -r 8fb0a46d60b8 -r fbef81292ff9 src/train_cmd.cpp --- 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); diff -r 8fb0a46d60b8 -r fbef81292ff9 src/viewport.cpp --- 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; }