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