Use std::optional to express functions that may not return a value

This commit is contained in:
Chris Thrasher 2024-04-28 10:14:46 -06:00
parent 71395e746e
commit 73126c93a3
4 changed files with 10 additions and 12 deletions

View File

@ -213,10 +213,10 @@ protected:
/// the seek position for a loop. We then determine whether we are looping on a /// 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. /// 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: private:
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////

View File

@ -35,6 +35,7 @@
#include <SFML/System/Time.hpp> #include <SFML/System/Time.hpp>
#include <memory> #include <memory>
#include <optional>
#include <vector> #include <vector>
#include <cstddef> #include <cstddef>
@ -194,9 +195,6 @@ public:
bool getLoop() const; bool getLoop() const;
protected: protected:
// NOLINTNEXTLINE(readability-identifier-naming)
static constexpr std::int64_t NoLoop = -1; //!< "Invalid" onLoop return value, telling us to continue uninterrupted
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// \brief Default constructor /// \brief Default constructor
/// ///
@ -259,10 +257,10 @@ protected:
/// allow implementation of custom loop points. Otherwise, /// allow implementation of custom loop points. Otherwise,
/// it just calls onSeek(Time::Zero) and returns 0. /// 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: private:
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////

View File

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

View File

@ -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 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 (!impl.streaming && impl.loop)
{ {
if (const auto seekPositionAfterLoop = owner->onLoop(); seekPositionAfterLoop != NoLoop) if (const auto seekPositionAfterLoop = owner->onLoop())
{ {
impl.streaming = true; impl.streaming = true;
impl.samplesProcessed = static_cast<std::uint64_t>(seekPositionAfterLoop); impl.samplesProcessed = *seekPositionAfterLoop;
} }
} }
} }
@ -396,7 +396,7 @@ bool SoundStream::getLoop() const
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
std::int64_t SoundStream::onLoop() std::optional<std::uint64_t> SoundStream::onLoop()
{ {
onSeek(Time::Zero); onSeek(Time::Zero);
return 0; return 0;