Skip to content

Instantly share code, notes, and snippets.

@itfrombit
Created June 18, 2011 16:41
Show Gist options
  • Save itfrombit/1033259 to your computer and use it in GitHub Desktop.
Save itfrombit/1033259 to your computer and use it in GitHub Desktop.
Handler generator
static IMP handler_returning_void(void* userdata)
{
return imp_implementationWithBlock(^(id receiver, ...) {
struct handler_description description;
description.handler = NULL;
description.description = userdata;
va_list ap;
va_start(ap, receiver);
nu_handler(0, &description, receiver, ap);
});
}
#define MAKE_HANDLER_WITH_TYPE(type) \
static IMP handler_returning_ ## type (void* userdata) \
{ \
return imp_implementationWithBlock(^(id receiver, ...) { \
struct handler_description description; \
description.handler = NULL; \
description.description = userdata; \
va_list ap; \
va_start(ap, receiver); \
type result; \
nu_handler(&result, &description, receiver, ap); \
return result; \
}); \
}
MAKE_HANDLER_WITH_TYPE(id)
MAKE_HANDLER_WITH_TYPE(int)
MAKE_HANDLER_WITH_TYPE(bool)
MAKE_HANDLER_WITH_TYPE(float)
MAKE_HANDLER_WITH_TYPE(double)
MAKE_HANDLER_WITH_TYPE(CGRect)
MAKE_HANDLER_WITH_TYPE(CGPoint)
MAKE_HANDLER_WITH_TYPE(CGSize)
MAKE_HANDLER_WITH_TYPE(NSRange)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment