Skip to content

Commit f297287

Browse files
ZXShadyChrisThrasher
authored andcommitted
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.
1 parent ef81fbd commit f297287

File tree

4 files changed

+16
-18
lines changed

4 files changed

+16
-18
lines changed

include/CSFML/Audio/SoundRecorder.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ CSFML_AUDIO_API bool sfSoundRecorder_isAvailable(void);
129129
/// \return An array of strings containing the names
130130
///
131131
////////////////////////////////////////////////////////////
132-
CSFML_AUDIO_API const char** sfSoundRecorder_getAvailableDevices(size_t* count);
132+
CSFML_AUDIO_API const char* const* sfSoundRecorder_getAvailableDevices(size_t* count);
133133

134134
////////////////////////////////////////////////////////////
135135
/// \brief Get the name of the default audio capture device

src/CSFML/Audio/SoundRecorder.cpp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -81,18 +81,17 @@ bool sfSoundRecorder_isAvailable()
8181

8282

8383
////////////////////////////////////////////////////////////
84-
const char** sfSoundRecorder_getAvailableDevices(size_t* count)
84+
const char* const* sfSoundRecorder_getAvailableDevices(size_t* count)
8585
{
86-
static std::vector<std::string> stringDevices = sf::SoundRecorder::getAvailableDevices();
87-
static std::vector<const char*> cstringDevices;
88-
89-
if (cstringDevices.empty() && !stringDevices.empty())
86+
static const auto cstringDevices = []
9087
{
88+
static const std::vector<std::string> stringDevices = sf::SoundRecorder::getAvailableDevices();
89+
std::vector<const char*> devices;
90+
devices.reserve(stringDevices.size());
9191
for (const auto& stringDevice : stringDevices)
92-
{
93-
cstringDevices.push_back(stringDevice.c_str());
94-
}
95-
}
92+
devices.push_back(stringDevice.c_str());
93+
return devices;
94+
}();
9695

9796
if (count)
9897
*count = cstringDevices.size();
@@ -104,7 +103,7 @@ const char** sfSoundRecorder_getAvailableDevices(size_t* count)
104103
////////////////////////////////////////////////////////////
105104
const char* sfSoundRecorder_getDefaultDevice()
106105
{
107-
static std::string defaultDevice = sf::SoundRecorder::getDefaultDevice();
106+
static const std::string defaultDevice = sf::SoundRecorder::getDefaultDevice();
108107

109108
return !defaultDevice.empty() ? defaultDevice.c_str() : nullptr;
110109
}

src/CSFML/Window/VideoMode.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,13 @@ sfVideoMode sfVideoMode_getDesktopMode()
4141
////////////////////////////////////////////////////////////
4242
const sfVideoMode* sfVideoMode_getFullscreenModes(size_t* count)
4343
{
44-
static std::vector<sfVideoMode> modes;
45-
46-
// Populate the array on first call
47-
if (modes.empty())
44+
static const auto modes = []
4845
{
46+
std::vector<sfVideoMode> videomodes;
4947
for (const auto& mode : sf::VideoMode::getFullscreenModes())
50-
modes.push_back(convertVideoMode(mode));
51-
}
48+
videomodes.push_back(convertVideoMode(mode));
49+
return videomodes;
50+
}();
5251

5352
if (count)
5453
*count = modes.size();

src/CSFML/Window/Vulkan.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ sfVulkanFunctionPointer sfVulkan_getFunction(const char* name)
5252
////////////////////////////////////////////////////////////
5353
const char* const* sfVulkan_getGraphicsRequiredInstanceExtensions(size_t* count)
5454
{
55-
static std::vector<const char*> extensions = sf::Vulkan::getGraphicsRequiredInstanceExtensions();
55+
static const std::vector<const char*> extensions = sf::Vulkan::getGraphicsRequiredInstanceExtensions();
5656

5757
if (count)
5858
*count = extensions.size();

0 commit comments

Comments
 (0)