cgv
Loading...
Searching...
No Matches
callback_stream.h
1#pragma once
2
3#include <iostream>
4#include <string>
5
6#include <cgv/signal/signal.h>
7
8namespace cgv {
9 namespace signal {
10
12struct callback_streambuf : public std::streambuf
13{
14 cgv::signal::signal<const std::string&>& write;
15
16 callback_streambuf(cgv::signal::signal<const std::string&>& _write) : write(_write)
17 {
18 }
19 std::streambuf *setbuf(char_type *buf, std::streamsize n)
20 {
21 setp(buf,buf+n-1);
22 return this;
23 }
24 int_type overflow(int_type c)
25 {
26 if (c != std::char_traits<char>::eof()) {
27 *pptr() = c;
28 pbump(1);
29 }
30 int num = static_cast<int>(pptr()-pbase());
31 std::string text(pbase(),num);
32 write(text);
33 pbump(-num);
34 return c;
35 }
36 int sync()
37 {
38 int num = static_cast<int>(pptr()-pbase());
39 std::string text(pbase(),num);
40 write(text);
41 pbump(-num);
42 return num;
43 }
44};
45
47class callback_stream : public std::ostream
48{
49protected:
51 char buffer[256];
52public:
54 cgv::signal::signal<const std::string&> write;
56 callback_stream() : std::ostream(0), buf(write)
57 {
58 buf.setbuf(buffer,256);
59 init(&buf);
60 }
61};
62
63 }
64}
connect to the write signal of the callback stream in order to process all text written to the stream
callback_stream()
constructor sets the stream buffer of the stream to the callback_streambuf
cgv::signal::signal< const std::string & > write
signal to which all text written to the stream is sent
the cgv namespace
Definition print.h:11
simple implementation of a streambuf that sends all written text to the write signal that is referenc...