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;
}
c++内存流的更多相关文章
- Mybatis拦截器 mysql load data local 内存流处理
Mybatis 拦截器不做解释了,用过的基本都知道,这里用load data local主要是应对大批量数据的处理,提高性能,也支持事务回滚,且不影响其他的DML操作,当然这个操作不要涉及到当前所lo ...
- [源码]ObjectIOStream 对象流 ByteArrayIOStream 数组流 内存流 ZipOutputStream 压缩流
1.对象流 import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File ...
- 不可或缺 Windows Native (13) - C++: 标准输入, 标准输出, 字符串内存流
[源码下载] 不可或缺 Windows Native (13) - C++: 标准输入, 标准输出, 字符串内存流 作者:webabcd 介绍不可或缺 Windows Native 之 C++ 标准输 ...
- 内存流和null字节
#include <stdio.h> #include <string.h> int main() { ]={}; FILE* fp = fmemopen(buf,," ...
- C#流总结(文件流、内存流、网络流、BufferedStream、StreamReader/StreamWriter、TextReader/TextWriter)
一.文件流 FileStream类主要用于读写磁盘文件.常用于向磁盘存储数据或读取配置文件. 读取文件: //文件流:读取 FileStream fileStream = File.Open(@&qu ...
- 如何在MVC中显示条形码图片(以内存流的方式)
前台代码: <script type="text/javascript"> function fresh() { var getimagecode = document ...
- UNIX编程之冲洗内存流与null追加策略(APUE F5-15)
博文链接:http://haoyuanliu.github.io/2016/04/29/mysql/ 对,我是来骗访问量的!O(∩_∩)O~~ 最近一直在拜读APUE(Advanced Program ...
- IO流(随机流,数组内存流
File file1=new File("test1.txt"); RandomAccessFile in2=new RandomAccessFile(file1,"rw ...
- Delphi从内存流中判断图片格式(好多相关文章)
废话不多说了,利用内存流来判断文件的格式,其实判断文件的前几个字节就可以简单的判断这个文件是什么类型的文件,例如jpg文件 是 FFD8 (从低位到高位就要反过来 D8FF 下面都是一样)BMP文件 ...
- 用内存流 文件流 资源生成客户端(Delphi开源)
正文:很多木马生成器就是用的内存流和文件流生成客户端的,废话不多说了,代码如下: unit Main; interface usesWindows, Messages, SysUtils, Varia ...
随机推荐
- top.location != self.location
top.location != self.location 就是说当前窗体的url和父窗体的 url是不是相同 这个是为了防止别的网站嵌入你的网站的内容(比如用iframe嵌入的你的网站的页面)
- Leetcode 39 40 216 Combination Sum I II III
Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combin ...
- CSS 背景 background 讲解
背景语法:background: background-color || background-image || background-repeat || background-attachment ...
- tab一些 添加 删除 搜索
tab一些 添加 删除 搜索 案例 <!DOCTYPE html><html lang="en"><head> <meta charset ...
- Hexo搭建静态个人博客
Hexo简介 之前在Github上托管的博客就是使用jekyll搭建的,官方的Github Pages同样推荐使用它.我之前体验了一下jekyll,没有达到我想要的效果.于是寻找替代方案,搜索同类博客 ...
- IntelliJ IDEA 13.1.4新建java web项目
打开软件
- Python基础(四)-集合
补充: 数据类型分类: 1.按照数据可变不可变: 可变:列表.字典 不可变:数字.字符串.元组 x={':1} print(id(x)) x.update({':2}) print(x) print( ...
- 12C dbca silent
dbca needs a template file to create a database. These template can be found in $ORACLE_HOME/assista ...
- echarts图表里遇到的问题
1. 取消echarts的默认动画 : series:[{ animation:false }] 2. 控制散点图里散点的大小: series:[{ symbolSize:5 }] 3. 去除内部网格 ...
- Java Queue 各种方法的区别
再Java里的某些集合类,其实是实现了多个接口的,所以就会同时又多种方法针对同一种操作,比如LinkedList类. 首先看一下java集合类的继承关系图: 这里简单对其重复的方法做点简单的区分. o ...