Subclassing to provide functionality consists of the following steps:
Consider for example the CFileEvent class. This class provides a framework for handling events which occur as a result of a file descriptor becomming readable, writable or having an exceptional condition. (For user documentation of CFileEvent, see the File Event handling class ):
#include <iostream.h> #include <stdio.h> #include <spectrodaq.h> #include <SpectroFramework.h> class Echo : public CFileEvent { public: Echo(int fd, const char* pName); virtual void OnReadable(istream& rin); }; Echo::Echo(int fd, const char* pName): CFileEvent(fd, pName) { AppendClassInfo(); } void Echo::OnReadable(istream& rin) { CFileEvent::OnReadable(rin); string word; rin >> word; cout << word << endl; } class MyApp : public DAQROCNode { protected: virtual int operator()(int argc, char** argv); }; int MyApp::operator()(int argc, char** argv) { Echo echo(fileno(stdin), "EchoProcessor"); echo.Enable(); DAQThreadId id = echo.getThreadId(); Join(id); // Wait for echo to exit. } MyApp theapp;
The class declaration for Echo defines a new class derived from CFileEvent. In addition to implementing a constructor, this application specific class overrides CFileEvent::OnReadable. That member is called when the file descriptor represented by the object becomes readable.
The implementation of our override is to call the base class function (CFileEvent::OnReadable), and to read a string from the input file and echo it back to stdout (cout).
The application's operator() (entry point) creates an instance of echo monitoring stdin. It enables execution of the event object via a call to the object's Enable() member. Enable creates an independent thread of execution. The event serializes execution of the OnReadable member, and other user overridable members so that the programmer, for the most part, doe not need to worry about thread/thread synchronization (see Synchronizing multiple threads of execution ).
Once the thread is started, it's thread Id is gotten and the main program blocks until the Event thread exits (never).