changeset 15159:5930e0240c61 draft

(svn r19788) -Codechange: make FOR_EACH_SET_BIT not change the value of the passed bit variable, i.e. allow expressions as parameter
author rubidium <rubidium@openttd.org>
date Tue, 11 May 2010 20:48:06 +0000
parents b4516bc34b30
children 771499c17fd3
files src/core/bitmath_func.hpp src/pathfinder/follow_track.hpp src/pathfinder/npf/npf.cpp src/station_gui.cpp src/water_cmd.cpp
diffstat 5 files changed, 12 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/src/core/bitmath_func.hpp
+++ b/src/core/bitmath_func.hpp
@@ -310,16 +310,18 @@
  * Do an operation for each set set bit in a value.
  *
  * This macros is used to do an operation for each set
- * bit in a variable. The first variable can be reused
- * in the operation due to it's the bit position counter.
- * The second variable will be cleared during the usage
+ * bit in a variable. The first parameter is a variable
+ * that is used as the bit position counter.
+ * The second parameter is an expression of the bits
+ * we need to iterate over. This expression will be
+ * evaluated once.
  *
  * @param i The position counter
  * @param b The value which we check for set bits
  */
 #define FOR_EACH_SET_BIT(i, b)      \
-	for (i = 0; b != 0; i++, b >>= 1) \
-		if (b & 1)
+	for (uint __FESB_bits = (i = 0, b); __FESB_bits != 0; i++, __FESB_bits >>= 1) \
+		if (__FESB_bits & 1)
 
 
 #if defined(__APPLE__)
--- a/src/pathfinder/follow_track.hpp
+++ b/src/pathfinder/follow_track.hpp
@@ -160,9 +160,8 @@
 		/* Mask already reserved trackdirs. */
 		m_new_td_bits &= ~TrackBitsToTrackdirBits(reserved);
 		/* Mask out all trackdirs that conflict with the reservation. */
-		uint bits = (uint)TrackdirBitsToTrackBits(m_new_td_bits);
 		int i;
-		FOR_EACH_SET_BIT(i, bits) {
+		FOR_EACH_SET_BIT(i, TrackdirBitsToTrackBits(m_new_td_bits)) {
 			if (TracksOverlap(reserved | TrackToTrackBits((Track)i))) m_new_td_bits &= ~TrackToTrackdirBits((Track)i);
 		}
 		if (m_new_td_bits == TRACKDIR_BIT_NONE) {
--- a/src/pathfinder/npf/npf.cpp
+++ b/src/pathfinder/npf/npf.cpp
@@ -921,9 +921,8 @@
 		TrackBits reserved = GetReservedTrackbits(dst_tile);
 		trackdirbits &= ~TrackBitsToTrackdirBits(reserved);
 
-		uint bits = TrackdirBitsToTrackBits(trackdirbits);
 		int i;
-		FOR_EACH_SET_BIT(i, bits) {
+		FOR_EACH_SET_BIT(i, TrackdirBitsToTrackBits(trackdirbits)) {
 			if (TracksOverlap(reserved | TrackToTrackBits((Track)i))) trackdirbits &= ~TrackToTrackdirBits((Track)i);
 		}
 	}
--- a/src/station_gui.cpp
+++ b/src/station_gui.cpp
@@ -567,7 +567,7 @@
 					FOR_EACH_SET_BIT(i, this->facilities) {
 						this->RaiseWidget(i + SLW_TRAIN);
 					}
-					SetBit(this->facilities, widget - SLW_TRAIN);
+					this->facilities = 1 << (widget - SLW_TRAIN);
 					this->LowerWidget(widget);
 				}
 				this->stations.ForceRebuild();
--- a/src/water_cmd.cpp
+++ b/src/water_cmd.cpp
@@ -1008,9 +1008,8 @@
 
 		case FLOOD_DRYUP: {
 			Slope slope_here = GetFoundationSlope(tile, NULL) & ~SLOPE_HALFTILE_MASK & ~SLOPE_STEEP;
-			uint check_dirs = _flood_from_dirs[slope_here];
 			uint dir;
-			FOR_EACH_SET_BIT(dir, check_dirs) {
+			FOR_EACH_SET_BIT(dir, _flood_from_dirs[slope_here]) {
 				TileIndex dest = AddTileIndexDiffCWrap(tile, TileIndexDiffCByDir((Direction)dir));
 				if (dest == INVALID_TILE) continue;
 
@@ -1048,9 +1047,8 @@
 					break;
 
 				default:
-					uint check_dirs = _flood_from_dirs[slope & ~SLOPE_STEEP];
 					uint dir;
-					FOR_EACH_SET_BIT(dir, check_dirs) {
+					FOR_EACH_SET_BIT(dir, _flood_from_dirs[slope & ~SLOPE_STEEP]) {
 						TileIndex dest = TILE_ADD(tile, TileOffsByDir((Direction)dir));
 						Slope slope_dest = GetTileSlope(dest, NULL) & ~SLOPE_STEEP;
 						if (slope_dest == SLOPE_FLAT || IsSlopeWithOneCornerRaised(slope_dest)) {