tags:

views:

268

answers:

3

I currently use the following template simply as a way to check for NULL pointer and if NULL then print out an error message to a log file and then return false.

template< typename T >
static bool isnull(T * t, std::string name = "")
{
    _ASSERTE( t != 0 );
    if( !t )
    {
        if( !(name.length()) ) name = "pointer";
        PANTHEIOS_TRACE_ERROR(name + " is NULL");
        return false;
    }
    return true;
}

I currently call this as follows:

if( !(isnull(dim, BOOST_STRINGIZE(dim))) ) return false;

If you notice I need to pass in the "name" of the pointer-variable that I want to print to the log file, as the 2nd parameter. I am currently using BOOST_STRINGIZE that simply converts any text inside the parentheses to a string.

The following are the disadvantages of my template implementation (for my usage at least)

  • Anyone could pass in anything as parameter to BOOST_STRINGIZE to print out in log file - since the 2 parameters are not related in anyway - so I would not necessarily see the "variable name" that is actually NULL
  • We have to remember to pass in the 2nd parameter, else useless.

Is there anyway I can have the "name" of that 1st variable be automatically determined, so that I can omit passing it in, as the 2nd parameter, with every call?

+8  A: 

You could put it all in one macro:

#define IS_NULL(name_) isnull(name_, #name_)

Note that BOOST_STRINGIZE expands its argument if its a macro, which may or may not be what you want:

#define X(x_) std::cout << BOOST_STRINGIZE(x_) << " = " << x_ << std::endl;
X(NULL); // prints: "0 = 0"
Georg Fritzsche
You want a stringify macro like `BOOST_STRINGIZE`, or it'll always pass "name_".
GMan
How so, `#` stringifies `name_`? The difference of `BOOST_STRINGIZE` is only that it expands `name_` if its a macro which i don't neccessarily consider an advantage. :)
Georg Fritzsche
I was thinking of something else :3 Both my comments will mysteriously disappear in a unspecified amount of time.
GMan
Wow that was simple. I almost feel dumb to ask such a question. I initially was thinking some complicated Boost Preprocessor stuff. Thanks.
ossandcad
@ossabdcad: *no one* pays enough attention the fist time they are introduced to the c pre-processor, and they don't really study it until *after* they've spent far to long beating their head against some problem like this. Here's you certificate of club membership. Take two aspirin and come back in the morning...
dmckee
+1  A: 

Sure, why not:

#define new_isnull(x)  isnull(x, BOOST_STRINGIZE(x))
Carl Norum
Thanks. using # instead of BOOST_STRINGIZE was an advantage of the accepted answer.
ossandcad
+1  A: 

The only way to do anything lexically like this is with macros. If you always want the correct printout, your best option is to wrap the whole statement in a macro:

//if( !(isnull(dim, BOOST_STRINGIZE(dim))) ) return false;
#define ISNULL(a) isnull((a), #a)
if (!ISNULL(dim)) return false;

Note that, as always, macros have a number of disadvantages associated with them.

Dan Olson