Skip to content

Commit

Permalink
Fix const correctness of hsReadOnlyStream and hsWriteOnlyStream
Browse files Browse the repository at this point in the history
  • Loading branch information
dgelessus committed Aug 4, 2023
1 parent 8001dcd commit 0ab1957
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 7 deletions.
39 changes: 39 additions & 0 deletions Sources/Plasma/CoreLib/hsStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,12 @@ void hsReadOnlyStream::CopyToMem(void* mem)


////////////////////////////////////////////////////////////////////////////////////

bool hsWriteOnlyStream::AtEnd()
{
return fData >= fStop;
}

uint32_t hsWriteOnlyStream::Read(uint32_t byteCount, void* buffer)
{
hsThrow( "can't read to a writeonly stream");
Expand All @@ -804,6 +810,39 @@ uint32_t hsWriteOnlyStream::Write(uint32_t byteCount, const void* buffer)
return byteCount;
}

void hsWriteOnlyStream::Skip(uint32_t deltaByteCount)
{
fPosition += deltaByteCount;
fData += deltaByteCount;
if (fData > fStop) {
hsThrow("Skip went past end of stream");
}
}

void hsWriteOnlyStream::Rewind()
{
fPosition = 0;
fData = fStart;
}

void hsWriteOnlyStream::FastFwd()
{
fPosition = GetEOF();
fData = fStop;
}

void hsWriteOnlyStream::Truncate()
{
hsThrow("can't write to a readonly stream");
}

void hsWriteOnlyStream::CopyToMem(void* mem)
{
if (fData < fStop) {
memmove(mem, fData, fStop - fData);
}
}


///////////////////////////////////////////////////////////////////////////////////

Expand Down
32 changes: 25 additions & 7 deletions Sources/Plasma/CoreLib/hsStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -260,12 +260,14 @@ class hsNullStream : public hsStream {
// read only mem stream
class hsReadOnlyStream : public hsStream {
protected:
char* fStart;
char* fData;
char* fStop;
const char* fStart;
const char* fData;
const char* fStop;
public:
hsReadOnlyStream(int size, const void* data)
: fStart((char*)data), fData((char*)data), fStop((char*)data + size)
hsReadOnlyStream(int size, const void* data) :
fStart(static_cast<const char*>(data)),
fData(static_cast<const char*>(data)),
fStop(static_cast<const char*>(data) + size)
{}

bool AtEnd() override;
Expand All @@ -280,17 +282,33 @@ class hsReadOnlyStream : public hsStream {
};

// write only mem stream
class hsWriteOnlyStream : public hsReadOnlyStream {
class hsWriteOnlyStream : public hsStream {
protected:
char* fStart;
char* fData;
char* fStop;

public:
hsWriteOnlyStream(int size, const void* data) : hsReadOnlyStream(size, data) {}
hsWriteOnlyStream(int size, void* data) :
fStart(static_cast<char*>(data)),
fData(static_cast<char*>(data)),
fStop(static_cast<char*>(data) + size)
{}
hsWriteOnlyStream(const hsWriteOnlyStream& other) = delete;
hsWriteOnlyStream(hsWriteOnlyStream&& other) = delete;

const hsWriteOnlyStream& operator=(const hsWriteOnlyStream& other) = delete;
hsWriteOnlyStream& operator=(hsWriteOnlyStream&& other) = delete;

bool AtEnd() override;
uint32_t Read(uint32_t byteCount, void * buffer) override; // throws exception
uint32_t Write(uint32_t byteCount, const void* buffer) override;
void Skip(uint32_t deltaByteCount) override;
void Rewind() override;
void FastFwd() override;
void Truncate() override;
uint32_t GetEOF() override { return (uint32_t)(fStop - fStart); }
void CopyToMem(void* mem);
};

// circular queue stream
Expand Down

0 comments on commit 0ab1957

Please sign in to comment.