9 #ifndef STK_UTIL_UTIL_TEESTREAMBUF_HPP 10 #define STK_UTIL_UTIL_TEESTREAMBUF_HPP 27 template<
class Ch,
class Tr = std::
char_traits<Ch> >
28 class basic_tee_streambuf :
public std::basic_streambuf<Ch, Tr>
30 typedef std::set<std::ostream *> StreamSet;
31 typedef std::map<std::ostream *, int> StreamErrorMap;
63 return std::basic_streambuf<Ch, Tr>::traits_type::eof();
72 void add(std::ostream *os) {
73 m_destinations.insert(os);
83 void remove(std::ostream *os) {
84 m_destinations.erase(os);
92 m_destinations.clear();
102 if (m_destinations.empty())
105 StreamErrorMap return_code;
107 for (StreamSet::const_iterator it = m_destinations.begin(); it != m_destinations.end(); ++it) {
108 if ((*it)->rdbuf() !=
this) {
109 int ret = (*it)->rdbuf()->pubsync();
110 return_code[*it] = ret;
115 for (StreamSet::iterator it = m_destinations.begin(); it != m_destinations.end(); ++it)
116 if (return_code[*it] ==
eof())
117 m_destinations.erase(it);
119 if (m_destinations.empty())
133 virtual typename std::basic_streambuf<Ch, Tr>::int_type overflow(
const int c) {
134 if (m_destinations.empty())
137 StreamErrorMap return_code;
139 for (StreamSet::const_iterator it = m_destinations.begin(); it != m_destinations.end(); ++it) {
140 int ret = (*it)->rdbuf()->sputc(c);
141 return_code[*it] = ret;
145 for (StreamSet::iterator it = m_destinations.begin(); it != m_destinations.end(); ++it)
146 if (return_code[*it] ==
eof())
147 m_destinations.erase(it);
149 if (m_destinations.empty())
164 virtual std::streamsize xsputn(
char const *buffer, std::streamsize n) {
165 if (m_destinations.empty())
168 StreamErrorMap return_code;
170 for (StreamSet::const_iterator it = m_destinations.begin(); it != m_destinations.end(); ++it) {
171 std::ostream *os = (*it);
172 int ret = os->rdbuf()->sputn(buffer,n);
173 return_code[*it] = ret;
177 for (StreamSet::iterator it = m_destinations.begin(); it != m_destinations.end(); ++it) {
178 if (return_code[*it] < 0) {
179 m_destinations.erase(it);
183 if (m_destinations.empty())
190 StreamSet m_destinations;
197 #endif // STK_UTIL_UTIL_TEESTREAMBUF_HPP
stk_classic::basic_tee_streambuf< char, std::char_traits< char > > tee_streambuf
Tee stream buffer for char.
virtual ~basic_tee_streambuf()
Class basic_tee_streambuf maintains a list of destination output stream buffers to send written chara...
void clear()
Member function clear removes are destination output stream buffers.
int eof()
Member function eof returns the current end-of-file status.
void add(std::ostream *os)
Member function add adds the specified destination output stream buffer.
basic_tee_streambuf(std::basic_ostream< Ch, Tr > *os)