вторник, 10 июля 2012 г.

Two usefull C macroses

As for me, there are 2 very simple but usefull C macros for safe calling of function (or method - callback in struct member):
#define SAFE_CALL(F, ...) do { if ((F)) (*(F))(##__VA_ARGS__); } while(0)
#define MCALL(SELF, CB, ...) do { if((SELF)->CB) (*(SELF)->CB)(SELF, ##__VA_ARGS__); } while(0)
Note two '#' - this is the GNU GCC extension to avoid problem with comma in variadic macros - with they we can use or not var. args. Usage is simple too:
MCALL(my_struct_ptr, onpaint, area_t* area);
Something like this... :)

Комментариев нет:

Отправить комментарий

Thanks for your posting!