[Pyrex] classes and callbacks
Lenard Lindstrom
len-l at telus.net
Wed Oct 11 17:51:55 UTC 2006
On 11 Oct 2006 at 15:45, Test Drive wrote:
>
> Hi All,
>
> Thanks for considering this. I think Lenard Lindstrom got it pretty
> right.
> Greg, consider callabcks which take functions as parameters.
>
> gobj_callback_resize(gobj *g, void (*func) (gobj *g);
>
> Here if there are 10 instances of the class , each registering a diff
> function for the cb, I need to keep track of which instance
> registered which function.
> I use a global dict with instance_id as key and the callback func as
> value.
>
> The workaround by Lenard Lindstrom is pretty good. Look at the cb
> example, I need to have gobj *g in both the classes and way (sandwich
> function) to copy the pointer from one class to other.
>
> Sure if the callback is implemented it will make the code much
> cleaner.
> psuedo code will look something like this
>
> cdef extern from "g.h"
> gobj_callback_resize(gobj *g, void (*func) (gobj *g))
>
> cdef class ABC:
> cdef gobj *g
>
> def __init__(self):
> self.g=init_gobj()
> self.resize_callback=None
>
> cdef callback cb_func (gobj *g):
>
> do_anything_here_u_have_access_to_ptr_g_based_on_which_u_can_call_othe
> r_c_funcs_of_the_lib_or_manipulate_this_instance's_attributes
>
> def register_resize_callback(python_callback_func):
> self.resize_callback=python_callback_func
>
> gobj_callback_resize(self.g,self.cb_func)
>
> Thanks
>
I think you want to use a bound method as a C callback function. You
can do this with the ctypes module.
Lenard Lindstrom
<len-l at telus.net>
More information about the Pyrex
mailing list