stream manipulators which take arguments: HELP!

Scott Meyers sdm at cs.brown.edu
Wed Jul 25 02:28:11 AEST 1990


In article <331 at ndla.UUCP> platt at 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



More information about the Comp.unix.i386 mailing list