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