/*
 *    SCCS ID: %Z%%M% %I% %E% SST 
 *
 *    Copyright (c) 1992-1993 Mark Soloway.  All rights reserved.
 *    See SST_LEGAL_NOTICE file for terms of the license.
 */

#ifndef _POLL_FD_H
#define _POLL_FD_H

#include <stdio.h>
#include <poll.h>

#include "TTGenericArray.h"
#include "TTGenericHash.h"

typedef void (*PollFDHandler) (int, short, void *);

class PollFD : public TTGeneric {
public:
  PollFD ();
  PollFD (int, short=POLLIN);
  PollFD (const PollFD&);
  PollFD &operator= (const PollFD&);
  ~PollFD ();

  int   waitForActiveFD ();
  void *getData () const;
  short getEvents () const;

  void  addFD (int, short=POLLIN, void *data=NULL, PollFDHandler=NULL);
  void *removeFD (int);

  void setTimeOut (int);

private:
  TTGenericArray  activeFDs;
  void           *data;
  TTGenericHash   dataTable;
  short           events;
  TTGenericArray  fdsToPoll;
  int             timeOut;

  class DataEntry : public TTGeneric {
  public:
    DataEntry (void          *initial_data,
	       PollFDHandler  initial_handler)
      {
	data = initial_data;
	handler = initial_handler;
      }

    ~DataEntry ()
      {
      }

    void          *data;
    PollFDHandler  handler;
  };

  void       setPollFDDefaults ();
  void       addDataEntry (int, DataEntry *);
  DataEntry *removeDataEntry (int);
  DataEntry *getDataEntry (int) const;
  void       copy (const PollFD&);
  void       destroy ();
};

#endif
