[DM-dev] basic_istream
Brought to you by:
acdalton,
henningsen
From: Stephan B. <st...@wa...> - 2001-06-09 15:18:36
|
Good afternoon, all, Looking at the basic_istream STL class, I think this will be a better replacement for all FILE operations we do. For one, we could overload the << and >> operators, so we could do something like this: DungeonElement *de = 0; mystream >> de; // parses out x,y,w,h, etc. This would also allow us to use the same code for console and file i/o, by simply using different streams. You could even then do: cout << "enter dungeon item parameters (x,y,w,h): "; cinn >> de; if( de->isValid() )... // assume it sets a 'valid' flag if this works, since we can't (or shouldn't, actually, per Scott Meyers) return values in the normal sense from an operator overload. See: http://stephan.rootonfire.org/.cpp/istream.html#basic_iostream Overloading operators sounds hard, but it's not. Here's an overloaded operator from QUB: declaration: friend ostream & operator << (ostream&, const GCom &); implementation: operator<<(ostream &os, const GCom &gc) { os << gc.toString().data(); return os; } Some text from the docs: The functions:basic_istream& operator>>(short& val); basic_istream& operator>>(unsigned short& val); basic_istream& operator>>(int& val); basic_istream& operator>>(unsigned int& val); basic_istream& operator>>(long& val); basic_istream& operator>>(unsigned long& val); basic_istream& operator>>(void *& val); each extract a field and convert it to a numeric value by calling use_facet<num_get<Elem, InIt>(getloc()). get(InIt( rdbuf()), Init(0), *this, getloc(), val). Here, InIt is defined as istreambuf_iterator<Elem, Tr>, and val has type long, unsigned long, or void * as needed. Geez, they don't make these things simple to read. The top part, fine, but: use_facet<num_get<Elem, InIt>(getloc()). get(InIt( rdbuf()), Init(0), *this, getloc(), val) Actually... that text is incorrect. They left out a > somewhere. Ah, well, I can't make any sense out of multiply-nested template stuff, anyway. It confuses me. If you think about it, to subclass a template class it to have a child of something which doesn't exist until the child does. Unrelated: I've added <string.h> to DungeonMaker.cpp in the new tree so it'll build on Mandrake. On my system this is taken care of by <string>, it appears. ----- Stephan Beal - st...@wa... http://qub.sourceforge.net - http://gwm.rootonfire.org http://byoo.rootonfire.org - http://dungeonmaker.sourceforge.net "Now I'm not normally the kind of person who likes to trespass, but sometimes you just find yourself over the line." -- Bob Dylan |