1 | /* |
1 | /* |
2 | * libev simple C++ wrapper classes |
2 | * libev simple C++ wrapper classes |
3 | * |
3 | * |
4 | * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de> |
4 | * Copyright (c) 2007,2008 Marc Alexander Lehmann <libev@schmorp.de> |
5 | * All rights reserved. |
5 | * All rights reserved. |
6 | * |
6 | * |
7 | * Redistribution and use in source and binary forms, with or without modifica- |
7 | * Redistribution and use in source and binary forms, with or without modifica- |
8 | * tion, are permitted provided that the following conditions are met: |
8 | * tion, are permitted provided that the following conditions are met: |
9 | * |
9 | * |
… | |
… | |
70 | STAT = EV_STAT, |
70 | STAT = EV_STAT, |
71 | IDLE = EV_IDLE, |
71 | IDLE = EV_IDLE, |
72 | CHECK = EV_CHECK, |
72 | CHECK = EV_CHECK, |
73 | PREPARE = EV_PREPARE, |
73 | PREPARE = EV_PREPARE, |
74 | FORK = EV_FORK, |
74 | FORK = EV_FORK, |
|
|
75 | ASYNC = EV_ASYNC, |
75 | EMBED = EV_EMBED, |
76 | EMBED = EV_EMBED, |
76 | ERROR = EV_ERROR, |
77 | ERROR = EV_ERROR, |
77 | }; |
78 | }; |
78 | |
79 | |
79 | enum |
80 | enum |
80 | { |
81 | { |
81 | AUTO = EVFLAG_AUTO, |
82 | AUTO = EVFLAG_AUTO, |
82 | NOENV = EVFLAG_NOENV, |
83 | NOENV = EVFLAG_NOENV, |
83 | FORKCHECK = EVFLAG_FORKCHECK, |
84 | FORKCHECK = EVFLAG_FORKCHECK, |
|
|
85 | |
84 | SELECT = EVBACKEND_SELECT, |
86 | SELECT = EVBACKEND_SELECT, |
85 | POLL = EVBACKEND_POLL, |
87 | POLL = EVBACKEND_POLL, |
86 | EPOLL = EVBACKEND_EPOLL, |
88 | EPOLL = EVBACKEND_EPOLL, |
87 | KQUEUE = EVBACKEND_KQUEUE, |
89 | KQUEUE = EVBACKEND_KQUEUE, |
88 | DEVPOLL = EVBACKEND_DEVPOLL, |
90 | DEVPOLL = EVBACKEND_DEVPOLL, |
89 | PORT = EVBACKEND_PORT |
91 | PORT = EVBACKEND_PORT |
90 | }; |
92 | }; |
91 | |
93 | |
92 | enum |
94 | enum |
93 | { |
95 | { |
94 | NONBLOCK = EVLOOP_NONBLOCK, |
96 | NONBLOCK = EVLOOP_NONBLOCK, |
95 | ONESHOT = EVLOOP_ONESHOT |
97 | ONESHOT = EVLOOP_ONESHOT |
96 | }; |
98 | }; |
97 | |
99 | |
98 | enum how_t |
100 | enum how_t |
99 | { |
101 | { |
100 | ONE = EVUNLOOP_ONE, |
102 | ONE = EVUNLOOP_ONE, |
… | |
… | |
130 | # define EV_AX_ |
132 | # define EV_AX_ |
131 | #endif |
133 | #endif |
132 | |
134 | |
133 | struct loop_ref |
135 | struct loop_ref |
134 | { |
136 | { |
135 | |
|
|
136 | loop_ref (EV_P) throw (bad_loop) |
137 | loop_ref (EV_P) throw () |
137 | #if EV_MULTIPLICITY |
138 | #if EV_MULTIPLICITY |
138 | : EV_AX (EV_A) |
139 | : EV_AX (EV_A) |
139 | #endif |
140 | #endif |
140 | { |
141 | { |
141 | #if EV_MULTIPLICITY |
|
|
142 | if (!EV_AX) |
|
|
143 | throw bad_loop (); |
|
|
144 | #endif |
|
|
145 | } |
142 | } |
146 | |
143 | |
147 | bool operator == (const loop_ref &other) const throw () |
144 | bool operator == (const loop_ref &other) const throw () |
148 | { |
145 | { |
149 | #if EV_MULTIPLICITY |
146 | #if EV_MULTIPLICITY |
… | |
… | |
161 | return false; |
158 | return false; |
162 | #endif |
159 | #endif |
163 | } |
160 | } |
164 | |
161 | |
165 | #if EV_MULTIPLICITY |
162 | #if EV_MULTIPLICITY |
166 | bool operator== (struct ev_loop *other) const throw () |
163 | bool operator == (struct ev_loop *other) const throw () |
167 | { |
164 | { |
168 | return this->EV_AX == other; |
165 | return this->EV_AX == other; |
169 | } |
166 | } |
170 | |
167 | |
171 | bool operator!= (struct ev_loop *other) const throw () |
168 | bool operator != (struct ev_loop *other) const throw () |
172 | { |
169 | { |
173 | return ! (*this == other); |
170 | return ! (*this == other); |
174 | } |
171 | } |
175 | |
172 | |
176 | bool operator== (const struct ev_loop *other) const throw () |
173 | bool operator == (const struct ev_loop *other) const throw () |
177 | { |
174 | { |
178 | return this->EV_AX == other; |
175 | return this->EV_AX == other; |
179 | } |
176 | } |
180 | |
177 | |
181 | bool operator!= (const struct ev_loop *other) const throw () |
178 | bool operator != (const struct ev_loop *other) const throw () |
182 | { |
179 | { |
183 | return (*this == other); |
180 | return (*this == other); |
184 | } |
181 | } |
185 | |
182 | |
186 | operator struct ev_loop * () const throw () |
183 | operator struct ev_loop * () const throw () |
… | |
… | |
342 | #endif |
339 | #endif |
343 | |
340 | |
344 | }; |
341 | }; |
345 | |
342 | |
346 | #if EV_MULTIPLICITY |
343 | #if EV_MULTIPLICITY |
347 | struct dynamic_loop: loop_ref |
344 | struct dynamic_loop : loop_ref |
348 | { |
345 | { |
349 | |
346 | |
350 | dynamic_loop (unsigned int flags = AUTO) EV_THROW (bad_loop) |
347 | dynamic_loop (unsigned int flags = AUTO) throw (bad_loop) |
351 | : loop_ref (ev_loop_new (flags)) |
348 | : loop_ref (ev_loop_new (flags)) |
352 | { |
349 | { |
|
|
350 | if (!EV_AX) |
|
|
351 | throw bad_loop (); |
353 | } |
352 | } |
354 | |
353 | |
355 | ~dynamic_loop () throw () |
354 | ~dynamic_loop () throw () |
356 | { |
355 | { |
357 | ev_loop_destroy (EV_AX); |
356 | ev_loop_destroy (EV_AX); |
… | |
… | |
365 | dynamic_loop & operator= (const dynamic_loop &); |
364 | dynamic_loop & operator= (const dynamic_loop &); |
366 | |
365 | |
367 | }; |
366 | }; |
368 | #endif |
367 | #endif |
369 | |
368 | |
370 | struct default_loop: loop_ref |
369 | struct default_loop : loop_ref |
371 | { |
370 | { |
372 | |
|
|
373 | default_loop (unsigned int flags = AUTO) throw (bad_loop) |
371 | default_loop (unsigned int flags = AUTO) throw (bad_loop) |
374 | #if EV_MULTIPLICITY |
372 | #if EV_MULTIPLICITY |
375 | : loop_ref (ev_default_loop (flags)) |
373 | : loop_ref (ev_default_loop (flags)) |
376 | #endif |
374 | #endif |
377 | { |
375 | { |
|
|
376 | if ( |
378 | #ifndef EV_MULTIPLICITY |
377 | #if EV_MULTIPLICITY |
|
|
378 | !EV_AX |
|
|
379 | #else |
379 | if (!ev_default_loop (flags)) |
380 | !ev_default_loop (flags) |
|
|
381 | #endif |
|
|
382 | ) |
380 | throw bad_loop (); |
383 | throw bad_loop (); |
381 | #endif |
|
|
382 | } |
384 | } |
383 | |
385 | |
384 | ~default_loop () throw () |
386 | ~default_loop () throw () |
385 | { |
387 | { |
386 | ev_default_destroy (); |
388 | ev_default_destroy (); |
… | |
… | |
561 | ev_set_syserr_cb (cb); |
563 | ev_set_syserr_cb (cb); |
562 | } |
564 | } |
563 | |
565 | |
564 | #if EV_MULTIPLICITY |
566 | #if EV_MULTIPLICITY |
565 | #define EV_CONSTRUCT(cppstem,cstem) \ |
567 | #define EV_CONSTRUCT(cppstem,cstem) \ |
566 | (EV_PX = get_default_loop ()) throw () \ |
568 | (EV_PX = get_default_loop ()) throw () \ |
567 | : base<ev_ ## cstem, cppstem> (EV_A) \ |
569 | : base<ev_ ## cstem, cppstem> (EV_A) \ |
568 | { \ |
570 | { \ |
569 | } |
571 | } |
570 | #else |
572 | #else |
571 | #define EV_CONSTRUCT(cppstem,cstem) \ |
573 | #define EV_CONSTRUCT(cppstem,cstem) \ |
572 | () throw () \ |
574 | () throw () \ |
573 | { \ |
575 | { \ |
574 | } |
576 | } |
575 | #endif |
577 | #endif |
576 | |
578 | |
577 | /* using a template here would require quite a bit more lines, |
579 | /* using a template here would require quite a bit more lines, |
578 | * so a macro solution was chosen */ |
580 | * so a macro solution was chosen */ |
579 | #define EV_BEGIN_WATCHER(cppstem,cstem) \ |
581 | #define EV_BEGIN_WATCHER(cppstem,cstem) \ |
580 | \ |
582 | \ |
581 | struct cppstem : base<ev_ ## cstem, cppstem> \ |
583 | struct cppstem : base<ev_ ## cstem, cppstem> \ |
582 | { \ |
584 | { \ |
583 | void start () throw () \ |
585 | void start () throw () \ |
584 | { \ |
586 | { \ |
585 | ev_ ## cstem ## _start (EV_A_ static_cast<ev_ ## cstem *>(this)); \ |
587 | ev_ ## cstem ## _start (EV_A_ static_cast<ev_ ## cstem *>(this)); \ |
586 | } \ |
588 | } \ |
587 | \ |
589 | \ |
588 | void stop () throw () \ |
590 | void stop () throw () \ |
589 | { \ |
591 | { \ |
590 | ev_ ## cstem ## _stop (EV_A_ static_cast<ev_ ## cstem *>(this)); \ |
592 | ev_ ## cstem ## _stop (EV_A_ static_cast<ev_ ## cstem *>(this)); \ |
591 | } \ |
593 | } \ |
592 | \ |
594 | \ |
593 | cppstem EV_CONSTRUCT(cppstem,cstem) \ |
595 | cppstem EV_CONSTRUCT(cppstem,cstem) \ |
594 | \ |
596 | \ |
595 | ~cppstem () throw () \ |
597 | ~cppstem () throw () \ |
596 | { \ |
598 | { \ |
597 | stop (); \ |
599 | stop (); \ |
598 | } \ |
600 | } \ |
599 | \ |
601 | \ |
600 | using base<ev_ ## cstem, cppstem>::set; \ |
602 | using base<ev_ ## cstem, cppstem>::set; \ |
601 | \ |
603 | \ |
602 | private: \ |
604 | private: \ |
603 | \ |
605 | \ |
604 | cppstem (const cppstem &o); \ |
606 | cppstem (const cppstem &o); \ |
605 | \ |
607 | \ |
606 | cppstem & operator =(const cppstem &o); \ |
608 | cppstem &operator =(const cppstem &o); \ |
607 | \ |
609 | \ |
608 | public: |
610 | public: |
609 | |
611 | |
610 | #define EV_END_WATCHER(cppstem,cstem) \ |
612 | #define EV_END_WATCHER(cppstem,cstem) \ |
611 | }; |
613 | }; |
… | |
… | |
693 | start (); |
695 | start (); |
694 | } |
696 | } |
695 | EV_END_WATCHER (sig, signal) |
697 | EV_END_WATCHER (sig, signal) |
696 | |
698 | |
697 | EV_BEGIN_WATCHER (child, child) |
699 | EV_BEGIN_WATCHER (child, child) |
698 | void set (int pid) throw () |
700 | void set (int pid, int trace = 0) throw () |
699 | { |
701 | { |
700 | int active = is_active (); |
702 | int active = is_active (); |
701 | if (active) stop (); |
703 | if (active) stop (); |
702 | ev_child_set (static_cast<ev_child *>(this), pid); |
704 | ev_child_set (static_cast<ev_child *>(this), pid, trace); |
703 | if (active) start (); |
705 | if (active) start (); |
704 | } |
706 | } |
705 | |
707 | |
706 | void start (int pid) throw () |
708 | void start (int pid, int trace = 0) throw () |
707 | { |
709 | { |
708 | set (pid); |
710 | set (pid, trace); |
709 | start (); |
711 | start (); |
710 | } |
712 | } |
711 | EV_END_WATCHER (child, child) |
713 | EV_END_WATCHER (child, child) |
712 | |
714 | |
713 | #if EV_STAT_ENABLE |
715 | #if EV_STAT_ENABLE |
… | |
… | |
746 | void set () throw () { } |
748 | void set () throw () { } |
747 | EV_END_WATCHER (check, check) |
749 | EV_END_WATCHER (check, check) |
748 | |
750 | |
749 | #if EV_EMBED_ENABLE |
751 | #if EV_EMBED_ENABLE |
750 | EV_BEGIN_WATCHER (embed, embed) |
752 | EV_BEGIN_WATCHER (embed, embed) |
|
|
753 | void set (struct ev_loop *embedded_loop) throw () |
|
|
754 | { |
|
|
755 | int active = is_active (); |
|
|
756 | if (active) stop (); |
|
|
757 | ev_embed_set (static_cast<ev_embed *>(this), embedded_loop); |
|
|
758 | if (active) start (); |
|
|
759 | } |
|
|
760 | |
751 | void start (struct ev_loop *embedded_loop) throw () |
761 | void start (struct ev_loop *embedded_loop) throw () |
752 | { |
762 | { |
753 | stop (); |
763 | set (embedded_loop); |
754 | ev_embed_set (static_cast<ev_embed *>(this), embedded_loop); |
|
|
755 | start (); |
764 | start (); |
756 | } |
765 | } |
757 | |
766 | |
758 | void sweep () |
767 | void sweep () |
759 | { |
768 | { |
… | |
… | |
766 | EV_BEGIN_WATCHER (fork, fork) |
775 | EV_BEGIN_WATCHER (fork, fork) |
767 | void set () throw () { } |
776 | void set () throw () { } |
768 | EV_END_WATCHER (fork, fork) |
777 | EV_END_WATCHER (fork, fork) |
769 | #endif |
778 | #endif |
770 | |
779 | |
|
|
780 | #if EV_ASYNC_ENABLE |
|
|
781 | EV_BEGIN_WATCHER (async, async) |
|
|
782 | void set () throw () { } |
|
|
783 | |
|
|
784 | void send () throw () |
|
|
785 | { |
|
|
786 | ev_async_send (EV_A_ static_cast<ev_async *>(this)); |
|
|
787 | } |
|
|
788 | |
|
|
789 | bool async_pending () throw () |
|
|
790 | { |
|
|
791 | return ev_async_pending (static_cast<ev_async *>(this)); |
|
|
792 | } |
|
|
793 | EV_END_WATCHER (async, async) |
|
|
794 | #endif |
|
|
795 | |
771 | #undef EV_PX |
796 | #undef EV_PX |
772 | #undef EV_PX_ |
797 | #undef EV_PX_ |
773 | #undef EV_CONSTRUCT |
798 | #undef EV_CONSTRUCT |
774 | #undef EV_BEGIN_WATCHER |
799 | #undef EV_BEGIN_WATCHER |
775 | #undef EV_END_WATCHER |
800 | #undef EV_END_WATCHER |
776 | |
|
|
777 | } |
801 | } |
778 | |
802 | |
779 | #undef EV_THROW |
|
|
780 | |
|
|
781 | #endif |
803 | #endif |
782 | |
804 | |