将Boost的BufferStream转换为Istream

Convert boost`s bufferstream to istream

本文关键字:Istream 转换 BufferStream Boost      更新时间:2023-10-16

是否可以将Boost的BufferStream转换为Istream?我正在尝试进行转换,但是我仍然不清楚我是做错了什么还是根本无法做到这一点。我很感谢任何答案。

char *copy = static_cast <char*> (region.get_address());
for (int i = 0;i < length;i++) copy[i] = str[i];
bufferstream input_stream (copy, length);

然后我需要将BufferStream转换为ISTream。基本上,我需要将BufferSeam实例作为参数传递给接受IStream&amp;的函数。

这是不清楚的您想实现什么,这是我最好的猜测:

live coliru

#include <boost/interprocess/mapped_region.hpp>
#include <boost/interprocess/shared_memory_object.hpp>
#include <boost/interprocess/streams/bufferstream.hpp>
#include <iostream>
namespace bip = boost::interprocess;
int main() {
    bip::shared_memory_object smo(bip::open_or_create, "MySharedMemory", bip::read_write);
    std::string str = "test data";
    smo.truncate(10ull << 10); // 10 KiB
    bip::mapped_region r(smo, bip::read_write);
    bip::bufferstream stream(reinterpret_cast<char*>(r.get_address()), r.get_size());
    if (stream << str)
        std::cout << "Written";
}

¹https://meta.stackexchange.com/questions/66377/what-is-the-problem

²Coliru

不支持共享内存