Add move semantics to 'Sound'

This commit is contained in:
Vittorio Romeo 2022-02-15 16:06:12 +00:00
parent b2644e5c3f
commit a68743aa61
2 changed files with 41 additions and 2 deletions

View File

@ -68,6 +68,14 @@ public:
////////////////////////////////////////////////////////////
Sound(const Sound& copy);
////////////////////////////////////////////////////////////
/// \brief Move constructor
///
/// \param other Instance to move
///
////////////////////////////////////////////////////////////
Sound(Sound&& other) noexcept;
////////////////////////////////////////////////////////////
/// \brief Destructor
///
@ -192,7 +200,7 @@ public:
Status getStatus() const override;
////////////////////////////////////////////////////////////
/// \brief Overload of assignment operator
/// \brief Overload of copy assignment operator
///
/// \param right Instance to assign
///
@ -201,6 +209,16 @@ public:
////////////////////////////////////////////////////////////
Sound& operator =(const Sound& right);
////////////////////////////////////////////////////////////
/// \brief Overload of move assignment operator
///
/// \param right Instance to assign
///
/// \return Reference to self
///
////////////////////////////////////////////////////////////
Sound& operator =(Sound&& right) noexcept;
////////////////////////////////////////////////////////////
/// \brief Reset the internal buffer of the sound
///

View File

@ -28,6 +28,7 @@
#include <SFML/Audio/Sound.hpp>
#include <SFML/Audio/SoundBuffer.hpp>
#include <SFML/Audio/ALCheck.hpp>
#include <utility>
#if defined(__APPLE__)
#if defined(__clang__)
@ -65,6 +66,14 @@ m_buffer (nullptr)
}
////////////////////////////////////////////////////////////
Sound::Sound(Sound&& other) noexcept :
SoundSource(std::move(other)),
m_buffer (std::exchange(other.m_buffer, nullptr))
{
}
////////////////////////////////////////////////////////////
Sound::~Sound()
{
@ -108,7 +117,7 @@ void Sound::setBuffer(const SoundBuffer& buffer)
// Assign and use the new buffer
m_buffer = &buffer;
m_buffer->attachSound(this);
alCheck(alSourcei(m_source, AL_BUFFER, static_cast<ALint>(m_buffer->m_buffer)));
alCheck(alSourcei(m_source, AL_BUFFER, static_cast<ALint>(m_buffer->m_uniqueBufferId.get())));
}
@ -191,6 +200,18 @@ Sound& Sound::operator =(const Sound& right)
}
////////////////////////////////////////////////////////////
Sound& Sound::operator =(Sound&& right) noexcept
{
if (this == &right)
return *this;
SoundSource::operator=(std::move(right));
m_buffer = std::exchange(right.m_buffer, nullptr);
return *this;
}
////////////////////////////////////////////////////////////
void Sound::resetBuffer()
{