[comp.unix.i386] stream manipulators which take arguments: HELP!

platt@ndla.UUCP (Daniel E. Platt) (07/24/90)

Hi, 

I'd like some information on how to write stream manipulators which
takes arguments.  All of the documentation I currently have on C++ 2.0
doesn't cover it (I can't really call it 'documentation,' but its what
I have at the moment).  

If anybody could send a quick sample (perhaps a skeleton) I'd appreciate it.

Dan Platt

sdm@cs.brown.edu (Scott Meyers) (07/24/90)

In article <331@ndla.UUCP> platt@ndla.UUCP (Daniel E. Platt) writes:
>I'd like some information on how to write stream manipulators which
>takes arguments.  All of the documentation I currently have on C++ 2.0
>doesn't cover it (I can't really call it 'documentation,' but its what
>I have at the moment).  

From an application I have:

From the .h file:

    // ----------------------------------------------------------------------------
    // We need to create a manipulator taking a const int argument.  Because no
    // such manipulator exists by default, we have to instantiate one using the
    // IOMANIP macros.  These macros only understand simple typenames, so we have
    // to create a typedef for const int.  I figured out how to do this by
    // looking at the lengthdemo example that came with the CC 2.0 distribution.
    //
    // indent is an ostream manipulator that adds whitespace to s.  The amount
    // of whitespace is proportional to level.  indent is designed to format
    // hierarchical structures.  
    // ----------------------------------------------------------------------------
    typedef const int ConstInt;           // create simple typename
    IOMANIPdeclare(ConstInt);             // magic macro call #1
    OMANIP(ConstInt)
    indent(const int level);              // magic macro call #2
    ostream& indent(ostream& s,
                    const int level = 0); // this is the actual manip. function

From the .C file:

    const LEVEL_INDENT = 3;  // number of spaces to indent per level when 
                             //   printing out hierarchical information

    ostream& indent(ostream& s, const int level)
    {
      for (int i = level * LEVEL_INDENT; i > 0; i--) s << " ";
      return s;
    }

    OMANIP(ConstInt) indent(const int level)
    {
      return OMANIP(ConstInt)(indent,level);
    }

Scott

leo@atcmp.nl (Leo Willems) (07/25/90)

From article <331@ndla.UUCP>, by platt@ndla.UUCP (Daniel E. Platt):
> Hi, 
> 
> I'd like some information on how to write stream manipulators which
> takes arguments.  All of the documentation I currently have on C++ 2.0
> doesn't cover it (I can't really call it 'documentation,' but its what
> I have at the moment).  
> 
> If anybody could send a quick sample (perhaps a skeleton) I'd appreciate it.
> 
> Dan Platt

This should work:

===================================================
#include <stream.h>
#include "iomanip.h"

typedef ostream& (*spfunc)(ostream&, int);

class Iomanip{
	friend ostream& operator<<(ostream&, Iomanip);
	friend Iomanip setspeed(int);

	Iomanip(spfunc f, int s) : sf(f), speed(s){}

	spfunc sf;
	int speed;
};

ostream&
operator<<(ostream& os, Iomanip im)
{
	return im.sf(os, im.speed);
}

ostream&
iospeed(ostream& os, int speed)
{
	os << "setting speed (ioctl stuff) to: " << speed << "\n";
	return os;
}

Iomanip
setspeed(int speed)
{
	return Iomanip(iospeed, speed);
}

main()
{
	cout << setspeed(9600) << "hello\n";
}
========================================

The basic idea from this example comes from Dewhurst & Stark.
(ISBN 0-13-723156-3, Programming in C++, p 189).

Leo


 Leo Willems			Internet: leo@atcmp.nl
 AT Computing			UUCP:     mcsun!hp4nl!kunivv1!atcmpe!leo
 P. O. Box 1428				
 6501 BK  Nijmegen		Phone:    +31-80-566880
 The Netherlands		Fax:	  +31-80-555887

paul@dialogic.com (The Imaginative Moron aka Joey Pheromone) (07/26/90)

In article <638@atcmpe.atcmp.nl> leo@atcmp.nl (Leo  Willems) writes:
>From article <331@ndla.UUCP>, by platt@ndla.UUCP (Daniel E. Platt):
>> Hi, 
>> 
>> I'd like some information on how to write stream manipulators which
>> takes arguments.  All of the documentation I currently have on C++ 2.0
> [ c++ code deleted]

Please, STOP CROSS-POSTING THIS TO COMP.UNIX.I386 ! It belongs in
comp.lang.c+= ONLY !! There is enough in .i386 to keep us happy, and
this only adds to the signal-to-noise ratio.

EDIT THE GROUPS LINE WHEN YOU FOLLOW_UP !!!!!!!!!!

Paul.
--
Paul Bennett	      |  			| "I give in, to sin, because
Dialogic Corp.	      |   paul@dialogic.UUCP	|  You have to make this life
300 Littleton Road    | ..!uunet!dialogic!paul	|  livable"
Parsippany, NJ 07054  |	 			|  Martin Gore