#ifndef INCLUDED_BOBCAT_SELECTOR_
#define INCLUDED_BOBCAT_SELECTOR_

#include <sys/select.h>

#include <bobcat/exception>

namespace FBB
{

class Selector
{
    fd_set          d_read;
    fd_set          d_write;
    fd_set          d_except;
    fd_set          d_ret_read;
    fd_set          d_ret_write;
    fd_set          d_ret_except;
    timeval         d_alarm;
    int             d_max;
    int             d_ret;
    int             d_readidx;
    int             d_writeidx;
    int             d_exceptidx;

    public:
        Selector();

        int wait();             // nReady() and the get...() members
                                // perform defined behavior only after
                                // wait() returns.

        int nReady();           // number of available fd's. 0: timeout    .f
                                // occurred, -1: select() failed.

        int exceptFd();                                                 // .f
        int readFd();               // -1 if no more available             .f
                                    // descriptors otherwise the next
                                    // available fd in each category
        int writeFd();                                                  // .f
        void setAlarm(int sec, int usec = 0);
        void noAlarm();                                                 // .f
        void addReadFd(int fd);                                         // .f
        void addWriteFd(int fd);                                        // .f
        void addExceptFd(int fd);                                       // .f
        void rmReadFd(int fd);                                          // .f
        void rmWriteFd(int fd);                                         // .f
        void rmExceptFd(int fd);                                        // .f

    private:
        int checkSet(int *index, fd_set const &set);
        void addFd(fd_set *set, int fd);

        static bool isEmpty(fd_set const &set);
};

inline void Selector::addExceptFd(int fd)
{
    addFd(&d_except, fd);
}
inline void Selector::addReadFd(int fd)
{
    addFd(&d_read, fd);
}
inline void Selector::addWriteFd(int fd)
{
    addFd(&d_write, fd);
}
inline int Selector::exceptFd()
{
    return checkSet(&d_exceptidx, d_ret_except);
}
inline void Selector::noAlarm()
{
    d_alarm.tv_sec = d_alarm.tv_usec = -1;
}
inline int Selector::nReady()
{
    return d_ret;
}
inline int Selector::readFd()
{
    return checkSet(&d_readidx, d_ret_read);
}
inline void Selector::rmExceptFd(int fd)
{
    FD_CLR(fd, &d_except);
}
inline void Selector::rmReadFd(int fd)
{
    FD_CLR(fd, &d_read);
}
inline void Selector::rmWriteFd(int fd)
{
    FD_CLR(fd, &d_write);
}
inline int Selector::writeFd()
{
    return checkSet(&d_writeidx, d_ret_write);
}

} // FBB

#endif
