From 8169f6c264d2e2f79b716443b5f02711e66dc521 Mon Sep 17 00:00:00 2001 From: ZXShady <153229951+ZXShady@users.noreply.github.com> Date: Sat, 28 Sep 2024 21:33:09 +0100 Subject: [PATCH] make static varaibles const via lamdbas this pr also makes `sfSoundRecorder_getAvailableDevices` return a const pointer to a const char pointer so you can no longer modify where the pointers of the strings are pointing into becuase it doesn't make sense. --- include/CSFML/Audio/SoundRecorder.h | 2 +- src/CSFML/Audio/SoundRecorder.cpp | 19 +++++++++---------- src/CSFML/Window/VideoMode.cpp | 11 +++++------ src/CSFML/Window/Vulkan.cpp | 2 +- 4 files changed, 16 insertions(+), 18 deletions(-) diff --git a/include/CSFML/Audio/SoundRecorder.h b/include/CSFML/Audio/SoundRecorder.h index 9c704621..15257725 100644 --- a/include/CSFML/Audio/SoundRecorder.h +++ b/include/CSFML/Audio/SoundRecorder.h @@ -129,7 +129,7 @@ CSFML_AUDIO_API bool sfSoundRecorder_isAvailable(void); /// \return An array of strings containing the names /// //////////////////////////////////////////////////////////// -CSFML_AUDIO_API const char** sfSoundRecorder_getAvailableDevices(size_t* count); +CSFML_AUDIO_API const char* const* sfSoundRecorder_getAvailableDevices(size_t* count); //////////////////////////////////////////////////////////// /// \brief Get the name of the default audio capture device diff --git a/src/CSFML/Audio/SoundRecorder.cpp b/src/CSFML/Audio/SoundRecorder.cpp index 4c77ebe2..ac5ea48d 100644 --- a/src/CSFML/Audio/SoundRecorder.cpp +++ b/src/CSFML/Audio/SoundRecorder.cpp @@ -81,18 +81,17 @@ bool sfSoundRecorder_isAvailable() //////////////////////////////////////////////////////////// -const char** sfSoundRecorder_getAvailableDevices(size_t* count) +const char* const* sfSoundRecorder_getAvailableDevices(size_t* count) { - static std::vector stringDevices = sf::SoundRecorder::getAvailableDevices(); - static std::vector cstringDevices; - - if (cstringDevices.empty() && !stringDevices.empty()) + static const auto cstringDevices = [] { + static const std::vector stringDevices = sf::SoundRecorder::getAvailableDevices(); + std::vector devices; + devices.reserve(stringDevices.size()); for (const auto& stringDevice : stringDevices) - { - cstringDevices.push_back(stringDevice.c_str()); - } - } + devices.push_back(stringDevice.c_str()); + return devices; + }(); if (count) *count = cstringDevices.size(); @@ -104,7 +103,7 @@ const char** sfSoundRecorder_getAvailableDevices(size_t* count) //////////////////////////////////////////////////////////// const char* sfSoundRecorder_getDefaultDevice() { - static std::string defaultDevice = sf::SoundRecorder::getDefaultDevice(); + static const std::string defaultDevice = sf::SoundRecorder::getDefaultDevice(); return !defaultDevice.empty() ? defaultDevice.c_str() : nullptr; } diff --git a/src/CSFML/Window/VideoMode.cpp b/src/CSFML/Window/VideoMode.cpp index 82fe4e5f..109756f2 100644 --- a/src/CSFML/Window/VideoMode.cpp +++ b/src/CSFML/Window/VideoMode.cpp @@ -41,14 +41,13 @@ sfVideoMode sfVideoMode_getDesktopMode() //////////////////////////////////////////////////////////// const sfVideoMode* sfVideoMode_getFullscreenModes(size_t* count) { - static std::vector modes; - - // Populate the array on first call - if (modes.empty()) + static const auto modes = [] { + std::vector videomodes; for (const auto& mode : sf::VideoMode::getFullscreenModes()) - modes.push_back(convertVideoMode(mode)); - } + videomodes.push_back(convertVideoMode(mode)); + return videomodes; + }(); if (count) *count = modes.size(); diff --git a/src/CSFML/Window/Vulkan.cpp b/src/CSFML/Window/Vulkan.cpp index 781135b3..b087a868 100644 --- a/src/CSFML/Window/Vulkan.cpp +++ b/src/CSFML/Window/Vulkan.cpp @@ -52,7 +52,7 @@ sfVulkanFunctionPointer sfVulkan_getFunction(const char* name) //////////////////////////////////////////////////////////// const char* const* sfVulkan_getGraphicsRequiredInstanceExtensions(size_t* count) { - static std::vector extensions = sf::Vulkan::getGraphicsRequiredInstanceExtensions(); + static const std::vector extensions = sf::Vulkan::getGraphicsRequiredInstanceExtensions(); if (count) *count = extensions.size();