Remove default empty state of sf::MemoryInputStream

This commit is contained in:
Chris Thrasher 2024-05-02 11:06:56 -06:00
parent 3263ef8455
commit 366b119963
7 changed files with 27 additions and 49 deletions

View File

@ -47,13 +47,13 @@ class SFML_SYSTEM_API MemoryInputStream : public InputStream
{ {
public: public:
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// \brief Open the stream from its data /// \brief Construct the stream from its data
/// ///
/// \param data Pointer to the data in memory /// \param data Pointer to the data in memory
/// \param sizeInBytes Size of the data, in bytes /// \param sizeInBytes Size of the data, in bytes
/// ///
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
void open(const void* data, std::size_t sizeInBytes); MemoryInputStream(const void* data, std::size_t sizeInBytes);
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// \brief Read data from the stream /// \brief Read data from the stream
@ -130,8 +130,7 @@ private:
/// \code /// \code
/// void process(InputStream& stream); /// void process(InputStream& stream);
/// ///
/// MemoryInputStream stream; /// MemoryInputStream stream(thePtr, theSize);
/// stream.open(thePtr, theSize);
/// process(stream); /// process(stream);
/// \endcode /// \endcode
/// ///

View File

@ -98,10 +98,7 @@ std::optional<InputSoundFile> InputSoundFile::openFromMemory(const void* data, s
return std::nullopt; return std::nullopt;
// Wrap the memory file into a stream // Wrap the memory file into a stream
auto memory = std::make_unique<MemoryInputStream>(); auto memory = std::make_unique<MemoryInputStream>(data, sizeInBytes);
// Open it
memory->open(data, sizeInBytes);
// Pass the stream to the reader // Pass the stream to the reader
auto info = reader->open(*memory); auto info = reader->open(*memory);

View File

@ -78,8 +78,7 @@ std::unique_ptr<SoundFileReader> SoundFileFactory::createReaderFromFilename(cons
std::unique_ptr<SoundFileReader> SoundFileFactory::createReaderFromMemory(const void* data, std::size_t sizeInBytes) std::unique_ptr<SoundFileReader> SoundFileFactory::createReaderFromMemory(const void* data, std::size_t sizeInBytes)
{ {
// Wrap the memory file into a file stream // Wrap the memory file into a file stream
MemoryInputStream stream; MemoryInputStream stream(data, sizeInBytes);
stream.open(data, sizeInBytes);
// Test the stream for all the registered factories // Test the stream for all the registered factories
for (const auto& [fpCreate, fpCheck] : getReaderFactoryMap()) for (const auto& [fpCreate, fpCheck] : getReaderFactoryMap())

View File

@ -29,26 +29,24 @@
#include <algorithm> #include <algorithm>
#include <cassert>
#include <cstring> #include <cstring>
namespace sf namespace sf
{ {
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
void MemoryInputStream::open(const void* data, std::size_t sizeInBytes) MemoryInputStream::MemoryInputStream(const void* data, std::size_t sizeInBytes) :
m_data(static_cast<const std::byte*>(data)),
m_size(sizeInBytes)
{ {
m_data = static_cast<const std::byte*>(data); assert(m_data && "MemoryInputStream must be initialized with non-null data");
m_size = sizeInBytes;
m_offset = 0;
} }
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
std::optional<std::size_t> MemoryInputStream::read(void* data, std::size_t size) std::optional<std::size_t> MemoryInputStream::read(void* data, std::size_t size)
{ {
if (!m_data)
return std::nullopt;
const std::size_t count = std::min(size, m_size - m_offset); const std::size_t count = std::min(size, m_size - m_offset);
if (count > 0) if (count > 0)
{ {
@ -63,9 +61,6 @@ std::optional<std::size_t> MemoryInputStream::read(void* data, std::size_t size)
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
std::optional<std::size_t> MemoryInputStream::seek(std::size_t position) std::optional<std::size_t> MemoryInputStream::seek(std::size_t position)
{ {
if (!m_data)
return std::nullopt;
m_offset = position < m_size ? position : m_size; m_offset = position < m_size ? position : m_size;
return m_offset; return m_offset;
} }
@ -74,9 +69,6 @@ std::optional<std::size_t> MemoryInputStream::seek(std::size_t position)
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
std::optional<std::size_t> MemoryInputStream::tell() std::optional<std::size_t> MemoryInputStream::tell()
{ {
if (!m_data)
return std::nullopt;
return m_offset; return m_offset;
} }
@ -84,9 +76,6 @@ std::optional<std::size_t> MemoryInputStream::tell()
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
std::optional<std::size_t> MemoryInputStream::getSize() std::optional<std::size_t> MemoryInputStream::getSize()
{ {
if (!m_data)
return std::nullopt;
return m_size; return m_size;
} }

View File

@ -56,7 +56,7 @@ TEST_CASE("[Audio] sf::Music", runAudioDeviceTests())
SECTION("openFromMemory()") SECTION("openFromMemory()")
{ {
std::vector<std::byte> memory; std::vector<std::byte> memory(10, std::byte{0xCA});
SECTION("Invalid buffer") SECTION("Invalid buffer")
{ {

View File

@ -71,7 +71,6 @@ TEST_CASE("[Audio] sf::SoundBuffer", runAudioDeviceTests())
{ {
SECTION("Invalid memory") SECTION("Invalid memory")
{ {
CHECK(!sf::SoundBuffer::loadFromMemory(nullptr, 0));
constexpr std::array<std::byte, 5> memory{}; constexpr std::array<std::byte, 5> memory{};
CHECK(!sf::SoundBuffer::loadFromMemory(memory.data(), memory.size())); CHECK(!sf::SoundBuffer::loadFromMemory(memory.data(), memory.size()));
} }

View File

@ -16,35 +16,31 @@ TEST_CASE("[System] sf::MemoryInputStream")
STATIC_CHECK(std::is_nothrow_move_assignable_v<sf::MemoryInputStream>); STATIC_CHECK(std::is_nothrow_move_assignable_v<sf::MemoryInputStream>);
} }
SECTION("Default constructor")
{
sf::MemoryInputStream memoryInputStream;
CHECK(memoryInputStream.read(nullptr, 0) == std::nullopt);
CHECK(memoryInputStream.seek(0) == std::nullopt);
CHECK(memoryInputStream.tell() == std::nullopt);
CHECK(memoryInputStream.getSize() == std::nullopt);
}
using namespace std::literals::string_view_literals; using namespace std::literals::string_view_literals;
SECTION("open()") SECTION("open()")
{ {
sf::MemoryInputStream memoryInputStream;
memoryInputStream.open(nullptr, 0);
CHECK(memoryInputStream.tell() == std::nullopt);
CHECK(memoryInputStream.getSize() == std::nullopt);
static constexpr auto input = "hello world"sv; static constexpr auto input = "hello world"sv;
memoryInputStream.open(input.data(), input.size());
SECTION("Zero length")
{
sf::MemoryInputStream memoryInputStream(input.data(), 0);
CHECK(memoryInputStream.tell().value() == 0);
CHECK(memoryInputStream.getSize().value() == 0);
}
SECTION("Full length")
{
sf::MemoryInputStream memoryInputStream(input.data(), input.size());
CHECK(memoryInputStream.tell().value() == 0); CHECK(memoryInputStream.tell().value() == 0);
CHECK(memoryInputStream.getSize().value() == input.size()); CHECK(memoryInputStream.getSize().value() == input.size());
} }
}
SECTION("read()") SECTION("read()")
{ {
static constexpr auto input = "hello world"sv; static constexpr auto input = "hello world"sv;
sf::MemoryInputStream memoryInputStream; sf::MemoryInputStream memoryInputStream(input.data(), input.size());
memoryInputStream.open(input.data(), input.size());
CHECK(memoryInputStream.tell().value() == 0); CHECK(memoryInputStream.tell().value() == 0);
CHECK(memoryInputStream.getSize().value() == input.size()); CHECK(memoryInputStream.getSize().value() == input.size());
@ -65,8 +61,7 @@ TEST_CASE("[System] sf::MemoryInputStream")
SECTION("seek()") SECTION("seek()")
{ {
static constexpr auto input = "We Love SFML!"sv; static constexpr auto input = "We Love SFML!"sv;
sf::MemoryInputStream memoryInputStream; sf::MemoryInputStream memoryInputStream(input.data(), input.size());
memoryInputStream.open(input.data(), input.size());
CHECK(memoryInputStream.tell().value() == 0); CHECK(memoryInputStream.tell().value() == 0);
CHECK(memoryInputStream.getSize().value() == input.size()); CHECK(memoryInputStream.getSize().value() == input.size());