1 /*=========================================================================
3 Program: Visualization Toolkit
4 Module: $RCSfile: vtkObjectFactory.h,v $
6 Date: $Date: 2002/01/22 15:25:48 $
7 Version: $Revision: 1.23 $
9 Copyright (c) 1993-2002 Ken Martin, Will Schroeder, Bill Lorensen
11 See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
13 This software is distributed WITHOUT ANY WARRANTY; without even
14 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 PURPOSE. See the above copyright notice for more information.
17 =========================================================================*/
18 // .NAME vtkObjectFactory - abstract base class for vtkObjectFactories
19 // .SECTION Description
20 // vtkObjectFactory is used to create vtk objects. The base class
21 // vtkObjectFactory contains a static method CreateInstance which is used
22 // to create vtk objects from the list of registered vtkObjectFactory
23 // sub-classes. The first time CreateInstance is called, all dll's or shared
24 // libraries in the environment variable VTK_AUTOLOAD_PATH are loaded into
25 // the current process. The C functions vtkLoad, vtkGetFactoryCompilerUsed,
26 // and vtkGetFactoryVersion are called on each dll. To implement these
27 // functions in a shared library or dll, use the macro:
28 // VTK_FACTORY_INTERFACE_IMPLEMENT.
29 // VTK_AUTOLOAD_PATH is an environment variable
30 // containing a colon separated (semi-colon on win32) list of paths.
32 // The vtkObjectFactory can be use to override the creation of any object
33 // in VTK with a sub-class of that object. The factories can be registered
34 // either at run time with the VTK_AUTOLOAD_PATH, or at compile time
35 // with the vtkObjectFactory::RegisterFactory method.
40 #ifndef __vtkObjectFactory_h
41 #define __vtkObjectFactory_h
46 #include "vtkObject.h"
48 class vtkObjectFactoryCollection;
49 class vtkOverrideInformationCollection;
52 class VTK_COMMON_EXPORT vtkObjectFactory : public vtkObject
55 // Class Methods used to interface with the registered factories
58 // Create and return an instance of the named vtk object.
59 // Each loaded vtkObjectFactory will be asked in the order
60 // the factory was in the VTK_AUTOLOAD_PATH. After the
61 // first factory returns the object no other factories are asked.
62 static vtkObject* CreateInstance(const char* vtkclassname);
65 // Create all possible instances of the named vtk object.
66 // Each registered vtkObjectFactory will be asked, and the
67 // result will be stored in the user allocated vtkCollection
68 // passed in to the function.
69 static void CreateAllInstance(const char* vtkclassname,
70 vtkCollection* retList);
72 // Re-check the VTK_AUTOLOAD_PATH for new factory libraries.
73 // This calls UnRegisterAll before re-loading
76 // Register a factory so it can be used to create vtk objects
77 static void RegisterFactory(vtkObjectFactory* );
79 // Remove a factory from the list of registered factories
80 static void UnRegisterFactory(vtkObjectFactory*);
82 // Unregister all factories
83 static void UnRegisterAllFactories();
86 // Return the list of all registered factories. This is NOT a copy,
87 // do not remove items from this list!
88 static vtkObjectFactoryCollection* GetRegisteredFactories();
91 // return 1 if one of the registered factories
92 // overrides the given class name
93 static int HasOverrideAny(const char* className);
96 // Fill the given collection with all the overrides for
97 // the class with the given name.
98 static void GetOverrideInformation(const char* name,
99 vtkOverrideInformationCollection*);
102 // Set the enable flag for a given named class for all registered
104 static void SetAllEnableFlags(int flag,
105 const char* className);
107 // Set the enable flag for a given named class subclass pair
108 // for all registered factories.
109 static void SetAllEnableFlags(int flag,
110 const char* className,
111 const char* subclassName);
113 // Instance methods to be used on individual instances of vtkObjectFactory
115 // Methods from vtkObject
116 vtkTypeRevisionMacro(vtkObjectFactory,vtkObject);
118 // Print ObjectFactory to stream.
119 virtual void PrintSelf(ostream& os, vtkIndent indent);
122 // All sub-classes of vtkObjectFactory should must return the version of
123 // VTK they were built with. This should be implemented with the macro
124 // VTK_SOURCE_VERSION and NOT a call to vtkVersion::GetVTKSourceVersion.
125 // As the version needs to be compiled into the file as a string constant.
126 // This is critical to determine possible incompatible dynamic factory loads.
127 virtual const char* GetVTKSourceVersion() = 0;
130 // Return a descriptive string describing the factory.
131 virtual const char* GetDescription() = 0;
134 // Return number of overrides this factory can create.
135 virtual int GetNumberOfOverrides();
138 // Return the name of a class override at the given index.
139 virtual const char* GetClassOverrideName(int index);
142 // Return the name of the class that will override the class
143 // at the given index
144 virtual const char* GetClassOverrideWithName(int index);
147 // Return the enable flag for the class at the given index.
148 virtual int GetEnableFlag(int index);
151 // Return the description for a the class override at the given
153 virtual const char* GetOverrideDescription(int index);
156 // Set and Get the Enable flag for the specific override of className.
157 // if subclassName is null, then it is ignored.
158 virtual void SetEnableFlag(int flag,
159 const char* className,
160 const char* subclassName);
161 virtual int GetEnableFlag(const char* className,
162 const char* subclassName);
165 // Return 1 if this factory overrides the given class name, 0 otherwise.
166 virtual int HasOverride(const char* className);
168 // Return 1 if this factory overrides the given class name, 0 otherwise.
169 virtual int HasOverride(const char* className, const char* subclassName);
172 // Set all enable flags for the given class to 0. This will
173 // mean that the factory will stop producing class with the given
175 virtual void Disable(const char* className);
178 // This returns the path to a dynamically loaded factory.
179 vtkGetStringMacro(LibraryPath);
182 typedef vtkObject* (*CreateFunction)();
188 // Register object creation information with the factory.
189 void RegisterOverride(const char* classOverride,
190 const char* overrideClassName,
191 const char* description,
193 CreateFunction createFunction);
199 // This method is provided by sub-classes of vtkObjectFactory.
200 // It should create the named vtk object or return 0 if that object
201 // is not supported by the factory implementation.
202 virtual vtkObject* CreateObject(const char* vtkclassname );
207 struct OverrideInformation
210 char* OverrideWithName;
212 CreateFunction CreateCallback;
215 OverrideInformation* OverrideArray;
216 char** OverrideClassNames;
217 int SizeOverrideArray;
218 int OverrideArrayLength;
221 void GrowOverrideArray();
224 // Initialize the static members of vtkObjectFactory. RegisterDefaults
228 // Register default factories which are not loaded at run time.
229 static void RegisterDefaults();
231 // Load dynamic factories from the VTK_AUTOLOAD_PATH
232 static void LoadDynamicFactories();
234 // Load all dynamic libraries in the given path
235 static void LoadLibrariesInPath( const char*);
237 // list of registered factories
238 static vtkObjectFactoryCollection* RegisteredFactories;
240 // member variables for a factory set by the base class
241 // at load or register time
243 char* LibraryVTKVersion;
244 char* LibraryCompilerUsed;
247 vtkObjectFactory(const vtkObjectFactory&); // Not implemented.
248 void operator=(const vtkObjectFactory&); // Not implemented.
251 // Macro to create an object creation function.
252 // The name of the function will by vtkObjectFactoryCreateclassname
253 // where classname is the name of the class being created
254 #define VTK_CREATE_CREATE_FUNCTION(classname) \
255 static vtkObject* vtkObjectFactoryCreate##classname() \
256 { return classname::New(); }
262 #define VTK_FACTORY_INTERFACE_EXPORT __declspec( dllexport )
264 #define VTK_FACTORY_INTERFACE_EXPORT
267 // Macro to create the interface "C" functions used in
268 // a dll or shared library that contains a VTK object factory.
269 // Put this function in the .cxx file of your object factory,
270 // and pass in the name of the factory sub-class that you want
271 // the dll to create.
272 #define VTK_FACTORY_INTERFACE_IMPLEMENT(factoryName) \
274 VTK_FACTORY_INTERFACE_EXPORT \
275 const char* vtkGetFactoryCompilerUsed() \
277 return VTK_CXX_COMPILER; \
280 VTK_FACTORY_INTERFACE_EXPORT \
281 const char* vtkGetFactoryVersion() \
283 return VTK_SOURCE_VERSION; \
286 VTK_FACTORY_INTERFACE_EXPORT \
287 vtkObjectFactory* vtkLoad() \
289 return factoryName ::New(); \