📄 xbytebuffer.h
字号:
//---------------------------------------------------------------------------
#ifndef XByteBufferH
#define XByteBufferH
#ifndef XCommonTypeH
#include <XCommonType.h>
#endif
#ifndef XExcepMsgH
#include <XExcepMsg.h>
#endif
#ifndef XArrayH
#include <XArray.h>
#endif
//---------------------------------------------------------------------------
namespace zdhsoft
{
//定义XByteBuffer缺省块大小
const int XBYTE_BUFFER_MIN_BLOCK = 4096;
/*
///////////////////////////////////////////////////////////////////////////////
[Name]XByteBuffer
[Title]字节缓冲区
///////////////////////////////////////////////////////////////////////////////
[Description]
这个类主要实现数据处理,这个主要是针对DVB中PSI/SI的数据处理进行设计的,以方便数据的操作
缓冲区的大小是XBYTE_BUFFER_MIN_BLOCK的倍数,在这里XBYTE_BUFFER_MIN_BLOCK的值为4k,这样做则是
为了避免过多的内存碎片和过多的再分配内存
由于这个类主要适用于数字电视系统中,所以对于16位,32位,64位整数存放,与PC系列存放整数的
顺序刚好相反:如值0x1234在PC系列中存放为34 12,而XByteBuffer中的存放,则为12 34
为了便于对缓冲区的操作,在这里提供了大量的操作方法
主要有Append...Array追加数组,Append...追加数值,AppendData,AppendString等
重载了许多<<运算符
为了便于设置和取值,还提供了Set...Value和Get...Value系统操作方法
[Version]1.0
[Author]Rex Winter
[Date]2005-6-18
//////////////////////////////////////////////////////////////////////////////
*/
class XByteBuffer
{
public:
//缺省实现的方法
XByteBuffer(int iPrepareLength = XBYTE_BUFFER_MIN_BLOCK);
XByteBuffer(void * aData,int aDataSize,int iPrepareLength = XBYTE_BUFFER_MIN_BLOCK);
XByteBuffer(const XByteBuffer & aBuffer);
~XByteBuffer();
//一些常用方法
const void * Data() const { return (void *)m_Data; }
int GetLength() const { return m_Length; }
XByte & operator[](int Index);
const XByte & operator[] (int Index) const;
XByteBuffer & operator=(const XByteBuffer & aBuffer);
void Clear(bool bFree = false);
void ensureCapacity(int minimumCapacity)
{
if (minimumCapacity > m_DataSize ) {
expandCapacity(minimumCapacity);
}
}
//追加系列方法
XByteBuffer & AppendByteArray(const XArray<XByte> &aByteArray);
XByteBuffer & AppendCharArray(const XArray<XChar> &aCharArray);
XByteBuffer & AppendBoolArray(const XArray<XBoolean> &aBoolArray);
XByteBuffer & AppendShortArray(const XArray<XShort> &aShortArray);
XByteBuffer & AppendWordArray(const XArray<XWord> &aWordArray);
XByteBuffer & AppendIntArray(const XArray<XInt> &aIntArray);
XByteBuffer & AppendDWordArray(const XArray<XDWord> &aDWordArray);
XByteBuffer & AppendLongArray(const XArray<XLong> &aLongArray);
XByteBuffer & AppendDDWordArray(const XArray<XDDWord> &aDDWordArray);
XByteBuffer & AppendFloatArray(const XArray<XFloat> &aFloatArray);
XByteBuffer & AppendDoubleArray(const XArray<XDouble> &aDoubleArray);
XByteBuffer & AppendLongDoubleArray(const XArray<XLongDouble> &aLongDoubleArray);
XByteBuffer & AppendByteArray(const XDynamicArray<XByte> &aByteArray);
XByteBuffer & AppendCharArray(const XDynamicArray<XChar> &aCharArray);
XByteBuffer & AppendBoolArray(const XDynamicArray<XBoolean> &aBoolArray);
XByteBuffer & AppendShortArray(const XDynamicArray<XShort> &aShortArray);
XByteBuffer & AppendWordArray(const XDynamicArray<XWord> &aWordArray);
XByteBuffer & AppendIntArray(const XDynamicArray<XInt> &aIntArray);
XByteBuffer & AppendDWordArray(const XDynamicArray<XDWord> &aDWordArray);
XByteBuffer & AppendLongArray(const XDynamicArray<XLong> &aLongArray);
XByteBuffer & AppendDDWordArray(const XDynamicArray<XDDWord> &aDDWordArray);
XByteBuffer & AppendFloatArray(const XDynamicArray<XFloat> &aFloatArray);
XByteBuffer & AppendDoubleArray(const XDynamicArray<XDouble> &aDoubleArray);
XByteBuffer & AppendLongDoubleArray(const XDynamicArray<XLongDouble> &aLongDoubleArray);
XByteBuffer & operator<<(const XArray<XByte> &aByteArray)
{
return AppendByteArray(aByteArray);
}
XByteBuffer & operator<<(const XArray<XChar> &aCharArray)
{
return AppendCharArray(aCharArray);
}
XByteBuffer & operator<<(const XArray<XBoolean> &aBooleanArray)
{
return AppendBoolArray(aBooleanArray);
}
XByteBuffer & operator<<(const XArray<XShort> &aShortArray)
{
return AppendShortArray( aShortArray );
}
XByteBuffer & operator<<(const XArray<XWord> &aWordArray)
{
return AppendWordArray( aWordArray );
}
XByteBuffer & operator<<(const XArray<XInt> &aIntArray)
{
return AppendIntArray( aIntArray );
}
XByteBuffer & operator<<(const XArray<XDWord> &aDWordArray)
{
return AppendDWordArray( aDWordArray );
}
XByteBuffer & operator<<(const XArray<XLong> &aLongArray)
{
return AppendLongArray( aLongArray );
}
XByteBuffer & operator<<(const XArray<XDDWord> &aDDWordArray)
{
return AppendDDWordArray( aDDWordArray );
}
XByteBuffer & operator<<(const XArray<XFloat> &aFloatArray)
{
return AppendFloatArray( aFloatArray );
}
XByteBuffer & operator<<(const XArray<XDouble> &aDoubleArray)
{
return AppendDoubleArray( aDoubleArray );
}
XByteBuffer & operator<<(const XArray<XLongDouble> &aLongDoubleArray)
{
return AppendLongDoubleArray( aLongDoubleArray );
}
//
XByteBuffer & operator<<(const XDynamicArray<XByte> &aByteArray)
{
return AppendByteArray(aByteArray);
}
XByteBuffer & operator<<(const XDynamicArray<XChar> &aCharArray)
{
return AppendCharArray(aCharArray);
}
XByteBuffer & operator<<(const XDynamicArray<XBoolean> &aBooleanArray)
{
return AppendBoolArray(aBooleanArray);
}
XByteBuffer & operator<<(const XDynamicArray<XShort> &aShortArray)
{
return AppendShortArray( aShortArray );
}
XByteBuffer & operator<<(const XDynamicArray<XWord> &aWordArray)
{
return AppendWordArray( aWordArray );
}
XByteBuffer & operator<<(const XDynamicArray<XInt> &aIntArray)
{
return AppendIntArray( aIntArray );
}
XByteBuffer & operator<<(const XDynamicArray<XDWord> &aDWordArray)
{
return AppendDWordArray( aDWordArray );
}
XByteBuffer & operator<<(const XDynamicArray<XLong> &aLongArray)
{
return AppendLongArray( aLongArray );
}
XByteBuffer & operator<<(const XDynamicArray<XDDWord> &aDDWordArray)
{
return AppendDDWordArray( aDDWordArray );
}
XByteBuffer & operator<<(const XDynamicArray<XFloat> &aFloatArray)
{
return AppendFloatArray( aFloatArray );
}
XByteBuffer & operator<<(const XDynamicArray<XDouble> &aDoubleArray)
{
return AppendDoubleArray( aDoubleArray );
}
XByteBuffer & operator<<(const XDynamicArray<XLongDouble> &aLongDoubleArray)
{
return AppendLongDoubleArray( aLongDoubleArray );
}
//
XByteBuffer & SetByteValue(int iPos,XByte aByte);
XByteBuffer & SetCharValue(int iPos,XChar aChar);
XByteBuffer & SetWordValue(int iPos,XWord aWord);
XByteBuffer & SetShortValue(int iPos,XShort aShort);
XByteBuffer & SetIntValue(int iPos,XInt aInt);
XByteBuffer & SetDWordValue(int iPos,XDWord aDWord);
XByteBuffer & SetLongValue(int iPos,XLong aLong);
XByteBuffer & SetDDWordValue(int iPos,XDDWord aDDWord);
XByteBuffer & SetBoolValue(int iPos,XBoolean aBool);
XByteBuffer & SetFloatValue(int iPos,XFloat aFloat);
XByteBuffer & SetDoubleValue(int iPos,XDouble aDouble);
XByteBuffer & SetLongDoubleValue(int iPos,XLongDouble aLongDouble);
XByteBuffer & SetDataValue(int iPos,const void * aData,int iSize);
XByte GetByteValue(int iPos) const;
XChar GetCharValue(int iPos) const;
XWord GetWordValue(int iPos) const;
XShort GetShortValue(int iPos) const;
XInt GetIntValue(int iPos) const;
XDWord GetDWordValue(int iPos) const;
XLong GetLongValue(int iPos) const;
XDDWord GetDDWordValue(int iPos) const;
XBoolean GetBoolValue(int iPos) const;
XFloat GetFloatValue(int iPos) const;
XDouble GetDoubleValue(int iPos) const;
XLongDouble GetLongDoubleValue(int iPos) const;
XByteBuffer &GetByteValue(int iPos,XByte & v)
{
v = GetByteValue(iPos);
return *this;
}
XByteBuffer &GetCharValue(int iPos,XChar & v)
{
v = GetCharValue(iPos);
return *this;
}
XByteBuffer &GetWordValue(int iPos,XWord & v)
{
v = GetWordValue(iPos);
return *this;
}
XByteBuffer &GetShortValue(int iPos,XShort & v)
{
v = GetShortValue(iPos);
return *this;
}
XByteBuffer &GetIntValue(int iPos,XInt & v)
{
v = GetIntValue(iPos);
return *this;
}
XByteBuffer &GetDWordValue(int iPos,XDWord & v)
{
v = GetDWordValue(iPos);
return *this;
}
XByteBuffer &GetLongValue(int iPos,XLong & v)
{
v = GetLongValue(iPos);
return *this;
}
XByteBuffer &GetDDWordValue(int iPos,XDDWord & v)
{
v = GetDDWordValue(iPos);
return *this;
}
XByteBuffer &GetBoolValue(int iPos,XBoolean & v)
{
v = GetBoolValue(iPos);
return *this;
}
XByteBuffer &GetFloatValue(int iPos,XFloat & v)
{
v = GetFloatValue(iPos);
return *this;
}
XByteBuffer &GetDoubleValue(int iPos,XDouble & v)
{
v = GetDoubleValue(iPos);
return *this;
}
XByteBuffer &GetLongDoubleValue(int iPos,XLongDouble & v)
{
v = GetLongDoubleValue(iPos);
return *this;
}
XByteBuffer &GetDataValue(int iPos,void * aData,int iSize);
void GetDataValue(int iPos,void *aData,int iSize) const;
XByteBuffer & AppendBuffer(const XByteBuffer & aBuffer);
XByteBuffer & AppendString(const AnsiString & aString);
XByteBuffer & AppendData(const void * aData,int aDataSize);
XByteBuffer & AppendString(const char * aString);
XByteBuffer & AppendByte(XByte aByte);
XByteBuffer & AppendChar(XChar aChar);
XByteBuffer & AppendWord(XWord aWord);
XByteBuffer & AppendShort(XShort aShort);
XByteBuffer & AppendInt(XInt aInt);
XByteBuffer & AppendDWord(XDWord aDWord);
XByteBuffer & AppendLong(XLong aLong);
XByteBuffer & AppendDDWord(XDDWord aDDWord);
XByteBuffer & AppendFloat(XFloat aFloat);
XByteBuffer & AppendDouble(XDouble aDouble);
XByteBuffer & AppendLongDouble(XLongDouble aLongDouble);
XByteBuffer & AppendBoolean(XBoolean v);
XByteBuffer & operator<<(XBoolean v)
{
return AppendBoolean(v);
}
XByteBuffer & operator<<(const XByteBuffer & aBuffer)
{
return AppendBuffer(aBuffer);
}
XByteBuffer & operator<<(const AnsiString & aString)
{
return AppendString(aString);
}
XByteBuffer & operator<<(const char * aString)
{
return AppendString(aString);
}
XByteBuffer & operator<<(XByte aByte)
{
return AppendByte(aByte);
}
XByteBuffer & operator<<(XChar aChar)
{
return AppendChar(aChar);
}
XByteBuffer & operator<<(XWord v)
{
return AppendWord(v);
}
XByteBuffer & operator<<(XShort v)
{
return AppendShort(v);
}
XByteBuffer & operator<<(XInt v)
{
return AppendInt(v);
}
XByteBuffer & operator<<(XDWord v)
{
return AppendDWord(v);
}
XByteBuffer & operator<<(XLong v)
{
return AppendLong(v);
}
XByteBuffer & operator<<(XDDWord v)
{
return AppendDDWord(v);
}
XByteBuffer & operator<<(XFloat v)
{
return AppendFloat(v);
}
XByteBuffer & operator<<(XDouble v)
{
return AppendDouble(v);
}
XByteBuffer & operator<<(XLongDouble v)
{
return AppendLongDouble(v);
}
public:
__property int Length = { read = m_Length };
private:
int PrepareLength(iPrepareLength) //计算要准备的字节数
{
if( iPrepareLength < XBYTE_BUFFER_MIN_BLOCK ) iPrepareLength = XBYTE_BUFFER_MIN_BLOCK;
else if( (iPrepareLength % XBYTE_BUFFER_MIN_BLOCK) != 0) iPrepareLength = (iPrepareLength / XBYTE_BUFFER_MIN_BLOCK+1) * XBYTE_BUFFER_MIN_BLOCK;
return iPrepareLength;
}
void expandCapacity(int minimumCapacity,bool bCopyData = true);
private:
XByte * m_Data;
int m_DataSize;
int m_Length;
};
};
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -