Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #7218

Re: Overloading << to accept "multiple right parameters"

From Kai-Uwe Bux <jkherciueh@gmx.net>
Newsgroups comp.lang.c++
Subject Re: Overloading << to accept "multiple right parameters"
Followup-To comp.lang.c++
Date 2011-06-29 00:23 +0200
Organization University of Bielefeld
Message-ID <iudk9k$9ru$1@hoshi.visyn.net> (permalink)
References <Xns9F12D7FE582CAbubbachipsetone@130.133.4.11>

Followups directed to: comp.lang.c++

Show all headers | View raw


Bubba wrote:

> Greetings,
> 
> I don't know whether I have stipulated subject correctly, but this is what
> I need.
> 
> There is a class that consists of a STL map<string,short>, amongst other
> interfaces. Operator << should accept a string as right parameter. After
> that string, it should again be possible to enter either string or number.
> If number is entered, then it is associated with a map, if another string
> is entered, new member of map is created. Expression must end with a
> number and that number should be associated with all strings in the map.
> If it doesn't, it should throw an exception.
> 
> IOW, it should look like this:
> 
> mVar1 << "string1" << 2; //one entry in map, with values "string1",2
> mVar2 << "string2" << "string3" << "string4" << 10; //three map entries
>     //each with short
>     //valeu of 10
> 
> I'm simply clueless about what should I even Google for in order to
> implement something like that, let alone do it. Manipulating with map STL
> within the implementation is not a problem (I suppose?), but syntax and
> idea behind those multiple parameters is what bugs me...

What about something like this:


#include <iostream>
#include <vector>
#include <map>
#include <string>

class example {

  typedef std::map< std::string, int > table;

  table data;

  struct proxy {

    example * where;
    
    std::vector< std::string > stored;

    proxy ( example * ptr )
      : where ( ptr )
      , stored ()
    {}
    
    proxy & operator<< ( std::string const & str ) {
      stored.push_back( str );
      return ( *this );
    }

    proxy & operator<< ( int i ) {
      while ( ! stored.empty() ) {
	(where->data)[ stored.back() ] = i;
	stored.pop_back();
      }
      return ( *this );
    }

  };

public:

  void dump ( std::ostream & ostr ) {
    for ( table::const_iterator iter = data.begin();
	  iter != data.end(); ++iter ) {
      ostr << iter->first << " : " << iter->second << "\n";
    }
  }

  proxy operator<< ( std::string const & str ) {
    return ( proxy( this ) << str );
  }

};

int main ( void ) {
  example a;
  a << "a" << "b" << 1
    << "c" << 2;
  a.dump( std::cout );
  std::cout << "\n";
  a << "x" << "y" << 0
    << "g" << "h" << 2
    << "b" << 0;
  a.dump( std::cout );
}

  

Best,

Kai-Uwe Bux

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Overloading << to accept "multiple right parameters" Bubba <nickname@banelli.biz.invalid> - 2011-06-28 19:14 +0000
  Re: Overloading << to accept "multiple right parameters" Robert Wessel <robertwessel2@yahoo.com> - 2011-06-28 14:39 -0500
    Re: Overloading << to accept "multiple right parameters" Bubba <nickname@banelli.biz.invalid> - 2011-06-28 20:59 +0000
      Re: Overloading << to accept "multiple right parameters" Ian Collins <ian-news@hotmail.com> - 2011-06-29 09:14 +1200
  Re: Overloading << to accept "multiple right parameters" Kai-Uwe Bux <jkherciueh@gmx.net> - 2011-06-29 00:23 +0200
    Re: Overloading << to accept "multiple right parameters" Bubba <nickname@banelli.biz.invalid> - 2011-06-29 08:28 +0000
      Re: Overloading << to accept "multiple right parameters" Paavo Helde <myfirstname@osa.pri.ee> - 2011-06-29 14:31 -0500

csiph-web