Use std::byte
This commit is contained in:
parent
48a37230d4
commit
33030e94d9
@ -432,10 +432,10 @@ private:
|
||||
////////////////////////////////////////////////////////////
|
||||
// Member data
|
||||
////////////////////////////////////////////////////////////
|
||||
std::vector<char> 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<std::byte> 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
|
||||
|
@ -35,6 +35,8 @@
|
||||
|
||||
#include <optional>
|
||||
|
||||
#include <cstddef>
|
||||
|
||||
|
||||
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<char> 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<std::byte> Data; //!< Data of the packet
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
// Member data
|
||||
////////////////////////////////////////////////////////////
|
||||
PendingPacket m_pendingPacket; //!< Temporary data of the packet currently being received
|
||||
std::vector<char> 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<std::byte> m_blockToSendBuffer; //!< Buffer used to prepare data being sent from the socket
|
||||
};
|
||||
|
||||
} // namespace sf
|
||||
|
@ -35,6 +35,8 @@
|
||||
#include <optional>
|
||||
#include <vector>
|
||||
|
||||
#include <cstddef>
|
||||
|
||||
|
||||
namespace sf
|
||||
{
|
||||
@ -196,7 +198,7 @@ private:
|
||||
////////////////////////////////////////////////////////////
|
||||
// Member data
|
||||
////////////////////////////////////////////////////////////
|
||||
std::vector<char> m_buffer{std::vector<char>(MaxDatagramSize)}; //!< Temporary buffer holding the received data in Receive(Packet)
|
||||
std::vector<std::byte> m_buffer{MaxDatagramSize}; //!< Temporary buffer holding the received data in Receive(Packet)
|
||||
};
|
||||
|
||||
} // namespace sf
|
||||
|
@ -33,6 +33,7 @@
|
||||
|
||||
#include <SFML/System/InputStream.hpp>
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdlib>
|
||||
|
||||
|
||||
@ -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
|
||||
|
@ -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<std::uint8_t>((input | 0x80) & 0xBF); input >>= 6; [[fallthrough]];
|
||||
case 3: bytes[2] = static_cast<std::uint8_t>((input | 0x80) & 0xBF); input >>= 6; [[fallthrough]];
|
||||
case 2: bytes[1] = static_cast<std::uint8_t>((input | 0x80) & 0xBF); input >>= 6; [[fallthrough]];
|
||||
case 1: bytes[0] = static_cast<std::uint8_t> (input | firstBytes[bytestoWrite]);
|
||||
case 4: bytes[3] = static_cast<std::byte>((input | 0x80) & 0xBF); input >>= 6; [[fallthrough]];
|
||||
case 3: bytes[2] = static_cast<std::byte>((input | 0x80) & 0xBF); input >>= 6; [[fallthrough]];
|
||||
case 2: bytes[1] = static_cast<std::byte>((input | 0x80) & 0xBF); input >>= 6; [[fallthrough]];
|
||||
case 1: bytes[0] = static_cast<std::byte> (input | firstBytes[bytestoWrite]);
|
||||
}
|
||||
// clang-format on
|
||||
|
||||
|
@ -29,12 +29,14 @@
|
||||
|
||||
#include <SFML/System/Err.hpp>
|
||||
#include <SFML/System/InputStream.hpp>
|
||||
#include <SFML/System/Utils.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
#include <ostream>
|
||||
|
||||
#include <cassert>
|
||||
#include <cctype>
|
||||
#include <cstddef>
|
||||
#include <cstring>
|
||||
|
||||
|
||||
@ -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<std::size_t>(stream.read(bytes, static_cast<std::int64_t>(sizeof(bytes)))) != sizeof(bytes))
|
||||
return false;
|
||||
|
||||
value = static_cast<std::int16_t>(bytes[0] | (bytes[1] << 8));
|
||||
value = sf::toInteger<std::int16_t>(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<std::size_t>(stream.read(bytes, static_cast<std::int64_t>(sizeof(bytes)))) != sizeof(bytes))
|
||||
return false;
|
||||
|
||||
value = static_cast<std::uint16_t>(bytes[0] | (bytes[1] << 8));
|
||||
value = sf::toInteger<std::uint16_t>(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<std::size_t>(stream.read(bytes, static_cast<std::int64_t>(sizeof(bytes)))) != sizeof(bytes))
|
||||
return false;
|
||||
|
||||
value = static_cast<std::uint32_t>(bytes[0] | (bytes[1] << 8) | (bytes[2] << 16));
|
||||
value = sf::toInteger<std::uint32_t>(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<std::size_t>(stream.read(bytes, static_cast<std::int64_t>(sizeof(bytes)))) != sizeof(bytes))
|
||||
return false;
|
||||
|
||||
value = static_cast<std::uint32_t>(bytes[0] | (bytes[1] << 8) | (bytes[2] << 16) | (bytes[3] << 24));
|
||||
value = sf::toInteger<std::uint32_t>(bytes[0], bytes[1], bytes[2], bytes[3]);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
@ -33,6 +33,7 @@
|
||||
#include <ostream>
|
||||
|
||||
#include <cassert>
|
||||
#include <cstddef>
|
||||
|
||||
|
||||
namespace
|
||||
@ -42,23 +43,23 @@ namespace
|
||||
|
||||
void encode(std::ostream& stream, std::int16_t value)
|
||||
{
|
||||
unsigned char bytes[] = {static_cast<unsigned char>(value & 0xFF), static_cast<unsigned char>(value >> 8)};
|
||||
const std::byte bytes[] = {static_cast<std::byte>(value & 0xFF), static_cast<std::byte>(value >> 8)};
|
||||
stream.write(reinterpret_cast<const char*>(bytes), sizeof(bytes));
|
||||
}
|
||||
|
||||
void encode(std::ostream& stream, std::uint16_t value)
|
||||
{
|
||||
unsigned char bytes[] = {static_cast<unsigned char>(value & 0xFF), static_cast<unsigned char>(value >> 8)};
|
||||
const std::byte bytes[] = {static_cast<std::byte>(value & 0xFF), static_cast<std::byte>(value >> 8)};
|
||||
stream.write(reinterpret_cast<const char*>(bytes), sizeof(bytes));
|
||||
}
|
||||
|
||||
void encode(std::ostream& stream, std::uint32_t value)
|
||||
{
|
||||
unsigned char bytes[] = {
|
||||
static_cast<unsigned char>(value & 0x000000FF),
|
||||
static_cast<unsigned char>((value & 0x0000FF00) >> 8),
|
||||
static_cast<unsigned char>((value & 0x00FF0000) >> 16),
|
||||
static_cast<unsigned char>((value & 0xFF000000) >> 24),
|
||||
const std::byte bytes[] = {
|
||||
static_cast<std::byte>(value & 0x000000FF),
|
||||
static_cast<std::byte>((value & 0x0000FF00) >> 8),
|
||||
static_cast<std::byte>((value & 0x00FF0000) >> 16),
|
||||
static_cast<std::byte>((value & 0xFF000000) >> 24),
|
||||
};
|
||||
stream.write(reinterpret_cast<const char*>(bytes), sizeof(bytes));
|
||||
}
|
||||
|
@ -45,6 +45,7 @@
|
||||
|
||||
#include <cassert>
|
||||
#include <cmath>
|
||||
#include <cstddef>
|
||||
|
||||
|
||||
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<const char*>(vertices);
|
||||
const auto* data = reinterpret_cast<const std::byte*>(vertices);
|
||||
|
||||
// If we pre-transform the vertices, we must use our internal vertex cache
|
||||
if (useVertexCache)
|
||||
data = reinterpret_cast<const char*>(m_cache.vertexCache);
|
||||
data = reinterpret_cast<const std::byte*>(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<const char*>(m_cache.vertexCache);
|
||||
const auto* data = reinterpret_cast<const std::byte*>(m_cache.vertexCache);
|
||||
|
||||
glCheck(glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), data + 12));
|
||||
}
|
||||
|
@ -29,6 +29,7 @@
|
||||
#include <SFML/Network/SocketImpl.hpp>
|
||||
|
||||
#include <SFML/System/String.hpp>
|
||||
#include <SFML/System/Utils.hpp>
|
||||
|
||||
#include <cstring>
|
||||
#include <cwchar>
|
||||
@ -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<std::int64_t>(bytes[0]) << 56) | (static_cast<std::int64_t>(bytes[1]) << 48) |
|
||||
(static_cast<std::int64_t>(bytes[2]) << 40) | (static_cast<std::int64_t>(bytes[3]) << 32) |
|
||||
(static_cast<std::int64_t>(bytes[4]) << 24) | (static_cast<std::int64_t>(bytes[5]) << 16) |
|
||||
(static_cast<std::int64_t>(bytes[6]) << 8) | (static_cast<std::int64_t>(bytes[7]));
|
||||
data = toInteger<std::int64_t>(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<std::uint64_t>(bytes[0]) << 56) | (static_cast<std::uint64_t>(bytes[1]) << 48) |
|
||||
(static_cast<std::uint64_t>(bytes[2]) << 40) | (static_cast<std::uint64_t>(bytes[3]) << 32) |
|
||||
(static_cast<std::uint64_t>(bytes[4]) << 24) | (static_cast<std::uint64_t>(bytes[5]) << 16) |
|
||||
(static_cast<std::uint64_t>(bytes[6]) << 8) | (static_cast<std::uint64_t>(bytes[7]));
|
||||
data = toInteger<std::uint64_t>(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<char*>(&m_data[m_readPos]), length);
|
||||
|
||||
// Update reading position
|
||||
m_readPos += length;
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -39,7 +39,7 @@ MemoryInputStream::MemoryInputStream() = default;
|
||||
////////////////////////////////////////////////////////////
|
||||
void MemoryInputStream::open(const void* data, std::size_t sizeInBytes)
|
||||
{
|
||||
m_data = static_cast<const char*>(data);
|
||||
m_data = static_cast<const std::byte*>(data);
|
||||
m_size = static_cast<std::int64_t>(sizeInBytes);
|
||||
m_offset = 0;
|
||||
}
|
||||
|
@ -51,4 +51,15 @@ namespace sf
|
||||
return stream.str();
|
||||
}
|
||||
|
||||
// Convert byte sequence into integer
|
||||
// toInteger<int>(0x12, 0x34, 0x56) == 0x563412
|
||||
template <typename IntegerType, typename... Bytes>
|
||||
[[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<IntegerType>(static_cast<IntegerType>(byte) << 8 * index++)), ...);
|
||||
}
|
||||
} // namespace sf
|
||||
|
Loading…
Reference in New Issue
Block a user