From 366b119963c9d8e9fa8130492b356b86a5eb70b7 Mon Sep 17 00:00:00 2001 From: Chris Thrasher Date: Thu, 2 May 2024 11:06:56 -0600 Subject: [PATCH] Remove default empty state of `sf::MemoryInputStream` --- include/SFML/System/MemoryInputStream.hpp | 7 ++--- src/SFML/Audio/InputSoundFile.cpp | 5 +-- src/SFML/Audio/SoundFileFactory.cpp | 3 +- src/SFML/System/MemoryInputStream.cpp | 21 +++---------- test/Audio/Music.test.cpp | 2 +- test/Audio/SoundBuffer.test.cpp | 1 - test/System/MemoryInputStream.test.cpp | 37 ++++++++++------------- 7 files changed, 27 insertions(+), 49 deletions(-) diff --git a/include/SFML/System/MemoryInputStream.hpp b/include/SFML/System/MemoryInputStream.hpp index 18e2b3ef3..95a3fa925 100644 --- a/include/SFML/System/MemoryInputStream.hpp +++ b/include/SFML/System/MemoryInputStream.hpp @@ -47,13 +47,13 @@ class SFML_SYSTEM_API MemoryInputStream : public InputStream { public: //////////////////////////////////////////////////////////// - /// \brief Open the stream from its data + /// \brief Construct the stream from its data /// /// \param data Pointer to the data in memory /// \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 @@ -130,8 +130,7 @@ private: /// \code /// void process(InputStream& stream); /// -/// MemoryInputStream stream; -/// stream.open(thePtr, theSize); +/// MemoryInputStream stream(thePtr, theSize); /// process(stream); /// \endcode /// diff --git a/src/SFML/Audio/InputSoundFile.cpp b/src/SFML/Audio/InputSoundFile.cpp index 8cef74d9f..843934548 100644 --- a/src/SFML/Audio/InputSoundFile.cpp +++ b/src/SFML/Audio/InputSoundFile.cpp @@ -98,10 +98,7 @@ std::optional InputSoundFile::openFromMemory(const void* data, s return std::nullopt; // Wrap the memory file into a stream - auto memory = std::make_unique(); - - // Open it - memory->open(data, sizeInBytes); + auto memory = std::make_unique(data, sizeInBytes); // Pass the stream to the reader auto info = reader->open(*memory); diff --git a/src/SFML/Audio/SoundFileFactory.cpp b/src/SFML/Audio/SoundFileFactory.cpp index 4ad007e24..f74d95241 100644 --- a/src/SFML/Audio/SoundFileFactory.cpp +++ b/src/SFML/Audio/SoundFileFactory.cpp @@ -78,8 +78,7 @@ std::unique_ptr SoundFileFactory::createReaderFromFilename(cons std::unique_ptr SoundFileFactory::createReaderFromMemory(const void* data, std::size_t sizeInBytes) { // Wrap the memory file into a file stream - MemoryInputStream stream; - stream.open(data, sizeInBytes); + MemoryInputStream stream(data, sizeInBytes); // Test the stream for all the registered factories for (const auto& [fpCreate, fpCheck] : getReaderFactoryMap()) diff --git a/src/SFML/System/MemoryInputStream.cpp b/src/SFML/System/MemoryInputStream.cpp index abebd2087..6bed6325e 100644 --- a/src/SFML/System/MemoryInputStream.cpp +++ b/src/SFML/System/MemoryInputStream.cpp @@ -29,26 +29,24 @@ #include +#include #include 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(data)), +m_size(sizeInBytes) { - m_data = static_cast(data); - m_size = sizeInBytes; - m_offset = 0; + assert(m_data && "MemoryInputStream must be initialized with non-null data"); } //////////////////////////////////////////////////////////// std::optional 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); if (count > 0) { @@ -63,9 +61,6 @@ std::optional MemoryInputStream::read(void* data, std::size_t size) //////////////////////////////////////////////////////////// std::optional MemoryInputStream::seek(std::size_t position) { - if (!m_data) - return std::nullopt; - m_offset = position < m_size ? position : m_size; return m_offset; } @@ -74,9 +69,6 @@ std::optional MemoryInputStream::seek(std::size_t position) //////////////////////////////////////////////////////////// std::optional MemoryInputStream::tell() { - if (!m_data) - return std::nullopt; - return m_offset; } @@ -84,9 +76,6 @@ std::optional MemoryInputStream::tell() //////////////////////////////////////////////////////////// std::optional MemoryInputStream::getSize() { - if (!m_data) - return std::nullopt; - return m_size; } diff --git a/test/Audio/Music.test.cpp b/test/Audio/Music.test.cpp index 0e328039a..369378365 100644 --- a/test/Audio/Music.test.cpp +++ b/test/Audio/Music.test.cpp @@ -56,7 +56,7 @@ TEST_CASE("[Audio] sf::Music", runAudioDeviceTests()) SECTION("openFromMemory()") { - std::vector memory; + std::vector memory(10, std::byte{0xCA}); SECTION("Invalid buffer") { diff --git a/test/Audio/SoundBuffer.test.cpp b/test/Audio/SoundBuffer.test.cpp index 45af2a3ff..2831f9f8f 100644 --- a/test/Audio/SoundBuffer.test.cpp +++ b/test/Audio/SoundBuffer.test.cpp @@ -71,7 +71,6 @@ TEST_CASE("[Audio] sf::SoundBuffer", runAudioDeviceTests()) { SECTION("Invalid memory") { - CHECK(!sf::SoundBuffer::loadFromMemory(nullptr, 0)); constexpr std::array memory{}; CHECK(!sf::SoundBuffer::loadFromMemory(memory.data(), memory.size())); } diff --git a/test/System/MemoryInputStream.test.cpp b/test/System/MemoryInputStream.test.cpp index 0bfddeebb..e8ef66799 100644 --- a/test/System/MemoryInputStream.test.cpp +++ b/test/System/MemoryInputStream.test.cpp @@ -16,35 +16,31 @@ TEST_CASE("[System] sf::MemoryInputStream") STATIC_CHECK(std::is_nothrow_move_assignable_v); } - 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; 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; - memoryInputStream.open(input.data(), input.size()); - CHECK(memoryInputStream.tell().value() == 0); - CHECK(memoryInputStream.getSize().value() == 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.getSize().value() == input.size()); + } } SECTION("read()") { static constexpr auto input = "hello world"sv; - sf::MemoryInputStream memoryInputStream; - memoryInputStream.open(input.data(), input.size()); + sf::MemoryInputStream memoryInputStream(input.data(), input.size()); CHECK(memoryInputStream.tell().value() == 0); CHECK(memoryInputStream.getSize().value() == input.size()); @@ -65,8 +61,7 @@ TEST_CASE("[System] sf::MemoryInputStream") SECTION("seek()") { static constexpr auto input = "We Love SFML!"sv; - sf::MemoryInputStream memoryInputStream; - memoryInputStream.open(input.data(), input.size()); + sf::MemoryInputStream memoryInputStream(input.data(), input.size()); CHECK(memoryInputStream.tell().value() == 0); CHECK(memoryInputStream.getSize().value() == input.size());