Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Class Members | Examples

Async::FdWatch Class Reference

A class for watching file descriptors. More...

#include <AsyncFdWatch.h>

List of all members.

Public Types

Public Member Functions

Public Attributes


Detailed Description

A class for watching file descriptors.

Author:
Tobias Blomberg
Date:
2003-03-19
Use this class to watch a file descriptor for activity. The example below creates a read watch on the standard input file descriptor. That is, every time a character is typed on the keyboard (or something is piped to the application) the onActivity method in instance this of class MyClass will be called. In the handler function, the data on the file descriptor should be read. Otherwise the handler function will be called over and over again.
Note:
Since the stdin is line buffered, the ENTER key has to be pressed before anything will be shown.
#include <unistd.h>
#include <cstdio>
#include <iostream>
#include <AsyncCppApplication.h>
#include <AsyncFdWatch.h>

using namespace std;
using namespace Async;

class MyClass : public SigC::Object
{
  public:
    MyClass(void)
    {
      stdin_watch = new FdWatch(STDIN_FILENO, FdWatch::FD_WATCH_RD);
      stdin_watch->activity.connect(slot(this, &MyClass::onActivity));
    }
    
    ~MyClass(void)
    {
      delete stdin_watch;
    }

  private:
    FdWatch *stdin_watch;
    
    void onActivity(FdWatch *watch)
    {
      char buf[1024];
      int cnt = read(watch->fd(), buf, sizeof(buf)-1);
      if (cnt == -1)
      {
        perror("read");
        Application::app().quit();
        return;
      }
      buf[cnt] = 0;
      cout << "Read from STDIN: " << buf << endl;
    }
};

int main(int argc, char **argv)
{
  CppApplication app;
  MyClass my_class;
  app.exec();
}

Definition at line 119 of file AsyncFdWatch.h.


Member Enumeration Documentation

enum Async::FdWatch::FdWatchType
 

The type of the file descriptor watch.

Enumerator:
FD_WATCH_RD  File descriptor watch for incoming data.
FD_WATCH_WR  File descriptor watch for outgoing data.

Definition at line 125 of file AsyncFdWatch.h.


Constructor & Destructor Documentation

Async::FdWatch::FdWatch int  fd,
FdWatchType  type
 

Constructor.

Add the given file descriptor to the watch list and watch it for incoming data (FD_WATCH_RD) or write buffer space available (FD_WATCH_WR).

Parameters:
fd The file descriptor to watch
type The type of watch to create (see FdWatchType)

Async::FdWatch::~FdWatch void   ) 
 

Destructor.


Member Function Documentation

int Async::FdWatch::fd void   )  const [inline]
 

Return the file descriptor being watched.

Returns:
Returns the file descriptor

Definition at line 151 of file AsyncFdWatch.h.

bool Async::FdWatch::isEnabled void   )  const [inline]
 

Check if the watch is enabled or not.

Returns:
Returns true if the watch is enabled, or else false.

Definition at line 170 of file AsyncFdWatch.h.

void Async::FdWatch::setEnabled bool  enabled  ) 
 

Enable or disable the watch.

Parameters:
enabled Set to true to enable the watch or false to disable it.

FdWatchType Async::FdWatch::type void   )  const [inline]
 

Return the type of this watch.

Returns:
Returns the type (see FdWatchType)

Definition at line 157 of file AsyncFdWatch.h.


Member Data Documentation

SigC::Signal1<void, FdWatch*> Async::FdWatch::activity
 

Signal to indicate that the descriptor is active.

Parameters:
watch Pointer to the watch object

Definition at line 176 of file AsyncFdWatch.h.


The documentation for this class was generated from the following file:
Generated on Thu Dec 1 22:30:33 2005 for Async by  doxygen 1.4.4