[DM-dev] useful C++ macros
Brought to you by:
acdalton,
henningsen
From: Stephan B. <st...@wa...> - 2001-05-28 21:47:27
|
Okay, maybe this is elementary, but... Use something like this to easily check for null pointers: #define CERR cerr << "debug: " << __FILE__ << ": " << dec << __LINE__ << ": " #define G_ASSERT(ptr, msg, ret) if(!ptr) { CERR << msg << endl; return ret; } void foo( obj * ) { G_ASSERT( obj, "no, no, no: obj may ! == (!something)", ); // note the ending: ', )' because this is a void function. ... } I'm not a fan of assert(), since it exit()s, but these fit the bill nicely as long as you're in the habit of doing error checking on return values (and you are, aren't you?). If you're writing with a UI toolkit, change G_ASSERT to launch a dialog instead. I particularly like the CERR macro, because it's a drop-in replacement for cerr/cout/clog, with the benefit of additional debug info: CERR << "use it just like cin or cerr. " << dec << 42 << "." << endl; ----- 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 |