#ifndef _FDSET_
#define _FDSET_

#include "../sys/sys"
#include "../error/error"
#include "../config/config"
#include "../SocketHandling/socket/socket"

using namespace std;

class Fdset {
public:
    Fdset(int t);

    int timeout() const 		{ return tsec; }
    void timeout(int t) 		{ tsec = t; }

    void add(int fd) 			{ set.push_back(fd); }
    void add(Socket &s)			{ set.push_back(s.fd()); }

    unsigned size() const 		{ return set.size(); }

    int fd(unsigned index) 		{ return set[index]; }

    double wait(bool wait_read, bool wait_write);
    double wait_rw()			{ return wait(true, true); }
    double wait_r()			{ return wait(true, false); }
    double wait_w()			{ return wait(false, true); }
    
    bool readable(int fd)  		{ return FD_ISSET(fd, &readset); }
    bool readable(Socket &s)		{ return FD_ISSET(s.fd(), &readset); }
    bool writeable(int fd) 		{ return FD_ISSET(fd, &writeset); }
    bool writeable(Socket &s) 		{ return FD_ISSET(s.fd(), &writeset); }
    
    
private:
    int tsec;
    fd_set readset, writeset, exceptset;
    vector<int> set;
};

#endif
