ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libev/ev.pod
(Generate patch)

Comparing libev/ev.pod (file contents):
Revision 1.177 by root, Mon Sep 8 17:27:42 2008 UTC vs.
Revision 1.178 by root, Sat Sep 13 18:25:50 2008 UTC

1006 { 1006 {
1007 struct ev_io io; 1007 struct ev_io io;
1008 int otherfd; 1008 int otherfd;
1009 void *somedata; 1009 void *somedata;
1010 struct whatever *mostinteresting; 1010 struct whatever *mostinteresting;
1011 } 1011 };
1012
1013 ...
1014 struct my_io w;
1015 ev_io_init (&w.io, my_cb, fd, EV_READ);
1012 1016
1013And since your callback will be called with a pointer to the watcher, you 1017And since your callback will be called with a pointer to the watcher, you
1014can cast it back to your own type: 1018can cast it back to your own type:
1015 1019
1016 static void my_cb (struct ev_loop *loop, struct ev_io *w_, int revents) 1020 static void my_cb (struct ev_loop *loop, struct ev_io *w_, int revents)
1020 } 1024 }
1021 1025
1022More interesting and less C-conformant ways of casting your callback type 1026More interesting and less C-conformant ways of casting your callback type
1023instead have been omitted. 1027instead have been omitted.
1024 1028
1025Another common scenario is having some data structure with multiple 1029Another common scenario is to use some data structure with multiple
1026watchers: 1030embedded watchers:
1027 1031
1028 struct my_biggy 1032 struct my_biggy
1029 { 1033 {
1030 int some_data; 1034 int some_data;
1031 ev_timer t1; 1035 ev_timer t1;
1032 ev_timer t2; 1036 ev_timer t2;
1033 } 1037 }
1034 1038
1035In this case getting the pointer to C<my_biggy> is a bit more complicated, 1039In this case getting the pointer to C<my_biggy> is a bit more
1036you need to use C<offsetof>: 1040complicated: Either you store the address of your C<my_biggy> struct
1041in the C<data> member of the watcher, or you need to use some pointer
1042arithmetic using C<offsetof> inside your watchers:
1037 1043
1038 #include <stddef.h> 1044 #include <stddef.h>
1039 1045
1040 static void 1046 static void
1041 t1_cb (EV_P_ struct ev_timer *w, int revents) 1047 t1_cb (EV_P_ struct ev_timer *w, int revents)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines