Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace C arrays with std::array #2951

Merged
merged 1 commit into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 6 additions & 4 deletions include/SFML/Graphics/Transform.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@

#include <SFML/System/Vector2.hpp>

#include <array>


namespace sf
{
Expand Down Expand Up @@ -267,10 +269,10 @@ class Transform
// Member data
////////////////////////////////////////////////////////////
// clang-format off
float m_matrix[16]{1.f, 0.f, 0.f, 0.f,
0.f, 1.f, 0.f, 0.f,
0.f, 0.f, 1.f, 0.f,
0.f, 0.f, 0.f, 1.f}; //!< 4x4 matrix defining the transformation
std::array<float, 16> m_matrix{1.f, 0.f, 0.f, 0.f,
0.f, 1.f, 0.f, 0.f,
0.f, 0.f, 1.f, 0.f,
0.f, 0.f, 0.f, 1.f}; //!< 4x4 matrix defining the transformation
// clang-format off
};

Expand Down
6 changes: 3 additions & 3 deletions include/SFML/Graphics/Transform.inl
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ constexpr Transform::Transform(float a00, float a01, float a02,
////////////////////////////////////////////////////////////
constexpr const float* Transform::getMatrix() const
eXpl0it3r marked this conversation as resolved.
Show resolved Hide resolved
{
return m_matrix;
return m_matrix.data();
}


Expand Down Expand Up @@ -133,8 +133,8 @@ constexpr FloatRect Transform::transformRect(const FloatRect& rectangle) const
////////////////////////////////////////////////////////////
constexpr Transform& Transform::combine(const Transform& transform)
{
const float* a = m_matrix;
const float* b = transform.m_matrix;
const auto& a = m_matrix;
const auto& b = transform.m_matrix;

// clang-format off
*this = Transform(a[0] * b[0] + a[4] * b[1] + a[12] * b[3],
Expand Down
1 change: 1 addition & 0 deletions include/SFML/System/Utf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
////////////////////////////////////////////////////////////
#include <SFML/Config.hpp>

#include <array>
#include <locale>

#include <cstdint>
Expand Down
12 changes: 6 additions & 6 deletions include/SFML/System/Utf.inl
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ In Utf<8>::decode(In begin, In end, std::uint32_t& output, std::uint32_t replace
{
// clang-format off
// Some useful precomputed data
static constexpr int trailing[256] =
static constexpr std::array<std::uint8_t, 256> trailing =
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The template type was changed from int to std::uint8_t for two reasons. First of all using a smaller type saves memory. We don't need numbers bigger than 5 so std::uint8_t is a more efficient representation. Second, by using an unsigned type it avoids a sign conversion warning when using one of these array members to index into an array. C-style arrays let you index with signed types but std::array expects a std::size_t thus causing a compiler warning when trying to index into it with an int.

{
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
Expand All @@ -68,14 +68,14 @@ In Utf<8>::decode(In begin, In end, std::uint32_t& output, std::uint32_t replace
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5
};

static constexpr std::uint32_t offsets[6] =
static constexpr std::array<std::uint32_t, 6> offsets =
{
0x00000000, 0x00003080, 0x000E2080, 0x03C82080, 0xFA082080, 0x82082080
};
// clang-format on

// decode the character
const int trailingBytes = trailing[static_cast<std::uint8_t>(*begin)];
const auto trailingBytes = trailing[static_cast<std::uint8_t>(*begin)];
if (trailingBytes < std::distance(begin, end))
{
output = 0;
Expand Down Expand Up @@ -110,7 +110,7 @@ template <typename Out>
Out Utf<8>::encode(std::uint32_t input, Out output, std::uint8_t replacement)
{
// Some useful precomputed data
static constexpr std::uint8_t firstBytes[7] = {0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC};
static constexpr std::array<std::uint8_t, 7> firstBytes = {0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC};

// encode the character
if ((input > 0x0010FFFF) || ((input >= 0xD800) && (input <= 0xDBFF)))
Expand All @@ -134,7 +134,7 @@ Out Utf<8>::encode(std::uint32_t input, Out output, std::uint8_t replacement)
// clang-format on

// Extract the bytes to write
std::byte bytes[4];
std::array<std::byte, 4> bytes{};

// clang-format off
switch (bytestoWrite)
Expand All @@ -147,7 +147,7 @@ Out Utf<8>::encode(std::uint32_t input, Out output, std::uint8_t replacement)
// clang-format on

// Add them to the output
output = priv::copy(bytes, bytes + bytestoWrite, output);
output = priv::copy(bytes.data(), bytes.data() + bytestoWrite, output);
}

return output;
Expand Down
13 changes: 7 additions & 6 deletions src/SFML/Audio/SoundFileWriterOgg.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

#include <vorbis/vorbisenc.h>

#include <array>
#include <filesystem>
#include <fstream>

Expand Down Expand Up @@ -103,12 +104,12 @@ class SoundFileWriterOgg : public SoundFileWriter
////////////////////////////////////////////////////////////
// Member data
////////////////////////////////////////////////////////////
unsigned int m_channelCount{}; //!< Channel count of the sound being written
std::size_t m_remapTable[8]{}; //!< Table we use to remap source to target channel order
std::ofstream m_file; //!< Output file
ogg_stream_state m_ogg{}; //!< OGG stream
vorbis_info m_vorbis{}; //!< Vorbis handle
vorbis_dsp_state m_state{}; //!< Current encoding state
unsigned int m_channelCount{}; //!< Channel count of the sound being written
std::array<std::size_t, 8> m_remapTable{}; //!< Table we use to remap source to target channel order
std::ofstream m_file; //!< Output file
ogg_stream_state m_ogg{}; //!< OGG stream
vorbis_info m_vorbis{}; //!< Vorbis handle
vorbis_dsp_state m_state{}; //!< Current encoding state
};

} // namespace sf::priv
41 changes: 21 additions & 20 deletions src/SFML/Audio/SoundFileWriterWav.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <SFML/System/Utils.hpp>

#include <algorithm>
#include <array>
#include <ostream>

#include <cassert>
Expand All @@ -45,25 +46,25 @@ namespace

void encode(std::ostream& stream, std::int16_t value)
{
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));
const std::array bytes = {static_cast<char>(value & 0xFF), static_cast<char>(value >> 8)};
stream.write(bytes.data(), bytes.size());
Comment on lines +49 to +50
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I'm the one who started using std::byte here. It wasn't the right move since we immediately have to reintrepret_cast to a char*. Might as well just keeping using char to avoid extra casts.

}

void encode(std::ostream& stream, std::uint16_t value)
{
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));
const std::array bytes = {static_cast<char>(value & 0xFF), static_cast<char>(value >> 8)};
stream.write(bytes.data(), bytes.size());
}

void encode(std::ostream& stream, std::uint32_t value)
{
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),
const std::array bytes = {
static_cast<char>(value & 0x000000FF),
static_cast<char>((value & 0x0000FF00) >> 8),
static_cast<char>((value & 0x00FF0000) >> 16),
static_cast<char>((value & 0xFF000000) >> 24),
};
stream.write(reinterpret_cast<const char*>(bytes), sizeof(bytes));
stream.write(bytes.data(), bytes.size());
}
} // namespace

Expand Down Expand Up @@ -247,17 +248,17 @@ void SoundFileWriterWav::writeHeader(unsigned int sampleRate, unsigned int chann
assert(m_file.good() && "Most recent I/O operation failed");

// Write the main chunk ID
char mainChunkId[4] = {'R', 'I', 'F', 'F'};
m_file.write(mainChunkId, sizeof(mainChunkId));
std::array mainChunkId = {'R', 'I', 'F', 'F'};
m_file.write(mainChunkId.data(), mainChunkId.size());

// Write the main chunk header
encode(m_file, static_cast<std::uint32_t>(0)); // 0 is a placeholder, will be written later
char mainChunkFormat[4] = {'W', 'A', 'V', 'E'};
m_file.write(mainChunkFormat, sizeof(mainChunkFormat));
std::array mainChunkFormat = {'W', 'A', 'V', 'E'};
m_file.write(mainChunkFormat.data(), mainChunkFormat.size());

// Write the sub-chunk 1 ("format") id and size
char fmtChunkId[4] = {'f', 'm', 't', ' '};
m_file.write(fmtChunkId, sizeof(fmtChunkId));
std::array fmtChunkId = {'f', 'm', 't', ' '};
m_file.write(fmtChunkId.data(), fmtChunkId.size());

if (channelCount > 2)
{
Expand Down Expand Up @@ -295,14 +296,14 @@ void SoundFileWriterWav::writeHeader(unsigned int sampleRate, unsigned int chann
encode(m_file, bitsPerSample);
encode(m_file, channelMask);
// Write the subformat (PCM)
char subformat[16] =
std::array subformat =
{'\x01', '\x00', '\x00', '\x00', '\x00', '\x00', '\x10', '\x00', '\x80', '\x00', '\x00', '\xAA', '\x00', '\x38', '\x9B', '\x71'};
m_file.write(subformat, sizeof(subformat));
m_file.write(subformat.data(), subformat.size());
}

// Write the sub-chunk 2 ("data") id and size
char dataChunkId[4] = {'d', 'a', 't', 'a'};
m_file.write(dataChunkId, sizeof(dataChunkId));
std::array dataChunkId = {'d', 'a', 't', 'a'};
m_file.write(dataChunkId.data(), dataChunkId.size());
const std::uint32_t dataChunkSize = 0; // placeholder, will be written later
encode(m_file, dataChunkSize);
}
Expand Down
7 changes: 4 additions & 3 deletions src/SFML/Audio/SoundFileWriterWav.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
////////////////////////////////////////////////////////////
#include <SFML/Audio/SoundFileWriter.hpp>

#include <array>
#include <filesystem>
#include <fstream>

Expand Down Expand Up @@ -105,9 +106,9 @@ class SoundFileWriterWav : public SoundFileWriter
////////////////////////////////////////////////////////////
// Member data
////////////////////////////////////////////////////////////
std::ofstream m_file; //!< File stream to write to
unsigned int m_channelCount{}; //!< Channel count of the sound being written
std::size_t m_remapTable[18]{}; //!< Table we use to remap source to target channel order
std::ofstream m_file; //!< File stream to write to
unsigned int m_channelCount{}; //!< Channel count of the sound being written
std::array<std::size_t, 18> m_remapTable{}; //!< Table we use to remap source to target channel order
};

} // namespace sf::priv
11 changes: 6 additions & 5 deletions src/SFML/Graphics/Texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include <SFML/System/Err.hpp>

#include <algorithm>
#include <array>
#include <atomic>
#include <ostream>
#include <utility>
Expand Down Expand Up @@ -861,10 +862,10 @@ void Texture::bind(const Texture* texture, CoordinateType coordinateType)
if ((coordinateType == CoordinateType::Pixels) || texture->m_pixelsFlipped)
{
// clang-format off
GLfloat matrix[16] = {1.f, 0.f, 0.f, 0.f,
0.f, 1.f, 0.f, 0.f,
0.f, 0.f, 1.f, 0.f,
0.f, 0.f, 0.f, 1.f};
std::array matrix = {1.f, 0.f, 0.f, 0.f,
0.f, 1.f, 0.f, 0.f,
0.f, 0.f, 1.f, 0.f,
0.f, 0.f, 0.f, 1.f};
// clang-format on

// If non-normalized coordinates (= pixels) are requested, we need to
Expand All @@ -884,7 +885,7 @@ void Texture::bind(const Texture* texture, CoordinateType coordinateType)

// Load the matrix
glCheck(glMatrixMode(GL_TEXTURE));
glCheck(glLoadMatrixf(matrix));
glCheck(glLoadMatrixf(matrix.data()));
}
else
{
Expand Down
38 changes: 20 additions & 18 deletions src/SFML/Network/Packet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
#include <SFML/System/String.hpp>
#include <SFML/System/Utils.hpp>

#include <array>

#include <cstring>
#include <cwchar>

Expand Down Expand Up @@ -212,8 +214,8 @@ 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::byte bytes[sizeof(data)];
std::memcpy(bytes, &m_data[m_readPos], sizeof(data));
std::array<std::byte, sizeof(data)> bytes{};
std::memcpy(bytes.data(), &m_data[m_readPos], sizeof(data));

data = toInteger<std::uint64_t>(bytes[7], bytes[6], bytes[5], bytes[4], bytes[3], bytes[2], bytes[1], bytes[0]);

Expand Down Expand Up @@ -427,14 +429,14 @@ Packet& Packet::operator<<(std::int64_t data)
// Since htonll is not available everywhere, we have to convert
// to network byte order (big endian) manually

std::uint8_t toWrite[] = {static_cast<std::uint8_t>((data >> 56) & 0xFF),
static_cast<std::uint8_t>((data >> 48) & 0xFF),
static_cast<std::uint8_t>((data >> 40) & 0xFF),
static_cast<std::uint8_t>((data >> 32) & 0xFF),
static_cast<std::uint8_t>((data >> 24) & 0xFF),
static_cast<std::uint8_t>((data >> 16) & 0xFF),
static_cast<std::uint8_t>((data >> 8) & 0xFF),
static_cast<std::uint8_t>((data)&0xFF)};
std::array toWrite = {static_cast<std::uint8_t>((data >> 56) & 0xFF),
static_cast<std::uint8_t>((data >> 48) & 0xFF),
static_cast<std::uint8_t>((data >> 40) & 0xFF),
static_cast<std::uint8_t>((data >> 32) & 0xFF),
static_cast<std::uint8_t>((data >> 24) & 0xFF),
static_cast<std::uint8_t>((data >> 16) & 0xFF),
static_cast<std::uint8_t>((data >> 8) & 0xFF),
static_cast<std::uint8_t>((data)&0xFF)};

append(&toWrite, sizeof(toWrite));
return *this;
Expand All @@ -447,14 +449,14 @@ Packet& Packet::operator<<(std::uint64_t data)
// Since htonll is not available everywhere, we have to convert
// to network byte order (big endian) manually

std::uint8_t toWrite[] = {static_cast<std::uint8_t>((data >> 56) & 0xFF),
static_cast<std::uint8_t>((data >> 48) & 0xFF),
static_cast<std::uint8_t>((data >> 40) & 0xFF),
static_cast<std::uint8_t>((data >> 32) & 0xFF),
static_cast<std::uint8_t>((data >> 24) & 0xFF),
static_cast<std::uint8_t>((data >> 16) & 0xFF),
static_cast<std::uint8_t>((data >> 8) & 0xFF),
static_cast<std::uint8_t>((data)&0xFF)};
std::array toWrite = {static_cast<std::uint8_t>((data >> 56) & 0xFF),
static_cast<std::uint8_t>((data >> 48) & 0xFF),
static_cast<std::uint8_t>((data >> 40) & 0xFF),
static_cast<std::uint8_t>((data >> 32) & 0xFF),
static_cast<std::uint8_t>((data >> 24) & 0xFF),
static_cast<std::uint8_t>((data >> 16) & 0xFF),
static_cast<std::uint8_t>((data >> 8) & 0xFF),
static_cast<std::uint8_t>((data)&0xFF)};

append(&toWrite, sizeof(toWrite));
return *this;
Expand Down
7 changes: 4 additions & 3 deletions src/SFML/Network/TcpSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include <SFML/System/Err.hpp>

#include <algorithm>
#include <array>
#include <ostream>

#include <cstring>
Expand Down Expand Up @@ -401,12 +402,12 @@ Socket::Status TcpSocket::receive(Packet& packet)
}

// Loop until we receive all the packet data
char buffer[1024];
std::array<char, 1024> buffer{};
while (m_pendingPacket.data.size() < packetSize)
{
// Receive a chunk of data
const std::size_t sizeToGet = std::min(packetSize - m_pendingPacket.data.size(), sizeof(buffer));
const Status status = receive(buffer, sizeToGet, received);
const Status status = receive(buffer.data(), sizeToGet, received);
if (status != Status::Done)
return status;

Expand All @@ -415,7 +416,7 @@ Socket::Status TcpSocket::receive(Packet& packet)
{
m_pendingPacket.data.resize(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.data(), received);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/SFML/Window/JoystickImpl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ struct JoystickState
{
bool connected{}; //!< Is the joystick currently connected?
EnumArray<Joystick::Axis, float, Joystick::AxisCount> axes{}; //!< Position of each axis, in range [-100, 100]
bool buttons[Joystick::ButtonCount]{}; //!< Status of each button (true = pressed)
std::array<bool, Joystick::ButtonCount> buttons{}; //!< Status of each button (true = pressed)
};

} // namespace sf::priv
Expand Down
4 changes: 3 additions & 1 deletion src/SFML/Window/JoystickManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
#include <SFML/Window/Joystick.hpp>
#include <SFML/Window/JoystickImpl.hpp>

#include <array>


namespace sf::priv
{
Expand Down Expand Up @@ -124,7 +126,7 @@ class JoystickManager
////////////////////////////////////////////////////////////
// Member data
////////////////////////////////////////////////////////////
Item m_joysticks[Joystick::Count]; //!< Joysticks information and state
std::array<Item, Joystick::Count> m_joysticks; //!< Joysticks information and state
};

} // namespace sf::priv