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