Use std::byte
This commit is contained in:
parent
48a37230d4
commit
33030e94d9
@ -432,7 +432,7 @@ private:
|
|||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
// Member data
|
// Member data
|
||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
std::vector<char> m_data; //!< Data stored in 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_readPos{}; //!< Current reading position in the packet
|
||||||
std::size_t m_sendPos{}; //!< Current send position in the packet (for handling partial sends)
|
std::size_t m_sendPos{}; //!< Current send position in the packet (for handling partial sends)
|
||||||
bool m_isValid{true}; //!< Reading state of the packet
|
bool m_isValid{true}; //!< Reading state of the packet
|
||||||
|
@ -35,6 +35,8 @@
|
|||||||
|
|
||||||
#include <optional>
|
#include <optional>
|
||||||
|
|
||||||
|
#include <cstddef>
|
||||||
|
|
||||||
|
|
||||||
namespace sf
|
namespace sf
|
||||||
{
|
{
|
||||||
@ -221,14 +223,14 @@ private:
|
|||||||
{
|
{
|
||||||
std::uint32_t Size{}; //!< Data of packet size
|
std::uint32_t Size{}; //!< Data of packet size
|
||||||
std::size_t SizeReceived{}; //!< Number of size bytes received so far
|
std::size_t SizeReceived{}; //!< Number of size bytes received so far
|
||||||
std::vector<char> Data; //!< Data of the packet
|
std::vector<std::byte> Data; //!< Data of the packet
|
||||||
};
|
};
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
// Member data
|
// Member data
|
||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
PendingPacket m_pendingPacket; //!< Temporary data of the packet currently being received
|
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
|
std::vector<std::byte> m_blockToSendBuffer; //!< Buffer used to prepare data being sent from the socket
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace sf
|
} // namespace sf
|
||||||
|
@ -35,6 +35,8 @@
|
|||||||
#include <optional>
|
#include <optional>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
#include <cstddef>
|
||||||
|
|
||||||
|
|
||||||
namespace sf
|
namespace sf
|
||||||
{
|
{
|
||||||
@ -196,7 +198,7 @@ private:
|
|||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
// Member data
|
// 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
|
} // namespace sf
|
||||||
|
@ -33,6 +33,7 @@
|
|||||||
|
|
||||||
#include <SFML/System/InputStream.hpp>
|
#include <SFML/System/InputStream.hpp>
|
||||||
|
|
||||||
|
#include <cstddef>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
|
|
||||||
|
|
||||||
@ -104,7 +105,7 @@ private:
|
|||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
// Member data
|
// Member data
|
||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
const char* m_data{}; //!< Pointer to the data in memory
|
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_size{}; //!< Total size of the data
|
||||||
std::int64_t m_offset{}; //!< Current reading position
|
std::int64_t m_offset{}; //!< Current reading position
|
||||||
};
|
};
|
||||||
|
@ -127,15 +127,15 @@ Out Utf<8>::encode(std::uint32_t input, Out output, std::uint8_t replacement)
|
|||||||
// clang-format on
|
// clang-format on
|
||||||
|
|
||||||
// Extract the bytes to write
|
// Extract the bytes to write
|
||||||
std::uint8_t bytes[4];
|
std::byte bytes[4];
|
||||||
|
|
||||||
// clang-format off
|
// clang-format off
|
||||||
switch (bytestoWrite)
|
switch (bytestoWrite)
|
||||||
{
|
{
|
||||||
case 4: bytes[3] = static_cast<std::uint8_t>((input | 0x80) & 0xBF); input >>= 6; [[fallthrough]];
|
case 4: bytes[3] = static_cast<std::byte>((input | 0x80) & 0xBF); input >>= 6; [[fallthrough]];
|
||||||
case 3: bytes[2] = static_cast<std::uint8_t>((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::uint8_t>((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::uint8_t> (input | firstBytes[bytestoWrite]);
|
case 1: bytes[0] = static_cast<std::byte> (input | firstBytes[bytestoWrite]);
|
||||||
}
|
}
|
||||||
// clang-format on
|
// clang-format on
|
||||||
|
|
||||||
|
@ -29,12 +29,14 @@
|
|||||||
|
|
||||||
#include <SFML/System/Err.hpp>
|
#include <SFML/System/Err.hpp>
|
||||||
#include <SFML/System/InputStream.hpp>
|
#include <SFML/System/InputStream.hpp>
|
||||||
|
#include <SFML/System/Utils.hpp>
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <ostream>
|
#include <ostream>
|
||||||
|
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
#include <cctype>
|
#include <cctype>
|
||||||
|
#include <cstddef>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
|
||||||
|
|
||||||
@ -50,44 +52,44 @@ bool decode(sf::InputStream& stream, std::uint8_t& value)
|
|||||||
|
|
||||||
bool decode(sf::InputStream& stream, std::int16_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))
|
if (static_cast<std::size_t>(stream.read(bytes, static_cast<std::int64_t>(sizeof(bytes)))) != sizeof(bytes))
|
||||||
return false;
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool decode(sf::InputStream& stream, std::uint16_t& value)
|
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))
|
if (static_cast<std::size_t>(stream.read(bytes, static_cast<std::int64_t>(sizeof(bytes)))) != sizeof(bytes))
|
||||||
return false;
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool decode24bit(sf::InputStream& stream, std::uint32_t& value)
|
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))
|
if (static_cast<std::size_t>(stream.read(bytes, static_cast<std::int64_t>(sizeof(bytes)))) != sizeof(bytes))
|
||||||
return false;
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool decode(sf::InputStream& stream, std::uint32_t& value)
|
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))
|
if (static_cast<std::size_t>(stream.read(bytes, static_cast<std::int64_t>(sizeof(bytes)))) != sizeof(bytes))
|
||||||
return false;
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -33,6 +33,7 @@
|
|||||||
#include <ostream>
|
#include <ostream>
|
||||||
|
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
|
#include <cstddef>
|
||||||
|
|
||||||
|
|
||||||
namespace
|
namespace
|
||||||
@ -42,23 +43,23 @@ namespace
|
|||||||
|
|
||||||
void encode(std::ostream& stream, std::int16_t value)
|
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));
|
stream.write(reinterpret_cast<const char*>(bytes), sizeof(bytes));
|
||||||
}
|
}
|
||||||
|
|
||||||
void encode(std::ostream& stream, std::uint16_t value)
|
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));
|
stream.write(reinterpret_cast<const char*>(bytes), sizeof(bytes));
|
||||||
}
|
}
|
||||||
|
|
||||||
void encode(std::ostream& stream, std::uint32_t value)
|
void encode(std::ostream& stream, std::uint32_t value)
|
||||||
{
|
{
|
||||||
unsigned char bytes[] = {
|
const std::byte bytes[] = {
|
||||||
static_cast<unsigned char>(value & 0x000000FF),
|
static_cast<std::byte>(value & 0x000000FF),
|
||||||
static_cast<unsigned char>((value & 0x0000FF00) >> 8),
|
static_cast<std::byte>((value & 0x0000FF00) >> 8),
|
||||||
static_cast<unsigned char>((value & 0x00FF0000) >> 16),
|
static_cast<std::byte>((value & 0x00FF0000) >> 16),
|
||||||
static_cast<unsigned char>((value & 0xFF000000) >> 24),
|
static_cast<std::byte>((value & 0xFF000000) >> 24),
|
||||||
};
|
};
|
||||||
stream.write(reinterpret_cast<const char*>(bytes), sizeof(bytes));
|
stream.write(reinterpret_cast<const char*>(bytes), sizeof(bytes));
|
||||||
}
|
}
|
||||||
|
@ -45,6 +45,7 @@
|
|||||||
|
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
|
#include <cstddef>
|
||||||
|
|
||||||
|
|
||||||
namespace
|
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
|
// coordinates we need to set up the pointers to the vertices' components
|
||||||
if (!m_cache.enable || !useVertexCache || !m_cache.useVertexCache)
|
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 we pre-transform the vertices, we must use our internal vertex cache
|
||||||
if (useVertexCache)
|
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(glVertexPointer(2, GL_FLOAT, sizeof(Vertex), data + 0));
|
||||||
glCheck(glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(Vertex), data + 8));
|
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)
|
else if (enableTexCoordsArray && !m_cache.texCoordsArrayEnabled)
|
||||||
{
|
{
|
||||||
// If we enter this block, we are already using our internal vertex cache
|
// 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));
|
glCheck(glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), data + 12));
|
||||||
}
|
}
|
||||||
|
@ -29,6 +29,7 @@
|
|||||||
#include <SFML/Network/SocketImpl.hpp>
|
#include <SFML/Network/SocketImpl.hpp>
|
||||||
|
|
||||||
#include <SFML/System/String.hpp>
|
#include <SFML/System/String.hpp>
|
||||||
|
#include <SFML/System/Utils.hpp>
|
||||||
|
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <cwchar>
|
#include <cwchar>
|
||||||
@ -216,13 +217,10 @@ Packet& Packet::operator>>(std::int64_t& data)
|
|||||||
{
|
{
|
||||||
// Since ntohll is not available everywhere, we have to convert
|
// Since ntohll is not available everywhere, we have to convert
|
||||||
// to network byte order (big endian) manually
|
// 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));
|
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) |
|
data = toInteger<std::int64_t>(bytes[7], bytes[6], bytes[5], bytes[4], bytes[3], bytes[2], bytes[1], bytes[0]);
|
||||||
(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]));
|
|
||||||
|
|
||||||
m_readPos += sizeof(data);
|
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
|
// Since ntohll is not available everywhere, we have to convert
|
||||||
// to network byte order (big endian) manually
|
// 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));
|
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) |
|
data = toInteger<std::uint64_t>(bytes[7], bytes[6], bytes[5], bytes[4], bytes[3], bytes[2], bytes[1], bytes[0]);
|
||||||
(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]));
|
|
||||||
|
|
||||||
m_readPos += sizeof(data);
|
m_readPos += sizeof(data);
|
||||||
}
|
}
|
||||||
@ -311,7 +306,7 @@ Packet& Packet::operator>>(std::string& data)
|
|||||||
if ((length > 0) && checkSize(length))
|
if ((length > 0) && checkSize(length))
|
||||||
{
|
{
|
||||||
// Then extract characters
|
// Then extract characters
|
||||||
data.assign(&m_data[m_readPos], length);
|
data.assign(reinterpret_cast<char*>(&m_data[m_readPos]), length);
|
||||||
|
|
||||||
// Update reading position
|
// Update reading position
|
||||||
m_readPos += length;
|
m_readPos += length;
|
||||||
|
@ -412,7 +412,7 @@ Socket::Status TcpSocket::receive(Packet& packet)
|
|||||||
if (received > 0)
|
if (received > 0)
|
||||||
{
|
{
|
||||||
m_pendingPacket.Data.resize(m_pendingPacket.Data.size() + received);
|
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);
|
std::memcpy(begin, buffer, received);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -39,7 +39,7 @@ MemoryInputStream::MemoryInputStream() = default;
|
|||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
void MemoryInputStream::open(const void* data, std::size_t sizeInBytes)
|
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_size = static_cast<std::int64_t>(sizeInBytes);
|
||||||
m_offset = 0;
|
m_offset = 0;
|
||||||
}
|
}
|
||||||
|
@ -51,4 +51,15 @@ namespace sf
|
|||||||
return stream.str();
|
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
|
} // namespace sf
|
||||||
|
Loading…
Reference in New Issue
Block a user