Search code examples
c++boostboost-iostreams

Using boost::stream for more complex/structuered types then chars?


is it possible to use boost::iostreams for more complex / structured types?

What I want to do is to stream images but they should have some annotations like width, height, color depth,... My first idea is to use a struct instead of an char or an wchar

namespace io = boost::iostreams;

struct SingleImageStream{
   unsigned int  width;
   unsigned int height;
   unsigned char colordepth;
   unsigned char* frame;
};


class SingleImageSource {
public:
    typedef struct SingleImageStream            char_type;
    typedef io::source_tag  category;

    std::streamsize read(struct SingleImageStream* s, std::streamsize n)
    {
        char* frame = new char[640*480];
        std::fill( frame, frame + sizeof( frame ), 0 );

        s->width = 640;
        s->height = 480;

        std::copy(frame, frame + sizeof(frame), s->frame);

        return -1;
    }    
};


class SingleImageSink {
public:
    typedef struct SingleImageStream        char_type;
    typedef io::sink_tag                    category;

    std::streamsize write(const struct SingleImageStream* s, std::streamsize n)
    {
            std::cout << "Frame width : " << s->width << " frame height : " << s->height << std::endl;
            return n;
    }     
};

My problem now is how could I connect source and sink?

Thx


Solution

  • Boost.Iostreams seems to be the wrong tool for the job here.

    The goal of the source and sink mechanism is to allow you to specify where data gets serialized to - for example whether you want to write to a file, a location in memory or an i/o port.

    What you want to specify is how a certain kind of data gets serialized. The correct tool in Boost for this would be Boost.Serialization.