Closed yanminhui closed 5 years ago
你可以继承 streambuf, 定义自己的buffer类(例如MyCharBuffer),实现相关的接口,对pkbyContent进行封装。 MyCharBuffer charBuffer(pkbyContent, ui64ContentLength); std::shared_ptr < std::iostream > charContent = std::make_shared < std::iostream > (&charBuffer);
为了简化,继承std::stringbuf 作为例子。 class MyCharBuffer : public std::stringbuf { public: MyCharBuffer(char *ptr, std::streamsize size) : std::stringbuf(std::ios_base::in) { _Mysb::setg(ptr, ptr, ptr + size); _Mysb::setp(ptr + size, ptr + size, ptr + size); } };
class MyCharBuffer : public std::stringbuf { public: MyCharBuffer(char *ptr, std::streamsize size) : std::stringbuf(std::ios_base::in) { _Mysb::setg(ptr, ptr, ptr + size); _Mysb::setp(ptr + size, ptr + size, ptr + size); } }; 在linux 环境下编译不过
MyOssBuffer.h:12:3: 错误:‘_Mysb’未声明 _Mysb::setp(ptr + size, ptr + size, ptr + size);
MyOssBuffer(char *ptr, std::streamsize size):
std::stringbuf(std::ios_base::in)
{
basic_streambuf::setg(ptr, ptr, ptr + size);
basic_streambuf::setp(ptr + size, ptr + size, ptr + size);
basic_streambuf::setg(ptr, ptr, ptr + size);
basic_streambuf::setp(ptr + size, ptr + size);
}
使用这种方式 win linux下可以编译通过
提供的有关上传数据的接口参数为
std::shared_ptr<std::iostream>
,如果遇上传的数据来源于程序产生,那么使用如下方式解决:示例中,存在损耗性能的方面:
构建
std::string const kstrContent
将裸数据构建std::string
动态分配内存空间。构建
std::stringstream
对象 从std::string
构建std::stringstream
将导致std::string_buf
通过std::allocator
再次动态分配内存。智能指针的引用管理 对于一个大的文件循环上传分片,由
std::shared_ptr
不断的引用管理,存在一定的计算。可见,一块内存数据被反复折腾,而不是以较少代价的方式被处理。