Convert most remaining C-style casts to C++-style ones (#11867)

This commit is contained in:
Michael Steenbeek
2020-06-07 23:18:11 +02:00
committed by GitHub
parent 7646f1b7e4
commit 9ef8d6da42
43 changed files with 126 additions and 107 deletions

View File

@@ -452,7 +452,7 @@ static std::pair<rct_string_id, void*> widget_get_stringid_and_args(const rct_wi
else
{
stringId = STR_STRING;
formatArgs = (void*)&widget->string;
formatArgs = reinterpret_cast<void*>(widget->string);
}
}
return std::make_pair(stringId, formatArgs);

View File

@@ -840,7 +840,7 @@ namespace OpenRCT2::Ui::Windows
else if (desc.Type == "dropdown")
{
widget.type = WWT_DROPDOWN;
if (desc.SelectedIndex >= 0 && (size_t)desc.SelectedIndex < desc.Items.size())
if (desc.SelectedIndex >= 0 && static_cast<size_t>(desc.SelectedIndex) < desc.Items.size())
{
widget.string = const_cast<utf8*>(desc.Items[desc.SelectedIndex].c_str());
}

View File

@@ -637,8 +637,7 @@ namespace OpenRCT2
// which the window function doesn't like
auto intent = Intent(WC_OBJECT_LOAD_ERROR);
intent.putExtra(INTENT_EXTRA_PATH, path);
// TODO: CAST-IMPROVEMENT-NEEDED
intent.putExtra(INTENT_EXTRA_LIST, (void*)e.MissingObjects.data());
intent.putExtra(INTENT_EXTRA_LIST, const_cast<rct_object_entry*>(e.MissingObjects.data()));
intent.putExtra(INTENT_EXTRA_LIST_COUNT, static_cast<uint32_t>(e.MissingObjects.size()));
auto windowManager = _uiContext->GetWindowManager();

View File

@@ -95,7 +95,7 @@ static bool TryClassifyAsS4(IStream* stream, ClassifiedFileInfo* result)
uint64_t originalPosition = stream->GetPosition();
try
{
size_t dataLength = (size_t)stream->GetLength();
size_t dataLength = static_cast<size_t>(stream->GetLength());
auto deleter_lambda = [dataLength](uint8_t* ptr) { Memory::FreeArray(ptr, dataLength); };
std::unique_ptr<uint8_t, decltype(deleter_lambda)> data(stream->ReadArray<uint8_t>(dataLength), deleter_lambda);
stream->SetPosition(originalPosition);
@@ -132,7 +132,7 @@ static bool TryClassifyAsTD4_TD6(IStream* stream, ClassifiedFileInfo* result)
uint64_t originalPosition = stream->GetPosition();
try
{
size_t dataLength = (size_t)stream->GetLength();
size_t dataLength = static_cast<size_t>(stream->GetLength());
auto deleter_lambda = [dataLength](uint8_t* ptr) { Memory::FreeArray(ptr, dataLength); };
std::unique_ptr<uint8_t, decltype(deleter_lambda)> data(stream->ReadArray<uint8_t>(dataLength), deleter_lambda);
stream->SetPosition(originalPosition);

View File

@@ -683,8 +683,7 @@ void save_game_as()
static int32_t compare_autosave_file_paths(const void* a, const void* b)
{
// TODO: CAST-IMPROVEMENT-NEEDED
return strcmp(*(char**)a, *(char**)b);
return strcmp(static_cast<const char*>(a), static_cast<const char*>(b));
}
static void limit_autosave_count(const size_t numberOfFilesToKeep, bool processLandscapeFolder)

View File

@@ -26,7 +26,7 @@ private:
public:
explicit PlatformEnvironment(DIRBASE_VALUES basePaths)
{
for (int32_t i = 0; i < DIRBASE_COUNT; i++)
for (size_t i = 0; i < DIRBASE_COUNT; i++)
{
_basePath[i] = basePaths[i];
}
@@ -124,35 +124,38 @@ std::unique_ptr<IPlatformEnvironment> OpenRCT2::CreatePlatformEnvironment()
// Set default paths
std::string basePaths[DIRBASE_COUNT];
basePaths[(size_t)DIRBASE::OPENRCT2] = Platform::GetInstallPath();
basePaths[(size_t)DIRBASE::USER] = Path::Combine(Platform::GetFolderPath(SPECIAL_FOLDER::USER_DATA), subDirectory);
basePaths[(size_t)DIRBASE::CONFIG] = Path::Combine(Platform::GetFolderPath(SPECIAL_FOLDER::USER_CONFIG), subDirectory);
basePaths[(size_t)DIRBASE::CACHE] = Path::Combine(Platform::GetFolderPath(SPECIAL_FOLDER::USER_CACHE), subDirectory);
basePaths[(size_t)DIRBASE::DOCUMENTATION] = Platform::GetDocsPath();
basePaths[static_cast<size_t>(DIRBASE::OPENRCT2)] = Platform::GetInstallPath();
basePaths[static_cast<size_t>(DIRBASE::USER)] = Path::Combine(
Platform::GetFolderPath(SPECIAL_FOLDER::USER_DATA), subDirectory);
basePaths[static_cast<size_t>(DIRBASE::CONFIG)] = Path::Combine(
Platform::GetFolderPath(SPECIAL_FOLDER::USER_CONFIG), subDirectory);
basePaths[static_cast<size_t>(DIRBASE::CACHE)] = Path::Combine(
Platform::GetFolderPath(SPECIAL_FOLDER::USER_CACHE), subDirectory);
basePaths[static_cast<size_t>(DIRBASE::DOCUMENTATION)] = Platform::GetDocsPath();
// Override paths that have been specified via the command line
if (!String::IsNullOrEmpty(gCustomRCT1DataPath))
{
basePaths[(size_t)DIRBASE::RCT1] = gCustomRCT1DataPath;
basePaths[static_cast<size_t>(DIRBASE::RCT1)] = gCustomRCT1DataPath;
}
if (!String::IsNullOrEmpty(gCustomRCT2DataPath))
{
basePaths[(size_t)DIRBASE::RCT2] = gCustomRCT2DataPath;
basePaths[static_cast<size_t>(DIRBASE::RCT2)] = gCustomRCT2DataPath;
}
if (!String::IsNullOrEmpty(gCustomOpenrctDataPath))
{
basePaths[(size_t)DIRBASE::OPENRCT2] = gCustomOpenrctDataPath;
basePaths[static_cast<size_t>(DIRBASE::OPENRCT2)] = gCustomOpenrctDataPath;
}
if (!String::IsNullOrEmpty(gCustomUserDataPath))
{
basePaths[(size_t)DIRBASE::USER] = gCustomUserDataPath;
basePaths[(size_t)DIRBASE::CONFIG] = gCustomUserDataPath;
basePaths[(size_t)DIRBASE::CACHE] = gCustomUserDataPath;
basePaths[static_cast<size_t>(DIRBASE::USER)] = gCustomUserDataPath;
basePaths[static_cast<size_t>(DIRBASE::CONFIG)] = gCustomUserDataPath;
basePaths[static_cast<size_t>(DIRBASE::CACHE)] = gCustomUserDataPath;
}
if (basePaths[(size_t)DIRBASE::DOCUMENTATION].empty())
if (basePaths[static_cast<size_t>(DIRBASE::DOCUMENTATION)].empty())
{
basePaths[(size_t)DIRBASE::DOCUMENTATION] = basePaths[static_cast<size_t>(DIRBASE::OPENRCT2)];
basePaths[static_cast<size_t>(DIRBASE::DOCUMENTATION)] = basePaths[static_cast<size_t>(DIRBASE::OPENRCT2)];
}
auto env = OpenRCT2::CreatePlatformEnvironment(basePaths);

View File

@@ -16,7 +16,7 @@
namespace OpenRCT2
{
enum class DIRBASE : int32_t
enum class DIRBASE : size_t
{
RCT1, // Base directory for original RollerCoaster Tycoon 1 content.
RCT2, // Base directory for original RollerCoaster Tycoon 2 content.
@@ -26,7 +26,7 @@ namespace OpenRCT2
CACHE, // Base directory for OpenRCT2 cache files.
DOCUMENTATION, // Base directory for OpenRCT2 doc files.
};
constexpr int32_t DIRBASE_COUNT = 7;
constexpr size_t DIRBASE_COUNT = 7;
using DIRBASE_VALUES = std::string[DIRBASE_COUNT];
enum class DIRID

View File

@@ -305,7 +305,7 @@ private:
if (it.element->GetType() != TILE_ELEMENT_TYPE_TRACK)
continue;
if (it.element->AsTrack()->GetRideIndex() != (ride_id_t)_rideIndex)
if (it.element->AsTrack()->GetRideIndex() != static_cast<ride_id_t>(_rideIndex))
continue;
auto location = CoordsXYZD(

View File

@@ -60,7 +60,7 @@ namespace File
}
fs.seekg(0, std::ios::end);
auto fsize = (size_t)fs.tellg();
auto fsize = static_cast<size_t>(fs.tellg());
if (fsize > SIZE_MAX)
{
std::string message = String::StdFormat(

View File

@@ -99,7 +99,7 @@ namespace Http
curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
curl_easy_setopt(curl, CURLOPT_READDATA, &wt);
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)wt.sizeleft);
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, static_cast<long>(wt.sizeleft));
}
if (req.forceIPv4)
@@ -114,9 +114,9 @@ namespace Http
curl_easy_setopt(curl, CURLOPT_URL, req.url.c_str());
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, true);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeData);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*)&res);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, static_cast<void*>(&res));
curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, header_callback);
curl_easy_setopt(curl, CURLOPT_HEADERDATA, (void*)&res);
curl_easy_setopt(curl, CURLOPT_HEADERDATA, static_cast<void*>(&res));
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, true);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, true);
curl_easy_setopt(curl, CURLOPT_USERAGENT, OPENRCT2_USER_AGENT);

View File

@@ -226,7 +226,10 @@ private:
public:
explicit vector_streambuf(const std::vector<T>& vec)
{
this->setg((char*)vec.data(), (char*)vec.data(), (char*)(vec.data() + vec.size()));
this->setg(
reinterpret_cast<char*>(const_cast<unsigned char*>(vec.data())),
reinterpret_cast<char*>(const_cast<unsigned char*>(vec.data())),
reinterpret_cast<char*>(const_cast<unsigned char*>(vec.data() + vec.size())));
}
};

View File

@@ -23,21 +23,21 @@ namespace Memory
{
template<typename T> static T* Allocate()
{
T* result = (T*)malloc(sizeof(T));
T* result = static_cast<T*>(malloc(sizeof(T)));
Guard::ArgumentNotNull(result, "Failed to allocate %zu bytes for %s", sizeof(T), typeid(T).name());
return result;
}
template<typename T> static T* Allocate(size_t size)
{
T* result = (T*)malloc(size);
T* result = static_cast<T*>(malloc(size));
Guard::ArgumentNotNull(result, "Failed to allocate %zu bytes for %s", size, typeid(T).name());
return result;
}
template<typename T> static T* AllocateArray(size_t count)
{
T* result = (T*)malloc(count * sizeof(T));
T* result = static_cast<T*>(malloc(count * sizeof(T)));
Guard::ArgumentNotNull(result, "Failed to allocate array of %zu * %s (%zu bytes)", count, typeid(T).name(), sizeof(T));
return result;
}
@@ -47,11 +47,11 @@ namespace Memory
T* result;
if (ptr == nullptr)
{
result = (T*)malloc(size);
result = static_cast<T*>(malloc(size));
}
else
{
result = (T*)realloc((void*)ptr, size);
result = static_cast<T*>(realloc(reinterpret_cast<void*>(ptr), size));
}
Guard::ArgumentNotNull(result, "Failed to reallocate %x (%s) to have %zu bytes", ptr, typeid(T).name(), size);
return result;
@@ -62,11 +62,11 @@ namespace Memory
T* result;
if (ptr == nullptr)
{
result = (T*)malloc(count * sizeof(T));
result = static_cast<T*>(malloc(count * sizeof(T)));
}
else
{
result = (T*)realloc((void*)ptr, count * sizeof(T));
result = static_cast<T*>(realloc(reinterpret_cast<void*>(ptr), count * sizeof(T)));
}
Guard::ArgumentNotNull(
result, "Failed to reallocate array at %x (%s) to have %zu entries", ptr, typeid(T).name(), count);

View File

@@ -27,7 +27,7 @@ namespace Numerics
{
static_assert(std::is_unsigned<_UIntType>::value, "result_type must be an unsigned integral type");
using limits = typename std::numeric_limits<_UIntType>;
return (((_UIntType)(x) << shift) | ((_UIntType)(x) >> (limits::digits - shift)));
return ((static_cast<_UIntType>(x) << shift) | (static_cast<_UIntType>(x) >> (limits::digits - shift)));
}
/**
@@ -53,7 +53,7 @@ namespace Numerics
{
static_assert(std::is_unsigned<_UIntType>::value, "result_type must be an unsigned integral type");
using limits = std::numeric_limits<_UIntType>;
return (((_UIntType)(x) >> shift) | ((_UIntType)(x) << (limits::digits - shift)));
return ((static_cast<_UIntType>(x) >> shift) | (static_cast<_UIntType>(x) << (limits::digits - shift)));
}
/**

View File

@@ -62,7 +62,7 @@ namespace Random
template<typename _TIt> void generate(_TIt begin, _TIt end) const
{
std::copy_n(v.begin(), std::min((size_t)(end - begin), N), begin);
std::copy_n(v.begin(), std::min(static_cast<size_t>(end - begin), N), begin);
}
constexpr size_t size() const

View File

@@ -454,7 +454,7 @@ namespace String
utf8* SkipBOM(utf8* buffer)
{
return const_cast<utf8*>(SkipBOM((const utf8*)buffer));
return const_cast<utf8*>(SkipBOM(static_cast<const utf8*>(buffer)));
}
const utf8* SkipBOM(const utf8* buffer)
@@ -474,7 +474,7 @@ namespace String
codepoint_t GetNextCodepoint(utf8* ptr, utf8** nextPtr)
{
return GetNextCodepoint((const utf8*)ptr, (const utf8**)nextPtr);
return GetNextCodepoint(static_cast<const utf8*>(ptr), const_cast<const utf8**>(nextPtr));
}
codepoint_t GetNextCodepoint(const utf8* ptr, const utf8** nextPtr)

View File

@@ -59,7 +59,7 @@ public:
jclass zipClass = env->GetObjectClass(_zip);
jmethodID fileCountMethod = env->GetMethodID(zipClass, "getNumFiles", "()I");
return (size_t)env->CallIntMethod(_zip, fileCountMethod);
return static_cast<size_t>(env->CallIntMethod(_zip, fileCountMethod));
}
std::string GetFileName(size_t index) const override

View File

@@ -46,7 +46,7 @@ int32_t gfx_get_string_width_new_lined(utf8* text)
int32_t codepoint;
int32_t maxWidth = 0;
while ((codepoint = utf8_get_next(ch, (const utf8**)&nextCh)) != 0)
while ((codepoint = utf8_get_next(ch, const_cast<const utf8**>(&nextCh))) != 0)
{
if (codepoint == FORMAT_NEWLINE || codepoint == FORMAT_NEWLINE_SMALLER)
{
@@ -102,7 +102,7 @@ int32_t gfx_clip_string(utf8* text, int32_t width)
utf8* nextCh = text;
utf8* clipCh = text;
int32_t codepoint;
while ((codepoint = utf8_get_next(ch, (const utf8**)&nextCh)) != 0)
while ((codepoint = utf8_get_next(ch, const_cast<const utf8**>(&nextCh))) != 0)
{
if (utf8_is_format_code(codepoint))
{
@@ -176,7 +176,7 @@ int32_t gfx_wrap_string(utf8* text, int32_t width, int32_t* outNumLines, int32_t
utf8* nextCh;
int32_t codepoint;
int32_t numCharactersOnLine = 0;
while ((codepoint = utf8_get_next(ch, (const utf8**)&nextCh)) != 0)
while ((codepoint = utf8_get_next(ch, const_cast<const utf8**>(&nextCh))) != 0)
{
if (codepoint == ' ')
{
@@ -313,7 +313,7 @@ void draw_string_centred_raw(rct_drawpixelinfo* dpi, int32_t x, int32_t y, int32
{
ScreenCoordsXY screenCoords(dpi->x, dpi->y);
gCurrentFontSpriteBase = FONT_SPRITE_BASE_MEDIUM;
gfx_draw_string(dpi, (char*)"", COLOUR_BLACK, screenCoords);
gfx_draw_string(dpi, "", COLOUR_BLACK, screenCoords);
screenCoords = { x, y };
gCurrentFontFlags = 0;
@@ -429,7 +429,7 @@ void gfx_draw_string_centred_wrapped_partial(
ScreenCoordsXY screenCoords(dpi->x, dpi->y);
gCurrentFontSpriteBase = FONT_SPRITE_BASE_MEDIUM;
gfx_draw_string(dpi, (char*)"", colour, screenCoords);
gfx_draw_string(dpi, "", colour, screenCoords);
format_string(buffer, 256, format, args);
gCurrentFontSpriteBase = FONT_SPRITE_BASE_MEDIUM;
@@ -448,7 +448,7 @@ void gfx_draw_string_centred_wrapped_partial(
utf8* ch = buffer;
utf8* nextCh;
int32_t codepoint;
while ((codepoint = utf8_get_next(ch, (const utf8**)&nextCh)) != 0)
while ((codepoint = utf8_get_next(ch, const_cast<const utf8**>(&nextCh))) != 0)
{
if (!utf8_is_format_code(codepoint))
{

View File

@@ -142,7 +142,7 @@ std::tuple<void*, size_t> ImageImporter::EncodeRLE(const int32_t* pixels, uint32
{
yOffsets[y] = static_cast<uint16_t>(dst - buffer);
auto previousCode = (RLECode*)nullptr;
auto previousCode = static_cast<RLECode*>(nullptr);
auto currentCode = reinterpret_cast<RLECode*>(dst);
dst += 2;

View File

@@ -1497,7 +1497,7 @@ static void scrolling_text_set_bitmap_for_sprite(
utf8* ch = text;
while (true)
{
uint32_t codepoint = utf8_get_next(ch, (const utf8**)&ch);
uint32_t codepoint = utf8_get_next(ch, const_cast<const utf8**>(&ch));
// If at the end of the string loop back to the start
if (codepoint == 0)
@@ -1567,7 +1567,7 @@ static void scrolling_text_set_bitmap_for_ttf(
utf8* dstCh = text;
utf8* ch = text;
int32_t codepoint;
while ((codepoint = utf8_get_next(ch, (const utf8**)&ch)) != 0)
while ((codepoint = utf8_get_next(ch, const_cast<const utf8**>(&ch))) != 0)
{
if (utf8_is_format_code(codepoint))
{

View File

@@ -1,3 +1,7 @@
// Adapted from freetype.h in order to avoid C-style casts.
#define FT_LOAD_TARGET_ALT(x) (static_cast<FT_Int32>((x)&15) << 16)
/**
* The following code is from SDL2_ttf (2 Jan 2017).
* Taking just what was needed for OpenRCT2 with all SDL2 calls
@@ -1530,9 +1534,9 @@ TTFSurface* TTF_RenderUTF8_Shaded(TTF_Font* font, const char* text, [[maybe_unus
void TTF_SetFontHinting(TTF_Font* font, int hinting)
{
if (hinting == TTF_HINTING_LIGHT)
font->hinting = FT_LOAD_TARGET_LIGHT;
font->hinting = FT_LOAD_TARGET_ALT(FT_RENDER_MODE_LIGHT);
else if (hinting == TTF_HINTING_MONO)
font->hinting = FT_LOAD_TARGET_MONO;
font->hinting = FT_LOAD_TARGET_ALT(FT_RENDER_MODE_MONO);
else if (hinting == TTF_HINTING_NONE)
font->hinting = FT_LOAD_NO_HINTING;
else
@@ -1543,9 +1547,9 @@ void TTF_SetFontHinting(TTF_Font* font, int hinting)
int TTF_GetFontHinting(const TTF_Font* font)
{
if (font->hinting == FT_LOAD_TARGET_LIGHT)
if (font->hinting == FT_LOAD_TARGET_ALT(FT_RENDER_MODE_LIGHT))
return TTF_HINTING_LIGHT;
else if (font->hinting == FT_LOAD_TARGET_MONO)
else if (font->hinting == FT_LOAD_TARGET_ALT(FT_RENDER_MODE_MONO))
return TTF_HINTING_MONO;
else if (font->hinting == FT_LOAD_NO_HINTING)
return TTF_HINTING_NONE;

Some files were not shown because too many files have changed in this diff Show More