diff options
| author | jsmall-nvidia <jsmall@nvidia.com> | 2019-10-23 09:28:58 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-10-23 09:28:58 -0400 |
| commit | 9c2d1766ea33101b551ac521ddc39516b98b6641 (patch) | |
| tree | feee466c977575ebcb15b9057a59c2efce5d9ae1 /source/core/slang-stream.h | |
| parent | 6a7f4c9cef766e538a808a8f03411af2f10106e1 (diff) | |
Expose more repro in API, support output params. (#1087)
* Added spEnableReproCapture to the API.
* Added MemoryStreamBase - which can be used to read from without copyin the data.
Added the missing Repro API functions - spEnableReproCapture and spExtractRepro.
Added support for serializing output filenames.
* Improved naming around Stream.
Brought Stream and sub types closer to code conventions.
* Renamed content -> contents in Stream.
Diffstat (limited to 'source/core/slang-stream.h')
| -rw-r--r-- | source/core/slang-stream.h | 255 |
1 files changed, 158 insertions, 97 deletions
diff --git a/source/core/slang-stream.h b/source/core/slang-stream.h index 67e04fa6a..8b8a6decf 100644 --- a/source/core/slang-stream.h +++ b/source/core/slang-stream.h @@ -5,109 +5,170 @@ namespace Slang { - class IOException : public Exception - { - public: - IOException() - {} - IOException(const String & message) - : Slang::Exception(message) - { - } - }; - - class EndOfStreamException : public IOException - { - public: - EndOfStreamException() - {} - EndOfStreamException(const String & message) - : IOException(message) - { - } - }; - - enum class SeekOrigin - { - Start, End, Current - }; - class Stream : public RefObject - { - public: - virtual ~Stream() {} - virtual Int64 GetPosition()=0; - virtual void Seek(SeekOrigin origin, Int64 offset)=0; - virtual Int64 Read(void * buffer, Int64 length) = 0; - virtual Int64 Write(const void * buffer, Int64 length) = 0; - virtual bool IsEnd() = 0; - virtual bool CanRead() = 0; - virtual bool CanWrite() = 0; - virtual void Close() = 0; - }; - - enum class FileMode +class IOException : public Exception +{ +public: + IOException() + {} + IOException(const String & message) + : Slang::Exception(message) { - Create, Open, CreateNew, Append - }; + } +}; - enum class FileAccess +class EndOfStreamException : public IOException +{ +public: + EndOfStreamException() + {} + EndOfStreamException(const String & message) + : IOException(message) { - None = 0, Read = 1, Write = 2, ReadWrite = 3 - }; + } +}; - enum class FileShare - { - None, ReadOnly, WriteOnly, ReadWrite - }; +enum class SeekOrigin +{ + Start, End, Current +}; + +class Stream : public RefObject +{ +public: + virtual ~Stream() {} + virtual Int64 getPosition()=0; + virtual void seek(SeekOrigin origin, Int64 offset)=0; + virtual Int64 read(void * buffer, Int64 length) = 0; + virtual Int64 write(const void * buffer, Int64 length) = 0; + virtual bool isEnd() = 0; + virtual bool canRead() = 0; + virtual bool canWrite() = 0; + virtual void close() = 0; +}; + +enum class FileMode +{ + Create, Open, CreateNew, Append +}; + +enum class FileAccess +{ + None = 0, Read = 1, Write = 2, ReadWrite = 3 +}; - class MemoryStream : public Stream +enum class FileShare +{ + None, ReadOnly, WriteOnly, ReadWrite +}; + +/// Base class for memory streams. Only supports reading and does NOT own contained data. +class MemoryStreamBase : public Stream +{ +public: + typedef Stream Super; + + virtual Int64 getPosition() SLANG_OVERRIDE { return m_position; } + virtual void seek(SeekOrigin origin, Int64 offset) SLANG_OVERRIDE; + virtual Int64 read(void * buffer, Int64 length) SLANG_OVERRIDE; + virtual Int64 write(const void * buffer, Int64 length) SLANG_OVERRIDE { SLANG_UNUSED(buffer); SLANG_UNUSED(length); return 0; } + virtual bool isEnd() SLANG_OVERRIDE { return m_atEnd; } + virtual bool canRead() SLANG_OVERRIDE { return (int(m_access) & int(FileAccess::Read)) != 0; } + virtual bool canWrite() SLANG_OVERRIDE { return (int(m_access) & int(FileAccess::Write)) != 0; } + virtual void close() SLANG_OVERRIDE { m_access = FileAccess::None; } + + MemoryStreamBase(FileAccess access = FileAccess::Read, const void* contents = nullptr, size_t contentsSize = 0): + m_access(access) { - public: - virtual Int64 GetPosition() SLANG_OVERRIDE { return m_position; } - virtual void Seek(SeekOrigin origin, Int64 offset) SLANG_OVERRIDE; - virtual Int64 Read(void * buffer, Int64 length) SLANG_OVERRIDE; - virtual Int64 Write(const void * buffer, Int64 length) SLANG_OVERRIDE; - virtual bool IsEnd() SLANG_OVERRIDE { return m_atEnd; } - virtual bool CanRead() SLANG_OVERRIDE { return (int(m_access) & int(FileAccess::Read)) != 0; } - virtual bool CanWrite() SLANG_OVERRIDE { return (int(m_access) & int(FileAccess::Write)) != 0; } - virtual void Close() SLANG_OVERRIDE { m_access = FileAccess::None; } - - MemoryStream(FileAccess access) : - m_access(access), - m_position(0), - m_atEnd(false) - {} - - Index m_position; - - bool m_atEnd; ///< Happens when a read is done and nothing can be returned because already at end - - FileAccess m_access; - List<uint8_t> m_contents; - }; - - class FileStream : public Stream - { - private: - FILE * handle; - FileAccess fileAccess; - bool endReached = false; - void Init(const Slang::String & fileName, FileMode fileMode, FileAccess access, FileShare share); - public: - FileStream(const Slang::String & fileName, FileMode fileMode = FileMode::Open); - FileStream(const Slang::String & fileName, FileMode fileMode, FileAccess access, FileShare share); - ~FileStream(); - public: - virtual Int64 GetPosition(); - virtual void Seek(SeekOrigin origin, Int64 offset); - virtual Int64 Read(void * buffer, Int64 length); - virtual Int64 Write(const void * buffer, Int64 length); - virtual bool CanRead(); - virtual bool CanWrite(); - virtual void Close(); - virtual bool IsEnd(); - }; -} + _setContents(contents, contentsSize); + } + +protected: + /// Set to replace wholly current content with specified content + void _setContents(const void* contents, size_t contentsSize) + { + m_contents = (const uint8_t*)contents; + m_contentsSize = ptrdiff_t(contentsSize); + m_position = 0; + m_atEnd = false; + } + /// Update means that the content has changed, but position should be maintained + void _updateContents(const void* contents, size_t contentsSize) + { + const ptrdiff_t newPosition = (m_position > ptrdiff_t(contentsSize)) ? ptrdiff_t(contentsSize) : m_position; + _setContents(contents, contentsSize); + m_position = newPosition; + } + + const uint8_t* m_contents; ///< The content held in the stream + + // Using ptrdiff_t (as opposed to size_t) as makes maths simpler + ptrdiff_t m_contentsSize; ///< Total size of the content in bytes + ptrdiff_t m_position; ///< The current position within content (valid values can only be between 0 and m_contentSize) + + bool m_atEnd; ///< Happens when a read is done and nothing can be returned because already at end + + FileAccess m_access; +}; + +/// Memory stream that owns it's contents +class OwnedMemoryStream : public MemoryStreamBase +{ +public: + typedef MemoryStreamBase Super; + + virtual Int64 write(const void* buffer, Int64 length) SLANG_OVERRIDE; + + /// Set the contents + void setContent(const void* contents, size_t contentsSize) + { + m_ownedContents.setCount(contentsSize); + ::memcpy(m_ownedContents.getBuffer(), contents, contentsSize); + _setContents(m_ownedContents.getBuffer(), m_ownedContents.getCount()); + } + + void swapContents(List<uint8_t>& rhs) + { + rhs.swapWith(m_ownedContents); + _setContents(m_ownedContents.getBuffer(), m_ownedContents.getCount()); + } + + OwnedMemoryStream(FileAccess access) : + Super(access) + {} + +protected: + + List<uint8_t> m_ownedContents; +}; + +class FileStream : public Stream +{ +public: + typedef Stream Super; + + // Stream interface + virtual Int64 getPosition(); + virtual void seek(SeekOrigin origin, Int64 offset); + virtual Int64 read(void* buffer, Int64 length); + virtual Int64 write(const void* buffer, Int64 length); + virtual bool canRead(); + virtual bool canWrite(); + virtual void close(); + virtual bool isEnd(); + + FileStream(const String& fileName, FileMode fileMode = FileMode::Open); + FileStream(const String& fileName, FileMode fileMode, FileAccess access, FileShare share); + ~FileStream(); + +private: + void _init(const String& fileName, FileMode fileMode, FileAccess access, FileShare share); + + FILE* m_handle; + FileAccess m_fileAccess; + bool m_endReached = false; +}; + +} // namespace Slang #endif |
