📄 xinputbytebuffer.h
字号:
//---------------------------------------------------------------------------
#ifndef XInputByteBufferH
#define XInputByteBufferH
//---------------------------------------------------------------------------
#ifndef XByteBufferH
#include <XByteBuffer.h>
#endif
namespace zdhsoft
{
class XEReadBufferFail : public Exception //读取缓冲区失败
{
public:
XEReadBufferFail(int iHelpContext=0);
private:
};
class XInputByteBuffer
{
public:
XInputByteBuffer(const XByteBuffer & aBuffer);
XInputByteBuffer(const XInputByteBuffer & aInputByteBuffer);
XInputByteBuffer & operator=(const XInputByteBuffer & aInputByteBuffer)
{
m_Buffer = aInputByteBuffer.m_Buffer;
m_Pos = aInputByteBuffer.m_Pos;
return *this;
}
//method
int GetPos() const { return m_Pos; }
void MoveToBegin() { SetPos(0); }
void MoveToEnd() { SetPos(m_Buffer->GetLength()-1); }
void SetPos(int v)
{
int iLen = m_Buffer->GetLength();
if( v >= iLen ) v = iLen;
if( v < 0 ) v = -1;
m_Pos = v;
}
bool IsBof() const
{
if( m_Buffer->GetLength() == 0 ) return true;
if( m_Pos < 0 ) return true;
else return false;
}
bool IsEof() const
{
if( m_Pos >= m_Buffer->GetLength() ) return true;
else return false;
}
XInputByteBuffer & operator+=(int aOffset)
{
SetPos(m_Pos + aOffset);
return *this;
}
XInputByteBuffer & operator-=(int aOffset)
{
SetPos(m_Pos - aOffset);
return *this;
}
XInputByteBuffer & operator++(int)
{
SetPos(m_Pos + 1);
return *this;
}
XInputByteBuffer operator++()
{
XInputByteBuffer tmp( *this );
SetPos(m_Pos + 1);
return tmp;
}
XInputByteBuffer & operator--(int)
{
SetPos(m_Pos - 1);
return *this;
}
XInputByteBuffer operator--()
{
XInputByteBuffer tmp( *this );
SetPos(m_Pos - 1);
return tmp;
}
//
bool IsCanRead(int iPos,int iSize)
{
if( iSize < 1 ) return false;
if( iPos < 0 || iPos >= m_Buffer->GetLength() ) return false;
if( iPos + iSize > m_Buffer->GetLength() ) return false;
return true;
}
bool GetBool(XBoolean & v)
{
if( IsCanRead( m_Pos,sizeof(XBoolean) ) )
{
if( m_Buffer->operator[](m_Pos++) == 0) v = false;
else v = true;
return true;
}else return false;
}
bool GetByte(XByte & v)
{
if( IsCanRead( m_Pos,sizeof(XByte) ) )
{
v = m_Buffer->operator[](m_Pos++);
return true;
}else return false;
}
bool GetChar(XChar &v)
{
if( IsCanRead( m_Pos,sizeof(XChar) ) )
{
v =(XChar)m_Buffer->operator[](m_Pos++);
return true;
}else return false;
}
bool GetWord(XWord &v)
{
if( IsCanRead( m_Pos,sizeof(XWord) ) )
{
v = m_Buffer->GetWordValue(m_Pos);
m_Pos += sizeof(XWord);
return true;
}else return false;
}
bool GetShort(XShort & v)
{
if( IsCanRead( m_Pos,sizeof(XShort) ) )
{
v = m_Buffer->GetShortValue(m_Pos);
m_Pos += sizeof(XShort);
return true;
}else return false;
}
bool GetDWord(XDWord &v)
{
if( IsCanRead( m_Pos,sizeof(XDWord) ) )
{
v = m_Buffer->GetDWordValue(m_Pos);
m_Pos += sizeof(XDWord);
return true;
}else return false;
}
bool GetInt(XInt & v)
{
if( IsCanRead( m_Pos,sizeof(XInt) ) )
{
v = m_Buffer->GetIntValue(m_Pos);
m_Pos += sizeof(XInt);
return true;
}else return false;
}
bool GetDDWord(XDDWord &v)
{
if( IsCanRead( m_Pos,sizeof(XDDWord) ) )
{
v = m_Buffer->GetDDWordValue(m_Pos);
m_Pos += sizeof(XDDWord);
return true;
}else return false;
}
bool GetLong(XLong & v)
{
if( IsCanRead( m_Pos,sizeof(XLong) ) )
{
v = m_Buffer->GetLongValue(m_Pos);
m_Pos += sizeof(XLong);
return true;
}else return false;
}
bool GetFloat(XFloat & v)
{
if( IsCanRead( m_Pos, sizeof(XFloat) ) )
{
v = m_Buffer->GetFloatValue(m_Pos);
m_Pos += sizeof(XFloat);
return true;
}else return false;
}
bool GetDouble(XDouble & v)
{
if( IsCanRead( m_Pos, sizeof(XDouble) ) )
{
v = m_Buffer->GetDoubleValue(m_Pos);
m_Pos += sizeof(XDouble);
return true;
}else return false;
}
bool GetLongDouble(XLongDouble & v)
{
if( IsCanRead( m_Pos, sizeof(XDouble) ) )
{
v = m_Buffer->GetLongDoubleValue(m_Pos);
m_Pos += sizeof(XLongDouble);
return true;
}else return false;
}
bool GetData(void * aData,int iSize)
{
if( iSize <= 0 ) return true;
if( IsCanRead( m_Pos, iSize ))
{
m_Buffer->GetDataValue(m_Pos,aData,iSize);
m_Pos += iSize;
return true;
}else return false;
}
//
XByte GetByte()
{
XByte v;
if(!GetByte(v)) throw XEReadBufferFail();
return v;
}
XChar GetChar()
{
XChar v;
if(!GetChar(v)) throw XEReadBufferFail();
return v;
}
XWord GetWord()
{
XWord v;
if(!GetWord(v)) throw XEReadBufferFail();
return v;
}
XShort GetShort()
{
XShort v;
if(!GetShort(v)) throw XEReadBufferFail();
return v;
}
XDWord GetDWord()
{
XDWord v;
if(!GetDWord(v)) throw XEReadBufferFail();
return v;
}
XInt GetInt()
{
XInt v;
if(!GetInt(v)) throw XEReadBufferFail();
return v;
}
XDDWord GetDDWord()
{
XDDWord v;
if(!GetDDWord(v)) throw XEReadBufferFail();
return v;
}
XLong GetLong()
{
XLong v;
if(!GetLong(v)) throw XEReadBufferFail();
return v;
}
XBoolean GetBool()
{
XBoolean v;
if(!GetBool(v)) throw XEReadBufferFail();
return v;
}
XFloat GetFloat()
{
XFloat v;
if(!GetFloat(v)) throw XEReadBufferFail();
return v;
}
XDouble GetDouble()
{
XDouble v;
if(!GetDouble(v)) throw XEReadBufferFail();
return v;
}
XLongDouble GetLongDouble()
{
XLongDouble v;
if(!GetLongDouble(v)) throw XEReadBufferFail();
return v;
}
XInputByteBuffer & operator>>(XBoolean & v)
{
if( IsCanRead( m_Pos,sizeof(XBoolean) ) )
{
if( m_Buffer->operator[](m_Pos++) == 0) v = false;
else v = true;
}
return *this;
}
XInputByteBuffer & operator>>(XByte & v)
{
if( IsCanRead( m_Pos,sizeof(XByte) ) )
{
v = m_Buffer->operator[](m_Pos++);
}
return *this;
}
XInputByteBuffer & operator>>(XChar &v)
{
if( IsCanRead( m_Pos,sizeof(XChar) ) )
{
v =(XChar)m_Buffer->operator[](m_Pos++);
}
return *this;
}
XInputByteBuffer & operator>>(XWord &v)
{
if( IsCanRead( m_Pos,sizeof(XWord) ) )
{
v = m_Buffer->GetWordValue(m_Pos);
m_Pos += sizeof(XWord);
}
return *this;
}
XInputByteBuffer & operator>>(XShort & v)
{
if( IsCanRead( m_Pos,sizeof(XShort) ) )
{
v = m_Buffer->GetShortValue(m_Pos);
m_Pos += sizeof(XShort);
}
return *this;
}
XInputByteBuffer & operator>>(XDWord &v)
{
if( IsCanRead( m_Pos,sizeof(XDWord) ) )
{
v = m_Buffer->GetDWordValue(m_Pos);
m_Pos += sizeof(XDWord);
}
return *this;
}
XInputByteBuffer & operator>>(XInt & v)
{
if( IsCanRead( m_Pos,sizeof(XInt) ) )
{
v = m_Buffer->GetIntValue(m_Pos);
m_Pos += sizeof(XInt);
}
return *this;
}
XInputByteBuffer & operator>>(XDDWord &v)
{
if( IsCanRead( m_Pos,sizeof(XDDWord) ) )
{
v = m_Buffer->GetDDWordValue(m_Pos);
m_Pos += sizeof(XDDWord);
}
return *this;
}
XInputByteBuffer & operator>>(XLong & v)
{
if( IsCanRead( m_Pos,sizeof(XLong) ) )
{
v = m_Buffer->GetLongValue(m_Pos);
m_Pos += sizeof(XLong);
}
return *this;
}
XInputByteBuffer & operator>>(XFloat & v)
{
if( IsCanRead( m_Pos, sizeof(XFloat) ) )
{
v = m_Buffer->GetFloatValue(m_Pos);
m_Pos += sizeof(XFloat);
}
return *this;
}
XInputByteBuffer & operator>>(XDouble & v)
{
if( IsCanRead( m_Pos, sizeof(XDouble) ) )
{
v = m_Buffer->GetDoubleValue(m_Pos);
m_Pos += sizeof(XDouble);
}
return *this;
}
XInputByteBuffer & operator>>(XLongDouble & v)
{
if( IsCanRead( m_Pos, sizeof(XDouble) ) )
{
v = m_Buffer->GetLongDoubleValue(m_Pos);
m_Pos += sizeof(XLongDouble);
}
return *this;
}
//property
__property int Pos = { read = GetPos ,write = SetPos };
private:
const XByteBuffer * m_Buffer;
int m_Pos;
};
};
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -