/**///////////////////////////////////////// CStreamPacket by 太子 2005-12-13 //
/**///////////////////////////////////////#pragma once
const c_BufferBlockSize=0x10000;
class CStreamPacket
{
private:
unsigned char*m_pBuffer;
size_t m_BufferLength;
size_t m_DataStart;
size_t m_DataSize;
public:
CStreamPacket() : m_BufferLength(0), m_DataStart(0), m_DataSize(0)
{
m_pBuffer=new unsigned char[c_BufferBlockSize];
m_BufferLength=c_BufferBlockSize;
}
~CStreamPacket(){ delete[]m_pBuffer; }
private:
bool ResizeBuffer()
{
size_t size=m_BufferLength<<1;
unsigned char*tmp=new unsigned char[size];
unsigned char*tmp2;
memcpy(tmp,m_pBuffer+m_DataStart,m_DataSize);
tmp2=m_pBuffer;
m_pBuffer=tmp;
delete[]tmp2;
m_DataStart=0;
m_BufferLength=size;
return true;
}
bool RecomposeBuffer()
{
if(m_DataStart>0)
{
memcpy(m_pBuffer,m_pBuffer+m_DataStart,m_DataSize);
m_DataStart=0;
return true;
}
return false;
}
public:
// 从缓冲中取一个变量
// 可以是基本数据类型 也可以是struct
// 但不能为数组 否则不能正常取值
template <class T> CStreamPacket& operator>>( T& Data )
{
size_t Size=sizeof(T);
Data=*((T*)(m_pBuffer+m_DataStart));
m_DataStart+=Size;
m_DataSize-=Size;
return *this;
}
// 从缓冲中取一个0结尾数组
// 如C字符串
template <class T> CStreamPacket& operator>>( T Data[] )
{
size_t Size=sizeof(T);
size_t i=0;
while(true)
{
operator >>(Data[i]);
if(Data[i]==(T)0)break;
i++;
}
return *this;
}
// 向缓冲中存一个变量
template <class T> CStreamPacket& operator<<( const T& Data )
{
size_t Size=sizeof(T);
while(m_DataSize+m_DataStart+Size>m_BufferLength)//不能直接内存copy
{
if(m_DataSize+Size>m_BufferLength)//缓冲大小不够
{
ResizeBuffer();//扩大
}
else
{
RecomposeBuffer();//重组
}
}
*((T*)(m_pBuffer+m_DataStart+m_DataSize))=Data;
m_DataSize+=Size;
return *this;
}
// 向缓冲中存一个0结尾的数组
// 如C字符串
template <class T> CStreamPacket& operator<<( T Data[] )
{
size_t i=0;
while(true)
{
operator <<(Data[i]);
if(Data[i]==(T)0)break;
i++;
}
return *this;
}
//来自另外一个CStreamPacket
CStreamPacket& operator<<( const CStreamPacket& Packet )
{
size_t Size=Packet.m_DataSize;
while(m_DataSize+m_DataStart+Size>m_BufferLength)//不能直接内存copy
{
if(m_DataSize+Size>m_BufferLength)//缓冲大小不够
{
ResizeBuffer();//扩大
}
else
{
RecomposeBuffer();//重组
}
}
memcpy(m_pBuffer+m_DataStart+m_DataSize,Packet.m_pBuffer+Packet.m_DataStart,Packet.m_DataSize);
m_DataSize+=Size;
return *this;
}
size_t GetSize()
{
return m_DataSize;
}
void* GetBuffer()
{
return m_pBuffer+m_DataStart;
}
};
// BUFFER.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include ".\buffer.h"
#include <windows.h>
using namespace std;
struct testStruct
{
char name[10];
long age;
float ff;
bool sex;
};
int _tmain(int argc, _TCHAR* argv[])
{
//CBuffer buf1;
//buf1<<"Hello World!"<<L"我是中国人!!";
//buf1<<"1234567890";
//buf1<<(unsigned char)0x12;
//buf1<<(unsigned short)0x5634;
//buf1<<(unsigned long)0x34129078;
//testStruct ss;
//strcpy(ss.name,"ss.name");
//ss.age=0x86AE;
//ss.ff=3.;
//ss.sex=true;
//buf1<<ss;
//CBuffer buf2;
//long double ll,jj=3.141592653589793;
//long xx,yy=123456789;
//buf2<<jj;
//buf2<<yy;
//buf2>>ll;
//buf2>>xx;
CStreamPacket buf3;
#define MB_COUNT (0x100000)
#define MB_SIZE (0x400)
char*pp=new char[MB_SIZE];
char*pp2=new char[MB_SIZE];
memset(pp,'#',MB_SIZE);
pp[MB_SIZE-1]=(char)0;
DWORD t1,t2;
cout<<"块大小"<<MB_SIZE<<",测试次数"<<MB_COUNT<<endl;;
cout<<"开始...";
t1=GetTickCount();
for(int ii=0;ii<MB_COUNT;ii++)
{
buf3<<pp;
//cout<<"buf3.GetSize()="<<buf3.GetSize()<<endl;
buf3>>pp2;
//cout<<"buf3.GetSize()="<<buf3.GetSize()<<endl;
}
t2=GetTickCount()-t1;
cout<<"完成!"<<endl;
buf3<<pp;
cout<<(char*)buf3.GetBuffer()<<endl;
delete[]pp;
delete[]pp2;
//cout<<t2<<endl;
//cout<<(MB_SIZE*MB_COUNT)/(t2)<<" byte/ms"<<endl;
cout<<"处理"<<MB_SIZE*MB_COUNT<<"字节,耗时"<<t2<<"毫秒!速度"<<(MB_SIZE*MB_COUNT)/(t2)<<"字节每毫秒"<<endl;
system("pause");
return 0;
}
posted on 2007-04-23 23:44
shaker(太子) 阅读(1891)
评论(3) 编辑 收藏 引用 所属分类:
C++