Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

ArtOfWarfare

macrumors G3
Original poster
Nov 26, 2007
9,671
6,212
If you attempt to run a null block, your program crashes, so you need to wrap it in a check if it's null, like so:

Code:
if (block != nil) {
    block(arg);
}

I had a lot of different spots where I needed to do this kind of thing, so I ended up writing this macro:

Code:
#define CHECK_NULL_EXEC_BLOCK(BLOCK, ...) if (BLOCK != nil) BLOCK(__VA_ARGS__)

And you use it like:

Code:
CHECK_NULL_EXEC_BLOCK(block, arg);

My questions are:

Does anyone see a problem with my macro? Is there some way it can be improved? I think it may not work with blocks which take no arguments at all - what's the best way I can modify it to handle that?

Also, as some of you may be aware, I hate macros in C. They're not hygienic and lead to extremely frustrating to debug situations. But I don't think there's a way to write this without a macro... unless someone can think of that I didn't.
 
What is the significance of making it a static inline function as opposed to an ordinary function?

"inline" should be clear; the compiler may put the function's body in the calling code instead of making a call to a real function. The program is (probably) faster and (probably) bigger when you use inline. This corresponds closely to the effect of the macro.

"static" essentially hides the function definition from the linker, so its scope is only the file.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.