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

Replace C arrays with std::array #2951

merged 1 commit into from May 14, 2024

Conversation

ChrisThrasher
Copy link
Member

Description

SFML uses C style arrays quite a lot so I wanted to start chipping away at replacing them with std::array. I chose to start with instances where switching to std::array was particularly straightforward. Eventually it would be nice to enable the clang-tidy check that forbids C arrays.

For more reading on why C style arrays are not ideal, see the C++ Core Guidelines.

https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#slcon1-prefer-using-stl-array-or-vector-instead-of-a-c-array

src/SFML/Window/macOS/JoystickImpl.hpp Show resolved Hide resolved
Comment on lines +46 to +50
const std::array bytes = {static_cast<char>(value & 0xFF), static_cast<char>(value >> 8)};
stream.write(bytes.data(), bytes.size());
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.

@@ -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.

@coveralls
Copy link
Collaborator

coveralls commented Apr 21, 2024

Pull Request Test Coverage Report for Build 9057908394

Details

  • 41 of 65 (63.08%) changed or added relevant lines in 14 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.04%) to 51.865%

Changes Missing Coverage Covered Lines Changed/Added Lines %
src/SFML/Graphics/Texture.cpp 0 2 0.0%
src/SFML/Network/TcpSocket.cpp 0 3 0.0%
src/SFML/Window/Unix/JoystickImpl.cpp 0 5 0.0%
src/SFML/Audio/SoundFileWriterWav.cpp 0 14 0.0%
Totals Coverage Status
Change from base Build 9057848497: -0.04%
Covered Lines: 10474
Relevant Lines: 19003

💛 - Coveralls

@ChrisThrasher ChrisThrasher force-pushed the remove_c_arrays branch 4 times, most recently from 9ae6f05 to 67200df Compare April 26, 2024 19:56
JoystickState m_state; ///< Current state of the joystick
sf::Joystick::Identification m_identification; ///< Identification of the joystick
int m_file{-1}; ///< File descriptor of the joystick
std::array<char, ABS_CNT> m_mapping{}; ///< Axes mapping (index to axis id)
Copy link
Member Author

Choose a reason for hiding this comment

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

Screenshot 2024-04-26 at 1 57 55 PM

ABS_CNT exists as a simpler way to express ABS_MAX + 1.

@ChrisThrasher ChrisThrasher force-pushed the remove_c_arrays branch 2 times, most recently from ab1da1c to cad70bd Compare April 28, 2024 03:05
@ChrisThrasher
Copy link
Member Author

Rebased on master and fixed merge conflicts

Copy link
Member

@eXpl0it3r eXpl0it3r left a comment

Choose a reason for hiding this comment

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

Just one question, but maybe you want to tackle that separately.

include/SFML/Graphics/Transform.inl Show resolved Hide resolved
@ChrisThrasher ChrisThrasher merged commit 593c4fe into master May 14, 2024
209 checks passed
@ChrisThrasher ChrisThrasher deleted the remove_c_arrays branch May 14, 2024 17:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

None yet

3 participants