#pragma once #include #include #include namespace cgv { namespace signal { /// simple implementation of a streambuf that sends all written text to the write signal that is referenced from the possessing stream struct callback_streambuf : public std::streambuf { cgv::signal::signal& write; callback_streambuf(cgv::signal::signal& _write) : write(_write) { } std::streambuf *setbuf(char_type *buf, std::streamsize n) { setp(buf,buf+n-1); return this; } int_type overflow(int_type c) { if (c != std::char_traits::eof()) { *pptr() = c; pbump(1); } int num = static_cast(pptr()-pbase()); std::string text(pbase(),num); write(text); pbump(-num); return c; } int sync() { int num = static_cast(pptr()-pbase()); std::string text(pbase(),num); write(text); pbump(-num); return num; } }; /// connect to the write signal of the callback stream in order to process all text written to the stream class callback_stream : public std::ostream { protected: callback_streambuf buf; char buffer[256]; public: /// signal to which all text written to the stream is sent cgv::signal::signal write; /// constructor sets the stream buffer of the stream to the callback_streambuf callback_stream() : std::ostream(0), buf(write) { buf.setbuf(buffer,256); init(&buf); } }; } }