Async  1.5.0
Public Types | Public Member Functions | Public Attributes | List of all members
Async::FdWatch Class Reference

A class for watching file descriptors. More...

#include <AsyncFdWatch.h>

Inheritance diagram for Async::FdWatch:

Public Types

enum  FdWatchType { FD_WATCH_RD, FD_WATCH_WR }
 The type of the file descriptor watch. More...
 

Public Member Functions

 FdWatch (void)
 Default constructor. More...
 
 FdWatch (int fd, FdWatchType type)
 Constructor. More...
 
 ~FdWatch (void)
 Destructor. More...
 
int fd (void) const
 Return the file descriptor being watched. More...
 
FdWatchType type (void) const
 Return the type of this watch. More...
 
void setEnabled (bool enabled)
 Enable or disable the watch. More...
 
bool isEnabled (void) const
 Check if the watch is enabled or not. More...
 
void setFd (int fd, FdWatchType type)
 Set the file descriptor to watch. More...
 

Public Attributes

sigc::signal< void, FdWatch * > activity
 Signal to indicate that the descriptor is active. More...
 

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 <AsyncFdWatch.h>
using namespace std;
using namespace Async;
class MyClass : public sigc::trackable
{
public:
MyClass(void)
{
stdin_watch = new FdWatch(STDIN_FILENO, FdWatch::FD_WATCH_RD);
stdin_watch->activity.connect(mem_fun(*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");
return;
}
buf[cnt] = 0;
cout << "Read from STDIN: " << buf << endl;
}
};
int main(int argc, char **argv)
{
MyClass my_class;
app.exec();
}
Examples
AsyncFdWatch_demo.cpp.

Definition at line 139 of file AsyncFdWatch.h.

Member Enumeration Documentation

◆ 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 177 of file AsyncFdWatch.h.

Constructor & Destructor Documentation

◆ FdWatch() [1/2]

Async::FdWatch::FdWatch ( void  )

Default constructor.

Create a disabled FdWatch. Use the setFd function to set the filedescriptor to watch and the type of watch.

◆ FdWatch() [2/2]

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
fdThe file descriptor to watch
typeThe type of watch to create (see FdWatchType)

◆ ~FdWatch()

Async::FdWatch::~FdWatch ( void  )

Destructor.

Member Function Documentation

◆ fd()

int Async::FdWatch::fd ( void  ) const
inline

Return the file descriptor being watched.

Returns
Returns the file descriptor
Examples
AsyncFdWatch_demo.cpp.

Definition at line 211 of file AsyncFdWatch.h.

◆ isEnabled()

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 230 of file AsyncFdWatch.h.

◆ setEnabled()

void Async::FdWatch::setEnabled ( bool  enabled)

Enable or disable the watch.

Parameters
enabledSet to true to enable the watch or false to disable it.

◆ setFd()

void Async::FdWatch::setFd ( int  fd,
FdWatchType  type 
)

Set the file descriptor to watch.

Parameters
fdThe file descriptor to watch
typeThe type of watch to create (see FdWatchType)

This function can be used at any time to change the file descriptor or type of watch. If the watch was disabled it will stay disabled until explicitly being enabled.

◆ type()

FdWatchType Async::FdWatch::type ( void  ) const
inline

Return the type of this watch.

Returns
Returns the type (see FdWatchType)

Definition at line 217 of file AsyncFdWatch.h.

Member Data Documentation

◆ activity

sigc::signal<void, FdWatch*> Async::FdWatch::activity

Signal to indicate that the descriptor is active.

Parameters
watchPointer to the watch object

Definition at line 247 of file AsyncFdWatch.h.


The documentation for this class was generated from the following file:
Async::FdWatch::FdWatch
FdWatch(void)
Default constructor.
Async::FdWatch::fd
int fd(void) const
Return the file descriptor being watched.
Definition: AsyncFdWatch.h:211
Async::FdWatch::FD_WATCH_RD
@ FD_WATCH_RD
File descriptor watch for incoming data.
Definition: AsyncFdWatch.h:211
Async::CppApplication::exec
void exec(void)
Execute the application main loop.
Async::CppApplication
An application class for writing non GUI applications.
Definition: AsyncCppApplication.h:156
AsyncFdWatch.h
Contains a watch for file descriptors.
Async::Application::app
static Application & app(void)
Get the one and only application instance.
Async::Application::quit
virtual void quit(void)=0
Exit the application main loop.
AsyncCppApplication.h
The core class for writing asyncronous cpp applications.
Async
Namespace for the asynchronous programming classes.
Definition: AsyncApplication.h:75
Async::FdWatch
A class for watching file descriptors.
Definition: AsyncFdWatch.h:139