Improve static initialization of vectors

This commit is contained in:
Vittorio Romeo 2022-10-06 19:27:50 +02:00
parent 0e475f3f50
commit e948a5f41e
3 changed files with 7 additions and 23 deletions

View File

@ -184,14 +184,7 @@ VulkanFunctionPointer VulkanImplX11::getFunction(const char* name)
////////////////////////////////////////////////////////////
const std::vector<const char*>& VulkanImplX11::getGraphicsRequiredInstanceExtensions()
{
static std::vector<const char*> extensions;
if (extensions.empty())
{
extensions.push_back(VK_KHR_SURFACE_EXTENSION_NAME);
extensions.push_back(VK_KHR_XLIB_SURFACE_EXTENSION_NAME);
}
static const std::vector<const char*> extensions{VK_KHR_SURFACE_EXTENSION_NAME, VK_KHR_XLIB_SURFACE_EXTENSION_NAME};
return extensions;
}

View File

@ -59,14 +59,12 @@ VideoMode VideoMode::getDesktopMode()
////////////////////////////////////////////////////////////
const std::vector<VideoMode>& VideoMode::getFullscreenModes()
{
static std::vector<VideoMode> modes;
// Populate the array on first call
if (modes.empty())
static const auto modes = []
{
modes = priv::VideoModeImpl::getFullscreenModes();
std::sort(modes.begin(), modes.end(), std::greater<VideoMode>());
}
std::vector<VideoMode> result = priv::VideoModeImpl::getFullscreenModes();
std::sort(result.begin(), result.end(), std::greater<VideoMode>());
return result;
}();
return modes;
}

View File

@ -183,14 +183,7 @@ VulkanFunctionPointer VulkanImplWin32::getFunction(const char* name)
////////////////////////////////////////////////////////////
const std::vector<const char*>& VulkanImplWin32::getGraphicsRequiredInstanceExtensions()
{
static std::vector<const char*> extensions;
if (extensions.empty())
{
extensions.push_back(VK_KHR_SURFACE_EXTENSION_NAME);
extensions.push_back(VK_KHR_WIN32_SURFACE_EXTENSION_NAME);
}
static const std::vector<const char*> extensions{VK_KHR_SURFACE_EXTENSION_NAME, VK_KHR_WIN32_SURFACE_EXTENSION_NAME};
return extensions;
}