cisst-saw
Loading...
Searching...
No Matches
cmnMultiplexerStreambuf.h
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/* ex: set filetype=cpp softtabstop=4 shiftwidth=4 tabstop=4 cindent expandtab: */
3
4/*
5
6 Author(s): Ofri Sadowsky
7 Created on: 2002-04-19
8
9 (C) Copyright 2002-2007 Johns Hopkins University (JHU), All Rights
10 Reserved.
11
12--- begin cisst license - do not edit ---
13
14This software is provided "as is" under an open source license, with
15no warranty. The complete license can be found in license.txt and
16http://www.cisst.org/cisst/license.txt.
17
18--- end cisst license ---
19
20*/
21
22
26#pragma once
27
28#ifndef _cmnMultiplexerStreambuf_h
29#define _cmnMultiplexerStreambuf_h
30
32
33#include <list>
34#include <algorithm>
35
93template<class _element, class _trait = std::char_traits<_element> >
94class cmnMultiplexerStreambuf : public std::basic_streambuf<_element, _trait>
95{
96 public:
97
98 typedef std::basic_streambuf<_element, _trait> ChannelType;
99 typedef typename std::basic_streambuf<_element, _trait>::int_type int_type;
100
104 typedef std::list<ChannelType *> ChannelContainerType;
105
111
115 void AddChannel(ChannelType *channel);
116
122 void RemoveChannel(ChannelType * channel);
123
130 {
131 return m_ChannelContainer;
132 }
133
134
135 // Here we override the basic_streambuf methods for multiplexing.
136 // This part is declared 'protected' following the declarations of
137 // basic_streambuf. See basic_streambuf documentation for more
138 // details.
139 protected:
140
142 virtual std::streamsize xsputn(const _element *s, std::streamsize n);
143
145 virtual int sync();
146
151 virtual int_type overflow(int_type c = _trait::eof());
152
153 private:
155 ChannelContainerType m_ChannelContainer;
156
157};
158
159
160//********************************
161// Template method implementation
162//********************************
163
164
165template<class _element, class _trait>
167{
168 typename ChannelContainerType::iterator it = find(m_ChannelContainer.begin(), m_ChannelContainer.end(),
169 channel);
170
171 if (it == m_ChannelContainer.end()) {
172 m_ChannelContainer.insert(it, channel);
173 }
174}
175
176
177template<class _element, class _trait>
179{
180 m_ChannelContainer.remove(channel);
181}
182
183
184template<class _element, class _trait>
185std::streamsize cmnMultiplexerStreambuf<_element, _trait>::xsputn(const _element *s, std::streamsize n)
186{
187 std::streamsize ssize;
188 typename ChannelContainerType::iterator it;
189 for (it = m_ChannelContainer.begin(); it != m_ChannelContainer.end(); it++) {
190 ssize = (*it)->sputn(s, n);
191 }
192 return ssize;
193}
194
195
196template<class _element, class _trait>
198{
199 typename ChannelContainerType::iterator it;
200 // synchronize all the channels
201 for (it = m_ChannelContainer.begin(); it != m_ChannelContainer.end(); it++) {
202 (*it)->pubsync();
203 }
204 return 0;
205}
206
207
208template<class _element, class _trait>
211{
212 // follow the basic_streambuf standard
213 if (_trait::eq_int_type(_trait::eof(), c))
214 return (_trait::not_eof(c));
215
216 typename ChannelContainerType::iterator it;
217
218 // multiplexing
219 for (it = m_ChannelContainer.begin(); it != m_ChannelContainer.end(); it++) {
220 (*it)->sputc(_trait::to_char_type(c));
221 }
222
223 // follow the basic_streambuf standard
224 return _trait::not_eof(c);
225}
226
227
228#endif // _cmnMultiplexerStreambuf_h
229
std::basic_streambuf< _element, _trait >::int_type int_type
Definition cmnMultiplexerStreambuf.h:99
virtual std::streamsize xsputn(const _element *s, std::streamsize n)
Definition cmnMultiplexerStreambuf.h:185
void AddChannel(ChannelType *channel)
Definition cmnMultiplexerStreambuf.h:166
std::list< ChannelType * > ChannelContainerType
Definition cmnMultiplexerStreambuf.h:104
virtual int sync()
Definition cmnMultiplexerStreambuf.h:197
cmnMultiplexerStreambuf()
Definition cmnMultiplexerStreambuf.h:109
std::basic_streambuf< _element, _trait > ChannelType
Definition cmnMultiplexerStreambuf.h:98
virtual int_type overflow(int_type c=_trait::eof())
Definition cmnMultiplexerStreambuf.h:210
const ChannelContainerType & GetChannels() const
Definition cmnMultiplexerStreambuf.h:129
void RemoveChannel(ChannelType *channel)
Definition cmnMultiplexerStreambuf.h:178
Portability across compilers and operating systems tools.