60.5. The main function

The main function of the program is where the user instantiates the experiment specific filters and registers them to the framework. The framework clones the filter objects and manages the cloned objects. The original filter objects are left to the user to clean up. The SkeletonMain.cpp file contains the main function for the filter kit.

#include <iostream>
#include <CFatalException.h> 
#include <CFilterMain.h>     

#include "CTemplateFilter.cpp"     (1)

int main(int argc, char* argv[])
  int status = 0;

  try {

    // Create the main
    CFilterMain theApp(argc,argv); (2)

    // Construct filter(s) here.
    CTemplateFilter user_filter;   (3)

    // Register the filter(s) here. Note that if more than
    // one filter will be registered, the order of registration
    // will define the order of execution. If multiple filters are
    // registered, the output of the first filter will become the
    // input of the second filter and so on. 
    theApp.registerFilter(&user_filter); (4)

    // Run the main loop
    theApp();                       (5)

  } catch (CFatalException exc) {   (6)
    status = 1;
  } catch (...) {                   (7)
    std::cout << "Caught unknown fatal error...!" << std::endl;
    status = 2;

  return status;

In the discussion below, the numbers refer to the same numbers in the example above.

Include the source code for the CTemplateFilter class. In more elaborate filter programs this would likely just include the header file and then link the implementation at compile time. However, for simplicity, the CTemplateFilter class is defined and implemented in the same source file.
Instantiate the CFilterMain object. This object sets up the framework, including the source and sink. It also handles the parsing of command line parameters.
Instantiate the user's filter(s). There can be any number of filters instantiated here. If the user creates the filter on the heap instead of the stack, they must delete their filter at the end of the main program.
Register the user's filter(s). More than one filter can be registered and the same filter can be registered multiple times. The framework clones the filter and handles the memory allocated for the cloned object.
Run the main loop of the program. Once this is called, data start being read from the source, processed by the registered filters, and then are passed to the sink.
Catch fatal exception. The CFatalException class is a dummy class that has an empty implementation. It is just used to pass fatal errors to the main function to abort execution. By default, the filter framework will not throw fata exceptions from the main loop. These should be limited to the first stage of filter.
Catch all unhandled exceptions to exit gracefully.