Replaced function-local static variables with C++11 constructs.

This commit is contained in:
binary1248 2017-04-01 22:34:41 +02:00
parent 2ef0d36569
commit d6dcafbc90
12 changed files with 64 additions and 86 deletions

View file

@ -35,6 +35,7 @@
#include <SFML/System/FileInputStream.hpp> #include <SFML/System/FileInputStream.hpp>
#include <SFML/System/MemoryInputStream.hpp> #include <SFML/System/MemoryInputStream.hpp>
#include <SFML/System/Err.hpp> #include <SFML/System/Err.hpp>
#include <mutex>
namespace namespace
@ -42,8 +43,8 @@ namespace
// Register all the built-in readers and writers if not already done // Register all the built-in readers and writers if not already done
void ensureDefaultReadersWritersRegistered() void ensureDefaultReadersWritersRegistered()
{ {
static bool registered = false; static std::once_flag registered;
if (!registered) std::call_once(registered, []
{ {
sf::SoundFileFactory::registerReader<sf::priv::SoundFileReaderFlac>(); sf::SoundFileFactory::registerReader<sf::priv::SoundFileReaderFlac>();
sf::SoundFileFactory::registerWriter<sf::priv::SoundFileWriterFlac>(); sf::SoundFileFactory::registerWriter<sf::priv::SoundFileWriterFlac>();
@ -51,8 +52,7 @@ namespace
sf::SoundFileFactory::registerWriter<sf::priv::SoundFileWriterOgg>(); sf::SoundFileFactory::registerWriter<sf::priv::SoundFileWriterOgg>();
sf::SoundFileFactory::registerReader<sf::priv::SoundFileReaderWav>(); sf::SoundFileFactory::registerReader<sf::priv::SoundFileReaderWav>();
sf::SoundFileFactory::registerWriter<sf::priv::SoundFileWriterWav>(); sf::SoundFileFactory::registerWriter<sf::priv::SoundFileWriterWav>();
registered = true; });
}
} }
} }

View file

@ -131,7 +131,7 @@ bool SoundFileWriterOgg::open(const std::string& filename, unsigned int sampleRa
void SoundFileWriterOgg::write(const Int16* samples, Uint64 count) void SoundFileWriterOgg::write(const Int16* samples, Uint64 count)
{ {
// Vorbis has issues with buffers that are too large, so we ask for 64K // 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 // A frame contains a sample from each channel
int frameCount = static_cast<int>(count / m_channelCount); int frameCount = static_cast<int>(count / m_channelCount);

View file

@ -72,7 +72,7 @@ std::size_t CircleShape::getPointCount() const
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
Vector2f CircleShape::getPoint(std::size_t index) 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 angle = index * 2 * pi / m_pointCount - pi / 2;
float x = std::cos(angle) * m_radius; float x = std::cos(angle) * m_radius;

View file

@ -28,6 +28,7 @@
#include <SFML/Graphics/GLExtensions.hpp> #include <SFML/Graphics/GLExtensions.hpp>
#include <SFML/Window/Context.hpp> #include <SFML/Window/Context.hpp>
#include <SFML/System/Err.hpp> #include <SFML/System/Err.hpp>
#include <mutex>
#if !defined(GL_MAJOR_VERSION) #if !defined(GL_MAJOR_VERSION)
#define GL_MAJOR_VERSION 0x821B #define GL_MAJOR_VERSION 0x821B
@ -46,11 +47,10 @@ namespace priv
void ensureExtensionsInit() void ensureExtensionsInit()
{ {
#if !defined(SFML_OPENGL_ES) #if !defined(SFML_OPENGL_ES)
static bool initialized = false; static std::once_flag initialized;
if (!initialized)
{
initialized = true;
std::call_once(initialized, []
{
sfogl_LoadFunctions(); sfogl_LoadFunctions();
// Retrieve the context version number // 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() << "sfml-graphics requires support for OpenGL 1.1 or greater" << std::endl;
err() << "Ensure that hardware acceleration is enabled if available" << std::endl; err() << "Ensure that hardware acceleration is enabled if available" << std::endl;
} }
} });
#endif #endif
} }

View file

@ -34,6 +34,7 @@
#include <SFML/System/Err.hpp> #include <SFML/System/Err.hpp>
#include <cassert> #include <cassert>
#include <iostream> #include <iostream>
#include <mutex>
namespace namespace
{ {
@ -279,7 +280,7 @@ void RenderTarget::draw(const Vertex* vertices, std::size_t vertexCount,
} }
// Find the OpenGL primitive type // 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}; GL_TRIANGLE_STRIP, GL_TRIANGLE_FAN, GL_QUADS};
GLenum mode = modes[type]; GLenum mode = modes[type];
@ -461,16 +462,13 @@ void RenderTarget::applyBlendMode(const BlendMode& mode)
} }
else if ((mode.colorEquation != BlendMode::Add) || (mode.alphaEquation != BlendMode::Add)) else if ((mode.colorEquation != BlendMode::Add) || (mode.alphaEquation != BlendMode::Add))
{ {
static bool warned = false; static std::once_flag warned;
std::call_once(warned, []
if (!warned)
{ {
err() << "OpenGL extension EXT_blend_minmax and/or EXT_blend_subtract unavailable" << std::endl; err() << "OpenGL extension EXT_blend_minmax and/or EXT_blend_subtract unavailable" << std::endl;
err() << "Selecting a blend equation not possible" << std::endl; err() << "Selecting a blend equation not possible" << std::endl;
err() << "Ensure that hardware acceleration is enabled if available" << std::endl; err() << "Ensure that hardware acceleration is enabled if available" << std::endl;
});
warned = true;
}
} }
m_cache.lastBlendMode = mode; m_cache.lastBlendMode = mode;

View file

@ -534,7 +534,7 @@ void Shader::setUniform(const std::string& name, const Texture& texture)
if (it == m_textures.end()) if (it == m_textures.end())
{ {
// New entry, make sure there are enough texture units // 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 // Retrieve the maximum number of texture units available
GLint maxUnits = 0; GLint maxUnits = 0;
@ -759,7 +759,7 @@ void Shader::bind(const Shader* shader)
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
bool Shader::isAvailable() bool Shader::isAvailable()
{ {
const static auto available = [] static const auto available = []
{ {
TransientContextLock contextLock; TransientContextLock contextLock;
@ -780,7 +780,7 @@ bool Shader::isAvailable()
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
bool Shader::isGeometryAvailable() bool Shader::isGeometryAvailable()
{ {
const static auto available = [] static const auto available = []
{ {
TransientContextLock contextLock; TransientContextLock contextLock;

View file

@ -36,6 +36,7 @@
#include <SFML/System/Err.hpp> #include <SFML/System/Err.hpp>
#include <cassert> #include <cassert>
#include <cstring> #include <cstring>
#include <mutex>
namespace namespace
@ -165,29 +166,25 @@ bool Texture::create(unsigned int width, unsigned int height)
// Make sure that the current texture binding will be preserved // Make sure that the current texture binding will be preserved
priv::TextureSaver save; 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) if (!m_isRepeated && !textureEdgeClamp)
{ {
static bool warned = false; std::once_flag warned;
std::call_once(warned, []
if (!warned)
{ {
err() << "OpenGL extension SGIS_texture_edge_clamp unavailable" << std::endl; err() << "OpenGL extension SGIS_texture_edge_clamp unavailable" << std::endl;
err() << "Artifacts may occur along texture edges" << std::endl; err() << "Artifacts may occur along texture edges" << std::endl;
err() << "Ensure that hardware acceleration is enabled if available" << 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) if (m_sRgb && !textureSrgb)
{ {
static bool warned = false; std::once_flag warned;
std::call_once(warned, []
if (!warned)
{ {
#ifndef SFML_OPENGL_ES #ifndef SFML_OPENGL_ES
err() << "OpenGL extension EXT_texture_sRGB unavailable" << std::endl; 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; err() << "OpenGL ES extension EXT_sRGB unavailable" << std::endl;
#endif #endif
err() << "Automatic sRGB to linear conversion disabled" << std::endl; err() << "Automatic sRGB to linear conversion disabled" << std::endl;
});
warned = true;
}
m_sRgb = false; m_sRgb = false;
} }
@ -640,20 +635,17 @@ void Texture::setRepeated(bool repeated)
// Make sure that the current texture binding will be preserved // Make sure that the current texture binding will be preserved
priv::TextureSaver save; 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) if (!m_isRepeated && !textureEdgeClamp)
{ {
static bool warned = false; std::once_flag warned;
std::call_once(warned, []
if (!warned)
{ {
err() << "OpenGL extension SGIS_texture_edge_clamp unavailable" << std::endl; err() << "OpenGL extension SGIS_texture_edge_clamp unavailable" << std::endl;
err() << "Artifacts may occur along texture edges" << std::endl; err() << "Artifacts may occur along texture edges" << std::endl;
err() << "Ensure that hardware acceleration is enabled if available" << std::endl; err() << "Ensure that hardware acceleration is enabled if available" << std::endl;
});
warned = true;
}
} }
glCheck(glBindTexture(GL_TEXTURE_2D, m_texture)); glCheck(glBindTexture(GL_TEXTURE_2D, m_texture));
@ -775,7 +767,7 @@ void Texture::bind(const Texture* texture, CoordinateType coordinateType)
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
unsigned int Texture::getMaximumSize() unsigned int Texture::getMaximumSize()
{ {
const static auto size = [] static const auto size = []
{ {
TransientContextLock lock; TransientContextLock lock;

View file

@ -41,7 +41,7 @@ public:
DefaultErrStreamBuf() DefaultErrStreamBuf()
{ {
// Allocate the write buffer // Allocate the write buffer
static const int size = 64; constexpr auto size = 64;
char* buffer = new char[size]; char* buffer = new char[size];
setp(buffer, buffer + size); setp(buffer, buffer + size);
} }

View file

@ -30,25 +30,6 @@
#include <SFML/System/Lock.hpp> #include <SFML/System/Lock.hpp>
#include <windows.h> #include <windows.h>
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<DWORD>(LOBYTE(LOWORD(GetVersion()))) < 6;
}
}
namespace sf namespace sf
{ {
namespace priv namespace priv
@ -58,15 +39,26 @@ Time ClockImpl::getCurrentTime()
{ {
// Get the frequency of the performance counter // Get the frequency of the performance counter
// (it is constant across the program lifetime) // (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 // 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<DWORD>(LOBYTE(LOWORD(GetVersion()))) < 6;
}();
LARGE_INTEGER time; LARGE_INTEGER time;
if (oldWindows) if (oldWindows)
{ {
static sf::Mutex oldWindowsMutex;
// Acquire a lock (CRITICAL_SECTION) to prevent travelling back in time // Acquire a lock (CRITICAL_SECTION) to prevent travelling back in time
Lock lock(oldWindowsMutex); Lock lock(oldWindowsMutex);

View file

@ -64,14 +64,13 @@ VideoMode VideoMode::getDesktopMode()
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
const std::vector<VideoMode>& VideoMode::getFullscreenModes() const std::vector<VideoMode>& VideoMode::getFullscreenModes()
{ {
static std::vector<VideoMode> modes;
// Populate the array on first call // 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<VideoMode>()); std::sort(modes.begin(), modes.end(), std::greater<VideoMode>());
} return modes;
}();
return modes; return modes;
} }

View file

@ -34,6 +34,7 @@
#include <SFML/System/Err.hpp> #include <SFML/System/Err.hpp>
#include <sstream> #include <sstream>
#include <vector> #include <vector>
#include <mutex>
namespace namespace
@ -51,15 +52,13 @@ namespace priv
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
void ensureExtensionsInit(HDC deviceContext) void ensureExtensionsInit(HDC deviceContext)
{ {
static bool initialized = false; std::once_flag initialized;
if (!initialized) std::call_once(initialized, [deviceContext]
{ {
initialized = true;
// We don't check the return value since the extension // We don't check the return value since the extension
// flags are cleared even if loading fails // flags are cleared even if loading fails
sfwgl_LoadFunctions(deviceContext); sfwgl_LoadFunctions(deviceContext);
} });
} }
@ -200,10 +199,11 @@ GlFunctionPointer WglContext::getFunction(const char* name)
return address; return address;
} }
static HMODULE module = NULL; static const auto module = []
{
if (!module) return GetModuleHandleA("OpenGL32.dll");
module = GetModuleHandleA("OpenGL32.dll"); }();
if (module) if (module)
return reinterpret_cast<GlFunctionPointer>(GetProcAddress(module, reinterpret_cast<LPCSTR>(name))); return reinterpret_cast<GlFunctionPointer>(GetProcAddress(module, reinterpret_cast<LPCSTR>(name)));
@ -251,15 +251,12 @@ void WglContext::setVerticalSyncEnabled(bool enabled)
} }
else else
{ {
static bool warned = false; std::once_flag warned;
std::call_once(warned, []
if (!warned)
{ {
// wglSwapIntervalEXT not supported // wglSwapIntervalEXT not supported
err() << "Setting vertical sync not supported" << std::endl; err() << "Setting vertical sync not supported" << std::endl;
});
warned = true;
}
} }
} }

View file

@ -984,7 +984,7 @@ Keyboard::Key WindowImplWin32::virtualKeyCodeToSF(WPARAM key, LPARAM flags)
// Check the scancode to distinguish between left and right shift // Check the scancode to distinguish between left and right shift
case VK_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<UINT>((flags & (0xFF << 16)) >> 16); UINT scancode = static_cast<UINT>((flags & (0xFF << 16)) >> 16);
return scancode == lShift ? Keyboard::LShift : Keyboard::RShift; return scancode == lShift ? Keyboard::LShift : Keyboard::RShift;
} }