Boost logo

Boost :

From: Larry Evans (jcampbell3_at_[hidden])
Date: 2002-01-24 10:02:19


"Schoenborn, Oliver" wrote:

> yes definitely but I would change the interface
>

Here's the interface:
<--------marg_ostream.hpp --------------------
#ifndef marg_ostream_hpp__
#define marg_ostream_hpp__
//ChangeLog:
// 98-11-21.1512 evansl
// WHAT:
// Added operator<<'s for standard types and manipulators.
// WHY:
// So operator<< could be cascaded and return marg_ostream& instead of ostream&
// for standard types.
// 98-12-04.0948 evansl
// WHAT:
// changed all ostream::operator<<(n)'s to ostream&me=*this;me<<n.
// WHY:
// when printing char*, getting pointer value, not character string value.
// 2001-03-27.2104: Larry Evans
// WHAT:
// tried rm'ing all operator<< to see if superclass ostream would handle it.
// RESULT:
// compile time error. See previous version.
// CONSEQUENCE:
// restored code to previous version.
#include "marg_ostreambuf.hpp"
#include "ostream_buf.hpp"
  class
marg_ostream
  : public ostream_buf<marg_ostreambuf>
  {
  public:
    marg_ostream(ostream& sout, unsigned a_ind=2)
      ;
      marg_ostream&
    operator++(void)
      ;
      marg_ostream&
    operator--(void)
      ;
      marg_ostream&
    margin(const char* s)
      ;
      int
    get_ind(void)const
      { return m_ind
      ;}
      marg_ostream&
    marg_flush(void)
      { this->tmpl_buf()->pubsync()
      ; this->tmpl_buf()->pubsync_fwdbuf()
      ; return *this
      ;}
      marg_ostream& operator<<
          (ostream& (*n)(ostream&))
          {ostream&me=*this;me<<n;return *this;}
      marg_ostream& operator<<
          (marg_ostream& (*n)(marg_ostream&))
          { return n(*this);}
      marg_ostream& operator<<(bool n){ostream&me=*this;me<<n;return *this;}
      marg_ostream& operator<<(short n){ostream&me=*this;me<<n;return *this;}
      marg_ostream& operator<<(unsigned short n){ostream&me=*this;me<<n;return *this;}
      marg_ostream& operator<<(int n){ostream&me=*this;me<<n;return *this;}
      marg_ostream& operator<<(unsigned int n){ostream&me=*this;me<<n;return *this;}
      marg_ostream& operator<<(long n){ostream&me=*this;me<<n;return *this;}
      marg_ostream& operator<<(unsigned long n){ostream&me=*this;me<<n;return *this;}
      marg_ostream& operator<<(float n){ostream&me=*this;me<<n;return *this;}
      marg_ostream& operator<<(double n){ostream&me=*this;me<<n;return *this;}
      marg_ostream& operator<<(long double n){ostream&me=*this;me<<n;return *this;}
      marg_ostream& operator<<(void* n){ostream&me=*this;me<<n;return *this;}
      marg_ostream& operator<<(const char* n){ostream&me=*this;me<<n;return *this;}
      marg_ostream& operator<<(const unsigned char* n){ostream&me=*this;me<<n;return *this;}
      marg_ostream& operator<<(const string& n){ostream&me=*this;me<<n;return *this;}
      marg_ostream& operator<<(char n){ostream&me=*this;me<<n;return *this;}
  private:
      marg_buffer_put&
    m_margin
      ;
      unsigned
    m_ind
      ;
  };//end marg_ostream class
    inline
  marg_ostream&
marg_flush(marg_ostream& mos)
  { return mos.marg_flush()
  ;}
#endif
>--------marg_ostream.hpp --------------------

What would you like changed? I could also use suggestions on how to avoid
all the operator<< for primitive types (e.g. char, double).

I noticed my previous post on this subject was incomplete; so, here's a repeat w/o
attachments (since they seem to be part of the problem). The other problem may
be variable width fonts.
<--------main_cout.cpp --------------------
#include "marg_ostream.hpp"
  int
main(void)
  { const char* line = "line"
  ; int num=0
  ; ostream& fout=cout
  ; fout<<num++<<endl
  ; fout<<line<<endl
  ; fout<<"begin marg_test"<<endl
  ; {
    ; marg_ostream mout(fout)
    ; mout<<num++<<endl
    ; mout<<"????"<<endl
    ; ++mout
    ; mout<<num++<<endl
    ; mout<<line<<endl
    ; ++mout
    ; mout.margin("xxx")
    ; mout<<num++<<endl
    ; mout<<line<<endl
    ; --mout
    ; mout<<num++<<endl
    ; mout<<"!!!"<<endl
    ; --mout
    ; mout.margin("")
    ; mout<<num++<<endl
    ; mout<<line<<endl
    ; {
      ; unsigned const n=5
      ; char const* long_line =
"1234567890123456789012345678901234567890123456789012345678901234567890"
      ; mout<<n<<" long lines="<<endl
      ; for(unsigned i=0; i<n; ++i)
        { mout<<long_line<<endl
        ;}
      ;}
    ;}
  ; fout<<"end marg_test"<<endl
  ; fout<<num++<<endl
  ; fout<<line<<endl
  ; return 0
  ;}
>--------main_cout.cpp --------------------
<--------main_cout.exp --------------------
0
line
begin marg_test
1
????
  2
  line
 xxx3
 xxxline
xxx4
xxx!!!
5
line
5 long lines=
1234567890123456789012345678901234567890123456789012345678901234567890
1234567890123456789012345678901234567890123456789012345678901234567890
1234567890123456789012345678901234567890123456789012345678901234567890
1234567890123456789012345678901234567890123456789012345678901234567890
1234567890123456789012345678901234567890123456789012345678901234567890
end marg_test
6
line
>--------main_cout.exp --------------------


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