g2o
Public Types | Public Member Functions | Protected Member Functions | Private Attributes | List of all members
StreamRedirect Class Reference

redirect a stream to a QPlainTextEdit More...

#include <stream_redirect.h>

Inheritance diagram for StreamRedirect:
Inheritance graph
[legend]
Collaboration diagram for StreamRedirect:
Collaboration graph
[legend]

Public Types

typedef std::char_traits< char >::int_type int_type
 

Public Member Functions

 StreamRedirect (std::ostream &stream, QPlainTextEdit *te)
 
 ~StreamRedirect ()
 

Protected Member Functions

virtual std::char_traits< char >::int_type overflow (int_type v)
 
virtual std::streamsize xsputn (const char *p, std::streamsize n)
 

Private Attributes

std::ostream & _stream
 
std::streambuf * _old_buf
 
std::string _buffer
 
QPlainTextEdit * _te
 
QMutex _mutex
 

Detailed Description

redirect a stream to a QPlainTextEdit

Definition at line 32 of file stream_redirect.h.

Member Typedef Documentation

typedef std::char_traits<char>::int_type StreamRedirect::int_type

Definition at line 35 of file stream_redirect.h.

Constructor & Destructor Documentation

StreamRedirect::StreamRedirect ( std::ostream &  stream,
QPlainTextEdit *  te 
)

Definition at line 25 of file stream_redirect.cpp.

References _old_buf, and _stream.

25  :
26  _stream(stream), _te(te)
27 {
28  _old_buf = stream.rdbuf();
29  _stream.rdbuf(this);
30 }
std::streambuf * _old_buf
QPlainTextEdit * _te
std::ostream & _stream
StreamRedirect::~StreamRedirect ( )

Definition at line 32 of file stream_redirect.cpp.

References _buffer, _old_buf, _stream, and xsputn().

33 {
34  if (!_buffer.empty())
35  xsputn(_buffer.c_str(), _buffer.size());
36  _stream.rdbuf(_old_buf);
37 }
virtual std::streamsize xsputn(const char *p, std::streamsize n)
std::string _buffer
std::streambuf * _old_buf
std::ostream & _stream

Member Function Documentation

std::char_traits< char >::int_type StreamRedirect::overflow ( int_type  v)
protectedvirtual

Definition at line 39 of file stream_redirect.cpp.

References _buffer, _mutex, and _te.

40 {
41  _mutex.lock();
42  if (v == '\n') {
43  _te->appendPlainText(QString::fromLatin1(_buffer.c_str(), _buffer.size()));
44  _buffer.clear();
45  }
46  else
47  _buffer.push_back(v);
48 
49  _mutex.unlock();
50  return v;
51 }
std::string _buffer
QPlainTextEdit * _te
std::streamsize StreamRedirect::xsputn ( const char *  p,
std::streamsize  n 
)
protectedvirtual

Definition at line 53 of file stream_redirect.cpp.

References _buffer, _mutex, and _te.

Referenced by ~StreamRedirect().

54 {
55  _mutex.lock();
56  _buffer.append(p, p + n);
57 
58  std::string::size_type pos = 0;
59  while (1) {
60  pos = _buffer.find('\n');
61  if (pos != std::string::npos) {
62  _te->appendPlainText(QString::fromLatin1(_buffer.c_str(), pos));
63  _buffer.erase(_buffer.begin(), _buffer.begin() + pos + 1);
64  } else
65  break;
66  }
67 
68  _mutex.unlock();
69  return n;
70 }
std::string _buffer
QPlainTextEdit * _te

Member Data Documentation

std::string StreamRedirect::_buffer
private

Definition at line 48 of file stream_redirect.h.

Referenced by overflow(), xsputn(), and ~StreamRedirect().

QMutex StreamRedirect::_mutex
private

Definition at line 50 of file stream_redirect.h.

Referenced by overflow(), and xsputn().

std::streambuf* StreamRedirect::_old_buf
private

Definition at line 47 of file stream_redirect.h.

Referenced by StreamRedirect(), and ~StreamRedirect().

std::ostream& StreamRedirect::_stream
private

Definition at line 46 of file stream_redirect.h.

Referenced by StreamRedirect(), and ~StreamRedirect().

QPlainTextEdit* StreamRedirect::_te
private

Definition at line 49 of file stream_redirect.h.

Referenced by overflow(), and xsputn().


The documentation for this class was generated from the following files: