From d6dcafbc905850f8b9d9342d67497217d75c68fb Mon Sep 17 00:00:00 2001 From: binary1248 Date: Sat, 1 Apr 2017 22:34:41 +0200 Subject: [PATCH] Replaced function-local static variables with C++11 constructs. --- src/SFML/Audio/SoundFileFactory.cpp | 8 ++--- src/SFML/Audio/SoundFileWriterOgg.cpp | 2 +- src/SFML/Graphics/CircleShape.cpp | 2 +- src/SFML/Graphics/GLExtensions.cpp | 10 +++---- src/SFML/Graphics/RenderTarget.cpp | 12 ++++---- src/SFML/Graphics/Shader.cpp | 6 ++-- src/SFML/Graphics/Texture.cpp | 36 +++++++++-------------- src/SFML/System/Err.cpp | 2 +- src/SFML/System/Win32/ClockImpl.cpp | 34 ++++++++------------- src/SFML/Window/VideoMode.cpp | 9 +++--- src/SFML/Window/Win32/WglContext.cpp | 27 ++++++++--------- src/SFML/Window/Win32/WindowImplWin32.cpp | 2 +- 12 files changed, 64 insertions(+), 86 deletions(-) diff --git a/src/SFML/Audio/SoundFileFactory.cpp b/src/SFML/Audio/SoundFileFactory.cpp index 10b1c1e1..181e7775 100644 --- a/src/SFML/Audio/SoundFileFactory.cpp +++ b/src/SFML/Audio/SoundFileFactory.cpp @@ -35,6 +35,7 @@ #include #include #include +#include namespace @@ -42,8 +43,8 @@ namespace // Register all the built-in readers and writers if not already done void ensureDefaultReadersWritersRegistered() { - static bool registered = false; - if (!registered) + static std::once_flag registered; + std::call_once(registered, [] { sf::SoundFileFactory::registerReader(); sf::SoundFileFactory::registerWriter(); @@ -51,8 +52,7 @@ namespace sf::SoundFileFactory::registerWriter(); sf::SoundFileFactory::registerReader(); sf::SoundFileFactory::registerWriter(); - registered = true; - } + }); } } diff --git a/src/SFML/Audio/SoundFileWriterOgg.cpp b/src/SFML/Audio/SoundFileWriterOgg.cpp index c661b746..a5cbf15b 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 auto bufferSize = 65536; // A frame contains a sample from each channel int frameCount = static_cast(count / m_channelCount); diff --git a/src/SFML/Graphics/CircleShape.cpp b/src/SFML/Graphics/CircleShape.cpp index 8e555105..2e8b1ca3 100644 --- a/src/SFML/Graphics/CircleShape.cpp +++ b/src/SFML/Graphics/CircleShape.cpp @@ -72,7 +72,7 @@ std::size_t CircleShape::getPointCount() const //////////////////////////////////////////////////////////// Vector2f CircleShape::getPoint(std::size_t index) const { - static const float pi = 3.141592654f; + constexpr auto pi = 3.141592654f; float angle = index * 2 * pi / m_pointCount - pi / 2; float x = std::cos(angle) * m_radius; diff --git a/src/SFML/Graphics/GLExtensions.cpp b/src/SFML/Graphics/GLExtensions.cpp index 57f5bcc7..c8f41152 100644 --- a/src/SFML/Graphics/GLExtensions.cpp +++ b/src/SFML/Graphics/GLExtensions.cpp @@ -28,6 +28,7 @@ #include #include #include +#include #if !defined(GL_MAJOR_VERSION) #define GL_MAJOR_VERSION 0x821B @@ -46,11 +47,10 @@ namespace priv void ensureExtensionsInit() { #if !defined(SFML_OPENGL_ES) - static bool initialized = false; - if (!initialized) - { - initialized = true; + static std::once_flag initialized; + std::call_once(initialized, [] + { sfogl_LoadFunctions(); // Retrieve the context version number @@ -84,7 +84,7 @@ void ensureExtensionsInit() err() << "sfml-graphics requires support for OpenGL 1.1 or greater" << std::endl; err() << "Ensure that hardware acceleration is enabled if available" << std::endl; } - } + }); #endif } diff --git a/src/SFML/Graphics/RenderTarget.cpp b/src/SFML/Graphics/RenderTarget.cpp index 08009a58..146f48a2 100644 --- a/src/SFML/Graphics/RenderTarget.cpp +++ b/src/SFML/Graphics/RenderTarget.cpp @@ -34,6 +34,7 @@ #include #include #include +#include namespace { @@ -279,7 +280,7 @@ void RenderTarget::draw(const Vertex* vertices, std::size_t vertexCount, } // Find the OpenGL primitive type - static const GLenum modes[] = {GL_POINTS, GL_LINES, GL_LINE_STRIP, GL_TRIANGLES, + constexpr GLenum modes[] = {GL_POINTS, GL_LINES, GL_LINE_STRIP, GL_TRIANGLES, GL_TRIANGLE_STRIP, GL_TRIANGLE_FAN, GL_QUADS}; GLenum mode = modes[type]; @@ -461,16 +462,13 @@ void RenderTarget::applyBlendMode(const BlendMode& mode) } else if ((mode.colorEquation != BlendMode::Add) || (mode.alphaEquation != BlendMode::Add)) { - static bool warned = false; - - if (!warned) + static std::once_flag warned; + std::call_once(warned, [] { err() << "OpenGL extension EXT_blend_minmax and/or EXT_blend_subtract unavailable" << std::endl; err() << "Selecting a blend equation not possible" << std::endl; err() << "Ensure that hardware acceleration is enabled if available" << std::endl; - - warned = true; - } + }); } m_cache.lastBlendMode = mode; diff --git a/src/SFML/Graphics/Shader.cpp b/src/SFML/Graphics/Shader.cpp index 78770a6a..5d36e14c 100644 --- a/src/SFML/Graphics/Shader.cpp +++ b/src/SFML/Graphics/Shader.cpp @@ -534,7 +534,7 @@ void Shader::setUniform(const std::string& name, const Texture& texture) if (it == m_textures.end()) { // New entry, make sure there are enough texture units - const static auto maxUnits = [] + static const auto maxUnits = [] { // Retrieve the maximum number of texture units available GLint maxUnits = 0; @@ -759,7 +759,7 @@ void Shader::bind(const Shader* shader) //////////////////////////////////////////////////////////// bool Shader::isAvailable() { - const static auto available = [] + static const auto available = [] { TransientContextLock contextLock; @@ -780,7 +780,7 @@ bool Shader::isAvailable() //////////////////////////////////////////////////////////// bool Shader::isGeometryAvailable() { - const static auto available = [] + static const auto available = [] { TransientContextLock contextLock; diff --git a/src/SFML/Graphics/Texture.cpp b/src/SFML/Graphics/Texture.cpp index 66f11370..8825d75a 100644 --- a/src/SFML/Graphics/Texture.cpp +++ b/src/SFML/Graphics/Texture.cpp @@ -36,6 +36,7 @@ #include #include #include +#include namespace @@ -165,29 +166,25 @@ bool Texture::create(unsigned int width, unsigned int height) // Make sure that the current texture binding will be preserved priv::TextureSaver save; - static bool textureEdgeClamp = GLEXT_texture_edge_clamp || GLEXT_EXT_texture_edge_clamp; + static const auto textureEdgeClamp = GLEXT_texture_edge_clamp || GLEXT_EXT_texture_edge_clamp; if (!m_isRepeated && !textureEdgeClamp) { - static bool warned = false; - - if (!warned) + std::once_flag warned; + std::call_once(warned, [] { err() << "OpenGL extension SGIS_texture_edge_clamp unavailable" << std::endl; err() << "Artifacts may occur along texture edges" << std::endl; err() << "Ensure that hardware acceleration is enabled if available" << std::endl; - - warned = true; - } + }); } - static bool textureSrgb = GLEXT_texture_sRGB; + static const auto textureSrgb = GLEXT_texture_sRGB; if (m_sRgb && !textureSrgb) { - static bool warned = false; - - if (!warned) + std::once_flag warned; + std::call_once(warned, [] { #ifndef SFML_OPENGL_ES err() << "OpenGL extension EXT_texture_sRGB unavailable" << std::endl; @@ -195,9 +192,7 @@ bool Texture::create(unsigned int width, unsigned int height) err() << "OpenGL ES extension EXT_sRGB unavailable" << std::endl; #endif err() << "Automatic sRGB to linear conversion disabled" << std::endl; - - warned = true; - } + }); m_sRgb = false; } @@ -640,20 +635,17 @@ void Texture::setRepeated(bool repeated) // Make sure that the current texture binding will be preserved priv::TextureSaver save; - static bool textureEdgeClamp = GLEXT_texture_edge_clamp || GLEXT_EXT_texture_edge_clamp; + static const auto textureEdgeClamp = GLEXT_texture_edge_clamp || GLEXT_EXT_texture_edge_clamp; if (!m_isRepeated && !textureEdgeClamp) { - static bool warned = false; - - if (!warned) + std::once_flag warned; + std::call_once(warned, [] { err() << "OpenGL extension SGIS_texture_edge_clamp unavailable" << std::endl; err() << "Artifacts may occur along texture edges" << std::endl; err() << "Ensure that hardware acceleration is enabled if available" << std::endl; - - warned = true; - } + }); } glCheck(glBindTexture(GL_TEXTURE_2D, m_texture)); @@ -775,7 +767,7 @@ void Texture::bind(const Texture* texture, CoordinateType coordinateType) //////////////////////////////////////////////////////////// unsigned int Texture::getMaximumSize() { - const static auto size = [] + static const auto size = [] { TransientContextLock lock; diff --git a/src/SFML/System/Err.cpp b/src/SFML/System/Err.cpp index 5d2084ec..51be9aa3 100644 --- a/src/SFML/System/Err.cpp +++ b/src/SFML/System/Err.cpp @@ -41,7 +41,7 @@ public: DefaultErrStreamBuf() { // Allocate the write buffer - static const int size = 64; + constexpr auto size = 64; char* buffer = new char[size]; setp(buffer, buffer + size); } diff --git a/src/SFML/System/Win32/ClockImpl.cpp b/src/SFML/System/Win32/ClockImpl.cpp index 8098a7d4..c81288d2 100644 --- a/src/SFML/System/Win32/ClockImpl.cpp +++ b/src/SFML/System/Win32/ClockImpl.cpp @@ -30,25 +30,6 @@ #include #include - -namespace -{ - sf::Mutex oldWindowsMutex; - - LARGE_INTEGER getFrequency() - { - LARGE_INTEGER frequency; - QueryPerformanceFrequency(&frequency); - return frequency; - } - - bool isWindowsXpOrOlder() - { - // Windows XP was the last 5.x version of Windows - return static_cast(LOBYTE(LOWORD(GetVersion()))) < 6; - } -} - namespace sf { namespace priv @@ -58,15 +39,26 @@ Time ClockImpl::getCurrentTime() { // Get the frequency of the performance counter // (it is constant across the program lifetime) - static LARGE_INTEGER frequency = getFrequency(); + static const auto frequency = [] + { + LARGE_INTEGER frequency; + QueryPerformanceFrequency(&frequency); + return frequency; + }(); // Detect if we are on Windows XP or older - static bool oldWindows = isWindowsXpOrOlder(); + static const auto oldWindows = [] + { + // Windows XP was the last 5.x version of Windows + return static_cast(LOBYTE(LOWORD(GetVersion()))) < 6; + }(); LARGE_INTEGER time; if (oldWindows) { + static sf::Mutex oldWindowsMutex; + // Acquire a lock (CRITICAL_SECTION) to prevent travelling back in time Lock lock(oldWindowsMutex); diff --git a/src/SFML/Window/VideoMode.cpp b/src/SFML/Window/VideoMode.cpp index 01103892..968919c6 100644 --- a/src/SFML/Window/VideoMode.cpp +++ b/src/SFML/Window/VideoMode.cpp @@ -64,14 +64,13 @@ VideoMode VideoMode::getDesktopMode() //////////////////////////////////////////////////////////// const std::vector& VideoMode::getFullscreenModes() { - static std::vector modes; - // Populate the array on first call - if (modes.empty()) + static const auto modes = [] { - modes = priv::VideoModeImpl::getFullscreenModes(); + auto modes = priv::VideoModeImpl::getFullscreenModes(); std::sort(modes.begin(), modes.end(), std::greater()); - } + return modes; + }(); return modes; } diff --git a/src/SFML/Window/Win32/WglContext.cpp b/src/SFML/Window/Win32/WglContext.cpp index 3e07c48a..c2a813f7 100644 --- a/src/SFML/Window/Win32/WglContext.cpp +++ b/src/SFML/Window/Win32/WglContext.cpp @@ -34,6 +34,7 @@ #include #include #include +#include namespace @@ -51,15 +52,13 @@ namespace priv //////////////////////////////////////////////////////////// void ensureExtensionsInit(HDC deviceContext) { - static bool initialized = false; - if (!initialized) + std::once_flag initialized; + std::call_once(initialized, [deviceContext] { - initialized = true; - // We don't check the return value since the extension // flags are cleared even if loading fails sfwgl_LoadFunctions(deviceContext); - } + }); } @@ -200,10 +199,11 @@ GlFunctionPointer WglContext::getFunction(const char* name) return address; } - static HMODULE module = NULL; - - if (!module) - module = GetModuleHandleA("OpenGL32.dll"); + static const auto module = [] + { + return GetModuleHandleA("OpenGL32.dll"); + }(); + if (module) return reinterpret_cast(GetProcAddress(module, reinterpret_cast(name))); @@ -251,15 +251,12 @@ void WglContext::setVerticalSyncEnabled(bool enabled) } else { - static bool warned = false; - - if (!warned) + std::once_flag warned; + std::call_once(warned, [] { // wglSwapIntervalEXT not supported err() << "Setting vertical sync not supported" << std::endl; - - warned = true; - } + }); } } diff --git a/src/SFML/Window/Win32/WindowImplWin32.cpp b/src/SFML/Window/Win32/WindowImplWin32.cpp index f8669b21..f21a85b3 100644 --- a/src/SFML/Window/Win32/WindowImplWin32.cpp +++ b/src/SFML/Window/Win32/WindowImplWin32.cpp @@ -984,7 +984,7 @@ Keyboard::Key WindowImplWin32::virtualKeyCodeToSF(WPARAM key, LPARAM flags) // Check the scancode to distinguish between left and right shift case VK_SHIFT: { - static UINT lShift = MapVirtualKeyW(VK_LSHIFT, MAPVK_VK_TO_VSC); + static const auto lShift = MapVirtualKeyW(VK_LSHIFT, MAPVK_VK_TO_VSC); UINT scancode = static_cast((flags & (0xFF << 16)) >> 16); return scancode == lShift ? Keyboard::LShift : Keyboard::RShift; }