… | |
… | |
703 | events but its callback has not yet been invoked). As long as a watcher |
703 | events but its callback has not yet been invoked). As long as a watcher |
704 | is pending (but not active) you must not call an init function on it (but |
704 | is pending (but not active) you must not call an init function on it (but |
705 | C<ev_TYPE_set> is safe) and you must make sure the watcher is available to |
705 | C<ev_TYPE_set> is safe) and you must make sure the watcher is available to |
706 | libev (e.g. you cnanot C<free ()> it). |
706 | libev (e.g. you cnanot C<free ()> it). |
707 | |
707 | |
708 | =item callback = ev_cb (ev_TYPE *watcher) |
708 | =item callback ev_cb (ev_TYPE *watcher) |
709 | |
709 | |
710 | Returns the callback currently set on the watcher. |
710 | Returns the callback currently set on the watcher. |
711 | |
711 | |
712 | =item ev_cb_set (ev_TYPE *watcher, callback) |
712 | =item ev_cb_set (ev_TYPE *watcher, callback) |
713 | |
713 | |
… | |
… | |
741 | { |
741 | { |
742 | struct my_io *w = (struct my_io *)w_; |
742 | struct my_io *w = (struct my_io *)w_; |
743 | ... |
743 | ... |
744 | } |
744 | } |
745 | |
745 | |
746 | More interesting and less C-conformant ways of catsing your callback type |
746 | More interesting and less C-conformant ways of casting your callback type |
747 | have been omitted.... |
747 | instead have been omitted. |
|
|
748 | |
|
|
749 | Another common scenario is having some data structure with multiple |
|
|
750 | watchers: |
|
|
751 | |
|
|
752 | struct my_biggy |
|
|
753 | { |
|
|
754 | int some_data; |
|
|
755 | ev_timer t1; |
|
|
756 | ev_timer t2; |
|
|
757 | } |
|
|
758 | |
|
|
759 | In this case getting the pointer to C<my_biggy> is a bit more complicated, |
|
|
760 | you need to use C<offsetof>: |
|
|
761 | |
|
|
762 | #include <stddef.h> |
|
|
763 | |
|
|
764 | static void |
|
|
765 | t1_cb (EV_P_ struct ev_timer *w, int revents) |
|
|
766 | { |
|
|
767 | struct my_biggy big = (struct my_biggy * |
|
|
768 | (((char *)w) - offsetof (struct my_biggy, t1)); |
|
|
769 | } |
|
|
770 | |
|
|
771 | static void |
|
|
772 | t2_cb (EV_P_ struct ev_timer *w, int revents) |
|
|
773 | { |
|
|
774 | struct my_biggy big = (struct my_biggy * |
|
|
775 | (((char *)w) - offsetof (struct my_biggy, t2)); |
|
|
776 | } |
748 | |
777 | |
749 | |
778 | |
750 | =head1 WATCHER TYPES |
779 | =head1 WATCHER TYPES |
751 | |
780 | |
752 | This section describes each watcher in detail, but will not repeat |
781 | This section describes each watcher in detail, but will not repeat |