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 | * |
… | |
… | |
70 | STAT = EV_STAT, |
70 | STAT = EV_STAT, |
71 | IDLE = EV_IDLE, |
71 | IDLE = EV_IDLE, |
72 | CHECK = EV_CHECK, |
72 | CHECK = EV_CHECK, |
73 | PREPARE = EV_PREPARE, |
73 | PREPARE = EV_PREPARE, |
74 | FORK = EV_FORK, |
74 | FORK = EV_FORK, |
|
|
75 | ASYNC = EV_ASYNC, |
75 | EMBED = EV_EMBED, |
76 | EMBED = EV_EMBED, |
76 | ERROR = EV_ERROR, |
77 | ERROR = EV_ERROR, |
77 | }; |
78 | }; |
78 | |
79 | |
79 | enum |
80 | enum |
80 | { |
81 | { |
81 | AUTO = EVFLAG_AUTO, |
82 | AUTO = EVFLAG_AUTO, |
82 | NOENV = EVFLAG_NOENV, |
83 | NOENV = EVFLAG_NOENV, |
83 | FORKCHECK = EVFLAG_FORKCHECK, |
84 | FORKCHECK = EVFLAG_FORKCHECK, |
|
|
85 | |
84 | SELECT = EVBACKEND_SELECT, |
86 | SELECT = EVBACKEND_SELECT, |
85 | POLL = EVBACKEND_POLL, |
87 | POLL = EVBACKEND_POLL, |
86 | EPOLL = EVBACKEND_EPOLL, |
88 | EPOLL = EVBACKEND_EPOLL, |
87 | KQUEUE = EVBACKEND_KQUEUE, |
89 | KQUEUE = EVBACKEND_KQUEUE, |
88 | DEVPOLL = EVBACKEND_DEVPOLL, |
90 | DEVPOLL = EVBACKEND_DEVPOLL, |
89 | PORT = EVBACKEND_PORT |
91 | PORT = EVBACKEND_PORT |
90 | }; |
92 | }; |
91 | |
93 | |
92 | enum |
94 | enum |
93 | { |
95 | { |
94 | NONBLOCK = EVLOOP_NONBLOCK, |
96 | NONBLOCK = EVLOOP_NONBLOCK, |
95 | ONESHOT = EVLOOP_ONESHOT |
97 | ONESHOT = EVLOOP_ONESHOT |
96 | }; |
98 | }; |
97 | |
99 | |
98 | enum how_t |
100 | enum how_t |
99 | { |
101 | { |
100 | ONE = EVUNLOOP_ONE, |
102 | ONE = EVUNLOOP_ONE, |
… | |
… | |
561 | ev_set_syserr_cb (cb); |
563 | ev_set_syserr_cb (cb); |
562 | } |
564 | } |
563 | |
565 | |
564 | #if EV_MULTIPLICITY |
566 | #if EV_MULTIPLICITY |
565 | #define EV_CONSTRUCT(cppstem,cstem) \ |
567 | #define EV_CONSTRUCT(cppstem,cstem) \ |
566 | (EV_PX = get_default_loop ()) throw () \ |
568 | (EV_PX = get_default_loop ()) throw () \ |
567 | : base<ev_ ## cstem, cppstem> (EV_A) \ |
569 | : base<ev_ ## cstem, cppstem> (EV_A) \ |
568 | { \ |
570 | { \ |
569 | } |
571 | } |
570 | #else |
572 | #else |
571 | #define EV_CONSTRUCT(cppstem,cstem) \ |
573 | #define EV_CONSTRUCT(cppstem,cstem) \ |
572 | () throw () \ |
574 | () throw () \ |
573 | { \ |
575 | { \ |
574 | } |
576 | } |
575 | #endif |
577 | #endif |
576 | |
578 | |
577 | /* using a template here would require quite a bit more lines, |
579 | /* using a template here would require quite a bit more lines, |
578 | * so a macro solution was chosen */ |
580 | * so a macro solution was chosen */ |
579 | #define EV_BEGIN_WATCHER(cppstem,cstem) \ |
581 | #define EV_BEGIN_WATCHER(cppstem,cstem) \ |
580 | \ |
582 | \ |
581 | struct cppstem : base<ev_ ## cstem, cppstem> \ |
583 | struct cppstem : base<ev_ ## cstem, cppstem> \ |
582 | { \ |
584 | { \ |
583 | void start () throw () \ |
585 | void start () throw () \ |
584 | { \ |
586 | { \ |
585 | ev_ ## cstem ## _start (EV_A_ static_cast<ev_ ## cstem *>(this)); \ |
587 | ev_ ## cstem ## _start (EV_A_ static_cast<ev_ ## cstem *>(this)); \ |
586 | } \ |
588 | } \ |
587 | \ |
589 | \ |
588 | void stop () throw () \ |
590 | void stop () throw () \ |
589 | { \ |
591 | { \ |
590 | ev_ ## cstem ## _stop (EV_A_ static_cast<ev_ ## cstem *>(this)); \ |
592 | ev_ ## cstem ## _stop (EV_A_ static_cast<ev_ ## cstem *>(this)); \ |
591 | } \ |
593 | } \ |
592 | \ |
594 | \ |
593 | cppstem EV_CONSTRUCT(cppstem,cstem) \ |
595 | cppstem EV_CONSTRUCT(cppstem,cstem) \ |
594 | \ |
596 | \ |
595 | ~cppstem () throw () \ |
597 | ~cppstem () throw () \ |
596 | { \ |
598 | { \ |
597 | stop (); \ |
599 | stop (); \ |
598 | } \ |
600 | } \ |
599 | \ |
601 | \ |
600 | using base<ev_ ## cstem, cppstem>::set; \ |
602 | using base<ev_ ## cstem, cppstem>::set; \ |
601 | \ |
603 | \ |
602 | private: \ |
604 | private: \ |
603 | \ |
605 | \ |
604 | cppstem (const cppstem &o); \ |
606 | cppstem (const cppstem &o); \ |
605 | \ |
607 | \ |
606 | cppstem & operator =(const cppstem &o); \ |
608 | cppstem &operator =(const cppstem &o); \ |
607 | \ |
609 | \ |
608 | public: |
610 | public: |
609 | |
611 | |
610 | #define EV_END_WATCHER(cppstem,cstem) \ |
612 | #define EV_END_WATCHER(cppstem,cstem) \ |
611 | }; |
613 | }; |
… | |
… | |
746 | void set () throw () { } |
748 | void set () throw () { } |
747 | EV_END_WATCHER (check, check) |
749 | EV_END_WATCHER (check, check) |
748 | |
750 | |
749 | #if EV_EMBED_ENABLE |
751 | #if EV_EMBED_ENABLE |
750 | 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 | |
751 | void start (struct ev_loop *embedded_loop) throw () |
761 | void start (struct ev_loop *embedded_loop) throw () |
752 | { |
762 | { |
753 | stop (); |
763 | set (embedded_loop); |
754 | ev_embed_set (static_cast<ev_embed *>(this), embedded_loop); |
|
|
755 | start (); |
764 | start (); |
756 | } |
765 | } |
757 | |
766 | |
758 | void sweep () |
767 | void sweep () |
759 | { |
768 | { |
… | |
… | |
764 | |
773 | |
765 | #if EV_FORK_ENABLE |
774 | #if EV_FORK_ENABLE |
766 | EV_BEGIN_WATCHER (fork, fork) |
775 | EV_BEGIN_WATCHER (fork, fork) |
767 | void set () throw () { } |
776 | void set () throw () { } |
768 | EV_END_WATCHER (fork, fork) |
777 | EV_END_WATCHER (fork, fork) |
|
|
778 | #endif |
|
|
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) |
769 | #endif |
794 | #endif |
770 | |
795 | |
771 | #undef EV_PX |
796 | #undef EV_PX |
772 | #undef EV_PX_ |
797 | #undef EV_PX_ |
773 | #undef EV_CONSTRUCT |
798 | #undef EV_CONSTRUCT |