Use 'std::optional' instead of 'std::vector' for temporary 'AudioDevice'

This commit is contained in:
Vittorio Romeo 2021-12-09 19:04:50 +00:00
parent 7343b112fc
commit d02d3590fd

View File

@ -29,7 +29,7 @@
#include <SFML/Audio/ALCheck.hpp> #include <SFML/Audio/ALCheck.hpp>
#include <SFML/Audio/Listener.hpp> #include <SFML/Audio/Listener.hpp>
#include <SFML/System/Err.hpp> #include <SFML/System/Err.hpp>
#include <vector> #include <optional>
namespace namespace
@ -107,13 +107,9 @@ bool AudioDevice::isExtensionSupported(const std::string& extension)
// This device will not be used in this function and merely // This device will not be used in this function and merely
// makes sure there is a valid OpenAL device for extension // makes sure there is a valid OpenAL device for extension
// queries if none has been created yet. // queries if none has been created yet.
// std::optional<AudioDevice> device;
// Using an std::vector for this since auto_ptr is deprecated
// and we have no better STL facility for dynamically allocating
// a temporary instance with strong exception guarantee.
std::vector<AudioDevice> device;
if (!audioDevice) if (!audioDevice)
device.resize(1); device.emplace();
if ((extension.length() > 2) && (extension.substr(0, 3) == "ALC")) if ((extension.length() > 2) && (extension.substr(0, 3) == "ALC"))
return alcIsExtensionPresent(audioDevice, extension.c_str()) != AL_FALSE; return alcIsExtensionPresent(audioDevice, extension.c_str()) != AL_FALSE;
@ -129,13 +125,9 @@ int AudioDevice::getFormatFromChannelCount(unsigned int channelCount)
// This device will not be used in this function and merely // This device will not be used in this function and merely
// makes sure there is a valid OpenAL device for format // makes sure there is a valid OpenAL device for format
// queries if none has been created yet. // queries if none has been created yet.
// std::optional<AudioDevice> device;
// Using an std::vector for this since auto_ptr is deprecated
// and we have no better STL facility for dynamically allocating
// a temporary instance with strong exception guarantee.
std::vector<AudioDevice> device;
if (!audioDevice) if (!audioDevice)
device.resize(1); device.emplace();
// Find the good format according to the number of channels // Find the good format according to the number of channels
int format = 0; int format = 0;