cisst-saw
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Public Types | Public Member Functions | Protected Member Functions | List of all members
cmnMultiplexerStreambuf< _element, _trait > Class Template Reference

A Streambuffer class that allows output to multiple streambuf objects. More...

#include <cmnMultiplexerStreambuf.h>

Inheritance diagram for cmnMultiplexerStreambuf< _element, _trait >:

Public Types

typedef std::basic_streambuf
< _element, _trait > 
ChannelType
 
typedef std::basic_streambuf
< _element, _trait >::int_type 
int_type
 
typedef std::list< ChannelType * > ChannelContainerType
 

Public Member Functions

 cmnMultiplexerStreambuf ()
 
void AddChannel (ChannelType *channel)
 
void RemoveChannel (ChannelType *channel)
 
const ChannelContainerTypeGetChannels () const
 

Protected Member Functions

virtual std::streamsize xsputn (const _element *s, std::streamsize n)
 
virtual int sync ()
 
virtual int_type overflow (int_type c=_trait::eof())
 

Detailed Description

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

A Streambuffer class that allows output to multiple streambuf objects.

Types for dynamic control of output messages. This includes debugging information, error reporting, state logging etc. This file declares class cmnMultiplexerStreambuf. It is a templated class derived from the standard library's basic_streambuf, with the main additional feature that enables channeling the output to multiple other streambuf objects. The multiplexing is implemented by overriding basic_streambuf output functions xsputn(), overflow() and sync().

Usage: Include the module in your application with: #include "cmnMultiplexerStreambuf.h"

Add output streambuf channels to the multiplexer using the AddChannel() method.

Remove output channels from the multiplexer using the RemoveChannel() method.

Attach a cmnMultiplexerStreambuf object with an ostream. The output functions << , put(), write() etc will operate directly on the cmnMultiplexerStreambuf.

Example of using a cmnOutputMultiplexer, which is an ostream using a cmnMultiplexerStrembuf (an output stream with multiplexer streambuf):

ofstream log("logfile.txt");
windowoutputstream display; // hypothesized class
multiplexer.AddChannel(&log);
multiplexer.AddChannel(&windowoutputstream);
multiplexer << "Hello, world" << endl; // channel the message to all streams.

Notes:

  1. cmnMultiplexerStreambuf does not OWN the output channels. They are created and destroyed externally.
  2. The initial implementation uses a list to store the addresses of the output channels. There is no guarantee on the order of storage or the order of output channelling.
  3. It is assumed that none of the output channels modifies the data arguments.
  4. It is guaranteed that unique streambuf instances are stored in a single cmnMultiplexerStreambuf. The AddChannel() function checks for uniqueness.
  5. cmnMultiplexerStreambuf does not buffer data. Instead, whenever at attempt is made to use stream::put() on a stream with a multiplexer streambuf, the cmnMultiplexerStreambuf::overflow() is called automatically, and it forwards the character to the output channels.

    See Also
    C++ manual on basic_ostream and basic_streambuf. cmnOutputMultiplexer.h

Member Typedef Documentation

template<class _element, class _trait = std::char_traits<_element>>
typedef std::list<ChannelType *> cmnMultiplexerStreambuf< _element, _trait >::ChannelContainerType

Type of internal data structure storing the channels.

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

Constructor & Destructor Documentation

template<class _element, class _trait = std::char_traits<_element>>
cmnMultiplexerStreambuf< _element, _trait >::cmnMultiplexerStreambuf ( )
inline

Constructor: currently empty.

Member Function Documentation

template<class _element , class _trait >
void cmnMultiplexerStreambuf< _element, _trait >::AddChannel ( ChannelType channel)

Add an output channel. See notes above.

Parameters
channelA pointer to the output channel to be added.
template<class _element, class _trait = std::char_traits<_element>>
const ChannelContainerType& cmnMultiplexerStreambuf< _element, _trait >::GetChannels ( void  ) const
inline

Enable access to the channel storage, without addition or removal of channels. Elements of the container can be accessed using the standard const_iterator interfaces. Note that the channels themselves are non-const, so individual manipulation of each is enabled.

template<class _element , class _trait >
cmnMultiplexerStreambuf< _element, _trait >::int_type cmnMultiplexerStreambuf< _element, _trait >::overflow ( int_type  c = _trait::eof())
protectedvirtual

Override the basic_streambuf overflow for multiplexing. overflow() is called when sputc() discovers it does not have space in the storage buffer. In our case, it's always. See more on it in the basic_streambuf documentation.

template<class _element , class _trait >
void cmnMultiplexerStreambuf< _element, _trait >::RemoveChannel ( ChannelType channel)

Remove an output channel.

Parameters
channelA pointer to the output channel to be removed. No change occurs if the pointer is not on the list of channels for this multiplexer.
template<class _element , class _trait >
int cmnMultiplexerStreambuf< _element, _trait >::sync ( void  )
protectedvirtual

Override the basic_streambuf sync for multiplexing.

template<class _element , class _trait >
std::streamsize cmnMultiplexerStreambuf< _element, _trait >::xsputn ( const _element *  s,
std::streamsize  n 
)
protectedvirtual

Override the basic_streambuf xsputn to do the multiplexing.


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