Skip to content

Instantly share code, notes, and snippets.

@tshirtman
Created August 28, 2012 23:59
Show Gist options
  • Select an option

  • Save tshirtman/3505479 to your computer and use it in GitHub Desktop.

Select an option

Save tshirtman/3505479 to your computer and use it in GitHub Desktop.
cdef class GenericNativeWrapper(object):
"""
This class is to be used to register python method as methods of
JavaObjects using RegisterNatives
"""
cdef JNIEnv* j_env
cdef args
def __cinit__(self, j_env, definition, callback):
self.j_env = NULL
def __init__(self, j_env, definition, callback):
self.callback = callback
self.definitions = parse_definition(definition)[1]
cdef void call_void(self, ...):
cdef va_list j_args
cdef void* n
cdef int i = 0
args = []
va_start(j_args, <void*>n)
while n != NULL:
i += 1
args.append(convert_jobject_to_python(self.j_env, self.definitions[i], n))
d = self.definitions[i]
if d == 'Z':
n = <void*>va_arg(j_args, bool_type)
elif d == 'B':
n = <void*>va_arg(j_args, byte_type)
elif d == 'C':
n = <void*>va_arg(j_args, int_type) # cython says it's better to put int here, than char
elif d == 'S':
n = <void*>va_arg(j_args, short_type)
elif d == 'I':
n = <void*>va_arg(j_args, int_type)
elif d == 'J':
n = <void*>va_arg(j_args, long_type)
elif d == 'F':
n = <void*>va_arg(j_args, float_type)
elif d == 'D':
n = <void*>va_arg(j_args, double_type)
else: # == L, java object
n = <void*>va_arg(j_args, pointer_type)
va_end(j_args)
self.callback(*args)
# XXX define call_int/call_bool/call_char/... and friends on the
# same model, and "array of" variants
cdef int call_int(self, ...):
pass
cdef int* call_int_a(self, ...):
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment