(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.
This commit is contained in:
tron 2007-01-19 11:47:48 +00:00
parent 7af3094bc3
commit b2def96248
6 changed files with 25 additions and 33 deletions

View File

@ -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;
}

View File

@ -30,14 +30,13 @@ template <typename T> FORCEINLINE T* ReallocT(T* t_ptr, size_t num_elements)
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> struct TinyEnumT
}
};
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;

View File

@ -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 */

View File

@ -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;

View File

@ -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);

View File

@ -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;
}