TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: CAMERON CLARK
from: DARIN MCBRIDE
date: 1997-07-29 20:16:00
subject: exceptions in c++

 -=> Quoting Cameron Clark to Darin Mcbride
 DM> void GetToken(
 DM>     if (is)
 DM>       if (isspace(c))
 DM>   } while (token.length() < MAX_TOKEN_LEN);
 CC> You've got it down to 3(logical) check per character.
 CC> Here's what I eventually came up with:
Not really.  None of the above checks are really difficult.
if (is)
This is generally an inline function returning an internal state.
if (isspace(c))
This is generally a lookup table.  And most often inline (i.e., macro-based
on some compilers!).
token.length()
No runtime penalty here - the token's length was determined as we added
characters to it.  Strings often have constant-time length determination
since they usually have a member variable keeping track of it.
 CC> void Token::getVariable() {
 CC> char c;
 CC> do {
 CC> inf->get(c);
 CC> if (inf->gcount() != 1) break;
 CC> if (cur>=30)            break;
 CC> if (!isMyChar(c))       {inf->putback(c);break;};
 CC> token[cur++] = c;
 CC> } while (1);
 CC> }
Where'd inf come from?  You still have three checks, and isMyChar isn't
necessarily a light-weight function.
Using inf->gcount() is no different than checking for EOF afterwards.
 CC> It's heavily unstructured, yet more readable than any structured
 CC> rule of thumb I could come up with. My goals are usually "easily
 CC> readable" over most others.
Usually structured is more easily readable than others.
 CC> Here's the unrefined "eatWhite" procedure:
 CC> void Token::eatWhite() {
 CC> // loop while there's a space (or space creating character)
 CC> char c;
 CC> do {
 CC> inf->get(c);
 CC> if (c == '\n') linenum++;      // keep track of new lines
 CC> } while (strContains(" \t\n",c) &&
 CC> (inf->gcount() ==1));
 CC> if (inf->gcount() == 1) inf->putback(c);
 CC> }
strContains isn't a light function, either.  Use isspace.
 DM> Generally I/O is slower than CPU-bound activity anyway.  Whereever 
ou're
 DM> reading from (whether disk, floppy, modem, named pipe, whatever), three
 DM> checks will likely be faster than the average is.getc() call.
 CC> I just need to know how to override the "get()" call to throw and
 CC> exception.
You really don't need to throw an exception.  Nothing exceptional has
happened...
 CC> myifstream::get(char& c) {
 CC> ifstream::get(c);
 CC> if (ifstream::gcount() == 0)  "throw EOF exception";
 CC> }
If get() isn't overridable, you have to create your own streambuf.  Override
sync(), I think, to throw your exception.  However, the need is small: the
iostream handles EOF quite well already.
... And the only thing the Borg left behind was Windows '95.
--- FastEcho 1.46
---------------
* Origin: House of Fire BBS - Toronto - (416)601-0085 - v.34 (1:250/536)

SOURCE: echomail via exec-pc

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.