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 | * |
… | |
… | |
44 | # include EV_H |
44 | # include EV_H |
45 | #else |
45 | #else |
46 | # include "ev.h" |
46 | # include "ev.h" |
47 | #endif |
47 | #endif |
48 | |
48 | |
49 | #ifndef EV_CXX_EXCEPTIONS |
49 | #ifndef EV_USE_STDEXCEPT |
50 | #define EV_CXX_EXCEPTIONS 1 |
50 | # define EV_USE_STDEXCEPT 1 |
51 | #endif |
51 | #endif |
52 | |
52 | |
53 | #undef EV_THROW |
53 | #if EV_USE_STDEXCEPT |
54 | #if EV_CXX_EXCEPTIONS |
|
|
55 | # include <stdexcept> |
54 | # include <stdexcept> |
56 | # define EV_THROW(exception) throw (exception) |
|
|
57 | #else |
|
|
58 | # define EV_THROW(exception) |
|
|
59 | #endif |
55 | #endif |
60 | |
56 | |
61 | namespace ev { |
57 | namespace ev { |
62 | |
58 | |
63 | typedef ev_tstamp tstamp; |
59 | typedef ev_tstamp tstamp; |
… | |
… | |
74 | STAT = EV_STAT, |
70 | STAT = EV_STAT, |
75 | IDLE = EV_IDLE, |
71 | IDLE = EV_IDLE, |
76 | CHECK = EV_CHECK, |
72 | CHECK = EV_CHECK, |
77 | PREPARE = EV_PREPARE, |
73 | PREPARE = EV_PREPARE, |
78 | FORK = EV_FORK, |
74 | FORK = EV_FORK, |
|
|
75 | ASYNC = EV_ASYNC, |
79 | EMBED = EV_EMBED, |
76 | EMBED = EV_EMBED, |
|
|
77 | # undef ERROR // some systems stupidly #define ERROR |
80 | ERROR = EV_ERROR, |
78 | ERROR = EV_ERROR, |
81 | }; |
79 | }; |
82 | |
80 | |
83 | enum |
81 | enum |
84 | { |
82 | { |
85 | AUTO = EVFLAG_AUTO, |
83 | AUTO = EVFLAG_AUTO, |
86 | NOENV = EVFLAG_NOENV, |
84 | NOENV = EVFLAG_NOENV, |
87 | FORKCHECK = EVFLAG_FORKCHECK, |
85 | FORKCHECK = EVFLAG_FORKCHECK, |
|
|
86 | |
88 | SELECT = EVBACKEND_SELECT, |
87 | SELECT = EVBACKEND_SELECT, |
89 | POLL = EVBACKEND_POLL, |
88 | POLL = EVBACKEND_POLL, |
90 | EPOLL = EVBACKEND_EPOLL, |
89 | EPOLL = EVBACKEND_EPOLL, |
91 | KQUEUE = EVBACKEND_KQUEUE, |
90 | KQUEUE = EVBACKEND_KQUEUE, |
92 | DEVPOLL = EVBACKEND_DEVPOLL, |
91 | DEVPOLL = EVBACKEND_DEVPOLL, |
93 | PORT = EVBACKEND_PORT |
92 | PORT = EVBACKEND_PORT |
94 | }; |
93 | }; |
95 | |
94 | |
96 | enum |
95 | enum |
97 | { |
96 | { |
98 | NONBLOCK = EVLOOP_NONBLOCK, |
97 | NONBLOCK = EVLOOP_NONBLOCK, |
99 | ONESHOT = EVLOOP_ONESHOT |
98 | ONESHOT = EVLOOP_ONESHOT |
100 | }; |
99 | }; |
101 | |
100 | |
102 | enum how_t |
101 | enum how_t |
103 | { |
102 | { |
104 | ONE = EVUNLOOP_ONE, |
103 | ONE = EVUNLOOP_ONE, |
105 | ALL = EVUNLOOP_ALL |
104 | ALL = EVUNLOOP_ALL |
106 | }; |
105 | }; |
107 | |
106 | |
108 | #if EV_CXX_EXCEPTIONS |
107 | struct bad_loop |
109 | struct bad_loop : std::runtime_error |
108 | #if EV_USE_STDEXCEPT |
|
|
109 | : std::runtime_error |
|
|
110 | #endif |
110 | { |
111 | { |
|
|
112 | #if EV_USE_STDEXCEPT |
111 | bad_loop () |
113 | bad_loop () |
112 | : std::runtime_error ("loop can't be initialized") |
114 | : std::runtime_error ("libev event loop cannot be initialized, bad value of LIBEV_FLAGS?") |
113 | { |
|
|
114 | } |
115 | { |
115 | }; |
116 | } |
116 | #endif |
117 | #endif |
|
|
118 | }; |
117 | |
119 | |
118 | #ifdef EV_AX |
120 | #ifdef EV_AX |
119 | # undef EV_AX |
121 | # undef EV_AX |
120 | #endif |
122 | #endif |
121 | |
123 | |
… | |
… | |
131 | # define EV_AX_ |
133 | # define EV_AX_ |
132 | #endif |
134 | #endif |
133 | |
135 | |
134 | struct loop_ref |
136 | struct loop_ref |
135 | { |
137 | { |
136 | |
|
|
137 | loop_ref (EV_P) |
138 | loop_ref (EV_P) throw () |
138 | #if EV_MULTIPLICITY |
139 | #if EV_MULTIPLICITY |
139 | EV_THROW (bad_loop) : EV_AX (EV_A) |
140 | : EV_AX (EV_A) |
140 | #endif |
141 | #endif |
141 | { |
|
|
142 | #if EV_MULTIPLICIY && EV_CXX_EXCEPTIONS |
|
|
143 | if (!EV_A) |
|
|
144 | throw bad_loop (); |
|
|
145 | #endif |
|
|
146 | } |
142 | { |
|
|
143 | } |
147 | |
144 | |
148 | bool operator== (const loop_ref &other) const throw () |
145 | bool operator == (const loop_ref &other) const throw () |
149 | { |
146 | { |
150 | #if EV_MULTIPLICITY |
147 | #if EV_MULTIPLICITY |
151 | return this->EV_AX == other.EV_AX; |
148 | return EV_AX == other.EV_AX; |
152 | #else |
149 | #else |
153 | return true; |
150 | return true; |
154 | #endif |
151 | #endif |
155 | } |
152 | } |
156 | |
153 | |
157 | bool operator!= (const loop_ref &other) const throw () |
154 | bool operator != (const loop_ref &other) const throw () |
158 | { |
155 | { |
159 | #if EV_MULTIPLICITY |
156 | #if EV_MULTIPLICITY |
160 | return ! (*this == other); |
157 | return ! (*this == other); |
161 | #else |
158 | #else |
162 | return false; |
159 | return false; |
163 | #endif |
160 | #endif |
164 | } |
161 | } |
165 | |
162 | |
166 | #if EV_MULTIPLICITY |
163 | #if EV_MULTIPLICITY |
167 | bool operator== (struct ev_loop *other) const throw () |
164 | bool operator == (struct ev_loop *other) const throw () |
168 | { |
165 | { |
169 | return this->EV_AX == other; |
166 | return this->EV_AX == other; |
170 | } |
167 | } |
171 | |
168 | |
172 | bool operator!= (struct ev_loop *other) const throw () |
169 | bool operator != (struct ev_loop *other) const throw () |
173 | { |
170 | { |
174 | return ! (*this == other); |
171 | return ! (*this == other); |
175 | } |
172 | } |
176 | |
173 | |
177 | bool operator== (const struct ev_loop *other) const throw () |
174 | bool operator == (const struct ev_loop *other) const throw () |
178 | { |
175 | { |
179 | return this->EV_AX == other; |
176 | return this->EV_AX == other; |
180 | } |
177 | } |
181 | |
178 | |
182 | bool operator!= (const struct ev_loop *other) const throw () |
179 | bool operator != (const struct ev_loop *other) const throw () |
183 | { |
180 | { |
184 | return (*this == other); |
181 | return (*this == other); |
185 | } |
182 | } |
186 | |
183 | |
187 | operator struct ev_loop * () const throw () |
184 | operator struct ev_loop * () const throw () |
… | |
… | |
343 | #endif |
340 | #endif |
344 | |
341 | |
345 | }; |
342 | }; |
346 | |
343 | |
347 | #if EV_MULTIPLICITY |
344 | #if EV_MULTIPLICITY |
348 | struct dynamic_loop: loop_ref |
345 | struct dynamic_loop : loop_ref |
349 | { |
346 | { |
350 | |
347 | |
351 | dynamic_loop (unsigned int flags = AUTO) EV_THROW (bad_loop) |
348 | dynamic_loop (unsigned int flags = AUTO) throw (bad_loop) |
352 | : loop_ref (ev_loop_new (flags)) |
349 | : loop_ref (ev_loop_new (flags)) |
353 | { |
350 | { |
|
|
351 | if (!EV_AX) |
|
|
352 | throw bad_loop (); |
354 | } |
353 | } |
355 | |
354 | |
356 | ~dynamic_loop () throw () |
355 | ~dynamic_loop () throw () |
357 | { |
356 | { |
358 | ev_loop_destroy (EV_AX); |
357 | ev_loop_destroy (EV_AX); |
… | |
… | |
366 | dynamic_loop & operator= (const dynamic_loop &); |
365 | dynamic_loop & operator= (const dynamic_loop &); |
367 | |
366 | |
368 | }; |
367 | }; |
369 | #endif |
368 | #endif |
370 | |
369 | |
371 | struct default_loop: loop_ref |
370 | struct default_loop : loop_ref |
372 | { |
371 | { |
373 | |
|
|
374 | default_loop (unsigned int flags = AUTO) EV_THROW (bad_loop) |
372 | default_loop (unsigned int flags = AUTO) throw (bad_loop) |
375 | #if EV_MULTIPLICITY |
373 | #if EV_MULTIPLICITY |
376 | : loop_ref (ev_default_loop (flags)) |
374 | : loop_ref (ev_default_loop (flags)) |
377 | { |
375 | #endif |
378 | } |
376 | { |
|
|
377 | if ( |
|
|
378 | #if EV_MULTIPLICITY |
|
|
379 | !EV_AX |
379 | #else |
380 | #else |
380 | { |
|
|
381 | #if EV_CXX_EXCEPTIONS |
|
|
382 | int r = |
|
|
383 | #endif |
|
|
384 | ev_default_loop (flags); |
381 | !ev_default_loop (flags) |
385 | #if EV_CXX_EXCEPTIONS |
382 | #endif |
386 | if (!r) |
383 | ) |
387 | throw bad_loop (); |
384 | throw bad_loop (); |
388 | #endif |
|
|
389 | } |
385 | } |
390 | #endif |
|
|
391 | |
386 | |
392 | ~default_loop () throw () |
387 | ~default_loop () throw () |
393 | { |
388 | { |
394 | ev_default_destroy (); |
389 | ev_default_destroy (); |
395 | #if EV_MULTIPLICITY |
|
|
396 | EV_AX = 0; |
|
|
397 | #endif |
|
|
398 | } |
390 | } |
399 | |
391 | |
400 | private: |
392 | private: |
401 | |
|
|
402 | default_loop (const default_loop &); |
393 | default_loop (const default_loop &); |
403 | |
|
|
404 | default_loop & operator= (const default_loop &); |
394 | default_loop &operator = (const default_loop &); |
405 | |
|
|
406 | }; |
395 | }; |
407 | |
396 | |
408 | inline loop_ref get_default_loop () throw () |
397 | inline loop_ref get_default_loop () throw () |
409 | { |
398 | { |
410 | #if EV_MULTIPLICITY |
399 | #if EV_MULTIPLICITY |
… | |
… | |
575 | ev_set_syserr_cb (cb); |
564 | ev_set_syserr_cb (cb); |
576 | } |
565 | } |
577 | |
566 | |
578 | #if EV_MULTIPLICITY |
567 | #if EV_MULTIPLICITY |
579 | #define EV_CONSTRUCT(cppstem,cstem) \ |
568 | #define EV_CONSTRUCT(cppstem,cstem) \ |
580 | (EV_PX = get_default_loop ()) throw () \ |
569 | (EV_PX = get_default_loop ()) throw () \ |
581 | : base<ev_ ## cstem, cppstem> (EV_A) \ |
570 | : base<ev_ ## cstem, cppstem> (EV_A) \ |
582 | { \ |
571 | { \ |
583 | } |
572 | } |
584 | #else |
573 | #else |
585 | #define EV_CONSTRUCT(cppstem,cstem) \ |
574 | #define EV_CONSTRUCT(cppstem,cstem) \ |
586 | () throw () \ |
575 | () throw () \ |
587 | { \ |
576 | { \ |
588 | } |
577 | } |
589 | #endif |
578 | #endif |
590 | |
579 | |
591 | /* using a template here would require quite a bit more lines, |
580 | /* using a template here would require quite a bit more lines, |
592 | * so a macro solution was chosen */ |
581 | * so a macro solution was chosen */ |
593 | #define EV_BEGIN_WATCHER(cppstem,cstem) \ |
582 | #define EV_BEGIN_WATCHER(cppstem,cstem) \ |
594 | \ |
583 | \ |
595 | struct cppstem : base<ev_ ## cstem, cppstem> \ |
584 | struct cppstem : base<ev_ ## cstem, cppstem> \ |
596 | { \ |
585 | { \ |
597 | void start () throw () \ |
586 | void start () throw () \ |
598 | { \ |
587 | { \ |
599 | ev_ ## cstem ## _start (EV_A_ static_cast<ev_ ## cstem *>(this)); \ |
588 | ev_ ## cstem ## _start (EV_A_ static_cast<ev_ ## cstem *>(this)); \ |
600 | } \ |
589 | } \ |
601 | \ |
590 | \ |
602 | void stop () throw () \ |
591 | void stop () throw () \ |
603 | { \ |
592 | { \ |
604 | ev_ ## cstem ## _stop (EV_A_ static_cast<ev_ ## cstem *>(this)); \ |
593 | ev_ ## cstem ## _stop (EV_A_ static_cast<ev_ ## cstem *>(this)); \ |
605 | } \ |
594 | } \ |
606 | \ |
595 | \ |
607 | cppstem EV_CONSTRUCT(cppstem,cstem) \ |
596 | cppstem EV_CONSTRUCT(cppstem,cstem) \ |
608 | \ |
597 | \ |
609 | ~cppstem () throw () \ |
598 | ~cppstem () throw () \ |
610 | { \ |
599 | { \ |
611 | stop (); \ |
600 | stop (); \ |
612 | } \ |
601 | } \ |
613 | \ |
602 | \ |
614 | using base<ev_ ## cstem, cppstem>::set; \ |
603 | using base<ev_ ## cstem, cppstem>::set; \ |
615 | \ |
604 | \ |
616 | private: \ |
605 | private: \ |
617 | \ |
606 | \ |
618 | cppstem (const cppstem &o); \ |
607 | cppstem (const cppstem &o); \ |
619 | \ |
608 | \ |
620 | cppstem & operator =(const cppstem &o); \ |
609 | cppstem &operator =(const cppstem &o); \ |
621 | \ |
610 | \ |
622 | public: |
611 | public: |
623 | |
612 | |
624 | #define EV_END_WATCHER(cppstem,cstem) \ |
613 | #define EV_END_WATCHER(cppstem,cstem) \ |
625 | }; |
614 | }; |
… | |
… | |
707 | start (); |
696 | start (); |
708 | } |
697 | } |
709 | EV_END_WATCHER (sig, signal) |
698 | EV_END_WATCHER (sig, signal) |
710 | |
699 | |
711 | EV_BEGIN_WATCHER (child, child) |
700 | EV_BEGIN_WATCHER (child, child) |
712 | void set (int pid) throw () |
701 | void set (int pid, int trace = 0) throw () |
713 | { |
702 | { |
714 | int active = is_active (); |
703 | int active = is_active (); |
715 | if (active) stop (); |
704 | if (active) stop (); |
716 | ev_child_set (static_cast<ev_child *>(this), pid); |
705 | ev_child_set (static_cast<ev_child *>(this), pid, trace); |
717 | if (active) start (); |
706 | if (active) start (); |
718 | } |
707 | } |
719 | |
708 | |
720 | void start (int pid) throw () |
709 | void start (int pid, int trace = 0) throw () |
721 | { |
710 | { |
722 | set (pid); |
711 | set (pid, trace); |
723 | start (); |
712 | start (); |
724 | } |
713 | } |
725 | EV_END_WATCHER (child, child) |
714 | EV_END_WATCHER (child, child) |
726 | |
715 | |
727 | #if EV_STAT_ENABLE |
716 | #if EV_STAT_ENABLE |
… | |
… | |
760 | void set () throw () { } |
749 | void set () throw () { } |
761 | EV_END_WATCHER (check, check) |
750 | EV_END_WATCHER (check, check) |
762 | |
751 | |
763 | #if EV_EMBED_ENABLE |
752 | #if EV_EMBED_ENABLE |
764 | 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 | |
765 | void start (struct ev_loop *embedded_loop) throw () |
762 | void start (struct ev_loop *embedded_loop) throw () |
766 | { |
763 | { |
767 | stop (); |
764 | set (embedded_loop); |
768 | ev_embed_set (static_cast<ev_embed *>(this), embedded_loop); |
|
|
769 | start (); |
765 | start (); |
770 | } |
766 | } |
771 | |
767 | |
772 | void sweep () |
768 | void sweep () |
773 | { |
769 | { |
… | |
… | |
780 | EV_BEGIN_WATCHER (fork, fork) |
776 | EV_BEGIN_WATCHER (fork, fork) |
781 | void set () throw () { } |
777 | void set () throw () { } |
782 | EV_END_WATCHER (fork, fork) |
778 | EV_END_WATCHER (fork, fork) |
783 | #endif |
779 | #endif |
784 | |
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 | |
785 | #undef EV_PX |
797 | #undef EV_PX |
786 | #undef EV_PX_ |
798 | #undef EV_PX_ |
787 | #undef EV_CONSTRUCT |
799 | #undef EV_CONSTRUCT |
788 | #undef EV_BEGIN_WATCHER |
800 | #undef EV_BEGIN_WATCHER |
789 | #undef EV_END_WATCHER |
801 | #undef EV_END_WATCHER |
790 | |
|
|
791 | } |
802 | } |
792 | |
803 | |
793 | #undef EV_THROW |
|
|
794 | |
|
|
795 | #endif |
804 | #endif |
796 | |
805 | |