diff --git a/include/SFML/Network/Packet.hpp b/include/SFML/Network/Packet.hpp index da8f89b73..a4a5927f5 100644 --- a/include/SFML/Network/Packet.hpp +++ b/include/SFML/Network/Packet.hpp @@ -432,10 +432,10 @@ private: //////////////////////////////////////////////////////////// // Member data //////////////////////////////////////////////////////////// - std::vector m_data; //!< Data stored in the packet - std::size_t m_readPos{}; //!< Current reading position in the packet - std::size_t m_sendPos{}; //!< Current send position in the packet (for handling partial sends) - bool m_isValid{true}; //!< Reading state of the packet + std::vector m_data; //!< Data stored in the packet + std::size_t m_readPos{}; //!< Current reading position in the packet + std::size_t m_sendPos{}; //!< Current send position in the packet (for handling partial sends) + bool m_isValid{true}; //!< Reading state of the packet }; } // namespace sf diff --git a/include/SFML/Network/TcpSocket.hpp b/include/SFML/Network/TcpSocket.hpp index afccfbd97..8759285a4 100644 --- a/include/SFML/Network/TcpSocket.hpp +++ b/include/SFML/Network/TcpSocket.hpp @@ -35,6 +35,8 @@ #include +#include + namespace sf { @@ -219,16 +221,16 @@ private: //////////////////////////////////////////////////////////// struct PendingPacket { - std::uint32_t Size{}; //!< Data of packet size - std::size_t SizeReceived{}; //!< Number of size bytes received so far - std::vector Data; //!< Data of the packet + std::uint32_t Size{}; //!< Data of packet size + std::size_t SizeReceived{}; //!< Number of size bytes received so far + std::vector Data; //!< Data of the packet }; //////////////////////////////////////////////////////////// // Member data //////////////////////////////////////////////////////////// - PendingPacket m_pendingPacket; //!< Temporary data of the packet currently being received - std::vector m_blockToSendBuffer; //!< Buffer used to prepare data being sent from the socket + PendingPacket m_pendingPacket; //!< Temporary data of the packet currently being received + std::vector m_blockToSendBuffer; //!< Buffer used to prepare data being sent from the socket }; } // namespace sf diff --git a/include/SFML/Network/UdpSocket.hpp b/include/SFML/Network/UdpSocket.hpp index eca1ab2bf..c1aa4b843 100644 --- a/include/SFML/Network/UdpSocket.hpp +++ b/include/SFML/Network/UdpSocket.hpp @@ -35,6 +35,8 @@ #include #include +#include + namespace sf { @@ -196,7 +198,7 @@ private: //////////////////////////////////////////////////////////// // Member data //////////////////////////////////////////////////////////// - std::vector m_buffer{std::vector(MaxDatagramSize)}; //!< Temporary buffer holding the received data in Receive(Packet) + std::vector m_buffer{MaxDatagramSize}; //!< Temporary buffer holding the received data in Receive(Packet) }; } // namespace sf diff --git a/include/SFML/System/MemoryInputStream.hpp b/include/SFML/System/MemoryInputStream.hpp index 0508ca0c9..89b44c52b 100644 --- a/include/SFML/System/MemoryInputStream.hpp +++ b/include/SFML/System/MemoryInputStream.hpp @@ -33,6 +33,7 @@ #include +#include #include @@ -104,9 +105,9 @@ private: //////////////////////////////////////////////////////////// // Member data //////////////////////////////////////////////////////////// - const char* m_data{}; //!< Pointer to the data in memory - std::int64_t m_size{}; //!< Total size of the data - std::int64_t m_offset{}; //!< Current reading position + const std::byte* m_data{}; //!< Pointer to the data in memory + std::int64_t m_size{}; //!< Total size of the data + std::int64_t m_offset{}; //!< Current reading position }; } // namespace sf diff --git a/include/SFML/System/Utf.inl b/include/SFML/System/Utf.inl index 4c38530b1..7756ad287 100644 --- a/include/SFML/System/Utf.inl +++ b/include/SFML/System/Utf.inl @@ -127,15 +127,15 @@ Out Utf<8>::encode(std::uint32_t input, Out output, std::uint8_t replacement) // clang-format on // Extract the bytes to write - std::uint8_t bytes[4]; + std::byte bytes[4]; // clang-format off switch (bytestoWrite) { - case 4: bytes[3] = static_cast((input | 0x80) & 0xBF); input >>= 6; [[fallthrough]]; - case 3: bytes[2] = static_cast((input | 0x80) & 0xBF); input >>= 6; [[fallthrough]]; - case 2: bytes[1] = static_cast((input | 0x80) & 0xBF); input >>= 6; [[fallthrough]]; - case 1: bytes[0] = static_cast (input | firstBytes[bytestoWrite]); + case 4: bytes[3] = static_cast((input | 0x80) & 0xBF); input >>= 6; [[fallthrough]]; + case 3: bytes[2] = static_cast((input | 0x80) & 0xBF); input >>= 6; [[fallthrough]]; + case 2: bytes[1] = static_cast((input | 0x80) & 0xBF); input >>= 6; [[fallthrough]]; + case 1: bytes[0] = static_cast (input | firstBytes[bytestoWrite]); } // clang-format on diff --git a/src/SFML/Audio/SoundFileReaderWav.cpp b/src/SFML/Audio/SoundFileReaderWav.cpp index 228e40f4b..251cafbc8 100644 --- a/src/SFML/Audio/SoundFileReaderWav.cpp +++ b/src/SFML/Audio/SoundFileReaderWav.cpp @@ -29,12 +29,14 @@ #include #include +#include #include #include #include #include +#include #include @@ -50,44 +52,44 @@ bool decode(sf::InputStream& stream, std::uint8_t& value) bool decode(sf::InputStream& stream, std::int16_t& value) { - unsigned char bytes[sizeof(value)]; + std::byte bytes[sizeof(value)]; if (static_cast(stream.read(bytes, static_cast(sizeof(bytes)))) != sizeof(bytes)) return false; - value = static_cast(bytes[0] | (bytes[1] << 8)); + value = sf::toInteger(bytes[0], bytes[1]); return true; } bool decode(sf::InputStream& stream, std::uint16_t& value) { - unsigned char bytes[sizeof(value)]; + std::byte bytes[sizeof(value)]; if (static_cast(stream.read(bytes, static_cast(sizeof(bytes)))) != sizeof(bytes)) return false; - value = static_cast(bytes[0] | (bytes[1] << 8)); + value = sf::toInteger(bytes[0], bytes[1]); return true; } bool decode24bit(sf::InputStream& stream, std::uint32_t& value) { - unsigned char bytes[3]; + std::byte bytes[3]; if (static_cast(stream.read(bytes, static_cast(sizeof(bytes)))) != sizeof(bytes)) return false; - value = static_cast(bytes[0] | (bytes[1] << 8) | (bytes[2] << 16)); + value = sf::toInteger(bytes[0], bytes[1], bytes[2]); return true; } bool decode(sf::InputStream& stream, std::uint32_t& value) { - unsigned char bytes[sizeof(value)]; + std::byte bytes[sizeof(value)]; if (static_cast(stream.read(bytes, static_cast(sizeof(bytes)))) != sizeof(bytes)) return false; - value = static_cast(bytes[0] | (bytes[1] << 8) | (bytes[2] << 16) | (bytes[3] << 24)); + value = sf::toInteger(bytes[0], bytes[1], bytes[2], bytes[3]); return true; } diff --git a/src/SFML/Audio/SoundFileWriterWav.cpp b/src/SFML/Audio/SoundFileWriterWav.cpp index 183e9a438..f2cc1fee5 100644 --- a/src/SFML/Audio/SoundFileWriterWav.cpp +++ b/src/SFML/Audio/SoundFileWriterWav.cpp @@ -33,6 +33,7 @@ #include #include +#include namespace @@ -42,23 +43,23 @@ namespace void encode(std::ostream& stream, std::int16_t value) { - unsigned char bytes[] = {static_cast(value & 0xFF), static_cast(value >> 8)}; + const std::byte bytes[] = {static_cast(value & 0xFF), static_cast(value >> 8)}; stream.write(reinterpret_cast(bytes), sizeof(bytes)); } void encode(std::ostream& stream, std::uint16_t value) { - unsigned char bytes[] = {static_cast(value & 0xFF), static_cast(value >> 8)}; + const std::byte bytes[] = {static_cast(value & 0xFF), static_cast(value >> 8)}; stream.write(reinterpret_cast(bytes), sizeof(bytes)); } void encode(std::ostream& stream, std::uint32_t value) { - unsigned char bytes[] = { - static_cast(value & 0x000000FF), - static_cast((value & 0x0000FF00) >> 8), - static_cast((value & 0x00FF0000) >> 16), - static_cast((value & 0xFF000000) >> 24), + const std::byte bytes[] = { + static_cast(value & 0x000000FF), + static_cast((value & 0x0000FF00) >> 8), + static_cast((value & 0x00FF0000) >> 16), + static_cast((value & 0xFF000000) >> 24), }; stream.write(reinterpret_cast(bytes), sizeof(bytes)); } diff --git a/src/SFML/Graphics/RenderTarget.cpp b/src/SFML/Graphics/RenderTarget.cpp index 35085840b..dccfc1377 100644 --- a/src/SFML/Graphics/RenderTarget.cpp +++ b/src/SFML/Graphics/RenderTarget.cpp @@ -45,6 +45,7 @@ #include #include +#include namespace @@ -293,11 +294,11 @@ void RenderTarget::draw(const Vertex* vertices, std::size_t vertexCount, Primiti // coordinates we need to set up the pointers to the vertices' components if (!m_cache.enable || !useVertexCache || !m_cache.useVertexCache) { - const char* data = reinterpret_cast(vertices); + const auto* data = reinterpret_cast(vertices); // If we pre-transform the vertices, we must use our internal vertex cache if (useVertexCache) - data = reinterpret_cast(m_cache.vertexCache); + data = reinterpret_cast(m_cache.vertexCache); glCheck(glVertexPointer(2, GL_FLOAT, sizeof(Vertex), data + 0)); glCheck(glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(Vertex), data + 8)); @@ -307,7 +308,7 @@ void RenderTarget::draw(const Vertex* vertices, std::size_t vertexCount, Primiti else if (enableTexCoordsArray && !m_cache.texCoordsArrayEnabled) { // If we enter this block, we are already using our internal vertex cache - const char* data = reinterpret_cast(m_cache.vertexCache); + const auto* data = reinterpret_cast(m_cache.vertexCache); glCheck(glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), data + 12)); } diff --git a/src/SFML/Network/Packet.cpp b/src/SFML/Network/Packet.cpp index b20ef7c21..0ad0d337d 100644 --- a/src/SFML/Network/Packet.cpp +++ b/src/SFML/Network/Packet.cpp @@ -29,6 +29,7 @@ #include #include +#include #include #include @@ -216,13 +217,10 @@ Packet& Packet::operator>>(std::int64_t& data) { // Since ntohll is not available everywhere, we have to convert // to network byte order (big endian) manually - std::uint8_t bytes[sizeof(data)]; + std::byte bytes[sizeof(data)]; std::memcpy(bytes, &m_data[m_readPos], sizeof(data)); - data = (static_cast(bytes[0]) << 56) | (static_cast(bytes[1]) << 48) | - (static_cast(bytes[2]) << 40) | (static_cast(bytes[3]) << 32) | - (static_cast(bytes[4]) << 24) | (static_cast(bytes[5]) << 16) | - (static_cast(bytes[6]) << 8) | (static_cast(bytes[7])); + data = toInteger(bytes[7], bytes[6], bytes[5], bytes[4], bytes[3], bytes[2], bytes[1], bytes[0]); m_readPos += sizeof(data); } @@ -238,13 +236,10 @@ Packet& Packet::operator>>(std::uint64_t& data) { // Since ntohll is not available everywhere, we have to convert // to network byte order (big endian) manually - std::uint8_t bytes[sizeof(data)]; + std::byte bytes[sizeof(data)]; std::memcpy(bytes, &m_data[m_readPos], sizeof(data)); - data = (static_cast(bytes[0]) << 56) | (static_cast(bytes[1]) << 48) | - (static_cast(bytes[2]) << 40) | (static_cast(bytes[3]) << 32) | - (static_cast(bytes[4]) << 24) | (static_cast(bytes[5]) << 16) | - (static_cast(bytes[6]) << 8) | (static_cast(bytes[7])); + data = toInteger(bytes[7], bytes[6], bytes[5], bytes[4], bytes[3], bytes[2], bytes[1], bytes[0]); m_readPos += sizeof(data); } @@ -311,7 +306,7 @@ Packet& Packet::operator>>(std::string& data) if ((length > 0) && checkSize(length)) { // Then extract characters - data.assign(&m_data[m_readPos], length); + data.assign(reinterpret_cast(&m_data[m_readPos]), length); // Update reading position m_readPos += length; diff --git a/src/SFML/Network/TcpSocket.cpp b/src/SFML/Network/TcpSocket.cpp index 76e2b8fae..8e6b15f33 100644 --- a/src/SFML/Network/TcpSocket.cpp +++ b/src/SFML/Network/TcpSocket.cpp @@ -412,7 +412,7 @@ Socket::Status TcpSocket::receive(Packet& packet) if (received > 0) { m_pendingPacket.Data.resize(m_pendingPacket.Data.size() + received); - char* begin = m_pendingPacket.Data.data() + m_pendingPacket.Data.size() - received; + std::byte* begin = m_pendingPacket.Data.data() + m_pendingPacket.Data.size() - received; std::memcpy(begin, buffer, received); } } diff --git a/src/SFML/System/MemoryInputStream.cpp b/src/SFML/System/MemoryInputStream.cpp index e24ce5f2a..a7c2d9aad 100644 --- a/src/SFML/System/MemoryInputStream.cpp +++ b/src/SFML/System/MemoryInputStream.cpp @@ -39,7 +39,7 @@ MemoryInputStream::MemoryInputStream() = default; //////////////////////////////////////////////////////////// void MemoryInputStream::open(const void* data, std::size_t sizeInBytes) { - m_data = static_cast(data); + m_data = static_cast(data); m_size = static_cast(sizeInBytes); m_offset = 0; } diff --git a/src/SFML/System/Utils.hpp b/src/SFML/System/Utils.hpp index 30dda7960..2f8555136 100644 --- a/src/SFML/System/Utils.hpp +++ b/src/SFML/System/Utils.hpp @@ -51,4 +51,15 @@ namespace sf return stream.str(); } +// Convert byte sequence into integer +// toInteger(0x12, 0x34, 0x56) == 0x563412 +template +[[nodiscard]] constexpr IntegerType toInteger(Bytes... byte) +{ + static_assert(sizeof(IntegerType) >= sizeof...(Bytes), "IntegerType not large enough to contain bytes"); + + IntegerType integer = 0; + std::size_t index = 0; + return ((integer |= static_cast(static_cast(byte) << 8 * index++)), ...); +} } // namespace sf