🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c++
Article #2470

Re: Streams Library

#2470
From: Juergen Fuhrmann
Date: Wed, 08 Nov 1995 18:26
124 lines
4868 bytes

>[ Moderator's note: This post is an incarnation of the
>  Frequently Asked Question "Why aren't the iostream
>  operators << and >> declared virtual?"  It would be great
>  if someone would write a clear, concise, and coherent
>  answer to this question for inclusion in the FAQ list.  -
>  moderator (fjh).  ]

    Tiaan> I have a question regarding the streams library's << and >>
    Tiaan> operators for the standard C types. I want to write a class
    Tiaan> derived from i/ostream and I want to overload all the
    Tiaan> operators and member functions. The reason I want to do
    Tiaan> this is because my stream will be a network stream and I
    Tiaan> want to convert the relevant types to network byte order by
    Tiaan> calling the network library's functions inside the ostream&
    Tiaan> << ( int ) and whatever member functions of i/ostream
    Tiaan> wherever byte order will cause a problem.  Furthermore I
    Tiaan> want to make my own structs streamable in my program by
    Tiaan> writing standalone functions taking a i/ostream and my
    Tiaan> struct as parameter and streaming it as I want to. The
    Tiaan> i/ostream parameter is passed by reference by using the &
    Tiaan> designator causing dynamic binding should a derived class
    Tiaan> of iostream be passed in the place of a standard stream
    Tiaan> such as fstream. The operators however were not declared
    Tiaan> virtual making what I want to do impossible. Is there any
    Tiaan> specific reason for those operators not being virtual ? I
    Tiaan> don't want to go and redefine stream operators for my
    Tiaan> structs for both standard C++ streams and my own custom
    Tiaan> streams seperately.

I have the problem that I want to double the output stream, i.e.
to write everything to a protocol file and to the screen (tee(1) would
damage my signal handling). Furthermore, I want to prefix each output line
by a prefix defined per stream and I want to be able to switch  streams on and off.
There are some examples in Stroustrup's 2nd edition which led me to the
solution attached below which allows  me to use ostream's << operators.

And now, to be not off-topic, how much can I rely on interna of the streams lib
as a virtual overflow(int c)  being standard ? (I compiled this with all compilers at hand
(Dec, SGI, g++ and it worked).

Juergen Fuhrmann

   Juergen Fuhrmann                          fuhrmann@wias-berlin.de
   Weierstrass Institute for Applied Analysis and Stochastics (WIAS)  
   Mohrenstrasse          39                 D-10117 Berlin, Germany     
   phone: (49) 030 20377 560                 fax:   (49) 030 2044975   
                                          
                    *  Bad News is No News *
                   
                   


//////////////////////////////////////////////////

class DebugStream : public streambuf, public ostream
{
  static ostream *prt;
  static ostream *out;
  int doprefix;
  int  useprefix;
  char *prefix;
  ...

  DebugStream(char *xname,int initial_state) : 
  streambuf(),
  ostream(this)
    {
     ... 
    }

  overflow (int c) // this is the overflow routine from streambuf which _is_ virtual
    {
      if (!is_on) return 0;
      if (doprefix)
	{
	  if (out) *out<< prefix;
	  if (prt) *prt<< prefix;
	  doprefix=0;
	}
      
      if ((char)c=='\n')
	{
      if (out)
        {
	  
          *out << '\n';
          out->flush();
        }
      if (prt)
        {
          *prt << '\n';
          prt->flush();
        }
      if (useprefix) doprefix=1;
    }
      else
	{
	  if (out) *out<<(char)c;
	  if (prt) *prt<<(char)c;
	}
      return 0;
    }
  
}

--
   Juergen Fuhrmann                          fuhrmann@wias-berlin.de
   Weierstrass Institute for Applied Analysis and Stochastics (WIAS)  
   Mohrenstrasse          39                 D-10117 Berlin, Germany     
   phone: (49) 030 20377 560                 fax:   (49) 030 2044975   
                                          
                    *  Bad News is No News *
                   
                   




---
[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Technical Details
Message-ID: <FUHRMANN.95Nov8145959@weyl.wias-berlin.de>
Path: arc1-ca.pugleaf.net!128.32.155.1!matt
References: <Pine.LNX.3.91.951107170313.3222A-100000@tiaan.inetsys.alt.za>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!128.32.155.1!matt
From: fuhrmann@weyl.wias-berlin.de (Juergen Fuhrmann)
Newsgroups: comp.std.c++
Subject: Re: Streams Library
Date: 08 Nov 1995 18:26:09 GMT
Organization: Technical University Berlin, Germany
Approved: matt@physics2.Berkeley.EDU (Matt Austern)
Message-ID: <FUHRMANN.95Nov8145959@weyl.wias-berlin.de>
References: <Pine.LNX.3.91.951107170313.3222A-100000@tiaan.inetsys.alt.za>
NNTP-Posting-Host: physics2.berkeley.edu