… | |
… | |
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 | |
1013 | And since your callback will be called with a pointer to the watcher, you |
1017 | And since your callback will be called with a pointer to the watcher, you |
1014 | can cast it back to your own type: |
1018 | can 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 | |
1022 | More interesting and less C-conformant ways of casting your callback type |
1026 | More interesting and less C-conformant ways of casting your callback type |
1023 | instead have been omitted. |
1027 | instead have been omitted. |
1024 | |
1028 | |
1025 | Another common scenario is having some data structure with multiple |
1029 | Another common scenario is to use some data structure with multiple |
1026 | watchers: |
1030 | embedded 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 | |
1035 | In this case getting the pointer to C<my_biggy> is a bit more complicated, |
1039 | In this case getting the pointer to C<my_biggy> is a bit more |
1036 | you need to use C<offsetof>: |
1040 | complicated: Either you store the address of your C<my_biggy> struct |
|
|
1041 | in the C<data> member of the watcher, or you need to use some pointer |
|
|
1042 | arithmetic 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) |