/* * Copyright (c) 1996 Gunther Schadow. All rights reserved. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #ifndef _FDSET_H #define _FDSET_H #pragma interface #include #include /* A simple C++ wrapper for fd_set used with select(2) calls. An fd_set is a set of file descriptors. The class FdSet inherits from struct fd_set (BSD declares the fd_set as a struct, I hope that there is a common agreement on this practice (POSIX?)). Thus an FdSet* can be directly used whereever an fd_set* is required, although most (all?) standard fd_set functions can be called as methods of class FdSet. */ class FdSet: public fd_set { public: FdSet() { FD_ZERO(this); } // init with an empty set FdSet(int ...); // term'd by -1 void zero () { FD_ZERO(this); } // clear the set void set (int fd) { FD_SET(fd, this); } // add a file descriptor void clear(int fd) { FD_CLR(fd, this); } // remove a file descriptor bool isset(int fd) { return FD_ISSET(fd, this); } // test for membership /* * iterators, return -1 if no channel left in set * example: for(int ch = cs.first(), ch >= 0, ch = cs.next(ch)) */ int first() const { return next(-1); } int next(int) const; int last() const { return prev(-1); } // vice versa ... int prev(int) const; int nfds() const { return last() + 1; } // the first argument of select(2) // and the select(2) call variants int select(FdSet *r, FdSet *w, FdSet *e, u_int sec, u_int usec) const; int select(FdSet *r, FdSet *w, FdSet *e) const; /* * the default bitwise copy constructor/operator are just adequate * only the likewise eq-op is missing: */ bool operator==(const FdSet &s) const { return memcmp(this, &s, sizeof(s))== 0; } }; #endif