g2o
Loading...
Searching...
No Matches
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 42 of file stream_redirect.h.

Member Typedef Documentation

◆ int_type

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

Definition at line 44 of file stream_redirect.h.

Constructor & Destructor Documentation

◆ StreamRedirect()

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

Definition at line 34 of file stream_redirect.cpp.

35 : _stream(stream), _te(te) {
36 _old_buf = stream.rdbuf();
37 _stream.rdbuf(this);
38}
QPlainTextEdit * _te
std::streambuf * _old_buf
std::ostream & _stream

References _old_buf, and _stream.

◆ ~StreamRedirect()

StreamRedirect::~StreamRedirect ( )

Definition at line 40 of file stream_redirect.cpp.

40 {
41 if (!_buffer.empty()) xsputn(_buffer.c_str(), _buffer.size());
42 _stream.rdbuf(_old_buf);
43}
std::string _buffer
virtual std::streamsize xsputn(const char *p, std::streamsize n)

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

Member Function Documentation

◆ overflow()

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

Definition at line 45 of file stream_redirect.cpp.

45 {
46 _mutex.lock();
47 if (v == '\n') {
48 _te->appendPlainText(QString::fromLatin1(_buffer.c_str(), _buffer.size()));
49 _buffer.clear();
50 } else
51 _buffer.push_back(v);
52
53 _mutex.unlock();
54 return v;
55}

References _buffer, _mutex, and _te.

◆ xsputn()

std::streamsize StreamRedirect::xsputn ( const char *  p,
std::streamsize  n 
)
protectedvirtual

Definition at line 57 of file stream_redirect.cpp.

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

References _buffer, _mutex, and _te.

Referenced by ~StreamRedirect().

Member Data Documentation

◆ _buffer

std::string StreamRedirect::_buffer
private

Definition at line 57 of file stream_redirect.h.

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

◆ _mutex

QMutex StreamRedirect::_mutex
private

Definition at line 59 of file stream_redirect.h.

Referenced by overflow(), and xsputn().

◆ _old_buf

std::streambuf* StreamRedirect::_old_buf
private

Definition at line 56 of file stream_redirect.h.

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

◆ _stream

std::ostream& StreamRedirect::_stream
private

Definition at line 55 of file stream_redirect.h.

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

◆ _te

QPlainTextEdit* StreamRedirect::_te
private

Definition at line 58 of file stream_redirect.h.

Referenced by overflow(), and xsputn().


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