cisst-saw
Loading...
Searching...
No Matches
cmnCallbackStreambuf< _element, _trait > Class Template Reference

A Streambuffer class that outputs via a callback function. More...

#include <cmnCallbackStreambuf.h>

Inheritance diagram for cmnCallbackStreambuf< _element, _trait >:

Public Types

typedef std::basic_streambuf< _element, _trait >::int_type int_type
typedef _element ElementType
typedef void(*) CallbackType(const ElementType *, int len)

Public Member Functions

 cmnCallbackStreambuf (CallbackType func)

Protected Member Functions

virtual void PrintLine ()
virtual int_type overflow (int_type c)
virtual std::streamsize xsputn (const ElementType *s, std::streamsize n)
virtual int sync ()

Protected Attributes

ElementType Buffer [256]
unsigned int Idx
CallbackType Callback

Detailed Description

template<class _element, class _trait = std::char_traits<_element>>
class cmnCallbackStreambuf< _element, _trait >

A Streambuffer class that outputs via a callback function.

This file declares class cmnCallbackStreambuf. It is a templated class derived from the standard library's basic_streambuf, with the additional feature that it outputs to a user-specified callback function. This is implemented by overriding the basic_streambuf output functions xsputn(), overflow() and sync().

Usage: Include the module in your application with: #include <cisstCommon/cmnCallbackStreambuf.h>

Specify the callback function in the constructor. Note that the callback function should accept two parameters:

  1. an array of elements to be printed (e.g., a character array)
  2. the number of elements to be printed

The callback function is invoked when a newline character is encountered or when the internal buffer is full.

This class could be used, for example, to redirect cmnLogger output to a GUI text window, assuming that the GUI text window provides a "WriteText" function. This class would not be needed, however, if the GUI text window is derived from the streambuf class.

Note: It is not clear whether the comparison to '
' (for the newline) will work if ElementType is not a char.

See also
C++ manual on basic_ostream and basic_streambuf.

Member Typedef Documentation

◆ CallbackType

template<class _element, class _trait = std::char_traits<_element>>
typedef void(*) cmnCallbackStreambuf< _element, _trait >::CallbackType(const ElementType *, int len)

Type of the callback function, e.g., void func(char *line, int len).

◆ ElementType

template<class _element, class _trait = std::char_traits<_element>>
typedef _element cmnCallbackStreambuf< _element, _trait >::ElementType

Type of the array, e.g., char.

◆ int_type

template<class _element, class _trait = std::char_traits<_element>>
typedef std::basic_streambuf<_element,_trait>::int_type cmnCallbackStreambuf< _element, _trait >::int_type

Constructor & Destructor Documentation

◆ cmnCallbackStreambuf()

template<class _element, class _trait = std::char_traits<_element>>
cmnCallbackStreambuf< _element, _trait >::cmnCallbackStreambuf ( CallbackType func)
inline

Constructor: Sets the callback function.

Parameters
funcThe callback function.

Member Function Documentation

◆ overflow()

template<class _element, class _trait = std::char_traits<_element>>
virtual int_type cmnCallbackStreambuf< _element, _trait >::overflow ( int_type c)
inlineprotectedvirtual

Override the basic_streambuf overflow to store the character in the buffer. The buffer is printed if the character is a newline or if it is full.

◆ PrintLine()

template<class _element, class _trait = std::char_traits<_element>>
virtual void cmnCallbackStreambuf< _element, _trait >::PrintLine ( )
inlineprotectedvirtual

Protected function to print the buffer (call the callback function).

◆ sync()

template<class _element, class _trait = std::char_traits<_element>>
virtual int cmnCallbackStreambuf< _element, _trait >::sync ( )
inlineprotectedvirtual

Override the basic_streambuf sync to flush (print) the buffer.

◆ xsputn()

template<class _element, class _trait = std::char_traits<_element>>
virtual std::streamsize cmnCallbackStreambuf< _element, _trait >::xsputn ( const ElementType * s,
std::streamsize n )
inlineprotectedvirtual

Override the basic_streambuf xsputn to store the characters in the buffer. The buffer is printed every time a newline is encountered or the buffer becomes full.

Member Data Documentation

◆ Buffer

template<class _element, class _trait = std::char_traits<_element>>
ElementType cmnCallbackStreambuf< _element, _trait >::Buffer[256]
protected

◆ Callback

template<class _element, class _trait = std::char_traits<_element>>
CallbackType cmnCallbackStreambuf< _element, _trait >::Callback
protected

◆ Idx

template<class _element, class _trait = std::char_traits<_element>>
unsigned int cmnCallbackStreambuf< _element, _trait >::Idx
protected

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