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