#pragma once #include #include "String.DString.hpp" #include "TmplBlockBuffer.tlh" #include "TmplBlockBuffer.tli" #include "Utility.Either+Error.hpp" #ifndef _IOBUFFER_EXPORTS #define _IOBuffer_API _declspec(dllimport) #else #define _IOBuffer_API _declspec(dllexport) #endif #ifndef _IOBUFFER_EXPORTS #ifdef _WIN64 #ifdef _DEBUG #pragma comment (lib, "ECOM.Utility.IOBuffer64D.lib") #else #pragma comment (lib, "ECOM.Utility.IOBuffer64.lib") #endif #else // X86 #ifdef _DEBUG #pragma comment (lib, "ECOM.Utility.IOBufferD.lib") #else #pragma comment (lib, "ECOM.Utility.IOBuffer.lib") #endif #endif #endif // _IOBUFFER_EXPORTS namespace ECOM::Utility::IOBuffer { class InBuffer; class OutBuffer; } namespace ECOM::IO::Stream { class BaseStream; class IStream; class OStream; //----------------------------------------------------------------------------- // IStream //----------------------------------------------------------------------------- class _IOBuffer_API BaseStream { template static constexpr bool dependent_false = false; public: BaseStream () = default; virtual ~BaseStream () = default; virtual eiResult Open () = 0; virtual void Close () = 0; virtual bool IsClosed () const { return false; } // 当流被关闭后, 返回 true // read data from disk, then save it to InBuffer/OutBuffer // int TransferTo (OStream & OB, int cbNumBytes); virtual void TransferTo (IBlockBuffer & BLOB) = 0; virtual eSTR::DString ToString () const { return eSTR::DString (); } // 用于日志,跟踪等 }; //----------------------------------------------------------------------------- // IStream //----------------------------------------------------------------------------- class _IOBuffer_API IStream : public virtual BaseStream { friend class OStream; friend class OMemoryStream; protected: ECOM::Utility::IOBuffer::InBuffer * m_NIB; protected: IStream (ECOM::Utility::IOBuffer::InBuffer * IB); IStream (IStream && from) { m_NIB = from.m_NIB; from.m_NIB = nullptr; } IStream & operator = (IStream && from) { m_NIB = from.m_NIB; from.m_NIB = nullptr; return (*this); } IStream (const IStream & from) = delete; IStream & operator = (const IStream & from) = delete; public: virtual ~IStream (); public: virtual IStream & operator >> (BYTE & x) { Read (&x, sizeof (x)); return (*this); } virtual IStream & operator >> (UINT16 & x) { Read (&x, sizeof (x)); return (*this); } virtual IStream & operator >> (UINT32 & x) { Read (&x, sizeof (x)); return (*this); } virtual IStream & operator >> (unsigned long & x) { Read (&x, sizeof (x)); return (*this); } virtual IStream & operator >> (unsigned long long & x) { Read (&x, sizeof (x)); return (*this); } IStream & operator >> (char & x) { return (*this) >> (BYTE &) x; }; IStream & operator >> (INT16 & x) { return (*this) >> (UINT16 &) x; }; IStream & operator >> (INT32 & x) { return (*this) >> (UINT32 &) x; }; IStream & operator >> (long & x) { return (*this) >> (unsigned long &) x; }; IStream & operator >> (long long & x) { return (*this) >> (unsigned long long &) x; }; virtual bool Fill (int Bytes); virtual bool Kill (int Bytes); virtual int Read (void * vPtr, int cbNumBytes); BYTE Peek (); int Peek (void * toData, UINT Bytes); template T Peek () { static_assert (dependent_false ); } template <> inline BYTE Peek () { BYTE v = 0; Peek (& v, sizeof (v)); return v; } template <> inline UINT16 Peek () { UINT16 v = 0; Peek (& v, sizeof (v)); return v; } template <> inline UINT32 Peek () { UINT32 v = 0; Peek (& v, sizeof (v)); return v; } template <> inline unsigned long Peek () { unsigned long v = 0; Peek (& v, sizeof (v)); return v; } void TransferTo (IBlockBuffer & BLOB) override; void CopyTo (IStream & IS); public: virtual eiResult Open () override;// { assert (m_NIB); m_NIB->Open (); } virtual void Close () override;// { assert (m_NIB); m_NIB->Close (); } // 当流被关闭后, 返回 true virtual bool IsClosed () const override;// { assert (m_NIB); return m_NIB->IsClosed (); } public: // 快速读 template T Read () { static_assert (dependent_false ); } template <> inline BYTE Read () { BYTE v = 0; operator >> (v); return v; } template <> inline UINT16 Read () { UINT16 v = 0; operator >> (v); return v; } template <> inline UINT32 Read () { UINT32 v = 0; operator >> (v); return v; } template <> inline unsigned long Read () { unsigned long v = 0; operator >> (v); return v; } template <> inline unsigned long long Read () { unsigned long long v = 0; operator >> (v); return v; } public: // 由 DICOM 模块使用的一些杂项辅助函数 UINT GetBreakSize () const; bool SetBreakSize (UINT ToSize); int TransferTo (OStream & OB, int cbNumBytes); bool PutBack (UINT8 u8); bool PutBack (UINT16 u16); bool PutBack (UINT32 u32); bool PutBack (const void * Data, UINT Bytes); int GetSizeInBuffer () const; // 缓冲区里面还剩多少个字节 ? int GetNbOfRead () const; // 我总共读了多少个字节 ? public: class _IOBuffer_API _BigEndian { private: IStream & IB; public: _BigEndian (IStream & _IB) : IB (_IB) { } _BigEndian (_BigEndian &&) = delete; _BigEndian (const _BigEndian &) = delete; _BigEndian & operator = (const _BigEndian &) = delete; _BigEndian & operator = (_BigEndian &&) = delete; public: void operator >> (BYTE & x); void operator >> (UINT16 & x); void operator >> (UINT32 & x); void operator >> (unsigned long & x); void operator >> (char & x) { (*this) >> (BYTE &) x; }; void operator >> (INT16 & x) { (*this) >> (UINT16 &) x; }; void operator >> (INT32 & x) { (*this) >> (UINT32 &) x; }; void operator >> (long & x) { (*this) >> (unsigned long &) x; }; // 快速读 template T Read () { static_assert (dependent_false ); } template <> inline BYTE Read () { BYTE v = 0; operator >> (v); return v; } template <> inline UINT16 Read () { UINT16 v = 0; operator >> (v); return v; } template <> inline UINT32 Read () { UINT32 v = 0; operator >> (v); return v; } template <> inline unsigned long Read () { unsigned long v = 0; operator >> (v); return v; } } BE () { return _BigEndian (*this); } public: class _IOBuffer_API _LittleEndian { private: IStream & IB; public: _LittleEndian (IStream & _IB) : IB (_IB) { } _LittleEndian (_LittleEndian &&) = delete; _LittleEndian (const _LittleEndian &) = delete; _LittleEndian & operator = (const _LittleEndian &) = delete; _LittleEndian & operator = (_LittleEndian &&) = delete; public: void operator >> (BYTE & x) { IB >> x; } void operator >> (UINT16 & x) { IB >> x; } void operator >> (UINT32 & x) { IB >> x; } void operator >> (unsigned long & x) { IB >> x; } void operator >> (unsigned long long & x) { IB >> x; } void operator >> (char & x) { (*this) >> (BYTE &) x; }; void operator >> (INT16 & x) { (*this) >> (UINT16 &) x; }; void operator >> (INT32 & x) { (*this) >> (UINT32 &) x; }; void operator >> (long & x) { (*this) >> (unsigned long &) x; }; void operator >> (long long & x) { (*this) >> (unsigned long long &) x; }; // 快速读 template T Read () { static_assert (dependent_false ); } template <> inline BYTE Read () { BYTE v = 0; operator >> (v); return v; } template <> inline UINT16 Read () { UINT16 v = 0; operator >> (v); return v; } template <> inline UINT32 Read () { UINT32 v = 0; operator >> (v); return v; } template <> inline unsigned long Read () { unsigned long v = 0; operator >> (v); return v; } template <> inline unsigned long long Read () { unsigned long long v = 0; operator >> (v); return v; } } LE () { return _LittleEndian (*this); } }; //----------------------------------------------------------------------------- // OStream //----------------------------------------------------------------------------- class _IOBuffer_API OStream : public virtual BaseStream { friend class IStream; friend class IMemoryStream; protected: ECOM::Utility::IOBuffer::OutBuffer * m_NOB; protected: OStream (ECOM::Utility::IOBuffer::OutBuffer * OB); OStream (OStream && from) { m_NOB = from.m_NOB; from.m_NOB = nullptr; } OStream & operator = (OStream && from) { m_NOB = from.m_NOB; from.m_NOB = nullptr; return (*this); } OStream (const OStream & from) = delete; OStream & operator = (const OStream & from) = delete; public: virtual ~OStream (); public: virtual OStream & operator << (BYTE x) { Write (&x, sizeof (x)); return (*this); } virtual OStream & operator << (UINT16 x) { Write (&x, sizeof (x)); return (*this); } virtual OStream & operator << (UINT32 x) { Write (&x, sizeof (x)); return (*this); } virtual OStream & operator << (unsigned long x) { Write (&x, sizeof (x)); return (*this); } virtual OStream & operator << (unsigned long long x) { Write (&x, sizeof (x)); return (*this); } OStream & operator << (char x) { return (*this) << (BYTE ) x; }; OStream & operator << (INT16 x) { return (*this) << (UINT16) x; }; OStream & operator << (INT32 x) { return (*this) << (UINT32) x; }; OStream & operator << (long x) { return (*this) << (unsigned long) x; }; OStream & operator << (long long x) { return (*this) << (unsigned long long) x; }; virtual int Write (const void * Data, int cbNumBytes); virtual void WriteThrough (const void * Data, int cbBytes); // same as SendRaw (); virtual bool Flush (); void TransferTo (IBlockBuffer & BLOB) override; public: virtual eiResult Open () override;// { assert (m_NIB); m_NIB->Open (); } virtual void Close () override;// { assert (m_NIB); m_NIB->Close (); } // 当流被关闭后, 返回 true virtual bool IsClosed () const override;// { assert (m_NIB); return m_NIB->IsClosed (); } public: // 由 DICOM 模块使用的一些杂项辅助函数 UINT GetBreakSize () const; bool SetBreakSize (UINT ToSize); int GetSizeInBuffer () const; // 缓冲区里面还剩多少个字节 ? int GetNbOfSend () const; // 我总共读了多少个字节 ? public: class _IOBuffer_API _BigEndian { private: OStream & OB; public: _BigEndian (OStream & _OB) : OB (_OB) { } _BigEndian (_BigEndian &&) = delete; _BigEndian (const _BigEndian &) = delete; _BigEndian & operator = (const _BigEndian &) = delete; _BigEndian & operator = (_BigEndian &&) = delete; public: void operator << (BYTE x); void operator << (UINT16 x); void operator << (UINT32 x); void operator << (unsigned long x); void operator << (char x) { (*this) << (BYTE) x; }; void operator << (INT16 x) { (*this) << (UINT16) x; }; void operator << (INT32 x) { (*this) << (UINT32) x; }; void operator << (long x) { (*this) << (unsigned long) x; }; } BE () { return _BigEndian (*this); } public: class _IOBuffer_API _LittleEndian { private: OStream & OB; public: _LittleEndian (OStream & _OB) : OB (_OB) { } _LittleEndian (_LittleEndian &&) = delete; _LittleEndian (const _LittleEndian &) = delete; _LittleEndian & operator = (const _LittleEndian &) = delete; _LittleEndian & operator = (_LittleEndian &&) = delete; public: void operator << (BYTE x) { OB << x; } void operator << (UINT16 x) { OB << x; } void operator << (UINT32 x) { OB << x; } void operator << (unsigned long x) { OB << x; } void operator << (char x) { (*this) << (BYTE) x; }; void operator << (INT16 x) { (*this) << (UINT16) x; }; void operator << (INT32 x) { (*this) << (UINT32) x; }; void operator << (long x) { (*this) << (unsigned long) x; }; } LE () { return _LittleEndian (*this); } }; #if 0 class _IOBuffer_API IOStream : public IStream, public OStream { public: IOStream () = default; virtual ~IOStream () = default; }; #endif //----------------------------------------------------------------------------- // IMemoryStream // 一次分配 //----------------------------------------------------------------------------- class _IOBuffer_API IMemoryStream : public IStream { using base = IStream; public: IMemoryStream (); IMemoryStream (int nbAlloc);// { Alloc (nbAlloc); } virtual ~IMemoryStream (); IMemoryStream (const IMemoryStream & OB) = delete; IMemoryStream & operator = (const IMemoryStream & OB) = delete; IMemoryStream & operator = (IMemoryStream && OB) = delete; public: void BorrowFrom (const void * pBuffer, int BufferSize); void * Alloc (int nbAlloc); void * GetBufferSetLength (int nbAlloc) { return Alloc (nbAlloc); } void AppendFrom (OStream && OB); }; //----------------------------------------------------------------------------- // OMemoryBuffer // 一次分配 //----------------------------------------------------------------------------- class _IOBuffer_API OMemoryStream : public OStream { using base = OStream; public: OMemoryStream (); OMemoryStream (int nbAlloc);// { Alloc (nbAlloc); } virtual ~OMemoryStream (); OMemoryStream (const OMemoryStream & OB) = delete; OMemoryStream & operator = (const OMemoryStream & OB) = delete; OMemoryStream & operator = (OMemoryStream && OB) = delete; public: void BorrowFrom (const void * pBuffer, int BufferSize); void * Alloc (int nbAlloc); void * GetBufferSetLength (int nbAlloc) { return Alloc (nbAlloc); } }; }