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, |
… | |
… | |
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 | }; |
… | |
… | |
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 | { |
… | |
… | |
764 | |
775 | |
765 | #if EV_FORK_ENABLE |
776 | #if EV_FORK_ENABLE |
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) |
|
|
780 | #endif |
|
|
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) |
769 | #endif |
796 | #endif |
770 | |
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 |