From 5b500ad2c2ce7ed901e36309d7f45bed73a2b85b Mon Sep 17 00:00:00 2001 From: Vittorio Romeo Date: Fri, 11 Feb 2022 00:02:59 +0000 Subject: [PATCH] Turn more compile-time constants into 'constexpr' variables --- include/SFML/Graphics/Color.inl | 1 - include/SFML/Graphics/Transform.inl | 1 - include/SFML/System/Angle.hpp | 6 ++-- include/SFML/System/Angle.inl | 9 +++++ include/SFML/System/Time.hpp | 2 +- include/SFML/System/Time.inl | 9 +++++ include/SFML/System/Utf.inl | 6 ++-- include/SFML/Window/ContextSettings.hpp | 2 +- src/SFML/Audio/SoundFileWriterOgg.cpp | 2 +- src/SFML/Graphics/RenderTarget.cpp | 4 +-- src/SFML/System/Angle.cpp | 38 -------------------- src/SFML/System/CMakeLists.txt | 3 +- src/SFML/System/Err.cpp | 2 +- src/SFML/System/Time.cpp | 38 -------------------- src/SFML/Window/OSX/JoystickImpl.cpp | 2 +- src/SFML/Window/Unix/WindowImplX11.cpp | 46 ++++++++++++------------- src/SFML/Window/Window.cpp | 2 +- src/SFML/Window/iOS/EaglContext.mm | 2 +- src/SFML/Window/iOS/SensorImpl.mm | 2 +- 19 files changed, 58 insertions(+), 119 deletions(-) delete mode 100644 src/SFML/System/Angle.cpp diff --git a/include/SFML/Graphics/Color.inl b/include/SFML/Graphics/Color.inl index e9c34889..5419c32f 100644 --- a/include/SFML/Graphics/Color.inl +++ b/include/SFML/Graphics/Color.inl @@ -155,7 +155,6 @@ constexpr Color& operator *=(Color& left, const Color& right) // Note: the 'inline' keyword here is technically not required, but VS2019 fails // to compile with a bogus "multiple definition" error if not explicitly used. - inline constexpr Color Color::Black(0, 0, 0); inline constexpr Color Color::White(255, 255, 255); inline constexpr Color Color::Red(255, 0, 0); diff --git a/include/SFML/Graphics/Transform.inl b/include/SFML/Graphics/Transform.inl index ecbf8225..e0b68fe2 100644 --- a/include/SFML/Graphics/Transform.inl +++ b/include/SFML/Graphics/Transform.inl @@ -218,5 +218,4 @@ constexpr bool operator !=(const Transform& left, const Transform& right) // Note: the 'inline' keyword here is technically not required, but VS2019 fails // to compile with a bogus "multiple definition" error if not explicitly used. - inline constexpr Transform Transform::Identity; diff --git a/include/SFML/System/Angle.hpp b/include/SFML/System/Angle.hpp index 306dcf58..fa25be05 100644 --- a/include/SFML/System/Angle.hpp +++ b/include/SFML/System/Angle.hpp @@ -72,7 +72,7 @@ public: //////////////////////////////////////////////////////////// /// \brief Wrap to a range such that -180° <= angle < 180° - /// + /// /// Similar to a modulo operation, this returns a copy of the angle /// constrained to the range [-180°, 180°) == [-Pi, Pi). /// The resulting angle represents a rotation which is equivalent to *this. @@ -105,7 +105,7 @@ public: //////////////////////////////////////////////////////////// /// \brief Wrap to a range such that 0° <= angle < 360° - /// + /// /// Similar to a modulo operation, this returns a copy of the angle /// constrained to the range [0°, 360°) == [0, Tau) == [0, 2*Pi). /// The resulting angle represents a rotation which is equivalent to *this. @@ -139,7 +139,7 @@ public: //////////////////////////////////////////////////////////// // Static member data //////////////////////////////////////////////////////////// - SFML_SYSTEM_API static const Angle Zero; //!< Predefined 0 degree angle value + static const Angle Zero; //!< Predefined 0 degree angle value private: diff --git a/include/SFML/System/Angle.inl b/include/SFML/System/Angle.inl index c71d6a58..447b0b2d 100644 --- a/include/SFML/System/Angle.inl +++ b/include/SFML/System/Angle.inl @@ -257,3 +257,12 @@ constexpr Angle operator "" _rad(unsigned long long angle) } } // namespace Literals + + +//////////////////////////////////////////////////////////// +// Static member data +//////////////////////////////////////////////////////////// + +// Note: the 'inline' keyword here is technically not required, but VS2019 fails +// to compile with a bogus "multiple definition" error if not explicitly used. +inline constexpr Angle Angle::Zero; diff --git a/include/SFML/System/Time.hpp b/include/SFML/System/Time.hpp index b231ed44..749492bb 100644 --- a/include/SFML/System/Time.hpp +++ b/include/SFML/System/Time.hpp @@ -82,7 +82,7 @@ public: //////////////////////////////////////////////////////////// // Static member data //////////////////////////////////////////////////////////// - SFML_SYSTEM_API static const Time Zero; //!< Predefined "zero" time value + static const Time Zero; //!< Predefined "zero" time value private: diff --git a/include/SFML/System/Time.inl b/include/SFML/System/Time.inl index 217c524d..e6d8e001 100644 --- a/include/SFML/System/Time.inl +++ b/include/SFML/System/Time.inl @@ -245,3 +245,12 @@ constexpr Time& operator %=(Time& left, Time right) { return left = left % right; } + + +//////////////////////////////////////////////////////////// +// Static member data +//////////////////////////////////////////////////////////// + +// Note: the 'inline' keyword here is technically not required, but VS2019 fails +// to compile with a bogus "multiple definition" error if not explicitly used. +inline constexpr Time Time::Zero; diff --git a/include/SFML/System/Utf.inl b/include/SFML/System/Utf.inl index 000270c0..48d343ca 100644 --- a/include/SFML/System/Utf.inl +++ b/include/SFML/System/Utf.inl @@ -48,7 +48,7 @@ template In Utf<8>::decode(In begin, In end, Uint32& output, Uint32 replacement) { // Some useful precomputed data - static const int trailing[256] = + static constexpr int trailing[256] = { 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, @@ -59,7 +59,7 @@ In Utf<8>::decode(In begin, In end, Uint32& output, Uint32 replacement) 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 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 const Uint32 offsets[6] = + static constexpr Uint32 offsets[6] = { 0x00000000, 0x00003080, 0x000E2080, 0x03C82080, 0xFA082080, 0x82082080 }; @@ -96,7 +96,7 @@ template Out Utf<8>::encode(Uint32 input, Out output, Uint8 replacement) { // Some useful precomputed data - static const Uint8 firstBytes[7] = + static constexpr Uint8 firstBytes[7] = { 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC }; diff --git a/include/SFML/Window/ContextSettings.hpp b/include/SFML/Window/ContextSettings.hpp index e01d0ac2..cd641de5 100644 --- a/include/SFML/Window/ContextSettings.hpp +++ b/include/SFML/Window/ContextSettings.hpp @@ -59,7 +59,7 @@ struct ContextSettings /// \param sRgb sRGB capable framebuffer /// //////////////////////////////////////////////////////////// - explicit ContextSettings(unsigned int depth = 0, unsigned int stencil = 0, unsigned int antialiasing = 0, unsigned int major = 1, unsigned int minor = 1, unsigned int attributes = Default, bool sRgb = false) : + constexpr explicit ContextSettings(unsigned int depth = 0, unsigned int stencil = 0, unsigned int antialiasing = 0, unsigned int major = 1, unsigned int minor = 1, unsigned int attributes = Default, bool sRgb = false) : depthBits (depth), stencilBits (stencil), antialiasingLevel(antialiasing), diff --git a/src/SFML/Audio/SoundFileWriterOgg.cpp b/src/SFML/Audio/SoundFileWriterOgg.cpp index d9ce70b5..89dcc255 100644 --- a/src/SFML/Audio/SoundFileWriterOgg.cpp +++ b/src/SFML/Audio/SoundFileWriterOgg.cpp @@ -131,7 +131,7 @@ bool SoundFileWriterOgg::open(const std::string& filename, unsigned int sampleRa void SoundFileWriterOgg::write(const Int16* samples, Uint64 count) { // Vorbis has issues with buffers that are too large, so we ask for 64K - static const int bufferSize = 65536; + constexpr int bufferSize = 65536; // A frame contains a sample from each channel int frameCount = static_cast(count / m_channelCount); diff --git a/src/SFML/Graphics/RenderTarget.cpp b/src/SFML/Graphics/RenderTarget.cpp index 0d92a47d..20647262 100644 --- a/src/SFML/Graphics/RenderTarget.cpp +++ b/src/SFML/Graphics/RenderTarget.cpp @@ -733,8 +733,8 @@ void RenderTarget::setupDraw(bool useVertexCache, const RenderStates& states) void RenderTarget::drawPrimitives(PrimitiveType type, std::size_t firstVertex, std::size_t vertexCount) { // Find the OpenGL primitive type - static const GLenum modes[] = {GL_POINTS, GL_LINES, GL_LINE_STRIP, GL_TRIANGLES, - GL_TRIANGLE_STRIP, GL_TRIANGLE_FAN}; + static constexpr GLenum modes[] = {GL_POINTS, GL_LINES, GL_LINE_STRIP, GL_TRIANGLES, + GL_TRIANGLE_STRIP, GL_TRIANGLE_FAN}; GLenum mode = modes[type]; // Draw the primitives diff --git a/src/SFML/System/Angle.cpp b/src/SFML/System/Angle.cpp deleted file mode 100644 index 59feda73..00000000 --- a/src/SFML/System/Angle.cpp +++ /dev/null @@ -1,38 +0,0 @@ -//////////////////////////////////////////////////////////// -// -// SFML - Simple and Fast Multimedia Library -// Copyright (C) 2007-2022 Laurent Gomila (laurent@sfml-dev.org) -// -// This software is provided 'as-is', without any express or implied warranty. -// In no event will the authors be held liable for any damages arising from the use of this software. -// -// Permission is granted to anyone to use this software for any purpose, -// including commercial applications, and to alter it and redistribute it freely, -// subject to the following restrictions: -// -// 1. The origin of this software must not be misrepresented; -// you must not claim that you wrote the original software. -// If you use this software in a product, an acknowledgment -// in the product documentation would be appreciated but is not required. -// -// 2. Altered source versions must be plainly marked as such, -// and must not be misrepresented as being the original software. -// -// 3. This notice may not be removed or altered from any source distribution. -// -//////////////////////////////////////////////////////////// - -//////////////////////////////////////////////////////////// -// Headers -//////////////////////////////////////////////////////////// -#include - - -namespace sf -{ -//////////////////////////////////////////////////////////// -// Static member data -//////////////////////////////////////////////////////////// -const Angle Angle::Zero; - -} // namespace sf diff --git a/src/SFML/System/CMakeLists.txt b/src/SFML/System/CMakeLists.txt index 9f28fdfe..7eef3952 100644 --- a/src/SFML/System/CMakeLists.txt +++ b/src/SFML/System/CMakeLists.txt @@ -4,8 +4,8 @@ set(SRCROOT ${PROJECT_SOURCE_DIR}/src/SFML/System) # all source files set(SRC - ${SRCROOT}/Angle.cpp ${INCROOT}/Angle.hpp + ${INCROOT}/Angle.inl ${SRCROOT}/Clock.cpp ${INCROOT}/Clock.hpp ${SRCROOT}/Err.cpp @@ -18,7 +18,6 @@ set(SRC ${SRCROOT}/String.cpp ${INCROOT}/String.hpp ${INCROOT}/String.inl - ${SRCROOT}/Time.cpp ${INCROOT}/Time.hpp ${INCROOT}/Time.inl ${INCROOT}/Utf.hpp diff --git a/src/SFML/System/Err.cpp b/src/SFML/System/Err.cpp index dbff24bb..e6d8aec5 100644 --- a/src/SFML/System/Err.cpp +++ b/src/SFML/System/Err.cpp @@ -42,7 +42,7 @@ public: DefaultErrStreamBuf() { // Allocate the write buffer - static const int size = 64; + constexpr int size = 64; char* buffer = new char[size]; setp(buffer, buffer + size); } diff --git a/src/SFML/System/Time.cpp b/src/SFML/System/Time.cpp index 3cde7a47..e69de29b 100644 --- a/src/SFML/System/Time.cpp +++ b/src/SFML/System/Time.cpp @@ -1,38 +0,0 @@ -//////////////////////////////////////////////////////////// -// -// SFML - Simple and Fast Multimedia Library -// Copyright (C) 2007-2022 Laurent Gomila (laurent@sfml-dev.org) -// -// This software is provided 'as-is', without any express or implied warranty. -// In no event will the authors be held liable for any damages arising from the use of this software. -// -// Permission is granted to anyone to use this software for any purpose, -// including commercial applications, and to alter it and redistribute it freely, -// subject to the following restrictions: -// -// 1. The origin of this software must not be misrepresented; -// you must not claim that you wrote the original software. -// If you use this software in a product, an acknowledgment -// in the product documentation would be appreciated but is not required. -// -// 2. Altered source versions must be plainly marked as such, -// and must not be misrepresented as being the original software. -// -// 3. This notice may not be removed or altered from any source distribution. -// -//////////////////////////////////////////////////////////// - -//////////////////////////////////////////////////////////// -// Headers -//////////////////////////////////////////////////////////// -#include - - -namespace sf -{ -//////////////////////////////////////////////////////////// -// Static member data -//////////////////////////////////////////////////////////// -const Time Time::Zero; - -} // namespace sf diff --git a/src/SFML/Window/OSX/JoystickImpl.cpp b/src/SFML/Window/OSX/JoystickImpl.cpp index f410cb56..7cee2b3f 100644 --- a/src/SFML/Window/OSX/JoystickImpl.cpp +++ b/src/SFML/Window/OSX/JoystickImpl.cpp @@ -384,7 +384,7 @@ Joystick::Identification JoystickImpl::getIdentification() const JoystickState JoystickImpl::update() { AutoreleasePool pool; - static const JoystickState disconnectedState; // return this if joystick was disconnected + static constexpr JoystickState disconnectedState; // return this if joystick was disconnected JoystickState state; // otherwise return that state.connected = true; diff --git a/src/SFML/Window/Unix/WindowImplX11.cpp b/src/SFML/Window/Unix/WindowImplX11.cpp index 636df98d..100cd2b1 100644 --- a/src/SFML/Window/Unix/WindowImplX11.cpp +++ b/src/SFML/Window/Unix/WindowImplX11.cpp @@ -78,14 +78,14 @@ namespace sf::String wmAbsPosGood[] = { "Enlightenment", "FVWM", "i3" }; - static const unsigned long eventMask = FocusChangeMask | ButtonPressMask | - ButtonReleaseMask | ButtonMotionMask | - PointerMotionMask | KeyPressMask | - KeyReleaseMask | StructureNotifyMask | - EnterWindowMask | LeaveWindowMask | - VisibilityChangeMask | PropertyChangeMask; + constexpr unsigned long eventMask = FocusChangeMask | ButtonPressMask | + ButtonReleaseMask | ButtonMotionMask | + PointerMotionMask | KeyPressMask | + KeyReleaseMask | StructureNotifyMask | + EnterWindowMask | LeaveWindowMask | + VisibilityChangeMask | PropertyChangeMask; - static const unsigned int maxTrialsCount = 5; + constexpr unsigned int maxTrialsCount = 5; // Predicate we use to find key repeat events in processEvent struct KeyRepeatFinder @@ -656,23 +656,23 @@ m_lastInputTime (0) Atom WMHintsAtom = getAtom("_MOTIF_WM_HINTS", false); if (WMHintsAtom) { - static const unsigned long MWM_HINTS_FUNCTIONS = 1 << 0; - static const unsigned long MWM_HINTS_DECORATIONS = 1 << 1; + constexpr unsigned long MWM_HINTS_FUNCTIONS = 1 << 0; + constexpr unsigned long MWM_HINTS_DECORATIONS = 1 << 1; - //static const unsigned long MWM_DECOR_ALL = 1 << 0; - static const unsigned long MWM_DECOR_BORDER = 1 << 1; - static const unsigned long MWM_DECOR_RESIZEH = 1 << 2; - static const unsigned long MWM_DECOR_TITLE = 1 << 3; - static const unsigned long MWM_DECOR_MENU = 1 << 4; - static const unsigned long MWM_DECOR_MINIMIZE = 1 << 5; - static const unsigned long MWM_DECOR_MAXIMIZE = 1 << 6; + //constexpr unsigned long MWM_DECOR_ALL = 1 << 0; + constexpr unsigned long MWM_DECOR_BORDER = 1 << 1; + constexpr unsigned long MWM_DECOR_RESIZEH = 1 << 2; + constexpr unsigned long MWM_DECOR_TITLE = 1 << 3; + constexpr unsigned long MWM_DECOR_MENU = 1 << 4; + constexpr unsigned long MWM_DECOR_MINIMIZE = 1 << 5; + constexpr unsigned long MWM_DECOR_MAXIMIZE = 1 << 6; - //static const unsigned long MWM_FUNC_ALL = 1 << 0; - static const unsigned long MWM_FUNC_RESIZE = 1 << 1; - static const unsigned long MWM_FUNC_MOVE = 1 << 2; - static const unsigned long MWM_FUNC_MINIMIZE = 1 << 3; - static const unsigned long MWM_FUNC_MAXIMIZE = 1 << 4; - static const unsigned long MWM_FUNC_CLOSE = 1 << 5; + //constexpr unsigned long MWM_FUNC_ALL = 1 << 0; + constexpr unsigned long MWM_FUNC_RESIZE = 1 << 1; + constexpr unsigned long MWM_FUNC_MOVE = 1 << 2; + constexpr unsigned long MWM_FUNC_MINIMIZE = 1 << 3; + constexpr unsigned long MWM_FUNC_MAXIMIZE = 1 << 4; + constexpr unsigned long MWM_FUNC_CLOSE = 1 << 5; struct WMHints { @@ -1495,7 +1495,7 @@ void WindowImplX11::switchToFullscreen() if (netWmBypassCompositor) { - static const unsigned long bypassCompositor = 1; + constexpr unsigned long bypassCompositor = 1; XChangeProperty(m_display, m_window, diff --git a/src/SFML/Window/Window.cpp b/src/SFML/Window/Window.cpp index 5d8aa9cd..29489f21 100644 --- a/src/SFML/Window/Window.cpp +++ b/src/SFML/Window/Window.cpp @@ -164,7 +164,7 @@ void Window::close() //////////////////////////////////////////////////////////// const ContextSettings& Window::getSettings() const { - static const ContextSettings empty(0, 0, 0); + static constexpr ContextSettings empty(0, 0, 0); return m_context ? m_context->getSettings() : empty; } diff --git a/src/SFML/Window/iOS/EaglContext.mm b/src/SFML/Window/iOS/EaglContext.mm index 013e2521..62e17cfe 100644 --- a/src/SFML/Window/iOS/EaglContext.mm +++ b/src/SFML/Window/iOS/EaglContext.mm @@ -271,7 +271,7 @@ void EaglContext::display() // therefore we fake it with a manual framerate limit if (m_vsyncEnabled) { - static const Time frameDuration = seconds(1.f / 60.f); + constexpr Time frameDuration = seconds(1.f / 60.f); sleep(frameDuration - m_clock.getElapsedTime()); m_clock.restart(); } diff --git a/src/SFML/Window/iOS/SensorImpl.mm b/src/SFML/Window/iOS/SensorImpl.mm index fc993fb5..68d93882 100644 --- a/src/SFML/Window/iOS/SensorImpl.mm +++ b/src/SFML/Window/iOS/SensorImpl.mm @@ -94,7 +94,7 @@ bool SensorImpl::open(Sensor::Type sensor) m_enabled = false; // Set the refresh rate (use the maximum allowed) - static const NSTimeInterval updateInterval = 1. / 60.; + constexpr NSTimeInterval updateInterval = 1. / 60.; switch (sensor) { case Sensor::Accelerometer: