I have a very simple FDMonitor class, shown below. Just a 
wrapper for fl_add_io_callback, fl_remove_io_callback.

My problem is that the xforms routine that watches this returns 
the bloody call back on every cycle if the file is not connected 
to both a read stream and a write stream. (On Tru64 Unix). I was 
thinking of testing also the timestamp of the thing and only 
emitting the boost::function if it changed.

Good idea? JMarc, I guess this is directed at you too since you 
too have a Tru64 machine...

Angus

namespace {

extern "C"
void C_read_callback(int, void * data)
{
        FDMonitor * ptr = static_cast<FDMonitor*>(data);
        ptr->ready();
}

}

class FDMonitor {
public:
        ///
        FDMonitor() : fd_(-1) {}

        /** One FDMonitor instance can monitor 
         *  only one file device at a time.
         */
        void start(int fd)
        {
                if (fd <= 0) return;
                stop();
                fd_ = fd;
                fl_add_io_callback(fd, FL_READ, C_read_callback, this);
        }
        ///
        void stop()
        {
                if (fd_ == -1)
                        return;
                fl_remove_io_callback(fd_, FL_READ,
                                         C_read_callback);
        }

        /** connect and you'll be informed when the 
         *  file device is ready for reading.
         */
        typedef boost::function0<void> function_type;
        ///
        function_type & ready() const { return ready_; }
private:
        ///
        boost::function0<void> ready_;
        ///
        int fd_;
};
        

Reply via email to