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:
////////////////////////////////////////////////////////////
/// \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
///

View File

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

View File

@ -29,26 +29,24 @@
#include <algorithm>
#include <cassert>
#include <cstring>
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);
m_size = sizeInBytes;
m_offset = 0;
assert(m_data && "MemoryInputStream must be initialized with non-null data");
}
////////////////////////////////////////////////////////////
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);
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)
{
if (!m_data)
return std::nullopt;
m_offset = position < m_size ? position : m_size;
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()
{
if (!m_data)
return std::nullopt;
return m_offset;
}
@ -84,9 +76,6 @@ std::optional<std::size_t> MemoryInputStream::tell()
////////////////////////////////////////////////////////////
std::optional<std::size_t> MemoryInputStream::getSize()
{
if (!m_data)
return std::nullopt;
return m_size;
}

View File

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

View File

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