Instanciate a vtk factory to override vtkObject for libraries using dtk-visualization
VTK OBJECT FACTORIES
-
Allow sub-classes to be used in place of parent classes in existing code.
-
Dynamic loading of new object implementations.
-
Extensions to compiled and linked vtk executables.
-
Provide a way to isolate vtk extensions from the main line vtk builds.
-
Removes the need for some ifdef's in code, for example, an OpenGL factory could replace all of the ifdef's in vtkRenderer and vtkRenderWindow.
-
Debugging of New calls. A factory can be created that does nothing, but track the number of times New is called for each class type.
-
Easier implementation of accelerated vtk objects on different hardware using a plugin model much like netscape and photoshop have.
OBJECT FACTORY IMPLEMENTATION:
The object factory follows the Abstract Factory Design Pattern. The root class is called vtkObjectFactory. vtkObjectFactory maintains a list of "registered" factories. It also contains a static class method used to create vtk objects by string name. The create method iterates over the registered factories asking each one to create the object. If a factory returns an object, no other factories are asked to create the object.
Factories can be registered in two ways. The first, at run time a program can call vtkObjectFactory::RegisterFactory(new MyFactory). The second way is using dynamic loading and the environment variable VTK_AUTOLOAD_PATH. The first time the vtkObjectFactory is asked to create an object, it loads all shared libraries or DLL's in the VTK_AUTOLOAD_PATH. For each library the "C" function vtkLoad is called to create an instance of vtkObjectFactory. This is only done the first time to avoid performance problems. However, it is possible to re-check the path for new factories at run time by calling vtkObjectFactory::ReHash().
HOW TO INSTALL A FACTORY
-
Compiled in factories need only call
vtkObjectFactory::RegisterFactory(MyFactory::New())
-
For dynamically loaded factories, a shared library or dll must be created which contains the object factory sub-class. The library must have a single "C" linkage function called vtkLoad which returns an instance of the factory provided by the library. An example vtkLoad looks like this:
extern "C" vtkLoad() { return myObjectFactory::New(); }
- The library must then be put in the VTK_AUTOLOAD_PATH. This variable follows the convention of PATH on your machine using ";" on windows, and ":" on Unix as a separator.