Boost logo

Boost :

From: Dietmar Kuehl (dietmar_kuehl_at_[hidden])
Date: 2000-03-07 08:55:54


Hi,

> I was aware that creating a new stream buffer was the "official" way
> - however, as all I was likely to do was to collect text into a
> string, which is precisely what a stringstream does, it seemed
> unreasonable to reimplement stringstream just for this...

Of course, you can use the important part of string streams, namely the
'std::stringbuf'. Here is a possible class for your debugging purposes:

  class odebugstream: public std::ostream {
  public:
    odebugstream(std::string const& t):
      std::ios(&m_sbuf), // virtual base of 'std::ostream'
      std::ostream(&m_sbuf),
      m_title(t)
    {
    }
    std::odebugstream& make_box() {
      ::MessageBox(m_title.c_str(), m_sbuf->str().c_str();
      m_sbuf->str("");
    }

  private:
    std::string m_title;
    std::stringbuf m_sbuf;
  };

  extern odebugstream dout;
  std::ostream& make_box(std:ostream& out) {
    dynamic_cast<odebugstream&>(out).make_box();
    return out;
  }

This class would then be used via the special output stream 'dout':

  int main() {
    dout << "hello, world!" << make_box;

    dout << "goodbye";
    dout.make_box();
  }
      
> And using a manipulator is no good, as manipulators need to work
> on ostreams, not on derived classes... [Yes, I did think pretty hard
> about all this...]

It is not that unusual that manipulators try a dynamic_cast and act
depending on the result of this cast. The above stream does, for
example, throw a bad_cast if the stream to which 'make_box' is applied
is not a 'odebugstream'.

__________________________________________________
Do You Yahoo!?
Talk to your friends online with Yahoo! Messenger.
http://im.yahoo.com


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk