From b2def962481476a5fae92f8539e5f061022bf442 Mon Sep 17 00:00:00 2001 From: tron Date: Fri, 19 Jan 2007 11:47:48 +0000 Subject: [PATCH] (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 | 16 ++++++++-------- src/helpers.hpp | 16 +++++----------- src/oldloader.cpp | 2 +- src/pathfind.cpp | 6 +++--- src/train_cmd.cpp | 14 ++++++-------- src/viewport.cpp | 4 ++-- 6 files changed, 25 insertions(+), 33 deletions(-) diff --git a/src/ai/default/default.cpp b/src/ai/default/default.cpp index 09e2f68a4b..63a3ff556d 100644 --- a/src/ai/default/default.cpp +++ b/src/ai/default/default.cpp @@ -2291,10 +2291,10 @@ static void AiStateBuildRail(Player *p) 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 @@ static void AiStateBuildRoad(Player *p) 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 --git a/src/helpers.hpp b/src/helpers.hpp index 14b6056b35..2fed51582e 100644 --- a/src/helpers.hpp +++ b/src/helpers.hpp @@ -30,14 +30,13 @@ template FORCEINLINE T* ReallocT(T* t_ptr, size_t num_elements) 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 struct TinyEnumT } }; -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 --git a/src/oldloader.cpp b/src/oldloader.cpp index 50dbeeea06..5cf2973d68 100644 --- a/src/oldloader.cpp +++ b/src/oldloader.cpp @@ -310,7 +310,7 @@ static void FixOldStations(void) 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 --git a/src/pathfind.cpp b/src/pathfind.cpp index 9553946037..056d961558 100644 --- a/src/pathfind.cpp +++ b/src/pathfind.cpp @@ -932,11 +932,11 @@ start_at: 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 --git a/src/train_cmd.cpp b/src/train_cmd.cpp index 00c998f8d5..750745081b 100644 --- a/src/train_cmd.cpp +++ b/src/train_cmd.cpp @@ -1594,19 +1594,17 @@ static void ReverseTrainSwapVeh(Vehicle *v, int l, int r) 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 --git a/src/viewport.cpp b/src/viewport.cpp index 5d9b395e9b..5be898e4c5 100644 --- a/src/viewport.cpp +++ b/src/viewport.cpp @@ -2065,7 +2065,7 @@ static int CalcHeightdiff(HighLightStyle style, uint distance, TileIndex start_t 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 @@ static int CalcHeightdiff(HighLightStyle style, uint distance, TileIndex start_t } 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; }