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) |
137 | loop_ref (EV_P) throw () |
137 | #if EV_MULTIPLICITY |
138 | #if EV_MULTIPLICITY |
138 | throw (bad_loop) : EV_AX (EV_A) |
139 | : EV_AX (EV_A) |
139 | { |
140 | #endif |
140 | if (!EV_AX) |
|
|
141 | throw bad_loop (); |
|
|
142 | } |
141 | { |
143 | #else |
|
|
144 | throw () |
|
|
145 | { |
142 | } |
146 | } |
|
|
147 | #endif |
|
|
148 | |
143 | |
149 | bool operator == (const loop_ref &other) const throw () |
144 | bool operator == (const loop_ref &other) const throw () |
150 | { |
145 | { |
151 | #if EV_MULTIPLICITY |
146 | #if EV_MULTIPLICITY |
152 | return EV_AX == other.EV_AX; |
147 | return EV_AX == other.EV_AX; |
… | |
… | |
163 | return false; |
158 | return false; |
164 | #endif |
159 | #endif |
165 | } |
160 | } |
166 | |
161 | |
167 | #if EV_MULTIPLICITY |
162 | #if EV_MULTIPLICITY |
168 | bool operator== (struct ev_loop *other) const throw () |
163 | bool operator == (struct ev_loop *other) const throw () |
169 | { |
164 | { |
170 | return this->EV_AX == other; |
165 | return this->EV_AX == other; |
171 | } |
166 | } |
172 | |
167 | |
173 | bool operator!= (struct ev_loop *other) const throw () |
168 | bool operator != (struct ev_loop *other) const throw () |
174 | { |
169 | { |
175 | return ! (*this == other); |
170 | return ! (*this == other); |
176 | } |
171 | } |
177 | |
172 | |
178 | bool operator== (const struct ev_loop *other) const throw () |
173 | bool operator == (const struct ev_loop *other) const throw () |
179 | { |
174 | { |
180 | return this->EV_AX == other; |
175 | return this->EV_AX == other; |
181 | } |
176 | } |
182 | |
177 | |
183 | bool operator!= (const struct ev_loop *other) const throw () |
178 | bool operator != (const struct ev_loop *other) const throw () |
184 | { |
179 | { |
185 | return (*this == other); |
180 | return (*this == other); |
186 | } |
181 | } |
187 | |
182 | |
188 | operator struct ev_loop * () const throw () |
183 | operator struct ev_loop * () const throw () |
… | |
… | |
344 | #endif |
339 | #endif |
345 | |
340 | |
346 | }; |
341 | }; |
347 | |
342 | |
348 | #if EV_MULTIPLICITY |
343 | #if EV_MULTIPLICITY |
349 | struct dynamic_loop: loop_ref |
344 | struct dynamic_loop : loop_ref |
350 | { |
345 | { |
351 | |
346 | |
352 | dynamic_loop (unsigned int flags = AUTO) throw (bad_loop) |
347 | dynamic_loop (unsigned int flags = AUTO) throw (bad_loop) |
353 | : loop_ref (ev_loop_new (flags)) |
348 | : loop_ref (ev_loop_new (flags)) |
354 | { |
349 | { |
|
|
350 | if (!EV_AX) |
|
|
351 | throw bad_loop (); |
355 | } |
352 | } |
356 | |
353 | |
357 | ~dynamic_loop () throw () |
354 | ~dynamic_loop () throw () |
358 | { |
355 | { |
359 | ev_loop_destroy (EV_AX); |
356 | ev_loop_destroy (EV_AX); |
… | |
… | |
367 | dynamic_loop & operator= (const dynamic_loop &); |
364 | dynamic_loop & operator= (const dynamic_loop &); |
368 | |
365 | |
369 | }; |
366 | }; |
370 | #endif |
367 | #endif |
371 | |
368 | |
372 | struct default_loop: loop_ref |
369 | struct default_loop : loop_ref |
373 | { |
370 | { |
374 | |
|
|
375 | default_loop (unsigned int flags = AUTO) throw (bad_loop) |
371 | default_loop (unsigned int flags = AUTO) throw (bad_loop) |
376 | #if EV_MULTIPLICITY |
372 | #if EV_MULTIPLICITY |
377 | : loop_ref (ev_default_loop (flags)) |
373 | : loop_ref (ev_default_loop (flags)) |
378 | #endif |
374 | #endif |
379 | { |
375 | { |
|
|
376 | if ( |
380 | #if !EV_MULTIPLICITY |
377 | #if EV_MULTIPLICITY |
|
|
378 | !EV_AX |
|
|
379 | #else |
381 | if (!ev_default_loop (flags)) |
380 | !ev_default_loop (flags) |
|
|
381 | #endif |
|
|
382 | ) |
382 | throw bad_loop (); |
383 | throw bad_loop (); |
383 | #endif |
|
|
384 | } |
384 | } |
385 | |
385 | |
386 | ~default_loop () throw () |
386 | ~default_loop () throw () |
387 | { |
387 | { |
388 | ev_default_destroy (); |
388 | ev_default_destroy (); |
… | |
… | |
563 | ev_set_syserr_cb (cb); |
563 | ev_set_syserr_cb (cb); |
564 | } |
564 | } |
565 | |
565 | |
566 | #if EV_MULTIPLICITY |
566 | #if EV_MULTIPLICITY |
567 | #define EV_CONSTRUCT(cppstem,cstem) \ |
567 | #define EV_CONSTRUCT(cppstem,cstem) \ |
568 | (EV_PX = get_default_loop ()) throw () \ |
568 | (EV_PX = get_default_loop ()) throw () \ |
569 | : base<ev_ ## cstem, cppstem> (EV_A) \ |
569 | : base<ev_ ## cstem, cppstem> (EV_A) \ |
570 | { \ |
570 | { \ |
571 | } |
571 | } |
572 | #else |
572 | #else |
573 | #define EV_CONSTRUCT(cppstem,cstem) \ |
573 | #define EV_CONSTRUCT(cppstem,cstem) \ |
574 | () throw () \ |
574 | () throw () \ |
575 | { \ |
575 | { \ |
576 | } |
576 | } |
577 | #endif |
577 | #endif |
578 | |
578 | |
579 | /* using a template here would require quite a bit more lines, |
579 | /* using a template here would require quite a bit more lines, |
580 | * so a macro solution was chosen */ |
580 | * so a macro solution was chosen */ |
581 | #define EV_BEGIN_WATCHER(cppstem,cstem) \ |
581 | #define EV_BEGIN_WATCHER(cppstem,cstem) \ |
582 | \ |
582 | \ |
583 | struct cppstem : base<ev_ ## cstem, cppstem> \ |
583 | struct cppstem : base<ev_ ## cstem, cppstem> \ |
584 | { \ |
584 | { \ |
585 | void start () throw () \ |
585 | void start () throw () \ |
586 | { \ |
586 | { \ |
587 | ev_ ## cstem ## _start (EV_A_ static_cast<ev_ ## cstem *>(this)); \ |
587 | ev_ ## cstem ## _start (EV_A_ static_cast<ev_ ## cstem *>(this)); \ |
588 | } \ |
588 | } \ |
589 | \ |
589 | \ |
590 | void stop () throw () \ |
590 | void stop () throw () \ |
591 | { \ |
591 | { \ |
592 | ev_ ## cstem ## _stop (EV_A_ static_cast<ev_ ## cstem *>(this)); \ |
592 | ev_ ## cstem ## _stop (EV_A_ static_cast<ev_ ## cstem *>(this)); \ |
593 | } \ |
593 | } \ |
594 | \ |
594 | \ |
595 | cppstem EV_CONSTRUCT(cppstem,cstem) \ |
595 | cppstem EV_CONSTRUCT(cppstem,cstem) \ |
596 | \ |
596 | \ |
597 | ~cppstem () throw () \ |
597 | ~cppstem () throw () \ |
598 | { \ |
598 | { \ |
599 | stop (); \ |
599 | stop (); \ |
600 | } \ |
600 | } \ |
601 | \ |
601 | \ |
602 | using base<ev_ ## cstem, cppstem>::set; \ |
602 | using base<ev_ ## cstem, cppstem>::set; \ |
603 | \ |
603 | \ |
604 | private: \ |
604 | private: \ |
605 | \ |
605 | \ |
606 | cppstem (const cppstem &o); \ |
606 | cppstem (const cppstem &o); \ |
607 | \ |
607 | \ |
608 | cppstem & operator =(const cppstem &o); \ |
608 | cppstem &operator =(const cppstem &o); \ |
609 | \ |
609 | \ |
610 | public: |
610 | public: |
611 | |
611 | |
612 | #define EV_END_WATCHER(cppstem,cstem) \ |
612 | #define EV_END_WATCHER(cppstem,cstem) \ |
613 | }; |
613 | }; |
… | |
… | |
695 | start (); |
695 | start (); |
696 | } |
696 | } |
697 | EV_END_WATCHER (sig, signal) |
697 | EV_END_WATCHER (sig, signal) |
698 | |
698 | |
699 | EV_BEGIN_WATCHER (child, child) |
699 | EV_BEGIN_WATCHER (child, child) |
700 | void set (int pid) throw () |
700 | void set (int pid, int trace = 0) throw () |
701 | { |
701 | { |
702 | int active = is_active (); |
702 | int active = is_active (); |
703 | if (active) stop (); |
703 | if (active) stop (); |
704 | ev_child_set (static_cast<ev_child *>(this), pid); |
704 | ev_child_set (static_cast<ev_child *>(this), pid, trace); |
705 | if (active) start (); |
705 | if (active) start (); |
706 | } |
706 | } |
707 | |
707 | |
708 | void start (int pid) throw () |
708 | void start (int pid, int trace = 0) throw () |
709 | { |
709 | { |
710 | set (pid); |
710 | set (pid, trace); |
711 | start (); |
711 | start (); |
712 | } |
712 | } |
713 | EV_END_WATCHER (child, child) |
713 | EV_END_WATCHER (child, child) |
714 | |
714 | |
715 | #if EV_STAT_ENABLE |
715 | #if EV_STAT_ENABLE |
… | |
… | |
748 | void set () throw () { } |
748 | void set () throw () { } |
749 | EV_END_WATCHER (check, check) |
749 | EV_END_WATCHER (check, check) |
750 | |
750 | |
751 | #if EV_EMBED_ENABLE |
751 | #if EV_EMBED_ENABLE |
752 | 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 | |
753 | void start (struct ev_loop *embedded_loop) throw () |
761 | void start (struct ev_loop *embedded_loop) throw () |
754 | { |
762 | { |
755 | stop (); |
763 | set (embedded_loop); |
756 | ev_embed_set (static_cast<ev_embed *>(this), embedded_loop); |
|
|
757 | start (); |
764 | start (); |
758 | } |
765 | } |
759 | |
766 | |
760 | void sweep () |
767 | void sweep () |
761 | { |
768 | { |
… | |
… | |
768 | EV_BEGIN_WATCHER (fork, fork) |
775 | EV_BEGIN_WATCHER (fork, fork) |
769 | void set () throw () { } |
776 | void set () throw () { } |
770 | EV_END_WATCHER (fork, fork) |
777 | EV_END_WATCHER (fork, fork) |
771 | #endif |
778 | #endif |
772 | |
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 | |
773 | #undef EV_PX |
796 | #undef EV_PX |
774 | #undef EV_PX_ |
797 | #undef EV_PX_ |
775 | #undef EV_CONSTRUCT |
798 | #undef EV_CONSTRUCT |
776 | #undef EV_BEGIN_WATCHER |
799 | #undef EV_BEGIN_WATCHER |
777 | #undef EV_END_WATCHER |
800 | #undef EV_END_WATCHER |
778 | |
|
|
779 | } |
801 | } |
780 | |
802 | |
781 | #endif |
803 | #endif |
782 | |
804 | |