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