From fa28722ff733d234b291a341df80af5fcb4ba970 Mon Sep 17 00:00:00 2001 From: Chris Thrasher Date: Sun, 14 Jan 2024 13:53:55 -0700 Subject: [PATCH] Improve const correctness --- examples/island/Island.cpp | 20 +++++++++----------- examples/joystick/Joystick.cpp | 17 +++++++++-------- examples/voip/Client.cpp | 4 ++-- examples/voip/Server.cpp | 4 ++-- src/SFML/Network/Packet.cpp | 8 ++++---- src/SFML/Network/TcpSocket.cpp | 4 ++-- src/SFML/Window/Android/SensorImpl.cpp | 2 +- src/SFML/Window/DRM/DRMContext.cpp | 17 +++-------------- src/SFML/Window/DRM/InputImpl.cpp | 2 +- src/SFML/Window/EglContext.cpp | 8 +------- src/SFML/Window/Unix/KeyboardImpl.cpp | 4 ++-- src/SFML/Window/Win32/JoystickImpl.cpp | 18 +++++++++--------- src/SFML/Window/Win32/WindowImplWin32.cpp | 2 +- 13 files changed, 46 insertions(+), 64 deletions(-) diff --git a/examples/island/Island.cpp b/examples/island/Island.cpp index bcf5cde2b..628c17c52 100644 --- a/examples/island/Island.cpp +++ b/examples/island/Island.cpp @@ -24,14 +24,15 @@ namespace { // Width and height of the application window -const sf::Vector2u windowSize(800, 600); +constexpr sf::Vector2u windowSize(800, 600); // Resolution of the generated terrain -const sf::Vector2u resolution(800, 600); +constexpr sf::Vector2u resolution(800, 600); // Thread pool parameters -const unsigned int threadCount = 4; -const unsigned int blockCount = 32; +constexpr unsigned int threadCount = 4; +constexpr unsigned int blockCount = 32; +constexpr unsigned int rowBlockSize = (resolution.y / blockCount) + 1; struct WorkItem { @@ -53,7 +54,7 @@ struct Setting }; // Terrain noise parameters -const int perlinOctaves = 3; +constexpr int perlinOctaves = 3; float perlinFrequency = 7.0f; float perlinFrequencyBase = 4.0f; @@ -242,8 +243,8 @@ sf::Vector2f computeNormal(float left, float right, float bottom, float top) //////////////////////////////////////////////////////////// sf::Vertex computeVertex(sf::Vector2u position) { - static const auto scalingFactors = sf::Vector2f(windowSize).componentWiseDiv(sf::Vector2f(resolution)); - sf::Vertex vertex; + static constexpr auto scalingFactors = sf::Vector2f(windowSize).componentWiseDiv(sf::Vector2f(resolution)); + sf::Vertex vertex; vertex.position = sf::Vector2f(position).componentWiseMul(scalingFactors); vertex.color = getTerrainColor(getElevation(position), getMoisture(position)); vertex.texCoords = computeNormal(getElevation(position - sf::Vector2u(1, 0)), @@ -262,8 +263,7 @@ sf::Vertex computeVertex(sf::Vector2u position) //////////////////////////////////////////////////////////// void processWorkItem(std::vector& vertices, const WorkItem& workItem) { - const unsigned int rowBlockSize = (resolution.y / blockCount) + 1; - const unsigned int rowStart = rowBlockSize * workItem.index; + const unsigned int rowStart = rowBlockSize * workItem.index; if (rowStart >= resolution.y) return; @@ -337,8 +337,6 @@ void processWorkItem(std::vector& vertices, const WorkItem& workItem //////////////////////////////////////////////////////////// void threadFunction() { - const unsigned int rowBlockSize = (resolution.y / blockCount) + 1; - std::vector vertices(resolution.x * rowBlockSize * 6); WorkItem workItem = {nullptr, 0}; diff --git a/examples/joystick/Joystick.cpp b/examples/joystick/Joystick.cpp index 05a5b6acc..bca3396d8 100644 --- a/examples/joystick/Joystick.cpp +++ b/examples/joystick/Joystick.cpp @@ -102,8 +102,8 @@ int main() // Set up our joystick identification sf::Text objects { - auto [it, success] = texts.try_emplace("ID", JoystickObject{{font, ""}, {font}}); - auto& [label, value] = it->second; + const auto [it, success] = texts.try_emplace("ID", JoystickObject{{font, ""}, {font}}); + auto& [label, value] = it->second; label.setPosition({5.f, 5.f}); value.setPosition({80.f, 5.f}); } @@ -112,8 +112,8 @@ int main() sstr.str(""); sstr << threshold << " (Change with up/down arrow keys)"; { - auto [it, success] = texts.try_emplace("Threshold", JoystickObject{{font, "Threshold:"}, {font, sstr.str()}}); - auto& [label, value] = it->second; + const auto [it, success] = texts.emplace("Threshold", JoystickObject{{font, "Threshold:"}, {font, sstr.str()}}); + auto& [label, value] = it->second; label.setPosition({5.f, 5.f + 2 * font.getLineSpacing(14)}); value.setPosition({80.f, 5.f + 2 * font.getLineSpacing(14)}); } @@ -121,9 +121,9 @@ int main() // Set up our label-value sf::Text objects for (unsigned int i = 0; i < sf::Joystick::AxisCount; ++i) { - auto [it, - success] = texts.try_emplace(axislabels[i], JoystickObject{{font, axislabels[i] + ":"}, {font, "N/A"}}); - auto& [label, value] = it->second; + const auto [it, success] = texts.try_emplace(axislabels[i], + JoystickObject{{font, axislabels[i] + ":"}, {font, "N/A"}}); + auto& [label, value] = it->second; label.setPosition({5.f, 5.f + (static_cast(i + 4) * font.getLineSpacing(14))}); value.setPosition({80.f, 5.f + (static_cast(i + 4) * font.getLineSpacing(14))}); } @@ -132,7 +132,8 @@ int main() { sstr.str(""); sstr << "Button " << i; - auto [it, success] = texts.try_emplace(sstr.str(), JoystickObject{{font, sstr.str() + ":"}, {font, "N/A"}}); + const auto [it, + success] = texts.try_emplace(sstr.str(), JoystickObject{{font, sstr.str() + ":"}, {font, "N/A"}}); auto& [label, value] = it->second; label.setPosition({5.f, 5.f + (static_cast(sf::Joystick::AxisCount + i + 4) * font.getLineSpacing(14))}); value.setPosition({80.f, 5.f + (static_cast(sf::Joystick::AxisCount + i + 4) * font.getLineSpacing(14))}); diff --git a/examples/voip/Client.cpp b/examples/voip/Client.cpp index 842ec6897..b14a2404d 100644 --- a/examples/voip/Client.cpp +++ b/examples/voip/Client.cpp @@ -14,8 +14,8 @@ #include -const std::uint8_t clientAudioData = 1; -const std::uint8_t clientEndOfStream = 2; +constexpr std::uint8_t clientAudioData = 1; +constexpr std::uint8_t clientEndOfStream = 2; //////////////////////////////////////////////////////////// diff --git a/examples/voip/Server.cpp b/examples/voip/Server.cpp index 978a654ef..c9f28064c 100644 --- a/examples/voip/Server.cpp +++ b/examples/voip/Server.cpp @@ -16,8 +16,8 @@ #include -const std::uint8_t serverAudioData = 1; -const std::uint8_t serverEndOfStream = 2; +constexpr std::uint8_t serverAudioData = 1; +constexpr std::uint8_t serverEndOfStream = 2; //////////////////////////////////////////////////////////// diff --git a/src/SFML/Network/Packet.cpp b/src/SFML/Network/Packet.cpp index f006d26a7..6c1d6c1d7 100644 --- a/src/SFML/Network/Packet.cpp +++ b/src/SFML/Network/Packet.cpp @@ -395,7 +395,7 @@ Packet& Packet::operator<<(std::uint8_t data) //////////////////////////////////////////////////////////// Packet& Packet::operator<<(std::int16_t data) { - auto toWrite = static_cast(htons(static_cast(data))); + const auto toWrite = static_cast(htons(static_cast(data))); append(&toWrite, sizeof(toWrite)); return *this; } @@ -404,7 +404,7 @@ Packet& Packet::operator<<(std::int16_t data) //////////////////////////////////////////////////////////// Packet& Packet::operator<<(std::uint16_t data) { - std::uint16_t toWrite = htons(data); + const std::uint16_t toWrite = htons(data); append(&toWrite, sizeof(toWrite)); return *this; } @@ -413,7 +413,7 @@ Packet& Packet::operator<<(std::uint16_t data) //////////////////////////////////////////////////////////// Packet& Packet::operator<<(std::int32_t data) { - auto toWrite = static_cast(htonl(static_cast(data))); + const auto toWrite = static_cast(htonl(static_cast(data))); append(&toWrite, sizeof(toWrite)); return *this; } @@ -422,7 +422,7 @@ Packet& Packet::operator<<(std::int32_t data) //////////////////////////////////////////////////////////// Packet& Packet::operator<<(std::uint32_t data) { - std::uint32_t toWrite = htonl(data); + const std::uint32_t toWrite = htonl(data); append(&toWrite, sizeof(toWrite)); return *this; } diff --git a/src/SFML/Network/TcpSocket.cpp b/src/SFML/Network/TcpSocket.cpp index 76b7b29d7..f7e2f6116 100644 --- a/src/SFML/Network/TcpSocket.cpp +++ b/src/SFML/Network/TcpSocket.cpp @@ -47,9 +47,9 @@ namespace { // Low-level send/receive flags (OS-dependent) #ifdef SFML_SYSTEM_LINUX -const int flags = MSG_NOSIGNAL; +constexpr int flags = MSG_NOSIGNAL; #else -const int flags = 0; +constexpr int flags = 0; #endif } // namespace diff --git a/src/SFML/Window/Android/SensorImpl.cpp b/src/SFML/Window/Android/SensorImpl.cpp index 3ddd7c430..f9cdef0d8 100644 --- a/src/SFML/Window/Android/SensorImpl.cpp +++ b/src/SFML/Window/Android/SensorImpl.cpp @@ -141,7 +141,7 @@ void SensorImpl::setEnabled(bool enabled) const ASensor* SensorImpl::getDefaultSensor(Sensor::Type sensor) { // Find the Android sensor type - static EnumArray types = + static constexpr EnumArray types = {ASENSOR_TYPE_ACCELEROMETER, ASENSOR_TYPE_GYROSCOPE, ASENSOR_TYPE_MAGNETIC_FIELD, diff --git a/src/SFML/Window/DRM/DRMContext.cpp b/src/SFML/Window/DRM/DRMContext.cpp index 26215a3d7..a0a652637 100644 --- a/src/SFML/Window/DRM/DRMContext.cpp +++ b/src/SFML/Window/DRM/DRMContext.cpp @@ -174,13 +174,8 @@ DrmFb* drmFbGetFromBo(gbm_bo& bo) modifiers[static_cast(i)] = modifiers[0]; } - std::uint32_t flags = 0; - if (modifiers[0]) - { - flags = DRM_MODE_FB_MODIFIERS; - } - - int result = drmModeAddFB2WithModifiers(drmFd, + const std::uint32_t flags = modifiers[0] ? DRM_MODE_FB_MODIFIERS : 0; + int result = drmModeAddFB2WithModifiers(drmFd, width, height, format, @@ -695,13 +690,7 @@ void DRMContext::createContext(DRMContext* shared) { static constexpr std::array contextVersion = {EGL_CONTEXT_CLIENT_VERSION, 1, EGL_NONE}; - EGLContext toShared = nullptr; - - if (shared) - toShared = shared->m_context; - else - toShared = EGL_NO_CONTEXT; - + const EGLContext toShared = shared ? shared->m_context : EGL_NO_CONTEXT; if (toShared != EGL_NO_CONTEXT) eglCheck(eglMakeCurrent(m_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT)); diff --git a/src/SFML/Window/DRM/InputImpl.cpp b/src/SFML/Window/DRM/InputImpl.cpp index 3cfb7c24b..4114066e6 100644 --- a/src/SFML/Window/DRM/InputImpl.cpp +++ b/src/SFML/Window/DRM/InputImpl.cpp @@ -71,7 +71,7 @@ std::vector touchSlots; // track the state of each touch "slot" int currentSlot = 0; // which slot are we currently updating? std::queue eventQueue; // events received and waiting to be consumed -const int maxQueue = 64; // The maximum size we let eventQueue grow to +constexpr int maxQueue = 64; // The maximum size we let eventQueue grow to termios newTerminalConfig, oldTerminalConfig; // Terminal configurations diff --git a/src/SFML/Window/EglContext.cpp b/src/SFML/Window/EglContext.cpp index 9ceba4624..9bb62e5ef 100644 --- a/src/SFML/Window/EglContext.cpp +++ b/src/SFML/Window/EglContext.cpp @@ -254,13 +254,7 @@ void EglContext::createContext(EglContext* shared) { static constexpr std::array contextVersion = {EGL_CONTEXT_CLIENT_VERSION, 1, EGL_NONE}; - EGLContext toShared = nullptr; - - if (shared) - toShared = shared->m_context; - else - toShared = EGL_NO_CONTEXT; - + const EGLContext toShared = shared ? shared->m_context : EGL_NO_CONTEXT; if (toShared != EGL_NO_CONTEXT) eglCheck(eglMakeCurrent(m_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT)); diff --git a/src/SFML/Window/Unix/KeyboardImpl.cpp b/src/SFML/Window/Unix/KeyboardImpl.cpp index 58781e675..7be0cd59a 100644 --- a/src/SFML/Window/Unix/KeyboardImpl.cpp +++ b/src/SFML/Window/Unix/KeyboardImpl.cpp @@ -48,8 +48,8 @@ namespace { -const KeyCode nullKeyCode = 0; -const int maxKeyCode = 256; +constexpr KeyCode nullKeyCode = 0; +constexpr int maxKeyCode = 256; sf::priv::EnumArray scancodeToKeycode; ///< Mapping of SFML scancode to X11 KeyCode std::array keycodeToScancode; ///< Mapping of X11 KeyCode to SFML scancode diff --git a/src/SFML/Window/Win32/JoystickImpl.cpp b/src/SFML/Window/Win32/JoystickImpl.cpp index 85f077831..cb7793fb2 100644 --- a/src/SFML/Window/Win32/JoystickImpl.cpp +++ b/src/SFML/Window/Win32/JoystickImpl.cpp @@ -61,18 +61,18 @@ namespace namespace guids { // NOLINTBEGIN(readability-identifier-naming) -const GUID IID_IDirectInput8W = {0xbf798031, 0x483a, 0x4da2, {0xaa, 0x99, 0x5d, 0x64, 0xed, 0x36, 0x97, 0x00}}; +constexpr GUID IID_IDirectInput8W = {0xbf798031, 0x483a, 0x4da2, {0xaa, 0x99, 0x5d, 0x64, 0xed, 0x36, 0x97, 0x00}}; -const GUID GUID_XAxis = {0xa36d02e0, 0xc9f3, 0x11cf, {0xbf, 0xc7, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00}}; -const GUID GUID_YAxis = {0xa36d02e1, 0xc9f3, 0x11cf, {0xbf, 0xc7, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00}}; -const GUID GUID_ZAxis = {0xa36d02e2, 0xc9f3, 0x11cf, {0xbf, 0xc7, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00}}; -const GUID GUID_RzAxis = {0xa36d02e3, 0xc9f3, 0x11cf, {0xbf, 0xc7, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00}}; -const GUID GUID_Slider = {0xa36d02e4, 0xc9f3, 0x11cf, {0xbf, 0xc7, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00}}; +constexpr GUID GUID_XAxis = {0xa36d02e0, 0xc9f3, 0x11cf, {0xbf, 0xc7, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00}}; +constexpr GUID GUID_YAxis = {0xa36d02e1, 0xc9f3, 0x11cf, {0xbf, 0xc7, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00}}; +constexpr GUID GUID_ZAxis = {0xa36d02e2, 0xc9f3, 0x11cf, {0xbf, 0xc7, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00}}; +constexpr GUID GUID_RzAxis = {0xa36d02e3, 0xc9f3, 0x11cf, {0xbf, 0xc7, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00}}; +constexpr GUID GUID_Slider = {0xa36d02e4, 0xc9f3, 0x11cf, {0xbf, 0xc7, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00}}; -const GUID GUID_POV = {0xa36d02f2, 0xc9f3, 0x11cf, {0xbf, 0xc7, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00}}; +constexpr GUID GUID_POV = {0xa36d02f2, 0xc9f3, 0x11cf, {0xbf, 0xc7, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00}}; -const GUID GUID_RxAxis = {0xa36d02f4, 0xc9f3, 0x11cf, {0xbf, 0xc7, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00}}; -const GUID GUID_RyAxis = {0xa36d02f5, 0xc9f3, 0x11cf, {0xbf, 0xc7, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00}}; +constexpr GUID GUID_RxAxis = {0xa36d02f4, 0xc9f3, 0x11cf, {0xbf, 0xc7, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00}}; +constexpr GUID GUID_RyAxis = {0xa36d02f5, 0xc9f3, 0x11cf, {0xbf, 0xc7, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00}}; // NOLINTEND(readability-identifier-naming) } // namespace guids diff --git a/src/SFML/Window/Win32/WindowImplWin32.cpp b/src/SFML/Window/Win32/WindowImplWin32.cpp index 801e96fda..68500d0f8 100644 --- a/src/SFML/Window/Win32/WindowImplWin32.cpp +++ b/src/SFML/Window/Win32/WindowImplWin32.cpp @@ -65,7 +65,7 @@ unsigned int handleCount = 0; // All window handles const wchar_t* className = L"SFML_Window"; sf::priv::WindowImplWin32* fullscreenWindow = nullptr; -const GUID guidDevinterfaceHid = {0x4d1e55b2, 0xf16f, 0x11cf, {0x88, 0xcb, 0x00, 0x11, 0x11, 0x00, 0x00, 0x30}}; +constexpr GUID guidDevinterfaceHid = {0x4d1e55b2, 0xf16f, 0x11cf, {0x88, 0xcb, 0x00, 0x11, 0x11, 0x00, 0x00, 0x30}}; void setProcessDpiAware() {