From a68743aa615fdfccd042133abd7dca415bf28757 Mon Sep 17 00:00:00 2001 From: Vittorio Romeo Date: Tue, 15 Feb 2022 16:06:12 +0000 Subject: [PATCH] Add move semantics to 'Sound' --- include/SFML/Audio/Sound.hpp | 20 +++++++++++++++++++- src/SFML/Audio/Sound.cpp | 23 ++++++++++++++++++++++- 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/include/SFML/Audio/Sound.hpp b/include/SFML/Audio/Sound.hpp index 7424acf07..2cbc385c7 100644 --- a/include/SFML/Audio/Sound.hpp +++ b/include/SFML/Audio/Sound.hpp @@ -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 /// diff --git a/src/SFML/Audio/Sound.cpp b/src/SFML/Audio/Sound.cpp index f6914ad3e..6f2d26436 100644 --- a/src/SFML/Audio/Sound.cpp +++ b/src/SFML/Audio/Sound.cpp @@ -28,6 +28,7 @@ #include #include #include +#include #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(m_buffer->m_buffer))); + alCheck(alSourcei(m_source, AL_BUFFER, static_cast(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() {