Search code examples
c++boostistreamboost-interprocess

Convert boost`s bufferstream to istream


Is it possible to convert boost`s bufferstream to istream? I am trying to do conversion, however it is still unclear to me whether I am doing something wrong or it is just not possible to do that at all. I would appreciate any answers.

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

And then I need to convert bufferstream into istream. Basically, I need to make bufferstream instances be passed as a parameter to a function which accepts istream &.


Solution

  • It's unclear what you want to achieve¹, here's my best guess:

    Live² On 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-xy-problem

    ² shared memory is not supported on Coliru