diff options
Diffstat (limited to 'llvm/include/llvm/DebugInfo/Msf/StreamRef.h')
-rw-r--r-- | llvm/include/llvm/DebugInfo/Msf/StreamRef.h | 112 |
1 files changed, 69 insertions, 43 deletions
diff --git a/llvm/include/llvm/DebugInfo/Msf/StreamRef.h b/llvm/include/llvm/DebugInfo/Msf/StreamRef.h index e87d298a834..87e53ce4fa4 100644 --- a/llvm/include/llvm/DebugInfo/Msf/StreamRef.h +++ b/llvm/include/llvm/DebugInfo/Msf/StreamRef.h @@ -15,17 +15,63 @@ namespace llvm { namespace msf { - -class StreamRef { +template <class StreamType, class RefType> class StreamRefBase { public: - StreamRef() : Stream(nullptr), ViewOffset(0), Length(0) {} - StreamRef(const StreamInterface &Stream) - : Stream(&Stream), ViewOffset(0), Length(Stream.getLength()) {} - StreamRef(const StreamInterface &Stream, uint32_t Offset, uint32_t Length) + StreamRefBase() : Stream(nullptr), Length(0), ViewOffset(0) {} + StreamRefBase(const StreamType &Stream, uint32_t Offset, uint32_t Length) : Stream(&Stream), ViewOffset(Offset), Length(Length) {} + uint32_t getLength() const { return Length; } + const StreamType *getStream() const { return Stream; } + + RefType drop_front(uint32_t N) const { + if (!Stream) + return RefType(); + + N = std::min(N, Length); + return RefType(*Stream, ViewOffset + N, Length - N); + } + + RefType keep_front(uint32_t N) const { + if (!Stream) + return RefType(); + N = std::min(N, Length); + return RefType(*Stream, ViewOffset, N); + } + + RefType slice(uint32_t Offset, uint32_t Len) const { + return drop_front(Offset).keep_front(Len); + } + + bool operator==(const RefType &Other) const { + if (Stream != Other.Stream) + return false; + if (ViewOffset != Other.ViewOffset) + return false; + if (Length != Other.Length) + return false; + return true; + } + +protected: + const StreamType *Stream; + uint32_t ViewOffset; + uint32_t Length; +}; + +class ReadableStreamRef + : public StreamRefBase<ReadableStream, ReadableStreamRef> { +public: + ReadableStreamRef() : StreamRefBase() {} + ReadableStreamRef(const ReadableStream &Stream) + : StreamRefBase(Stream, 0, Stream.getLength()) {} + ReadableStreamRef(const ReadableStream &Stream, uint32_t Offset, + uint32_t Length) + : StreamRefBase(Stream, Offset, Length) {} + // Use StreamRef.slice() instead. - StreamRef(const StreamRef &S, uint32_t Offset, uint32_t Length) = delete; + ReadableStreamRef(const ReadableStreamRef &S, uint32_t Offset, + uint32_t Length) = delete; Error readBytes(uint32_t Offset, uint32_t Size, ArrayRef<uint8_t> &Buffer) const { @@ -53,6 +99,21 @@ public: Buffer = Buffer.slice(0, MaxLength); return Error::success(); } +}; + +class WritableStreamRef + : public StreamRefBase<WritableStream, WritableStreamRef> { +public: + WritableStreamRef() : StreamRefBase() {} + WritableStreamRef(const WritableStream &Stream) + : StreamRefBase(Stream, 0, Stream.getLength()) {} + WritableStreamRef(const WritableStream &Stream, uint32_t Offset, + uint32_t Length) + : StreamRefBase(Stream, Offset, Length) {} + + // Use StreamRef.slice() instead. + WritableStreamRef(const WritableStreamRef &S, uint32_t Offset, + uint32_t Length) = delete; Error writeBytes(uint32_t Offset, ArrayRef<uint8_t> Data) const { if (Data.size() + Offset > Length) @@ -60,44 +121,9 @@ public: return Stream->writeBytes(ViewOffset + Offset, Data); } - uint32_t getLength() const { return Length; } - Error commit() const { return Stream->commit(); } - - StreamRef drop_front(uint32_t N) const { - if (!Stream) - return StreamRef(); - - N = std::min(N, Length); - return StreamRef(*Stream, ViewOffset + N, Length - N); - } - - StreamRef keep_front(uint32_t N) const { - if (!Stream) - return StreamRef(); - N = std::min(N, Length); - return StreamRef(*Stream, ViewOffset, N); - } - - StreamRef slice(uint32_t Offset, uint32_t Len) const { - return drop_front(Offset).keep_front(Len); - } - - bool operator==(const StreamRef &Other) const { - if (Stream != Other.Stream) - return false; - if (ViewOffset != Other.ViewOffset) - return false; - if (Length != Other.Length) - return false; - return true; - } - -private: - const StreamInterface *Stream; - uint32_t ViewOffset; - uint32_t Length; }; + } // namespace msf } // namespace llvm |