c++内存流

1、MemoryStream.h文件内容
ifndef _MEM_STREAM_H_
#define _MEM_STREAM_H_

#include <string>

class CMemoryStream
{
public:
	explicit CMemoryStream(std::size_t size)
	{
		m_pBuffer = new unsigned char[size];
		memset(m_pBuffer, 0, size);
		m_nSize = size;
		m_nCurrentWritePos = 0;
		m_nCurrentReadPos = 0;
		m_bManageMemory = true;
	}

	CMemoryStream(void* buffer, std::size_t size)
	{
		m_pBuffer = (unsigned char*)buffer;
		m_nSize = size;
		m_nCurrentWritePos = 0;
		m_nCurrentReadPos = 0;
		m_bManageMemory = false;
	}

	~CMemoryStream()
	{
		if (m_bManageMemory)
		{
			delete [] m_pBuffer;
		}
		m_pBuffer = NULL;
	}

public:
	template<typename T>
	CMemoryStream& operator <<(const T& value)
	{
		if (m_pBuffer &&
			m_nCurrentWritePos < m_nSize)
		{
			*(T*)(m_pBuffer + m_nCurrentWritePos) = value;
			m_nCurrentWritePos += sizeof(T);
		}

		return *this;
	}

	template<typename T>
	CMemoryStream& operator >>(T& value)
	{
		if (m_pBuffer &&
			m_nCurrentReadPos < m_nSize)
		{
			value = *(T*)(m_pBuffer + m_nCurrentReadPos);
			m_nCurrentReadPos += sizeof(T);
		}

		return *this;
	}

	CMemoryStream& operator <<(const char* pBuffer)
	{
		if (m_pBuffer &&
			m_nCurrentWritePos < m_nSize)
		{
			std::size_t size = strlen(pBuffer) + 1;
			memcpy(m_pBuffer + m_nCurrentWritePos, pBuffer, size);
			m_nCurrentWritePos += size;
		}

		return *this;
	}

	CMemoryStream& operator <<(const std::string& str)
	{
		return operator <<(str.c_str());
	}

	CMemoryStream& operator >>(const char* &pBuffer)
	{
		if (m_pBuffer &&
			m_nCurrentReadPos < m_nSize)
		{
			const char* p = (const char*)(m_pBuffer + m_nCurrentReadPos);
			std::size_t size = strlen(p);
			if (size + m_nCurrentReadPos < m_nSize)
			{
				pBuffer = p;
				m_nCurrentReadPos += size+1;
			}
		}

		return *this;
	}

	CMemoryStream& operator >>(std::string& str)
	{
		if (m_pBuffer &&
			m_nCurrentReadPos < m_nSize)
		{
			const char* p = (const char*)(m_pBuffer + m_nCurrentReadPos);
			std::size_t size = strlen(p);
			if (size + m_nCurrentReadPos < m_nSize)
			{
				str.assign(p, p + size);
				m_nCurrentReadPos += size+1;
			}
		}

		return *this;
	}

	CMemoryStream& operator <<(const wchar_t* pBuffer)
	{
		if (m_pBuffer &&
			m_nCurrentWritePos < m_nSize)
		{
			std::size_t size = (wcslen(pBuffer) + 1) * sizeof(wchar_t);
			memcpy(m_pBuffer + m_nCurrentWritePos, pBuffer, size);
			m_nCurrentWritePos += size;
		}

		return *this;
	}

	CMemoryStream& operator <<(const std::wstring& str)
	{
		return operator <<(str.c_str());
	}

	CMemoryStream& operator >>(const wchar_t*& pBuffer)
	{
		if (m_pBuffer &&
			m_nCurrentReadPos < m_nSize)
		{
			const wchar_t* p = (const wchar_t*)(m_pBuffer + m_nCurrentReadPos);
			std::size_t size = wcslen(p) * sizeof(wchar_t);
			if (size + m_nCurrentReadPos < m_nSize)
			{
				pBuffer = p;
				m_nCurrentReadPos += size+sizeof(wchar_t);
			}
		}

		return *this;
	}

	CMemoryStream& operator >>(std::wstring& str)
	{
		if (m_pBuffer &&
			m_nCurrentReadPos < m_nSize)
		{
			const wchar_t* p = (const wchar_t*)(m_pBuffer + m_nCurrentReadPos);
			std::size_t size = wcslen(p) * sizeof(wchar_t);
			if (size + m_nCurrentReadPos < m_nSize)
			{
				str.assign(p, p + size);
				m_nCurrentReadPos += size+sizeof(wchar_t);
			}
		}

		return *this;
	}

private:
	CMemoryStream(const CMemoryStream& memstream);

	const CMemoryStream& operator =(const CMemoryStream&);

private:
	unsigned char* m_pBuffer;
	std::size_t m_nSize;
	std::size_t m_nCurrentWritePos;
	std::size_t m_nCurrentReadPos;

	bool m_bManageMemory;
};

#endif


2、使用示例:

#include "stdafx.h"

#include "MemoryStream.h"

int _tmain(int argc, _TCHAR* argv[])
{
	CMemoryStream mem(100);

	int a = 100;
	int b;
	const char* p1 = "123456";
	const char* p2 = NULL;
	const std::string s1 = "abcedfg";
	std::string s2;
	const wchar_t* p3 = L"789456";
	const wchar_t* p4 = NULL;
	const std::wstring s3 = L"mmm";
	std::wstring s4;
	double d1 = 100.5;
	double d2 = 0;
	float f1 = 200.5;
	float f2 = 0;

	mem << a;
	mem << p1;
	mem << s1;
	mem << p3;
	mem << s3;
	mem << d1;
	mem << f1;

	mem >> b >> p2 >> s2 >> p4 >> s4 >> d2 >> f2;

	return 0;
}


 

posted on 2014-05-10 13:56  dchao  阅读(1409)  评论(0编辑  收藏  举报

导航