Turn more compile-time constants into 'constexpr' variables

This commit is contained in:
Vittorio Romeo 2022-02-11 00:02:59 +00:00
parent 9d51a6bd50
commit 5b500ad2c2
19 changed files with 58 additions and 119 deletions

View File

@ -155,7 +155,6 @@ constexpr Color& operator *=(Color& left, const Color& right)
// Note: the 'inline' keyword here is technically not required, but VS2019 fails // Note: the 'inline' keyword here is technically not required, but VS2019 fails
// to compile with a bogus "multiple definition" error if not explicitly used. // to compile with a bogus "multiple definition" error if not explicitly used.
inline constexpr Color Color::Black(0, 0, 0); inline constexpr Color Color::Black(0, 0, 0);
inline constexpr Color Color::White(255, 255, 255); inline constexpr Color Color::White(255, 255, 255);
inline constexpr Color Color::Red(255, 0, 0); inline constexpr Color Color::Red(255, 0, 0);

View File

@ -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 // Note: the 'inline' keyword here is technically not required, but VS2019 fails
// to compile with a bogus "multiple definition" error if not explicitly used. // to compile with a bogus "multiple definition" error if not explicitly used.
inline constexpr Transform Transform::Identity; inline constexpr Transform Transform::Identity;

View File

@ -139,7 +139,7 @@ public:
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
// Static member data // 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: private:

View File

@ -257,3 +257,12 @@ constexpr Angle operator "" _rad(unsigned long long angle)
} }
} // namespace Literals } // 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;

View File

@ -82,7 +82,7 @@ public:
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
// Static member data // Static member data
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
SFML_SYSTEM_API static const Time Zero; //!< Predefined "zero" time value static const Time Zero; //!< Predefined "zero" time value
private: private:

View File

@ -245,3 +245,12 @@ constexpr Time& operator %=(Time& left, Time right)
{ {
return left = left % 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;

View File

@ -48,7 +48,7 @@ template <typename In>
In Utf<8>::decode(In begin, In end, Uint32& output, Uint32 replacement) In Utf<8>::decode(In begin, In end, Uint32& output, Uint32 replacement)
{ {
// Some useful precomputed data // 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,
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, 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 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 0x00000000, 0x00003080, 0x000E2080, 0x03C82080, 0xFA082080, 0x82082080
}; };
@ -96,7 +96,7 @@ template <typename Out>
Out Utf<8>::encode(Uint32 input, Out output, Uint8 replacement) Out Utf<8>::encode(Uint32 input, Out output, Uint8 replacement)
{ {
// Some useful precomputed data // Some useful precomputed data
static const Uint8 firstBytes[7] = static constexpr Uint8 firstBytes[7] =
{ {
0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC
}; };

View File

@ -59,7 +59,7 @@ struct ContextSettings
/// \param sRgb sRGB capable framebuffer /// \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), depthBits (depth),
stencilBits (stencil), stencilBits (stencil),
antialiasingLevel(antialiasing), antialiasingLevel(antialiasing),

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 int 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

@ -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) void RenderTarget::drawPrimitives(PrimitiveType type, std::size_t firstVertex, 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, static constexpr GLenum modes[] = {GL_POINTS, GL_LINES, GL_LINE_STRIP, GL_TRIANGLES,
GL_TRIANGLE_STRIP, GL_TRIANGLE_FAN}; GL_TRIANGLE_STRIP, GL_TRIANGLE_FAN};
GLenum mode = modes[type]; GLenum mode = modes[type];
// Draw the primitives // Draw the primitives

View File

@ -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 <SFML/System/Angle.hpp>
namespace sf
{
////////////////////////////////////////////////////////////
// Static member data
////////////////////////////////////////////////////////////
const Angle Angle::Zero;
} // namespace sf

View File

@ -4,8 +4,8 @@ set(SRCROOT ${PROJECT_SOURCE_DIR}/src/SFML/System)
# all source files # all source files
set(SRC set(SRC
${SRCROOT}/Angle.cpp
${INCROOT}/Angle.hpp ${INCROOT}/Angle.hpp
${INCROOT}/Angle.inl
${SRCROOT}/Clock.cpp ${SRCROOT}/Clock.cpp
${INCROOT}/Clock.hpp ${INCROOT}/Clock.hpp
${SRCROOT}/Err.cpp ${SRCROOT}/Err.cpp
@ -18,7 +18,6 @@ set(SRC
${SRCROOT}/String.cpp ${SRCROOT}/String.cpp
${INCROOT}/String.hpp ${INCROOT}/String.hpp
${INCROOT}/String.inl ${INCROOT}/String.inl
${SRCROOT}/Time.cpp
${INCROOT}/Time.hpp ${INCROOT}/Time.hpp
${INCROOT}/Time.inl ${INCROOT}/Time.inl
${INCROOT}/Utf.hpp ${INCROOT}/Utf.hpp

View File

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

View File

@ -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 <SFML/System/Time.hpp>
namespace sf
{
////////////////////////////////////////////////////////////
// Static member data
////////////////////////////////////////////////////////////
const Time Time::Zero;
} // namespace sf

View File

@ -384,7 +384,7 @@ Joystick::Identification JoystickImpl::getIdentification() const
JoystickState JoystickImpl::update() JoystickState JoystickImpl::update()
{ {
AutoreleasePool pool; 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 JoystickState state; // otherwise return that
state.connected = true; state.connected = true;

View File

@ -78,14 +78,14 @@ namespace
sf::String wmAbsPosGood[] = { "Enlightenment", "FVWM", "i3" }; sf::String wmAbsPosGood[] = { "Enlightenment", "FVWM", "i3" };
static const unsigned long eventMask = FocusChangeMask | ButtonPressMask | constexpr unsigned long eventMask = FocusChangeMask | ButtonPressMask |
ButtonReleaseMask | ButtonMotionMask | ButtonReleaseMask | ButtonMotionMask |
PointerMotionMask | KeyPressMask | PointerMotionMask | KeyPressMask |
KeyReleaseMask | StructureNotifyMask | KeyReleaseMask | StructureNotifyMask |
EnterWindowMask | LeaveWindowMask | EnterWindowMask | LeaveWindowMask |
VisibilityChangeMask | PropertyChangeMask; VisibilityChangeMask | PropertyChangeMask;
static const unsigned int maxTrialsCount = 5; constexpr unsigned int maxTrialsCount = 5;
// Predicate we use to find key repeat events in processEvent // Predicate we use to find key repeat events in processEvent
struct KeyRepeatFinder struct KeyRepeatFinder
@ -656,23 +656,23 @@ m_lastInputTime (0)
Atom WMHintsAtom = getAtom("_MOTIF_WM_HINTS", false); Atom WMHintsAtom = getAtom("_MOTIF_WM_HINTS", false);
if (WMHintsAtom) if (WMHintsAtom)
{ {
static const unsigned long MWM_HINTS_FUNCTIONS = 1 << 0; constexpr unsigned long MWM_HINTS_FUNCTIONS = 1 << 0;
static const unsigned long MWM_HINTS_DECORATIONS = 1 << 1; constexpr unsigned long MWM_HINTS_DECORATIONS = 1 << 1;
//static const unsigned long MWM_DECOR_ALL = 1 << 0; //constexpr unsigned long MWM_DECOR_ALL = 1 << 0;
static const unsigned long MWM_DECOR_BORDER = 1 << 1; constexpr unsigned long MWM_DECOR_BORDER = 1 << 1;
static const unsigned long MWM_DECOR_RESIZEH = 1 << 2; constexpr unsigned long MWM_DECOR_RESIZEH = 1 << 2;
static const unsigned long MWM_DECOR_TITLE = 1 << 3; constexpr unsigned long MWM_DECOR_TITLE = 1 << 3;
static const unsigned long MWM_DECOR_MENU = 1 << 4; constexpr unsigned long MWM_DECOR_MENU = 1 << 4;
static const unsigned long MWM_DECOR_MINIMIZE = 1 << 5; constexpr unsigned long MWM_DECOR_MINIMIZE = 1 << 5;
static const unsigned long MWM_DECOR_MAXIMIZE = 1 << 6; constexpr unsigned long MWM_DECOR_MAXIMIZE = 1 << 6;
//static const unsigned long MWM_FUNC_ALL = 1 << 0; //constexpr unsigned long MWM_FUNC_ALL = 1 << 0;
static const unsigned long MWM_FUNC_RESIZE = 1 << 1; constexpr unsigned long MWM_FUNC_RESIZE = 1 << 1;
static const unsigned long MWM_FUNC_MOVE = 1 << 2; constexpr unsigned long MWM_FUNC_MOVE = 1 << 2;
static const unsigned long MWM_FUNC_MINIMIZE = 1 << 3; constexpr unsigned long MWM_FUNC_MINIMIZE = 1 << 3;
static const unsigned long MWM_FUNC_MAXIMIZE = 1 << 4; constexpr unsigned long MWM_FUNC_MAXIMIZE = 1 << 4;
static const unsigned long MWM_FUNC_CLOSE = 1 << 5; constexpr unsigned long MWM_FUNC_CLOSE = 1 << 5;
struct WMHints struct WMHints
{ {
@ -1495,7 +1495,7 @@ void WindowImplX11::switchToFullscreen()
if (netWmBypassCompositor) if (netWmBypassCompositor)
{ {
static const unsigned long bypassCompositor = 1; constexpr unsigned long bypassCompositor = 1;
XChangeProperty(m_display, XChangeProperty(m_display,
m_window, m_window,

View File

@ -164,7 +164,7 @@ void Window::close()
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
const ContextSettings& Window::getSettings() const 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; return m_context ? m_context->getSettings() : empty;
} }

View File

@ -271,7 +271,7 @@ void EaglContext::display()
// therefore we fake it with a manual framerate limit // therefore we fake it with a manual framerate limit
if (m_vsyncEnabled) 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()); sleep(frameDuration - m_clock.getElapsedTime());
m_clock.restart(); m_clock.restart();
} }

View File

@ -94,7 +94,7 @@ bool SensorImpl::open(Sensor::Type sensor)
m_enabled = false; m_enabled = false;
// Set the refresh rate (use the maximum allowed) // Set the refresh rate (use the maximum allowed)
static const NSTimeInterval updateInterval = 1. / 60.; constexpr NSTimeInterval updateInterval = 1. / 60.;
switch (sensor) switch (sensor)
{ {
case Sensor::Accelerometer: case Sensor::Accelerometer: