Skip to content

Commit

Permalink
make static varaibles const via lamdbas
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ZXShady authored and ChrisThrasher committed Sep 29, 2024
1 parent ef81fbd commit f297287
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 18 deletions.
2 changes: 1 addition & 1 deletion include/CSFML/Audio/SoundRecorder.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 9 additions & 10 deletions src/CSFML/Audio/SoundRecorder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string> stringDevices = sf::SoundRecorder::getAvailableDevices();
static std::vector<const char*> cstringDevices;

if (cstringDevices.empty() && !stringDevices.empty())
static const auto cstringDevices = []
{
static const std::vector<std::string> stringDevices = sf::SoundRecorder::getAvailableDevices();
std::vector<const char*> 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();
Expand All @@ -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;
}
Expand Down
11 changes: 5 additions & 6 deletions src/CSFML/Window/VideoMode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,13 @@ sfVideoMode sfVideoMode_getDesktopMode()
////////////////////////////////////////////////////////////
const sfVideoMode* sfVideoMode_getFullscreenModes(size_t* count)
{
static std::vector<sfVideoMode> modes;

// Populate the array on first call
if (modes.empty())
static const auto modes = []
{
std::vector<sfVideoMode> 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();
Expand Down
2 changes: 1 addition & 1 deletion src/CSFML/Window/Vulkan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ sfVulkanFunctionPointer sfVulkan_getFunction(const char* name)
////////////////////////////////////////////////////////////
const char* const* sfVulkan_getGraphicsRequiredInstanceExtensions(size_t* count)
{
static std::vector<const char*> extensions = sf::Vulkan::getGraphicsRequiredInstanceExtensions();
static const std::vector<const char*> extensions = sf::Vulkan::getGraphicsRequiredInstanceExtensions();

if (count)
*count = extensions.size();
Expand Down

0 comments on commit f297287

Please sign in to comment.