I would probably start by implementing a streaming device and then wrap it in boost :: iostream. Take a look at boost :: iostreams and use this as a starter:
#include <iosfwd> // streamsize, seekdir #include <boost/iostreams/categories.hpp> // seekable_device_tag #include <boost/iostreams/positioning.hpp> // stream_offset #include <boost/function.hpp> class MyDevice { public: typedef boost::function<void()> Callback; // or whatever the signature should be typedef char char_type; typedef boost::iostreams::seekable_device_tag category; explicit MyDevice(Callback &callback); std::streamsize read(char* s, std::streamsize n); std::streamsize write(const char* s, std::streamsize n); std::streampos seek(boost::iostreams::stream_offset off, std::ios_base::seekdir way); private: MyDevice(); Callback myCallback; };
This will be the main declaration. You will need to define in your .cpp file how each function will be implemented. One of these functions can be implemented as follows:
std::streampos MyDevice::write(const char* s, std::streamsize n) {
Then for use from other sources, for example. in your main function:
Callback callback; // some function MyDevice device(callback); boost::iostreams::stream<MyDevice> stream(device); stream << data; // etc.
Ben j source share