--- rxvt-unicode/src/iom.h 2003/12/19 06:17:03 1.5 +++ rxvt-unicode/src/iom.h 2004/01/16 22:11:09 1.6 @@ -22,18 +22,26 @@ #include -#include "rxvtvec.h" #include "callback.h" +#include "rxvtvec.h" -#define IOM_IO 1 -#define IOM_TIME 1 -#define IOM_CHECK 1 -#define IOM_IDLE 0 +#ifndef IOM_IO +# define IOM_IO 1 +#endif +#ifndef IOM_TIME +# define IOM_TIME 1 +#endif +#ifndef IOM_CHECK +# define IOM_CHECK 1 +#endif +#ifndef IOM_IDLE +# define IOM_IDLE 0 +#endif -#if IOM_IO - typedef double tstamp; - extern tstamp NOW; +typedef double tstamp; +extern tstamp NOW; +#if IOM_IO struct io_watcher; #endif #if IOM_TIME @@ -46,18 +54,33 @@ struct idle_watcher; #endif +template +struct io_manager_vec : protected simplevec { + friend class io_manager; +protected: + void erase_unordered (unsigned int pos) + { + watcher *w = (*this)[size () - 1]; + pop_back (); + + if (size ()) + if ((*this)[pos] = w) + w->active = pos + 1; + } +}; + class io_manager { #if IOM_IO - simplevec iow; + io_manager_vec iow; #endif #if IOM_CHECK - simplevec cw; + io_manager_vec cw; #endif #if IOM_TIME - simplevec tw; + io_manager_vec tw; #endif #if IOM_IDLE - simplevec iw; + io_manager_vec iw; #endif template @@ -89,42 +112,38 @@ extern io_manager iom; // a singleton, together with it's construction/destruction problems. +struct watcher { + int active; /* 0 == inactive, else index into respective vector */ + + watcher() : active(0) { } +}; + #if IOM_IO enum { EVENT_READ = 1, EVENT_WRITE = 2 }; -struct io_watcher : callback2 { +struct io_watcher : watcher, callback2 { int fd; short events; - template - io_watcher (O1 *object, void (O2::*method)(io_watcher &, short)) - : callback2(object,method) - { } - - ~io_watcher (); - void set (int fd_, short events_) { fd = fd_; events = events_; } void set (short events_) { set (fd, events_); } void start () { iom.reg (this); } void start (int fd_, short events_) { set (fd_, events_); iom.reg (this); } void stop () { iom.unreg (this); } + + template + io_watcher (O1 *object, void (O2::*method)(io_watcher &, short)) + : callback2(object,method) + { } + ~io_watcher () { stop (); } }; #endif #if IOM_TIME -enum { TSTAMP_CANCEL = -1 }; - -struct time_watcher : callback1 { +struct time_watcher : watcher, callback1 { tstamp at; - template - time_watcher (O1 *object, void (O2::*method)(time_watcher &)) - : callback1(object,method) - { } - - ~time_watcher (); - void trigger (); void set (tstamp when) { at = when; } @@ -133,41 +152,39 @@ void start (tstamp when) { set (when); iom.reg (this); } void stop () { iom.unreg (this); } - void reset (tstamp when = TSTAMP_CANCEL) - { - stop (); - at = when; - } + template + time_watcher (O1 *object, void (O2::*method)(time_watcher &)) + : callback1(object,method) + { } + ~time_watcher () { stop (); } }; #endif #if IOM_CHECK // run before checking for new events -struct check_watcher : callback1 { - template - check_watcher (O1 *object, void (O2::*method)(check_watcher &)) - : callback1(object,method) - { } - - ~check_watcher (); - +struct check_watcher : watcher, callback1 { void start () { iom.reg (this); } void stop () { iom.unreg (this); } + + template + check_watcher (O1 *object, void (O2::*method)(check_watcher &)) + : callback1(object,method) + { } + ~check_watcher () { stop (); } }; #endif #if IOM_IDLE // run after checking for any i/o, but before waiting -struct idle_watcher : callback1 { +struct idle_watcher : watcher, callback1 { + void start () { iom.reg (this); } + void stop () { iom.unreg (this); } + template idle_watcher (O1 *object, void (O2::*method)(idle_watcher &)) : callback1(object,method) { } - - ~idle_watcher (); - - void start () { iom.reg (this); } - void stop () { iom.unreg (this); } + ~idle_watcher () { stop (); } }; #endif