From 54848249482d7976f1c31ea12199ea4f069194c9 Mon Sep 17 00:00:00 2001 From: Chris Thrasher Date: Thu, 16 May 2024 20:56:30 -0600 Subject: [PATCH] Remove unnecessary static casts --- examples/island/Island.cpp | 4 ++-- examples/sound_effects/SoundEffects.cpp | 4 ++-- src/SFML/Audio/Music.cpp | 2 +- src/SFML/Audio/SoundFileReaderMp3.cpp | 2 +- src/SFML/Audio/SoundFileWriterWav.cpp | 2 +- src/SFML/Graphics/Font.cpp | 18 +++++++----------- test/Network/Packet.test.cpp | 14 +++++++------- test/System/String.test.cpp | 5 ++--- test/System/Time.test.cpp | 4 ++-- test/TestUtilities/SystemUtil.cpp | 2 +- 10 files changed, 26 insertions(+), 31 deletions(-) diff --git a/examples/island/Island.cpp b/examples/island/Island.cpp index 779dca982..4fba4b1a7 100644 --- a/examples/island/Island.cpp +++ b/examples/island/Island.cpp @@ -474,8 +474,8 @@ void processWorkItem(std::vector& vertices, const WorkItem& workItem const unsigned int rowEnd = std::min(rowStart + rowBlockSize, resolutionY); const unsigned int rowCount = rowEnd - rowStart; - const float scalingFactorX = static_cast(windowWidth) / static_cast(resolutionX); - const float scalingFactorY = static_cast(windowHeight) / static_cast(resolutionY); + const float scalingFactorX = float{windowWidth} / float{resolutionX}; + const float scalingFactorY = float{windowHeight} / float{resolutionY}; for (unsigned int y = rowStart; y < rowEnd; ++y) { diff --git a/examples/sound_effects/SoundEffects.cpp b/examples/sound_effects/SoundEffects.cpp index 56e0c6679..ea2dcdda4 100644 --- a/examples/sound_effects/SoundEffects.cpp +++ b/examples/sound_effects/SoundEffects.cpp @@ -494,7 +494,7 @@ private: static constexpr unsigned int sampleRate{44100}; static constexpr std::size_t chunkSize{sampleRate / 100}; - static constexpr float timePerSample{1.f / static_cast(sampleRate)}; + static constexpr float timePerSample{1.f / float{sampleRate}}; std::vector m_sampleBuffer = std::vector(chunkSize, 0); Type m_type{Type::Triangle}; @@ -600,7 +600,7 @@ private: static constexpr unsigned int sampleRate{44100}; static constexpr std::size_t chunkSize{sampleRate / 100}; - static constexpr float timePerSample{1.f / static_cast(sampleRate)}; + static constexpr float timePerSample{1.f / float{sampleRate}}; std::vector m_sampleBuffer = std::vector(chunkSize, 0); float m_amplitude{0.05f}; diff --git a/src/SFML/Audio/Music.cpp b/src/SFML/Audio/Music.cpp index 25bdd636f..485e3f2a4 100644 --- a/src/SFML/Audio/Music.cpp +++ b/src/SFML/Audio/Music.cpp @@ -227,7 +227,7 @@ std::optional Music::onLoop() // Looping is enabled, and either we're at the loop end, or we're at the EOF // when it's equivalent to the loop end (loop end takes priority). Send us to loop begin m_file->seek(m_loopSpan.offset); - return static_cast(m_file->getSampleOffset()); + return m_file->getSampleOffset(); } else if (getLoop() && (currentOffset >= m_file->getSampleCount())) { diff --git a/src/SFML/Audio/SoundFileReaderMp3.cpp b/src/SFML/Audio/SoundFileReaderMp3.cpp index d0ca6574c..bf73b66d8 100644 --- a/src/SFML/Audio/SoundFileReaderMp3.cpp +++ b/src/SFML/Audio/SoundFileReaderMp3.cpp @@ -173,7 +173,7 @@ void SoundFileReaderMp3::seek(std::uint64_t sampleOffset) std::uint64_t SoundFileReaderMp3::read(std::int16_t* samples, std::uint64_t maxCount) { std::uint64_t toRead = std::min(maxCount, m_numSamples - m_position); - toRead = static_cast(mp3dec_ex_read(&m_decoder, samples, static_cast(toRead))); + toRead = std::uint64_t{mp3dec_ex_read(&m_decoder, samples, static_cast(toRead))}; m_position += toRead; return toRead; } diff --git a/src/SFML/Audio/SoundFileWriterWav.cpp b/src/SFML/Audio/SoundFileWriterWav.cpp index d7fdaf8fd..b14bb2c24 100644 --- a/src/SFML/Audio/SoundFileWriterWav.cpp +++ b/src/SFML/Audio/SoundFileWriterWav.cpp @@ -252,7 +252,7 @@ void SoundFileWriterWav::writeHeader(unsigned int sampleRate, unsigned int chann m_file.write(mainChunkId.data(), mainChunkId.size()); // Write the main chunk header - encode(m_file, static_cast(0)); // 0 is a placeholder, will be written later + encode(m_file, std::uint32_t{0}); // 0 is a placeholder, will be written later std::array mainChunkFormat = {'W', 'A', 'V', 'E'}; m_file.write(mainChunkFormat.data(), mainChunkFormat.size()); diff --git a/src/SFML/Graphics/Font.cpp b/src/SFML/Graphics/Font.cpp index a3e406f56..f5eceff67 100644 --- a/src/SFML/Graphics/Font.cpp +++ b/src/SFML/Graphics/Font.cpp @@ -84,8 +84,7 @@ inline T reinterpret(const U& input) // Combine outline thickness, boldness and font glyph index into a single 64-bit key std::uint64_t combine(float outlineThickness, bool bold, std::uint32_t index) { - return (static_cast(reinterpret(outlineThickness)) << 32) | - (static_cast(bold) << 31) | index; + return (std::uint64_t{reinterpret(outlineThickness)} << 32) | (std::uint64_t{bold} << 31) | index; } } // namespace @@ -365,8 +364,7 @@ float Font::getKerning(std::uint32_t first, std::uint32_t second, unsigned int c // Combine kerning with compensation deltas and return the X advance // Flooring is required as we use FT_KERNING_UNFITTED flag which is not quantized in 64 based grid - return std::floor( - (secondLsbDelta - firstRsbDelta + static_cast(kerning.x) + 32) / static_cast(1 << 6)); + return std::floor((secondLsbDelta - firstRsbDelta + static_cast(kerning.x) + 32) / float{1 << 6}); } else { @@ -385,7 +383,7 @@ float Font::getLineSpacing(unsigned int characterSize) const if (setCurrentSize(characterSize)) { - return static_cast(face->size->metrics.height) / static_cast(1 << 6); + return static_cast(face->size->metrics.height) / float{1 << 6}; } else { @@ -407,8 +405,7 @@ float Font::getUnderlinePosition(unsigned int characterSize) const if (!FT_IS_SCALABLE(face)) return static_cast(characterSize) / 10.f; - return -static_cast(FT_MulFix(face->underline_position, face->size->metrics.y_scale)) / - static_cast(1 << 6); + return -static_cast(FT_MulFix(face->underline_position, face->size->metrics.y_scale)) / float{1 << 6}; } else { @@ -430,8 +427,7 @@ float Font::getUnderlineThickness(unsigned int characterSize) const if (!FT_IS_SCALABLE(face)) return static_cast(characterSize) / 14.f; - return static_cast(FT_MulFix(face->underline_thickness, face->size->metrics.y_scale)) / - static_cast(1 << 6); + return static_cast(FT_MulFix(face->underline_thickness, face->size->metrics.y_scale)) / float{1 << 6}; } else { @@ -519,7 +515,7 @@ Glyph Font::loadGlyph(std::uint32_t codePoint, unsigned int characterSize, bool FT_Stroker stroker = m_fontHandles->stroker; FT_Stroker_Set(stroker, - static_cast(outlineThickness * static_cast(1 << 6)), + static_cast(outlineThickness * float{1 << 6}), FT_STROKER_LINECAP_ROUND, FT_STROKER_LINEJOIN_ROUND, 0); @@ -547,7 +543,7 @@ Glyph Font::loadGlyph(std::uint32_t codePoint, unsigned int characterSize, bool // Compute the glyph's advance offset glyph.advance = static_cast(bitmapGlyph->root.advance.x >> 16); if (bold) - glyph.advance += static_cast(weight) / static_cast(1 << 6); + glyph.advance += static_cast(weight) / float{1 << 6}; glyph.lsbDelta = static_cast(face->glyph->lsb_delta); glyph.rsbDelta = static_cast(face->glyph->rsb_delta); diff --git a/test/Network/Packet.test.cpp b/test/Network/Packet.test.cpp index 1adf47752..8b68e6c2f 100644 --- a/test/Network/Packet.test.cpp +++ b/test/Network/Packet.test.cpp @@ -22,7 +22,7 @@ CHECK(packet.getData() != nullptr); \ CHECK(packet.getDataSize() == sizeof(expected)); \ CHECK(!packet.endOfPacket()); \ - CHECK(static_cast(packet)); \ + CHECK(bool{packet}); \ \ decltype(expected) received; \ packet >> received; \ @@ -30,7 +30,7 @@ CHECK(packet.getData() != nullptr); \ CHECK(packet.getDataSize() == sizeof(expected)); \ CHECK(packet.endOfPacket()); \ - CHECK(static_cast(packet)); \ + CHECK(bool{packet}); \ CHECK((expected) == received); \ } while (false) @@ -43,7 +43,7 @@ CHECK(packet.getData() != nullptr); \ CHECK(packet.getDataSize() == (size)); \ CHECK(!packet.endOfPacket()); \ - CHECK(static_cast(packet)); \ + CHECK(bool{packet}); \ \ std::remove_const_t received; \ packet >> received; \ @@ -51,7 +51,7 @@ CHECK(packet.getData() != nullptr); \ CHECK(packet.getDataSize() == (size)); \ CHECK(packet.endOfPacket()); \ - CHECK(static_cast(packet)); \ + CHECK(bool{packet}); \ CHECK(sf::String(expected) == sf::String(received)); \ } while (false) @@ -78,7 +78,7 @@ TEST_CASE("[Network] sf::Packet") CHECK(packet.getData() == nullptr); CHECK(packet.getDataSize() == 0); CHECK(packet.endOfPacket()); - CHECK(static_cast(packet)); + CHECK(bool{packet}); } static constexpr std::array data = {1, 2, 3, 4, 5, 6}; @@ -91,14 +91,14 @@ TEST_CASE("[Network] sf::Packet") CHECK(packet.getData() != nullptr); CHECK(packet.getDataSize() == data.size()); CHECK(!packet.endOfPacket()); - CHECK(static_cast(packet)); + CHECK(bool{packet}); packet.clear(); CHECK(packet.getReadPosition() == 0); CHECK(packet.getData() == nullptr); CHECK(packet.getDataSize() == 0); CHECK(packet.endOfPacket()); - CHECK(static_cast(packet)); + CHECK(bool{packet}); } SECTION("Network ordering") diff --git a/test/System/String.test.cpp b/test/System/String.test.cpp index 2ccd5976e..9d7111ade 100644 --- a/test/System/String.test.cpp +++ b/test/System/String.test.cpp @@ -26,11 +26,10 @@ auto select(const std::basic_string& string16, const std::basic_string& st return string32; } -template -auto toHex(const CharT character) +auto toHex(const std::uint32_t character) { std::ostringstream stream; - stream << "[\\x" << std::uppercase << std::hex << static_cast(character) << ']'; + stream << "[\\x" << std::uppercase << std::hex << character << ']'; return stream.str(); } } // namespace diff --git a/test/System/Time.test.cpp b/test/System/Time.test.cpp index 036a9de3b..032265058 100644 --- a/test/System/Time.test.cpp +++ b/test/System/Time.test.cpp @@ -243,7 +243,7 @@ TEST_CASE("[System] sf::Time") SECTION("operator*=") { sf::Time time = sf::milliseconds(1'000); - time *= static_cast(10); + time *= std::int64_t{10}; CHECK(time == sf::milliseconds(10'000)); time *= 0.1f; CHECK(time.asMilliseconds() == 1'000); @@ -262,7 +262,7 @@ TEST_CASE("[System] sf::Time") SECTION("operator/=") { sf::Time time = sf::milliseconds(1'000); - time /= static_cast(2); + time /= std::int64_t{2}; CHECK(time == sf::milliseconds(500)); time /= 0.5f; CHECK(time.asMilliseconds() == 1'000); diff --git a/test/TestUtilities/SystemUtil.cpp b/test/TestUtilities/SystemUtil.cpp index c731012ea..de2b25c3f 100644 --- a/test/TestUtilities/SystemUtil.cpp +++ b/test/TestUtilities/SystemUtil.cpp @@ -62,7 +62,7 @@ template std::ostream& operator<<(std::ostream&, const Vector3&); bool operator==(const float& lhs, const Approx& rhs) { - return static_cast(lhs) == Catch::Approx(static_cast(rhs.value)).margin(1e-5); + return lhs == Catch::Approx(rhs.value).margin(1e-5); } bool operator==(const sf::Vector2f& lhs, const Approx& rhs)