1 | /* |
1 | /* |
2 | * libev simple C++ wrapper classes |
2 | * libev simple C++ wrapper classes |
3 | * |
3 | * |
4 | * Copyright (c) 2007,2008,2010 Marc Alexander Lehmann <libev@schmorp.de> |
4 | * Copyright (c) 2007,2008,2010,2018,2020 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 | * |
… | |
… | |
419 | #endif |
419 | #endif |
420 | |
420 | |
421 | template<class ev_watcher, class watcher> |
421 | template<class ev_watcher, class watcher> |
422 | struct base : ev_watcher |
422 | struct base : ev_watcher |
423 | { |
423 | { |
|
|
424 | // scoped pause/unpause of a watcher |
|
|
425 | struct freeze_guard |
|
|
426 | { |
|
|
427 | watcher &w; |
|
|
428 | bool active; |
|
|
429 | |
|
|
430 | freeze_guard (watcher *self) EV_NOEXCEPT |
|
|
431 | : w (*self), active (w.is_active ()) |
|
|
432 | { |
|
|
433 | if (active) w.stop (); |
|
|
434 | } |
|
|
435 | |
|
|
436 | ~freeze_guard () |
|
|
437 | { |
|
|
438 | if (active) w.start (); |
|
|
439 | } |
|
|
440 | }; |
|
|
441 | |
424 | #if EV_MULTIPLICITY |
442 | #if EV_MULTIPLICITY |
425 | EV_PX; |
443 | EV_PX; |
426 | |
444 | |
427 | // loop set |
445 | // loop set |
428 | void set (EV_P) EV_NOEXCEPT |
446 | void set (EV_P) EV_NOEXCEPT |
… | |
… | |
562 | ev_set_syserr_cb (cb); |
580 | ev_set_syserr_cb (cb); |
563 | } |
581 | } |
564 | |
582 | |
565 | #if EV_MULTIPLICITY |
583 | #if EV_MULTIPLICITY |
566 | #define EV_CONSTRUCT(cppstem,cstem) \ |
584 | #define EV_CONSTRUCT(cppstem,cstem) \ |
567 | (EV_PX = get_default_loop ()) EV_NOEXCEPT \ |
585 | (EV_PX = get_default_loop ()) EV_NOEXCEPT \ |
568 | : base<ev_ ## cstem, cppstem> (EV_A) \ |
586 | : base<ev_ ## cstem, cppstem> (EV_A) \ |
569 | { \ |
587 | { \ |
570 | } |
588 | } |
571 | #else |
589 | #else |
572 | #define EV_CONSTRUCT(cppstem,cstem) \ |
590 | #define EV_CONSTRUCT(cppstem,cstem) \ |
573 | () EV_NOEXCEPT \ |
591 | () EV_NOEXCEPT \ |
574 | { \ |
592 | { \ |
575 | } |
593 | } |
576 | #endif |
594 | #endif |
577 | |
595 | |
578 | /* using a template here would require quite a few more lines, |
596 | /* using a template here would require quite a few more lines, |
579 | * so a macro solution was chosen */ |
597 | * so a macro solution was chosen */ |
580 | #define EV_BEGIN_WATCHER(cppstem,cstem) \ |
598 | #define EV_BEGIN_WATCHER(cppstem,cstem) \ |
581 | \ |
599 | \ |
582 | struct cppstem : base<ev_ ## cstem, cppstem> \ |
600 | struct cppstem : base<ev_ ## cstem, cppstem> \ |
583 | { \ |
601 | { \ |
584 | void start () EV_NOEXCEPT \ |
602 | void start () EV_NOEXCEPT \ |
585 | { \ |
603 | { \ |
586 | ev_ ## cstem ## _start (EV_A_ static_cast<ev_ ## cstem *>(this)); \ |
604 | ev_ ## cstem ## _start (EV_A_ static_cast<ev_ ## cstem *>(this)); \ |
587 | } \ |
605 | } \ |
588 | \ |
606 | \ |
589 | void stop () EV_NOEXCEPT \ |
607 | void stop () EV_NOEXCEPT \ |
590 | { \ |
608 | { \ |
591 | ev_ ## cstem ## _stop (EV_A_ static_cast<ev_ ## cstem *>(this)); \ |
609 | ev_ ## cstem ## _stop (EV_A_ static_cast<ev_ ## cstem *>(this)); \ |
592 | } \ |
610 | } \ |
593 | \ |
611 | \ |
594 | cppstem EV_CONSTRUCT(cppstem,cstem) \ |
612 | cppstem EV_CONSTRUCT(cppstem,cstem) \ |
595 | \ |
613 | \ |
596 | ~cppstem () EV_NOEXCEPT \ |
614 | ~cppstem () EV_NOEXCEPT \ |
597 | { \ |
615 | { \ |
598 | stop (); \ |
616 | stop (); \ |
599 | } \ |
617 | } \ |
600 | \ |
618 | \ |
601 | using base<ev_ ## cstem, cppstem>::set; \ |
619 | using base<ev_ ## cstem, cppstem>::set; \ |
… | |
… | |
612 | }; |
630 | }; |
613 | |
631 | |
614 | EV_BEGIN_WATCHER (io, io) |
632 | EV_BEGIN_WATCHER (io, io) |
615 | void set (int fd, int events) EV_NOEXCEPT |
633 | void set (int fd, int events) EV_NOEXCEPT |
616 | { |
634 | { |
617 | int active = is_active (); |
635 | freeze_guard freeze (this); |
618 | if (active) stop (); |
|
|
619 | ev_io_set (static_cast<ev_io *>(this), fd, events); |
636 | ev_io_set (static_cast<ev_io *>(this), fd, events); |
620 | if (active) start (); |
|
|
621 | } |
637 | } |
622 | |
638 | |
623 | void set (int events) EV_NOEXCEPT |
639 | void set (int events) EV_NOEXCEPT |
624 | { |
640 | { |
625 | int active = is_active (); |
641 | freeze_guard freeze (this); |
626 | if (active) stop (); |
|
|
627 | ev_io_set (static_cast<ev_io *>(this), fd, events); |
642 | ev_io_modify (static_cast<ev_io *>(this), events); |
628 | if (active) start (); |
|
|
629 | } |
643 | } |
630 | |
644 | |
631 | void start (int fd, int events) EV_NOEXCEPT |
645 | void start (int fd, int events) EV_NOEXCEPT |
632 | { |
646 | { |
633 | set (fd, events); |
647 | set (fd, events); |
… | |
… | |
636 | EV_END_WATCHER (io, io) |
650 | EV_END_WATCHER (io, io) |
637 | |
651 | |
638 | EV_BEGIN_WATCHER (timer, timer) |
652 | EV_BEGIN_WATCHER (timer, timer) |
639 | void set (ev_tstamp after, ev_tstamp repeat = 0.) EV_NOEXCEPT |
653 | void set (ev_tstamp after, ev_tstamp repeat = 0.) EV_NOEXCEPT |
640 | { |
654 | { |
641 | int active = is_active (); |
655 | freeze_guard freeze (this); |
642 | if (active) stop (); |
|
|
643 | ev_timer_set (static_cast<ev_timer *>(this), after, repeat); |
656 | ev_timer_set (static_cast<ev_timer *>(this), after, repeat); |
644 | if (active) start (); |
|
|
645 | } |
657 | } |
646 | |
658 | |
647 | void start (ev_tstamp after, ev_tstamp repeat = 0.) EV_NOEXCEPT |
659 | void start (ev_tstamp after, ev_tstamp repeat = 0.) EV_NOEXCEPT |
648 | { |
660 | { |
649 | set (after, repeat); |
661 | set (after, repeat); |
… | |
… | |
663 | |
675 | |
664 | #if EV_PERIODIC_ENABLE |
676 | #if EV_PERIODIC_ENABLE |
665 | EV_BEGIN_WATCHER (periodic, periodic) |
677 | EV_BEGIN_WATCHER (periodic, periodic) |
666 | void set (ev_tstamp at, ev_tstamp interval = 0.) EV_NOEXCEPT |
678 | void set (ev_tstamp at, ev_tstamp interval = 0.) EV_NOEXCEPT |
667 | { |
679 | { |
668 | int active = is_active (); |
680 | freeze_guard freeze (this); |
669 | if (active) stop (); |
|
|
670 | ev_periodic_set (static_cast<ev_periodic *>(this), at, interval, 0); |
681 | ev_periodic_set (static_cast<ev_periodic *>(this), at, interval, 0); |
671 | if (active) start (); |
|
|
672 | } |
682 | } |
673 | |
683 | |
674 | void start (ev_tstamp at, ev_tstamp interval = 0.) EV_NOEXCEPT |
684 | void start (ev_tstamp at, ev_tstamp interval = 0.) EV_NOEXCEPT |
675 | { |
685 | { |
676 | set (at, interval); |
686 | set (at, interval); |
… | |
… | |
686 | |
696 | |
687 | #if EV_SIGNAL_ENABLE |
697 | #if EV_SIGNAL_ENABLE |
688 | EV_BEGIN_WATCHER (sig, signal) |
698 | EV_BEGIN_WATCHER (sig, signal) |
689 | void set (int signum) EV_NOEXCEPT |
699 | void set (int signum) EV_NOEXCEPT |
690 | { |
700 | { |
691 | int active = is_active (); |
701 | freeze_guard freeze (this); |
692 | if (active) stop (); |
|
|
693 | ev_signal_set (static_cast<ev_signal *>(this), signum); |
702 | ev_signal_set (static_cast<ev_signal *>(this), signum); |
694 | if (active) start (); |
|
|
695 | } |
703 | } |
696 | |
704 | |
697 | void start (int signum) EV_NOEXCEPT |
705 | void start (int signum) EV_NOEXCEPT |
698 | { |
706 | { |
699 | set (signum); |
707 | set (signum); |
… | |
… | |
704 | |
712 | |
705 | #if EV_CHILD_ENABLE |
713 | #if EV_CHILD_ENABLE |
706 | EV_BEGIN_WATCHER (child, child) |
714 | EV_BEGIN_WATCHER (child, child) |
707 | void set (int pid, int trace = 0) EV_NOEXCEPT |
715 | void set (int pid, int trace = 0) EV_NOEXCEPT |
708 | { |
716 | { |
709 | int active = is_active (); |
717 | freeze_guard freeze (this); |
710 | if (active) stop (); |
|
|
711 | ev_child_set (static_cast<ev_child *>(this), pid, trace); |
718 | ev_child_set (static_cast<ev_child *>(this), pid, trace); |
712 | if (active) start (); |
|
|
713 | } |
719 | } |
714 | |
720 | |
715 | void start (int pid, int trace = 0) EV_NOEXCEPT |
721 | void start (int pid, int trace = 0) EV_NOEXCEPT |
716 | { |
722 | { |
717 | set (pid, trace); |
723 | set (pid, trace); |
… | |
… | |
722 | |
728 | |
723 | #if EV_STAT_ENABLE |
729 | #if EV_STAT_ENABLE |
724 | EV_BEGIN_WATCHER (stat, stat) |
730 | EV_BEGIN_WATCHER (stat, stat) |
725 | void set (const char *path, ev_tstamp interval = 0.) EV_NOEXCEPT |
731 | void set (const char *path, ev_tstamp interval = 0.) EV_NOEXCEPT |
726 | { |
732 | { |
727 | int active = is_active (); |
733 | freeze_guard freeze (this); |
728 | if (active) stop (); |
|
|
729 | ev_stat_set (static_cast<ev_stat *>(this), path, interval); |
734 | ev_stat_set (static_cast<ev_stat *>(this), path, interval); |
730 | if (active) start (); |
|
|
731 | } |
735 | } |
732 | |
736 | |
733 | void start (const char *path, ev_tstamp interval = 0.) EV_NOEXCEPT |
737 | void start (const char *path, ev_tstamp interval = 0.) EV_NOEXCEPT |
734 | { |
738 | { |
735 | stop (); |
739 | stop (); |
… | |
… | |
764 | |
768 | |
765 | #if EV_EMBED_ENABLE |
769 | #if EV_EMBED_ENABLE |
766 | EV_BEGIN_WATCHER (embed, embed) |
770 | EV_BEGIN_WATCHER (embed, embed) |
767 | void set_embed (struct ev_loop *embedded_loop) EV_NOEXCEPT |
771 | void set_embed (struct ev_loop *embedded_loop) EV_NOEXCEPT |
768 | { |
772 | { |
769 | int active = is_active (); |
773 | freeze_guard freeze (this); |
770 | if (active) stop (); |
|
|
771 | ev_embed_set (static_cast<ev_embed *>(this), embedded_loop); |
774 | ev_embed_set (static_cast<ev_embed *>(this), embedded_loop); |
772 | if (active) start (); |
|
|
773 | } |
775 | } |
774 | |
776 | |
775 | void start (struct ev_loop *embedded_loop) EV_NOEXCEPT |
777 | void start (struct ev_loop *embedded_loop) EV_NOEXCEPT |
776 | { |
778 | { |
777 | set (embedded_loop); |
779 | set (embedded_loop); |