cgv
callback_stream.h
1 #pragma once
2 
3 #include <iostream>
4 #include <string>
5 
6 #include <cgv/signal/signal.h>
7 
8 namespace cgv {
9  namespace signal {
10 
12 struct 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 
47 class callback_stream : public std::ostream
48 {
49 protected:
51  char buffer[256];
52 public:
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 }
cgv::signal::callback_stream
connect to the write signal of the callback stream in order to process all text written to the stream
Definition: callback_stream.h:48
cgv::signal::callback_streambuf
simple implementation of a streambuf that sends all written text to the write signal that is referenc...
Definition: callback_stream.h:13
cgv::signal::callback_stream::callback_stream
callback_stream()
constructor sets the stream buffer of the stream to the callback_streambuf
Definition: callback_stream.h:56
cgv
the cgv namespace
Definition: vr_calib.cxx:9
cgv::signal::callback_stream::write
cgv::signal::signal< const std::string & > write
signal to which all text written to the stream is sent
Definition: callback_stream.h:54