Skip to content

Commit

Permalink
Update self-referential owner ptr when moving SoundStream
Browse files Browse the repository at this point in the history
  • Loading branch information
vittorioromeo committed Jun 9, 2024
1 parent 52ce862 commit 9cac5ac
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions src/SFML/Audio/SoundStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ namespace sf
{
struct SoundStream::Impl : priv::MiniaudioUtils::SoundBase
{
// Memory stability required for self-referential owner pointer.
Impl(const Impl&) = delete;
Impl(Impl&&) = delete;

Impl(SoundStream* ownerPtr) :
SoundBase(vtable, [](void* ptr) { static_cast<Impl*>(ptr)->initialize(); }),
owner(ownerPtr)
Expand Down Expand Up @@ -210,8 +214,7 @@ struct SoundStream::Impl : priv::MiniaudioUtils::SoundBase
////////////////////////////////////////////////////////////
// Member data
////////////////////////////////////////////////////////////
static constexpr ma_data_source_vtable vtable{read, seek, getFormat, getCursor, getLength, setLooping, /* flags */ 0};
SoundStream* const owner; //!< Owning SoundStream object
SoundStream* owner; //!< Owning SoundStream object
std::vector<std::int16_t> sampleBuffer; //!< Our temporary sample buffer
std::size_t sampleBufferCursor{}; //!< The current read position in the temporary sample buffer
std::uint64_t samplesProcessed{}; //!< Number of samples processed since beginning of the stream
Expand All @@ -234,11 +237,26 @@ SoundStream::~SoundStream() = default;


////////////////////////////////////////////////////////////
SoundStream::SoundStream(SoundStream&&) noexcept = default;
SoundStream::SoundStream(SoundStream&& rhs) noexcept : m_impl(std::move(rhs.m_impl))
{
// Update self-referential owner pointer.
m_impl->owner = this;
}


////////////////////////////////////////////////////////////
SoundStream& SoundStream::operator=(SoundStream&&) noexcept = default;
SoundStream& SoundStream::operator=(SoundStream&& rhs) noexcept
{
if (this != &rhs)
{
m_impl = std::move(rhs.m_impl);

// Update self-referential owner pointer.
m_impl->owner = this;
}

return *this;
}


////////////////////////////////////////////////////////////
Expand Down

0 comments on commit 9cac5ac

Please sign in to comment.