tags:

views:

198

answers:

1

Hi, I want to define a following function:

    if(stmtToFinalize) {
     NSLog(@"Finalizing statement stmtToFinalize");
     if (sqlite3_finalize(stmtToFinalize) !=SQLITE_OK)
      NSLog(@"An error occured while trying to finalize a statement stmtToFinalize: '%s'", sqlite3_errmsg(database));
     stmtToFinalize = NULL;
    }

But I am not familiar with the #define directive. I want instead of stmtToFinalize to have any statement of type sqlite3_stmt.

How can I implement it?

Thank you.

+2  A: 

Syntax for #define is not Objective-C specific.

#define MY_FUNCTION(x) do { \
    if( (x) ) { \
        NSLog(@"Finalizing statement stmtToFinalize"); \
        if (sqlite3_finalize( (x) ) !=SQLITE_OK) \
                NSLog(@"An error occured while trying to finalize a statement stmtToFinalize: '%s'", sqlite3_errmsg(database)); \
        (x) = NULL; \
    } } while (0)

Text in NSLog() format string has not been changed.

mouviciel
Thank you, is it possible to change the text inside a string? To replace stmtToFinalize with x.
Ilya
Yes, you can replace the text 'stmtToFinalize' with the text 'x', but I doubt that it will add some value. Is there a way to get a string description of x? If yes I will update my answer.
mouviciel
I would wrap the if in a do{..} while(0) to avoid an empty statement at the end. I mean you're not going to write MY_FUNCTION(stmt) without semi-colon ? Do you want to MY_FUNCTION(stmt) else { another_function();}
LB
No, there is no way to get a string description of x. How can I replace stmtToFinalize with x? The following doesn't work: NSLog(@"Finalizing statement 'x'");
Ilya
@LB: I didn't think of MY_FUNC(x) else ... Considering this, you are right. @Ilya: I'm afraid no simple solution exists for logging the actual name of x instead of a generic text, sorry.
mouviciel