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