On Sat, Dec 1, 2012 at 2:24 AM, zocket.sprocket <[email protected]> wrote:
> How do you get a uv watcher to work with an object instance method as
> callback?
>
> Below is what I am trying to do but it is not working for me.
>
> uv_idle_init(uv_default_loop(), &obj->idler_);
> uv_idle_start(&obj->idler_, obj->CheckList);
>
>
> error: argument of type ‘void (Collector::)(uv_idle_t*, int)’ does not match
> ‘void (*)(uv_idle_t*, int)’
You cannot use member functions (methods) directly, not portably
anyway. Use a trampoline instead:
class Foo {
public:
static Foo* New() {
return new Foo();
}
void Dispose() {
uv_close(reinterpret_cast<uv_handle_t*>(&idle_), Dispose);
}
private:
Foo() {
uv_idle_init(uv_default_loop(), &idler_);
idler_.data = this;
uv_idle_start(&idler_, CheckList);
}
static void Dispose(uv_handle_t* handle) {
delete static_cast<Foo*>(handle->data);
}
static void CheckList(uv_idle_t* handle, int status) {
static_cast<Foo*>(handle->data)->CheckList(handle, status);
}
void CheckList(uv_idle_t* handle, int status) {
// ...
}
uv_idle_t idler_;
};
The example also shows how to properly clean up the handle after
you're done with it: that is, you call uv_close() and defer freeing
the memory until the close operation is complete.
Hope that helps.
--
Job Board: http://jobs.nodejs.org/
Posting guidelines:
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en