From d7637cd70f9aa888c518a40b5c3b3d488dad6f1e Mon Sep 17 00:00:00 2001 From: Michael Steenbeek Date: Mon, 11 May 2020 14:32:56 +0200 Subject: [PATCH] Replace many C-style casts (#11720) --- src/openrct2-ui/windows/Ride.cpp | 4 ++-- src/openrct2/actions/GuestSetFlagsAction.hpp | 4 ++-- src/openrct2/actions/RideEntranceExitPlaceAction.hpp | 4 ++-- .../actions/RideEntranceExitRemoveAction.hpp | 12 ++++++------ src/openrct2/actions/RideSetSetting.hpp | 4 ++-- src/openrct2/actions/StaffHireNewAction.hpp | 6 +++--- src/openrct2/actions/TrackPlaceAction.hpp | 8 ++++---- src/openrct2/common.h | 2 +- src/openrct2/core/Crypt.CNG.cpp | 4 ++-- src/openrct2/core/DataSerialiserTraits.h | 6 +++--- src/openrct2/core/FileIndex.hpp | 6 +++--- src/openrct2/drawing/Drawing.cpp | 2 +- src/openrct2/interface/Viewport.cpp | 4 +++- src/openrct2/paint/Paint.cpp | 12 ++++++------ src/openrct2/platform/Shared.cpp | 6 +++--- src/openrct2/platform/Windows.cpp | 4 ++-- src/openrct2/scripting/ScNetwork.hpp | 5 +++-- src/openrct2/world/Location.hpp | 2 +- test/tests/sawyercoding_test.cpp | 4 ++-- 19 files changed, 51 insertions(+), 48 deletions(-) diff --git a/src/openrct2-ui/windows/Ride.cpp b/src/openrct2-ui/windows/Ride.cpp index 8d2e0324a3..0a1fdcd81b 100644 --- a/src/openrct2-ui/windows/Ride.cpp +++ b/src/openrct2-ui/windows/Ride.cpp @@ -4027,7 +4027,7 @@ static void window_ride_maintenance_mousedown(rct_window* w, rct_widgetindex wid gDropdownItemsArgs[0] = STR_DEBUG_FIX_RIDE; for (int32_t i = 0; i < 8; i++) { - assert(j < (int32_t)std::size(rideEntry->ride_type)); + assert(j < static_cast(std::size(rideEntry->ride_type))); if (RideTypeDescriptors[rideEntry->ride_type[j]].AvailableBreakdowns & static_cast(1 << i)) { if (i == BREAKDOWN_BRAKES_FAILURE @@ -4170,7 +4170,7 @@ static void window_ride_maintenance_dropdown(rct_window* w, rct_widgetindex widg int32_t num_items = 1; for (i = 0; i < BREAKDOWN_COUNT; i++) { - assert(j < (int32_t)std::size(rideEntry->ride_type)); + assert(j < static_cast(std::size(rideEntry->ride_type))); if (RideTypeDescriptors[rideEntry->ride_type[j]].AvailableBreakdowns & static_cast(1 << i)) { if (i == BREAKDOWN_BRAKES_FAILURE diff --git a/src/openrct2/actions/GuestSetFlagsAction.hpp b/src/openrct2/actions/GuestSetFlagsAction.hpp index 56a1b86fb9..3bbaa3e1cf 100644 --- a/src/openrct2/actions/GuestSetFlagsAction.hpp +++ b/src/openrct2/actions/GuestSetFlagsAction.hpp @@ -48,7 +48,7 @@ public: Peep* peep = GET_PEEP(_peepId); if (peep == nullptr) { - log_error("Used invalid sprite index for peep: %u", (uint32_t)_peepId); + log_error("Used invalid sprite index for peep: %u", static_cast(_peepId)); return MakeResult(GA_ERROR::INVALID_PARAMETERS, STR_CANT_CHANGE_THIS); } return std::make_unique(); @@ -59,7 +59,7 @@ public: Peep* peep = GET_PEEP(_peepId); if (peep == nullptr) { - log_error("Used invalid sprite index for peep: %u", (uint32_t)_peepId); + log_error("Used invalid sprite index for peep: %u", static_cast(_peepId)); return MakeResult(GA_ERROR::INVALID_PARAMETERS, STR_CANT_CHANGE_THIS); } diff --git a/src/openrct2/actions/RideEntranceExitPlaceAction.hpp b/src/openrct2/actions/RideEntranceExitPlaceAction.hpp index da7a5659e6..9f45b6849d 100644 --- a/src/openrct2/actions/RideEntranceExitPlaceAction.hpp +++ b/src/openrct2/actions/RideEntranceExitPlaceAction.hpp @@ -73,7 +73,7 @@ public: auto ride = get_ride(_rideIndex); if (ride == nullptr) { - log_warning("Invalid game command for ride %d", (int32_t)_rideIndex); + log_warning("Invalid game command for ride %d", static_cast(_rideIndex)); return MakeResult(GA_ERROR::INVALID_PARAMETERS, errorTitle); } @@ -148,7 +148,7 @@ public: auto ride = get_ride(_rideIndex); if (ride == nullptr) { - log_warning("Invalid game command for ride %d", (int32_t)_rideIndex); + log_warning("Invalid game command for ride %d", static_cast(_rideIndex)); return MakeResult(GA_ERROR::INVALID_PARAMETERS, errorTitle); } diff --git a/src/openrct2/actions/RideEntranceExitRemoveAction.hpp b/src/openrct2/actions/RideEntranceExitRemoveAction.hpp index 235d632ec1..4e5082e6a1 100644 --- a/src/openrct2/actions/RideEntranceExitRemoveAction.hpp +++ b/src/openrct2/actions/RideEntranceExitRemoveAction.hpp @@ -58,7 +58,7 @@ public: auto ride = get_ride(_rideIndex); if (ride == nullptr) { - log_warning("Invalid ride id %d for entrance/exit removal", (int32_t)_rideIndex); + log_warning("Invalid ride id %d for entrance/exit removal", static_cast(_rideIndex)); return std::make_unique(GA_ERROR::INVALID_PARAMETERS, STR_NONE); } @@ -108,8 +108,8 @@ public: if (!found) { log_warning( - "Track Element not found. x = %d, y = %d, ride = %d, station = %d", _loc.x, _loc.y, (int32_t)_rideIndex, - _stationNum); + "Track Element not found. x = %d, y = %d, ride = %d, station = %d", _loc.x, _loc.y, + static_cast(_rideIndex), _stationNum); return MakeResult(GA_ERROR::INVALID_PARAMETERS, STR_NONE); } @@ -121,7 +121,7 @@ public: auto ride = get_ride(_rideIndex); if (ride == nullptr) { - log_warning("Invalid ride id %d for entrance/exit removal", (int32_t)_rideIndex); + log_warning("Invalid ride id %d for entrance/exit removal", static_cast(_rideIndex)); return std::make_unique(GA_ERROR::INVALID_PARAMETERS, STR_NONE); } @@ -168,8 +168,8 @@ public: if (!found) { log_warning( - "Track Element not found. x = %d, y = %d, ride = %d, station = %d", _loc.x, _loc.y, (int32_t)_rideIndex, - _stationNum); + "Track Element not found. x = %d, y = %d, ride = %d, station = %d", _loc.x, _loc.y, + static_cast(_rideIndex), _stationNum); return MakeResult(GA_ERROR::INVALID_PARAMETERS, STR_NONE); } diff --git a/src/openrct2/actions/RideSetSetting.hpp b/src/openrct2/actions/RideSetSetting.hpp index ec90231c0a..da4a43413f 100644 --- a/src/openrct2/actions/RideSetSetting.hpp +++ b/src/openrct2/actions/RideSetSetting.hpp @@ -70,7 +70,7 @@ public: auto ride = get_ride(_rideIndex); if (ride == nullptr) { - log_warning("Invalid ride: #%d.", (int32_t)_rideIndex); + log_warning("Invalid ride: #%d.", static_cast(_rideIndex)); return MakeResult(GA_ERROR::INVALID_PARAMETERS, STR_CANT_CHANGE_OPERATING_MODE); } @@ -175,7 +175,7 @@ public: auto ride = get_ride(_rideIndex); if (ride == nullptr) { - log_warning("Invalid ride: #%d.", (int32_t)_rideIndex); + log_warning("Invalid ride: #%d.", static_cast(_rideIndex)); return MakeResult(GA_ERROR::INVALID_PARAMETERS, STR_CANT_CHANGE_OPERATING_MODE); } diff --git a/src/openrct2/actions/StaffHireNewAction.hpp b/src/openrct2/actions/StaffHireNewAction.hpp index 1565e3bfb5..124564d020 100644 --- a/src/openrct2/actions/StaffHireNewAction.hpp +++ b/src/openrct2/actions/StaffHireNewAction.hpp @@ -100,7 +100,7 @@ private: if (_staffType >= STAFF_TYPE_COUNT) { // Invalid staff type. - log_error("Tried to use invalid staff type: %u", (uint32_t)_staffType); + log_error("Tried to use invalid staff type: %u", static_cast(_staffType)); return MakeResult(GA_ERROR::INVALID_PARAMETERS, STR_NONE); } @@ -115,7 +115,7 @@ private: if (_entertainerType >= ENTERTAINER_COSTUME_COUNT) { // Invalid entertainer costume - log_error("Tried to use invalid entertainer type: %u", (uint32_t)_entertainerType); + log_error("Tried to use invalid entertainer type: %u", static_cast(_entertainerType)); return MakeResult(GA_ERROR::INVALID_PARAMETERS, STR_NONE); } @@ -124,7 +124,7 @@ private: if (!(availableCostumes & (1 << _entertainerType))) { // Entertainer costume unavailable - log_error("Tried to use unavailable entertainer type: %u", (uint32_t)_entertainerType); + log_error("Tried to use unavailable entertainer type: %u", static_cast(_entertainerType)); return MakeResult(GA_ERROR::INVALID_PARAMETERS, STR_NONE); } diff --git a/src/openrct2/actions/TrackPlaceAction.hpp b/src/openrct2/actions/TrackPlaceAction.hpp index 41b34a2278..3ed60566c5 100644 --- a/src/openrct2/actions/TrackPlaceAction.hpp +++ b/src/openrct2/actions/TrackPlaceAction.hpp @@ -104,13 +104,13 @@ public: auto ride = get_ride(_rideIndex); if (ride == nullptr) { - log_warning("Invalid ride for track placement, rideIndex = %d", (int32_t)_rideIndex); + log_warning("Invalid ride for track placement, rideIndex = %d", static_cast(_rideIndex)); return std::make_unique(GA_ERROR::INVALID_PARAMETERS, STR_NONE); } rct_ride_entry* rideEntry = get_ride_entry(ride->subtype); if (rideEntry == nullptr) { - log_warning("Invalid ride subtype for track placement, rideIndex = %d", (int32_t)_rideIndex); + log_warning("Invalid ride subtype for track placement, rideIndex = %d", static_cast(_rideIndex)); return std::make_unique(GA_ERROR::INVALID_PARAMETERS, STR_NONE); } @@ -429,14 +429,14 @@ public: auto ride = get_ride(_rideIndex); if (ride == nullptr) { - log_warning("Invalid ride for track placement, rideIndex = %d", (int32_t)_rideIndex); + log_warning("Invalid ride for track placement, rideIndex = %d", static_cast(_rideIndex)); return std::make_unique(GA_ERROR::INVALID_PARAMETERS); } rct_ride_entry* rideEntry = get_ride_entry(ride->subtype); if (rideEntry == nullptr) { - log_warning("Invalid ride subtype for track placement, rideIndex = %d", (int32_t)_rideIndex); + log_warning("Invalid ride subtype for track placement, rideIndex = %d", static_cast(_rideIndex)); return std::make_unique(GA_ERROR::INVALID_PARAMETERS); } diff --git a/src/openrct2/common.h b/src/openrct2/common.h index ad181c3fc2..992fffd4e5 100644 --- a/src/openrct2/common.h +++ b/src/openrct2/common.h @@ -112,7 +112,7 @@ char* strndup(const char* src, size_t size); // Time (represented as number of 100-nanosecond intervals since 0001-01-01T00:00:00Z) using datetime64 = uint64_t; -#define DATETIME64_MIN ((datetime64)0) +constexpr const datetime64 DATETIME64_MIN = 0; // Represent fixed point numbers. dp = decimal point using fixed8_1dp = uint8_t; diff --git a/src/openrct2/core/Crypt.CNG.cpp b/src/openrct2/core/Crypt.CNG.cpp index b26b3c483f..ca35f4667d 100644 --- a/src/openrct2/core/Crypt.CNG.cpp +++ b/src/openrct2/core/Crypt.CNG.cpp @@ -268,12 +268,12 @@ public: { if (value < 128) { - _buffer.push_back((uint8_t)value); + _buffer.push_back(static_cast(value)); } else if (value <= std::numeric_limits().max()) { _buffer.push_back(0b10000001); - _buffer.push_back((uint8_t)value); + _buffer.push_back(static_cast(value)); } else if (value <= std::numeric_limits().max()) { diff --git a/src/openrct2/core/DataSerialiserTraits.h b/src/openrct2/core/DataSerialiserTraits.h index df11d757b4..a05382668e 100644 --- a/src/openrct2/core/DataSerialiserTraits.h +++ b/src/openrct2/core/DataSerialiserTraits.h @@ -260,7 +260,7 @@ template struct DataSerializerTraitsPODArray { static void encode(IStream* stream, const _Ty (&val)[_Size]) { - uint16_t len = (uint16_t)_Size; + uint16_t len = static_cast(_Size); uint16_t swapped = ByteSwapBE(len); stream->Write(&swapped); @@ -318,7 +318,7 @@ template struct DataSerializerTraits& val) { - uint16_t len = (uint16_t)_Size; + uint16_t len = static_cast(_Size); uint16_t swapped = ByteSwapBE(len); stream->Write(&swapped); @@ -360,7 +360,7 @@ template struct DataSerializerTraits> { static void encode(IStream* stream, const std::vector<_Ty>& val) { - uint16_t len = (uint16_t)val.size(); + uint16_t len = static_cast(val.size()); uint16_t swapped = ByteSwapBE(len); stream->Write(&swapped); diff --git a/src/openrct2/core/FileIndex.hpp b/src/openrct2/core/FileIndex.hpp index 07ca60305a..5d98acb10c 100644 --- a/src/openrct2/core/FileIndex.hpp +++ b/src/openrct2/core/FileIndex.hpp @@ -160,8 +160,8 @@ private: stats.TotalFiles++; stats.TotalFileSize += fileInfo->Size; - stats.FileDateModifiedChecksum ^= (uint32_t)(fileInfo->LastModified >> 32) - ^ (uint32_t)(fileInfo->LastModified & 0xFFFFFFFF); + stats.FileDateModifiedChecksum ^= static_cast(fileInfo->LastModified >> 32) + ^ static_cast(fileInfo->LastModified & 0xFFFFFFFF); stats.FileDateModifiedChecksum = ror32(stats.FileDateModifiedChecksum, 5); stats.PathChecksum += GetPathChecksum(path); } @@ -309,7 +309,7 @@ private: header.VersionB = _version; header.LanguageId = language; header.Stats = stats; - header.NumItems = (uint32_t)items.size(); + header.NumItems = static_cast(items.size()); fs.WriteValue(header); // Write items diff --git a/src/openrct2/drawing/Drawing.cpp b/src/openrct2/drawing/Drawing.cpp index 601f9ae66e..c80dd992af 100644 --- a/src/openrct2/drawing/Drawing.cpp +++ b/src/openrct2/drawing/Drawing.cpp @@ -572,7 +572,7 @@ void load_palette() if (water_type != nullptr) { - openrct2_assert(water_type->image_id != (uint32_t)-1, "Failed to load water palette"); + openrct2_assert(water_type->image_id != 0xFFFFFFFF, "Failed to load water palette"); palette = water_type->image_id; } diff --git a/src/openrct2/interface/Viewport.cpp b/src/openrct2/interface/Viewport.cpp index f4940da05c..4fb72314e9 100644 --- a/src/openrct2/interface/Viewport.cpp +++ b/src/openrct2/interface/Viewport.cpp @@ -1885,7 +1885,9 @@ uint8_t get_current_rotation() #if defined(DEBUG_LEVEL_1) && DEBUG_LEVEL_1 if (rotation != rotation_masked) { - log_error("Found wrong rotation %d! Will return %d instead.", (uint32_t)rotation, (uint32_t)rotation_masked); + log_error( + "Found wrong rotation %d! Will return %d instead.", static_cast(rotation), + static_cast(rotation_masked)); } #endif // DEBUG_LEVEL_1 return rotation_masked; diff --git a/src/openrct2/paint/Paint.cpp b/src/openrct2/paint/Paint.cpp index 30a1f07e48..0a43281a34 100644 --- a/src/openrct2/paint/Paint.cpp +++ b/src/openrct2/paint/Paint.cpp @@ -714,8 +714,8 @@ paint_struct* sub_98196C( paint_session* session, uint32_t image_id, int8_t x_offset, int8_t y_offset, int16_t bound_box_length_x, int16_t bound_box_length_y, int8_t bound_box_length_z, int16_t z_offset) { - assert((uint16_t)bound_box_length_x == (int16_t)bound_box_length_x); - assert((uint16_t)bound_box_length_y == (int16_t)bound_box_length_y); + assert(static_cast(bound_box_length_x) == static_cast(bound_box_length_x)); + assert(static_cast(bound_box_length_y) == static_cast(bound_box_length_y)); session->LastRootPS = nullptr; session->UnkF1AD2C = nullptr; @@ -923,8 +923,8 @@ paint_struct* sub_98198C( int16_t bound_box_length_y, int8_t bound_box_length_z, int16_t z_offset, int16_t bound_box_offset_x, int16_t bound_box_offset_y, int16_t bound_box_offset_z) { - assert((uint16_t)bound_box_length_x == bound_box_length_x); - assert((uint16_t)bound_box_length_y == bound_box_length_y); + assert(static_cast(bound_box_length_x) == static_cast(bound_box_length_x)); + assert(static_cast(bound_box_length_y) == static_cast(bound_box_length_y)); session->LastRootPS = nullptr; session->UnkF1AD2C = nullptr; @@ -965,8 +965,8 @@ paint_struct* sub_98199C( int16_t bound_box_length_y, int8_t bound_box_length_z, int16_t z_offset, int16_t bound_box_offset_x, int16_t bound_box_offset_y, int16_t bound_box_offset_z) { - assert((uint16_t)bound_box_length_x == (int16_t)bound_box_length_x); - assert((uint16_t)bound_box_length_y == (int16_t)bound_box_length_y); + assert(static_cast(bound_box_length_x) == static_cast(bound_box_length_x)); + assert(static_cast(bound_box_length_y) == static_cast(bound_box_length_y)); if (session->LastRootPS == nullptr) { diff --git a/src/openrct2/platform/Shared.cpp b/src/openrct2/platform/Shared.cpp index 8f5e64ad45..4710e5d036 100644 --- a/src/openrct2/platform/Shared.cpp +++ b/src/openrct2/platform/Shared.cpp @@ -144,7 +144,7 @@ static void platform_ticks_init() #ifdef _WIN32 LARGE_INTEGER freq; QueryPerformanceFrequency(&freq); - _frequency = (uint32_t)(freq.QuadPart / 1000); + _frequency = static_cast(freq.QuadPart / 1000); QueryPerformanceCounter(&_entryTimestamp); #endif } @@ -158,9 +158,9 @@ uint32_t platform_get_ticks() LARGE_INTEGER runningDelta; runningDelta.QuadPart = pfc.QuadPart - _entryTimestamp.QuadPart; - return (uint32_t)(runningDelta.QuadPart / _frequency); + return static_cast(runningDelta.QuadPart / _frequency); #elif defined(__APPLE__) && (__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ < 101200) - return (uint32_t)(((mach_absolute_time() * _mach_base_info.numer) / _mach_base_info.denom) / 1000000); + return static_cast(((mach_absolute_time() * _mach_base_info.numer) / _mach_base_info.denom) / 1000000); #else struct timespec ts; if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) diff --git a/src/openrct2/platform/Windows.cpp b/src/openrct2/platform/Windows.cpp index 957f53544f..4f22404a02 100644 --- a/src/openrct2/platform/Windows.cpp +++ b/src/openrct2/platform/Windows.cpp @@ -402,8 +402,8 @@ uint8_t platform_get_locale_date_format() wchar_t first[sizeof(dateFormat)]; wchar_t second[sizeof(dateFormat)]; if (swscanf_s( - dateFormat, L"%l[dyM]%*l[^dyM]%l[dyM]%*l[^dyM]%*l[dyM]", first, (uint32_t)std::size(first), second, - (uint32_t)std::size(second)) + dateFormat, L"%l[dyM]%*l[^dyM]%l[dyM]%*l[^dyM]%*l[dyM]", first, static_cast(std::size(first)), second, + static_cast(std::size(second))) != 2) { return DATE_FORMAT_DAY_MONTH_YEAR; diff --git a/src/openrct2/scripting/ScNetwork.hpp b/src/openrct2/scripting/ScNetwork.hpp index 3fae022955..84bf5a1cc1 100644 --- a/src/openrct2/scripting/ScNetwork.hpp +++ b/src/openrct2/scripting/ScNetwork.hpp @@ -110,11 +110,12 @@ namespace OpenRCT2::Scripting for (size_t i = 0; i < enabledPermissions.size(); i++) { - auto toggle = (enabledPermissions[i] != (network_can_perform_action(groupIndex, (uint32_t)i) != 0)); + auto toggle + = (enabledPermissions[i] != (network_can_perform_action(groupIndex, static_cast(i)) != 0)); if (toggle) { auto networkAction2 = NetworkModifyGroupAction( - ModifyGroupType::SetPermissions, _id, "", (uint32_t)i, PermissionState::Toggle); + ModifyGroupType::SetPermissions, _id, "", static_cast(i), PermissionState::Toggle); GameActions::Execute(&networkAction2); } } diff --git a/src/openrct2/world/Location.hpp b/src/openrct2/world/Location.hpp index d2222e66d6..48d694ab54 100644 --- a/src/openrct2/world/Location.hpp +++ b/src/openrct2/world/Location.hpp @@ -13,7 +13,7 @@ #include -#define LOCATION_NULL ((int16_t)(uint16_t)0x8000) +constexpr const int16_t LOCATION_NULL = -32768; constexpr const int32_t COORDS_XY_STEP = 32; constexpr const int32_t COORDS_Z_STEP = 8; diff --git a/test/tests/sawyercoding_test.cpp b/test/tests/sawyercoding_test.cpp index c2f5f416a7..7aeea21cb3 100644 --- a/test/tests/sawyercoding_test.cpp +++ b/test/tests/sawyercoding_test.cpp @@ -37,7 +37,7 @@ protected: MemoryStream ms(encodedDataBuffer, encodedDataSize); SawyerChunkReader reader(&ms); auto chunk = reader.ReadChunk(); - ASSERT_EQ((uint8_t)chunk->GetEncoding(), chdr_in.encoding); + ASSERT_EQ(static_cast(chunk->GetEncoding()), chdr_in.encoding); ASSERT_EQ(chunk->GetLength(), chdr_in.length); auto result = memcmp(chunk->GetData(), randomdata, sizeof(randomdata)); ASSERT_EQ(result, 0); @@ -54,7 +54,7 @@ protected: MemoryStream ms(data, size); SawyerChunkReader reader(&ms); auto chunk = reader.ReadChunk(); - ASSERT_EQ((uint8_t)chunk->GetEncoding(), chdr_in->encoding); + ASSERT_EQ(static_cast(chunk->GetEncoding()), chdr_in->encoding); ASSERT_EQ(chunk->GetLength(), sizeof(randomdata)); auto result = memcmp(chunk->GetData(), randomdata, sizeof(randomdata)); ASSERT_EQ(result, 0);