[Go to CFHT Home Page] Man Pages
Back to Software Index  BORDER=0Manpage Top Level
    IOHandler(3X) manual page Table of Contents

Name

IOCallback, IOHandler - read input, write output, or handle an I/O exception or timeout

Synopsis


#include <ivplus/iohandler.h>#include <ivplus/iocallback.h>

Description

IOHandler reads data from a file descriptor, writes data to a file descriptor, handles an I/O exception on a file descriptor, and handles a timer’s expiration. Once the user has requested the Dispatcher to attach an IOHandler to a file descriptor or a timer, the Dispatcher will automatically notify the IOHandler when the file descriptor’s I/O condition changes or the timer expires.

For convenience, InterViews Plus provides a template for deriving a generic class which will do nothing more than call an arbitrary object’s member function. A set of macros is defined in iocallback.h to allow a user to declare and implement a class derived directly from IOHandler. The mechanism used is described in The C++ Programming Language, by Stroustrup. See EXAMPLES for sample usage.

IOHandler is an abstract base class.

Class Hierarchy

IOHandler

Constructors

IOHandler()

Creates an instance of an IOHandler. Since IOHandler is an abstract base class, this constructor is protected.

Public Operations

virtual int exceptionRaised(int fd)

virtual int inputReady(int fd)

virtual int outputReady(int fd)

The dispatcher will call one of these functions to notify an iohandler that it can read data from a file descriptor, write data to a file descriptor, or handle an I/O exception on a file descriptor. The iohandler should perform the appropriate action and indicate (through the return value) to the dispatcher what to do next. A negative return value means that the iohandler encountered an error or it doesn’t need to read or write anything more. The dispatcher will unlink the iohandler from its file descriptor automatically. A positive return value means that the iohandler didn’t read or write everything. The dispatcher will call the iohandler again in round robin fashion without checking select, which means that the iohandler will be called again as soon as the rest of the iohandlers have performed their actions. A zero return value means that the iohandler finished reading or writing everything it was able to. The dispatcher must check the descriptor’s status with the select call before it can call the iohandler again. Each of these functions returns -1 by default.

virtual void timerExpired(long sec, long usec)

The dispatcher will call this function to notify an iohandler that a timer has expired. The parameters give the current time in seconds and microseconds since midnight January 1, 1970. If the iohandler wants to reset the timer, it must tell the dispatcher to start another timer. The default implementation does nothing.

Protected Operations

None.

X Resources

None.

Examples


//                          IOHandler/IOCallback Example
#include <ivplus/font.h>
#include <ivplus/frame.h>
#include <ivplus/painter.h>
#include <ivplus/shape.h>
#include <ivplus/world.h>
#include <ivplus/iohandler.h>
#include <ivplus/iocallback.h>
#include <ivplus/dispatcher.h>
class Blinker : public Interactor {
public:
   Blinker(const char* target);
   void Blink(long, long);
   void ActivateBlink(long s, long u);
protected:
   virtual void Redraw(IntCoord, IntCoord, IntCoord, IntCoord);
   virtual void Reconfig();
private:
   int width;
   int height;
   const char* text;
   long sec;
   long usec;
   // This object will talk to the dispatcher for us.
   IOHandler* blinker;
};
// Invoke the macro which declares a class derived from IOHandler.
// This must occur after class Blinker has been fully declared.
declare(IOCallback,Blinker)
   
// Invoke the macro which generates the implementation code.
implement(IOCallback,Blinker)
Blinker::Blinker(const char* msg)
{
   text = msg;
}
// Redraw() calculates where to draw text so it is centered.
void Blinker::Redraw(IntCoord l, IntCoord b, IntCoord r, IntCoord t)
{
   output->ClearRect(canvas, l, b, r, t);
   int tx = (xmax - output->GetFont()->Width(text))/2;
   int ty = (ymax - output->GetFont()->Height())/2;
   output->Text(canvas, text, tx, ty);
}
void Blinker::Reconfig()
{
   shape->width = output->GetFont()->Width(text);
   shape->height = output->GetFont()->Height();
   shape->Rigid(0, hfil, 0, vfil);
}
// ActivateBlink() instantiates the IOHandler that will call Blinker::Blink()
// and tells the dispatcher we want a timer started.
void Blinker::ActivateBlink(long s, long u)
{
   sec = s;
   usec = u;
   blinker = new IOCallback(Blinker)(this, Blinker::Blink);
   Dispatcher::instance().startTimer(sec, usec, blinker);
}
// Blink() ignores its arguments, since we always want to
// use the args from ActivateBlink().  Swaps the foreground and
// background colors, then forces a Redraw() by calling Draw()
// Resets the timer.
void Blinker::Blink(long, long)
{
   output->SetColors(output->GetBgColor(), output->GetFgColor());
   Draw();
   Dispatcher::instance().stopTimer(blinker);
   Dispatcher::instance().startTimer(sec, usec, blinker);
}
int main(int argc, char **argv)
{
   // Create the world to get the connection with the display.
   World* world = new World("MyApp", argc, argv);
  
   // Create the blinking thing.
   Blinker* blink = new Blinker("Blink, blink!");
 
   // Map the window.
   world->InsertApplication(new Frame(blink, 2, BevelOut));
   
   // Turn the blinking thing on.  Blink at 1 second intervals.
   blink->ActivateBlink(1,0);
   
   // Enter the event loop.
   world->Run();
   return 0;
}

Files


generic.h
iocallback.h
iohandler.h

See Also

Dispatcher(3X) .


Table of Contents