码迷,mamicode.com
首页 > 编程语言 > 详细

C++ 字节流 CByteBuffer

时间:2014-12-09 17:08:13      阅读:344      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   使用   sp   div   log   bs   amp   


CByteBuffer使用
static void Main()
{
    BYTE a;
    short b;
    int c;
    CByteBuffer buffer;
    buffer<<a<<b<<c;
}

 

 

class
CByteBuffer { public: BYTE* m_lpBuf; int nCurrentIndex; int nCapacity; int nLen; CByteBuffer() { Init(128); } CByteBuffer(int nCapacityIn) { Init(nCapacityIn); } ~CByteBuffer() { delete [] m_lpBuf; m_lpBuf=NULL; } void GetMoreCapacity() { BYTE *pBytesMore=new BYTE[nCapacity*2]; //两倍 memcpy(pBytesMore,m_lpBuf,nCapacity*sizeof(BYTE)); memset(pBytesMore+nCapacity,0x00,nCapacity*sizeof(BYTE)); delete [] m_lpBuf; m_lpBuf=pBytesMore; nCapacity*=2; } template<typename T> CByteBuffer& operator <<(T dw) { int nSizeNew=nCurrentIndex + sizeof(T); while(nSizeNew>nCapacity) { GetMoreCapacity(); //缓冲区空间不够 } *(T*)(&m_lpBuf[nCurrentIndex])=dw; //添入缓冲区 nCurrentIndex += sizeof(T); //移动当前索引 nLen+=sizeof(T); return *this; } private: void Init(int nCapatityIn) { nCapacity=nCapatityIn; m_lpBuf=new BYTE[nCapatityIn]; memset(m_lpBuf,0x00,nCapacity); nCurrentIndex=0; nLen=0; } };

 

C++ 字节流 CByteBuffer

标签:style   blog   color   使用   sp   div   log   bs   amp   

原文地址:http://www.cnblogs.com/JsonBlog/p/4153431.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!