Skip to content

Commit

Permalink
Use std::optional to express functions that may not return a value
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisThrasher committed Apr 29, 2024
1 parent 71395e7 commit 73126c9
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 12 deletions.
4 changes: 2 additions & 2 deletions include/SFML/Audio/Music.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,10 +213,10 @@ class SFML_AUDIO_API Music : public SoundStream
/// the seek position for a loop. We then determine whether we are looping on a
/// loop point or the end-of-file, perform the seek, and return the new position.
///
/// \return The seek position after looping (or -1 if there's no loop)
/// \return The seek position after looping (or std::nullopt if there's no loop)
///
////////////////////////////////////////////////////////////
std::int64_t onLoop() override;
std::optional<std::uint64_t> onLoop() override;

private:
////////////////////////////////////////////////////////////
Expand Down
8 changes: 3 additions & 5 deletions include/SFML/Audio/SoundStream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include <SFML/System/Time.hpp>

#include <memory>
#include <optional>
#include <vector>

#include <cstddef>
Expand Down Expand Up @@ -194,9 +195,6 @@ class SFML_AUDIO_API SoundStream : public SoundSource
bool getLoop() const;

protected:
// NOLINTNEXTLINE(readability-identifier-naming)
static constexpr std::int64_t NoLoop = -1; //!< "Invalid" onLoop return value, telling us to continue uninterrupted

////////////////////////////////////////////////////////////
/// \brief Default constructor
///
Expand Down Expand Up @@ -259,10 +257,10 @@ class SFML_AUDIO_API SoundStream : public SoundSource
/// allow implementation of custom loop points. Otherwise,
/// it just calls onSeek(Time::Zero) and returns 0.
///
/// \return The seek position after looping (or -1 if there's no loop)
/// \return The seek position after looping (or std::nullopt if there's no loop)
///
////////////////////////////////////////////////////////////
virtual std::int64_t onLoop();
virtual std::optional<std::uint64_t> onLoop();

private:
////////////////////////////////////////////////////////////
Expand Down
4 changes: 2 additions & 2 deletions src/SFML/Audio/Music.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ void Music::onSeek(Time timeOffset)


////////////////////////////////////////////////////////////
std::int64_t Music::onLoop()
std::optional<std::uint64_t> Music::onLoop()
{
// Called by underlying SoundStream so we can determine where to loop.
const std::lock_guard lock(m_mutex);
Expand All @@ -222,7 +222,7 @@ std::int64_t Music::onLoop()
m_file.seek(0);
return 0;
}
return NoLoop;
return std::nullopt;
}


Expand Down
6 changes: 3 additions & 3 deletions src/SFML/Audio/SoundStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,10 @@ struct SoundStream::Impl
// If we are looping and at the end of the loop, set the cursor back to the beginning of the loop
if (!impl.streaming && impl.loop)
{
if (const auto seekPositionAfterLoop = owner->onLoop(); seekPositionAfterLoop != NoLoop)
if (const auto seekPositionAfterLoop = owner->onLoop())
{
impl.streaming = true;
impl.samplesProcessed = static_cast<std::uint64_t>(seekPositionAfterLoop);
impl.samplesProcessed = *seekPositionAfterLoop;
}
}
}
Expand Down Expand Up @@ -396,7 +396,7 @@ bool SoundStream::getLoop() const


////////////////////////////////////////////////////////////
std::int64_t SoundStream::onLoop()
std::optional<std::uint64_t> SoundStream::onLoop()
{
onSeek(Time::Zero);
return 0;
Expand Down

0 comments on commit 73126c9

Please sign in to comment.