Skip to content

Commit

Permalink
Use std::optional rather than sentinel values
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisThrasher committed May 3, 2024
1 parent b7c526e commit f479ca7
Show file tree
Hide file tree
Showing 20 changed files with 230 additions and 202 deletions.
10 changes: 6 additions & 4 deletions examples/vulkan/Vulkan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -931,9 +931,10 @@ class VulkanExample
return;
}

std::vector<std::uint32_t> buffer(static_cast<std::size_t>(file.getSize()) / sizeof(std::uint32_t));
const auto fileSize = file.getSize().value();
std::vector<std::uint32_t> buffer(fileSize / sizeof(std::uint32_t));

if (file.read(buffer.data(), file.getSize()) != file.getSize())
if (file.read(buffer.data(), fileSize) != file.getSize())
{
vulkanAvailable = false;
return;
Expand All @@ -959,9 +960,10 @@ class VulkanExample
return;
}

std::vector<std::uint32_t> buffer(static_cast<std::size_t>(file.getSize()) / sizeof(std::uint32_t));
const auto fileSize = file.getSize().value();
std::vector<std::uint32_t> buffer(fileSize / sizeof(std::uint32_t));

if (file.read(buffer.data(), file.getSize()) != file.getSize())
if (file.read(buffer.data(), fileSize) != file.getSize())
{
vulkanAvailable = false;
return;
Expand Down
16 changes: 8 additions & 8 deletions include/SFML/System/FileInputStream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,36 +111,36 @@ class SFML_SYSTEM_API FileInputStream : public InputStream
/// \param data Buffer where to copy the read data
/// \param size Desired number of bytes to read
///
/// \return The number of bytes actually read, or -1 on error
/// \return The number of bytes actually read, or `std::nullopt` on error
///
////////////////////////////////////////////////////////////
[[nodiscard]] std::int64_t read(void* data, std::int64_t size) override;
[[nodiscard]] std::optional<std::size_t> read(void* data, std::size_t size) override;

////////////////////////////////////////////////////////////
/// \brief Change the current reading position
///
/// \param position The position to seek to, from the beginning
///
/// \return The position actually sought to, or -1 on error
/// \return The position actually sought to, or `std::nullopt` on error
///
////////////////////////////////////////////////////////////
[[nodiscard]] std::int64_t seek(std::int64_t position) override;
[[nodiscard]] std::optional<std::size_t> seek(std::size_t position) override;

////////////////////////////////////////////////////////////
/// \brief Get the current reading position in the stream
///
/// \return The current position, or -1 on error.
/// \return The current position, or `std::nullopt` on error.
///
////////////////////////////////////////////////////////////
[[nodiscard]] std::int64_t tell() override;
[[nodiscard]] std::optional<std::size_t> tell() override;

////////////////////////////////////////////////////////////
/// \brief Return the size of the stream
///
/// \return The total number of bytes available in the stream, or -1 on error
/// \return The total number of bytes available in the stream, or `std::nullopt` on error
///
////////////////////////////////////////////////////////////
std::int64_t getSize() override;
std::optional<std::size_t> getSize() override;

private:
////////////////////////////////////////////////////////////
Expand Down
26 changes: 14 additions & 12 deletions include/SFML/System/InputStream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@

#include <SFML/System/Export.hpp>

#include <optional>

#include <cstdint>


Expand Down Expand Up @@ -58,36 +60,36 @@ class SFML_SYSTEM_API InputStream
/// \param data Buffer where to copy the read data
/// \param size Desired number of bytes to read
///
/// \return The number of bytes actually read, or -1 on error
/// \return The number of bytes actually read, or `std::nullopt` on error
///
////////////////////////////////////////////////////////////
[[nodiscard]] virtual std::int64_t read(void* data, std::int64_t size) = 0;
[[nodiscard]] virtual std::optional<std::size_t> read(void* data, std::size_t size) = 0;

////////////////////////////////////////////////////////////
/// \brief Change the current reading position
///
/// \param position The position to seek to, from the beginning
///
/// \return The position actually sought to, or -1 on error
/// \return The position actually sought to, or `std::nullopt` on error
///
////////////////////////////////////////////////////////////
[[nodiscard]] virtual std::int64_t seek(std::int64_t position) = 0;
[[nodiscard]] virtual std::optional<std::size_t> seek(std::size_t position) = 0;

////////////////////////////////////////////////////////////
/// \brief Get the current reading position in the stream
///
/// \return The current position, or -1 on error.
/// \return The current position, or `std::nullopt` on error.
///
////////////////////////////////////////////////////////////
[[nodiscard]] virtual std::int64_t tell() = 0;
[[nodiscard]] virtual std::optional<std::size_t> tell() = 0;

////////////////////////////////////////////////////////////
/// \brief Return the size of the stream
///
/// \return The total number of bytes available in the stream, or -1 on error
/// \return The total number of bytes available in the stream, or `std::nullopt` on error
///
////////////////////////////////////////////////////////////
virtual std::int64_t getSize() = 0;
virtual std::optional<std::size_t> getSize() = 0;
};

} // namespace sf
Expand Down Expand Up @@ -119,13 +121,13 @@ class SFML_SYSTEM_API InputStream
///
/// [[nodiscard]] bool open(const std::filesystem::path& filename);
///
/// [[nodiscard]] std::int64_t read(void* data, std::int64_t size);
/// [[nodiscard]] std::optional<std::size_t> read(void* data, std::size_t size);
///
/// [[nodiscard]] std::int64_t seek(std::int64_t position);
/// [[nodiscard]] std::optional<std::size_t> seek(std::size_t position);
///
/// [[nodiscard]] std::int64_t tell();
/// [[nodiscard]] std::optional<std::size_t> tell();
///
/// std::int64_t getSize();
/// std::optional<std::size_t> getSize();
///
/// private:
///
Expand Down
20 changes: 10 additions & 10 deletions include/SFML/System/MemoryInputStream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,44 +64,44 @@ class SFML_SYSTEM_API MemoryInputStream : public InputStream
/// \param data Buffer where to copy the read data
/// \param size Desired number of bytes to read
///
/// \return The number of bytes actually read, or -1 on error
/// \return The number of bytes actually read, or `std::nullopt` on error
///
////////////////////////////////////////////////////////////
[[nodiscard]] std::int64_t read(void* data, std::int64_t size) override;
[[nodiscard]] std::optional<std::size_t> read(void* data, std::size_t size) override;

////////////////////////////////////////////////////////////
/// \brief Change the current reading position
///
/// \param position The position to seek to, from the beginning
///
/// \return The position actually sought to, or -1 on error
/// \return The position actually sought to, or `std::nullopt` on error
///
////////////////////////////////////////////////////////////
[[nodiscard]] std::int64_t seek(std::int64_t position) override;
[[nodiscard]] std::optional<std::size_t> seek(std::size_t position) override;

////////////////////////////////////////////////////////////
/// \brief Get the current reading position in the stream
///
/// \return The current position, or -1 on error.
/// \return The current position, or `std::nullopt` on error.
///
////////////////////////////////////////////////////////////
[[nodiscard]] std::int64_t tell() override;
[[nodiscard]] std::optional<std::size_t> tell() override;

////////////////////////////////////////////////////////////
/// \brief Return the size of the stream
///
/// \return The total number of bytes available in the stream, or -1 on error
/// \return The total number of bytes available in the stream, or `std::nullopt` on error
///
////////////////////////////////////////////////////////////
std::int64_t getSize() override;
std::optional<std::size_t> getSize() override;

private:
////////////////////////////////////////////////////////////
// Member data
////////////////////////////////////////////////////////////
const std::byte* m_data{}; //!< Pointer to the data in memory
std::int64_t m_size{}; //!< Total size of the data
std::int64_t m_offset{}; //!< Current reading position
std::size_t m_size{}; //!< Total size of the data
std::size_t m_offset{}; //!< Current reading position
};

} // namespace sf
Expand Down
2 changes: 1 addition & 1 deletion src/SFML/Audio/InputSoundFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ void InputSoundFile::seek(std::uint64_t sampleOffset)
////////////////////////////////////////////////////////////
void InputSoundFile::seek(Time timeOffset)
{
seek(static_cast<std::uint64_t>(timeOffset.asSeconds() * static_cast<float>(m_sampleRate)) * m_channelMap.size());
seek(static_cast<std::size_t>(timeOffset.asSeconds() * static_cast<float>(m_sampleRate)) * m_channelMap.size());
}


Expand Down
6 changes: 3 additions & 3 deletions src/SFML/Audio/SoundFileFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ std::unique_ptr<SoundFileReader> SoundFileFactory::createReaderFromFilename(cons
// Test the filename in all the registered factories
for (const auto& [fpCreate, fpCheck] : getReaderFactoryMap())
{
if (stream.seek(0) == -1)
if (!stream.seek(0).has_value())
{
err() << "Failed to seek sound stream" << std::endl;
return nullptr;
Expand All @@ -84,7 +84,7 @@ std::unique_ptr<SoundFileReader> SoundFileFactory::createReaderFromMemory(const
// Test the stream for all the registered factories
for (const auto& [fpCreate, fpCheck] : getReaderFactoryMap())
{
if (stream.seek(0) == -1)
if (!stream.seek(0).has_value())
{
err() << "Failed to seek sound stream" << std::endl;
return nullptr;
Expand All @@ -106,7 +106,7 @@ std::unique_ptr<SoundFileReader> SoundFileFactory::createReaderFromStream(InputS
// Test the stream for all the registered factories
for (const auto& [fpCreate, fpCheck] : getReaderFactoryMap())
{
if (stream.seek(0) == -1)
if (!stream.seek(0).has_value())
{
err() << "Failed to seek sound stream" << std::endl;
return nullptr;
Expand Down
31 changes: 15 additions & 16 deletions src/SFML/Audio/SoundFileReaderFlac.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,17 @@ FLAC__StreamDecoderReadStatus streamRead(const FLAC__StreamDecoder*, FLAC__byte
{
auto* data = static_cast<sf::priv::SoundFileReaderFlac::ClientData*>(clientData);

const std::int64_t count = data->stream->read(buffer, static_cast<std::int64_t>(*bytes));
if (count > 0)
if (const auto count = data->stream->read(buffer, *bytes))
{
*bytes = static_cast<std::size_t>(count);
return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
}
else if (count == 0)
{
return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
if (*count > 0)
{
*bytes = *count;
return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
}
else
{
return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
}
}
else
{
Expand All @@ -64,8 +66,7 @@ FLAC__StreamDecoderSeekStatus streamSeek(const FLAC__StreamDecoder*, FLAC__uint6
{
auto* data = static_cast<sf::priv::SoundFileReaderFlac::ClientData*>(clientData);

const std::int64_t position = data->stream->seek(static_cast<std::int64_t>(absoluteByteOffset));
if (position >= 0)
if (data->stream->seek(static_cast<std::size_t>(absoluteByteOffset)).has_value())
return FLAC__STREAM_DECODER_SEEK_STATUS_OK;
else
return FLAC__STREAM_DECODER_SEEK_STATUS_ERROR;
Expand All @@ -75,10 +76,9 @@ FLAC__StreamDecoderTellStatus streamTell(const FLAC__StreamDecoder*, FLAC__uint6
{
auto* data = static_cast<sf::priv::SoundFileReaderFlac::ClientData*>(clientData);

const std::int64_t position = data->stream->tell();
if (position >= 0)
if (const auto position = data->stream->tell())
{
*absoluteByteOffset = static_cast<FLAC__uint64>(position);
*absoluteByteOffset = *position;
return FLAC__STREAM_DECODER_TELL_STATUS_OK;
}
else
Expand All @@ -91,10 +91,9 @@ FLAC__StreamDecoderLengthStatus streamLength(const FLAC__StreamDecoder*, FLAC__u
{
auto* data = static_cast<sf::priv::SoundFileReaderFlac::ClientData*>(clientData);

const std::int64_t count = data->stream->getSize();
if (count >= 0)
if (const auto count = data->stream->getSize())
{
*streamLength = static_cast<FLAC__uint64>(count);
*streamLength = *count;
return FLAC__STREAM_DECODER_LENGTH_STATUS_OK;
}
else
Expand Down
10 changes: 5 additions & 5 deletions src/SFML/Audio/SoundFileReaderMp3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,14 @@ namespace
std::size_t readCallback(void* ptr, std::size_t size, void* data)
{
auto* stream = static_cast<sf::InputStream*>(data);
return static_cast<std::size_t>(stream->read(ptr, static_cast<std::int64_t>(size)));
return stream->read(ptr, size).value_or(-1);
}

int seekCallback(std::uint64_t offset, void* data)
{
auto* stream = static_cast<sf::InputStream*>(data);
const std::int64_t position = stream->seek(static_cast<std::int64_t>(offset));
return position < 0 ? -1 : 0;
auto* stream = static_cast<sf::InputStream*>(data);
const auto position = stream->seek(static_cast<std::size_t>(offset));
return position ? 0 : -1;
}

bool hasValidId3Tag(const std::uint8_t* header)
Expand All @@ -92,7 +92,7 @@ bool SoundFileReaderMp3::check(InputStream& stream)
{
std::uint8_t header[10];

if (static_cast<std::size_t>(stream.read(header, static_cast<std::int64_t>(sizeof(header)))) < sizeof(header))
if (stream.read(header, sizeof(header)) != sizeof(header))
return false;

if (hasValidId3Tag(header))
Expand Down
17 changes: 10 additions & 7 deletions src/SFML/Audio/SoundFileReaderOgg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,29 +41,32 @@ namespace
std::size_t read(void* ptr, std::size_t size, std::size_t nmemb, void* data)
{
auto* stream = static_cast<sf::InputStream*>(data);
return static_cast<std::size_t>(stream->read(ptr, static_cast<std::int64_t>(size * nmemb)));
return stream->read(ptr, size * nmemb).value_or(-1);
}

int seek(void* data, ogg_int64_t offset, int whence)
int seek(void* data, ogg_int64_t signedOffset, int whence)
{
auto* stream = static_cast<sf::InputStream*>(data);
auto offset = static_cast<std::size_t>(signedOffset);
switch (whence)
{
case SEEK_SET:
break;
case SEEK_CUR:
offset += stream->tell();
offset += stream->tell().value();
break;
case SEEK_END:
offset = stream->getSize() - offset;
offset = stream->getSize().value() - offset;
}
return static_cast<int>(stream->seek(offset));
const auto position = stream->seek(offset);
return position ? static_cast<int>(*position) : -1;
}

long tell(void* data)
{
auto* stream = static_cast<sf::InputStream*>(data);
return static_cast<long>(stream->tell());
auto* stream = static_cast<sf::InputStream*>(data);
const auto position = stream->tell();
return position ? static_cast<long>(*position) : -1;
}

ov_callbacks callbacks = {&read, &seek, nullptr, &tell};
Expand Down

0 comments on commit f479ca7

Please sign in to comment.