Skip to content

Instantly share code, notes, and snippets.

@jasonroelofs
Created January 22, 2010 19:13
Show Gist options
  • Save jasonroelofs/284049 to your computer and use it in GitHub Desktop.
Save jasonroelofs/284049 to your computer and use it in GitHub Desktop.
static int
genericCallback(cpArbiter *arb, cpSpace * space,
void * data, ID func)
{
int arity = 3;
/* int arity = rb_obj_method_arity((VALUE) data, func); */
/* XXX: this doesn't work. */
VALUE arbiter = Qnil;
cpShape *a = NULL;
cpShape *b = NULL;
VALUE toCall;
if(isBlock((VALUE) data)) {
toCall = (VALUE)data;
} else {
toCall = callbackToMethod((VALUE) data, func);
}
arity = NUM2INT(callbackArity(toCall));
switch (arity) {
case 1:
arbiter = ARBWRAP(arb);
return rb_funcall(toCall, rb_intern("call"), 1, arbiter);
case 2:
cpArbiterGetShapes(arb, &a, &b);
return rb_funcall(toCall, rb_intern("call"), 2, (VALUE)a->data, (VALUE)b->data);
case 3:
cpArbiterGetShapes(arb, &a, &b);
arbiter = ARBWRAP(arb);
return rb_funcall(toCall, rb_intern("call"), 3, (VALUE)a->data, (VALUE)b->data, arbiter);
default:
rb_raise(rb_eArgError, "Arity of callback must be 1, 2, or 3.");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment