VTK
dox/Common/Core/vtkObjectFactory.h
Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   Visualization Toolkit
00004   Module:    vtkObjectFactory.h
00005 
00006   Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
00007   All rights reserved.
00008   See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
00009 
00010      This software is distributed WITHOUT ANY WARRANTY; without even
00011      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
00012      PURPOSE.  See the above copyright notice for more information.
00013 
00014 =========================================================================*/
00040 #ifndef __vtkObjectFactory_h
00041 #define __vtkObjectFactory_h
00042 
00043 #include "vtkCommonCoreModule.h" // For export macro
00044 #include "vtkObject.h"
00045 
00046 class vtkObjectFactoryCollection;
00047 class vtkOverrideInformationCollection;
00048 class vtkCollection;
00049 
00050 class VTKCOMMONCORE_EXPORT vtkObjectFactory : public vtkObject
00051 {
00052 public:
00053   // Class Methods used to interface with the registered factories
00054 
00059   static vtkObject* CreateInstance(const char* vtkclassname);
00060 
00063   static void ConstructInstance(const char* vtkclassname);
00064 
00066 
00069   static void CreateAllInstance(const char* vtkclassname,
00070                                 vtkCollection* retList);
00072 
00074   static void ReHash();
00076   static void RegisterFactory(vtkObjectFactory* );
00078   static void UnRegisterFactory(vtkObjectFactory*);
00080   static void UnRegisterAllFactories();
00081 
00084   static vtkObjectFactoryCollection* GetRegisteredFactories();
00085 
00088   static int HasOverrideAny(const char* className);
00089 
00091 
00093   static void GetOverrideInformation(const char* name,
00094                                      vtkOverrideInformationCollection*);
00096 
00098 
00100   static void SetAllEnableFlags(int flag,
00101                                 const char* className);
00103 
00104 
00106   static void SetAllEnableFlags(int flag,
00107                                 const char* className,
00108                                 const char* subclassName);
00110 
00111   // Instance methods to be used on individual instances of vtkObjectFactory
00112 
00113   // Methods from vtkObject
00114   vtkTypeMacro(vtkObjectFactory,vtkObject);
00116   virtual void PrintSelf(ostream& os, vtkIndent indent);
00117 
00124   virtual const char* GetVTKSourceVersion() = 0;
00125 
00127   virtual const char* GetDescription() = 0;
00128 
00130   virtual int GetNumberOfOverrides();
00131 
00133   virtual const char* GetClassOverrideName(int index);
00134 
00137   virtual const char* GetClassOverrideWithName(int index);
00138 
00140   virtual int GetEnableFlag(int index);
00141 
00143   virtual const char* GetOverrideDescription(int index);
00144 
00146 
00148   virtual void SetEnableFlag(int flag,
00149                              const char* className,
00150                              const char* subclassName);
00151   virtual int GetEnableFlag(const char* className,
00152                             const char* subclassName);
00154 
00156   virtual int HasOverride(const char* className);
00158   virtual int HasOverride(const char* className, const char* subclassName);
00159 
00162   virtual void Disable(const char* className);
00163 
00165 
00166   vtkGetStringMacro(LibraryPath);
00168 
00169   //BTX
00170   typedef vtkObject* (*CreateFunction)();
00171   //ETX
00172 protected:
00173   //BTX
00174 
00176 
00177   void RegisterOverride(const char* classOverride,
00178                         const char* overrideClassName,
00179                         const char* description,
00180                         int enableFlag,
00181                         CreateFunction createFunction);
00183 
00184   //ETX
00185 
00186 
00190   virtual vtkObject* CreateObject(const char* vtkclassname );
00191 
00192   vtkObjectFactory();
00193   ~vtkObjectFactory();
00194   //BTX
00195   struct OverrideInformation
00196   {
00197     char* Description;
00198     char* OverrideWithName;
00199     int EnabledFlag;
00200     CreateFunction CreateCallback;
00201   };
00202   //ETX
00203   OverrideInformation* OverrideArray;
00204   char** OverrideClassNames;
00205   int SizeOverrideArray;
00206   int OverrideArrayLength;
00207 
00208 private:
00209   void GrowOverrideArray();
00210 
00213   static void Init();
00215   static void RegisterDefaults();
00217   static void LoadDynamicFactories();
00219   static void LoadLibrariesInPath( const char*);
00220 
00221   // list of registered factories
00222   static vtkObjectFactoryCollection* RegisteredFactories;
00223 
00224   // member variables for a factory set by the base class
00225   // at load or register time
00226   void* LibraryHandle;
00227   char* LibraryVTKVersion;
00228   char* LibraryCompilerUsed;
00229   char* LibraryPath;
00230 private:
00231   vtkObjectFactory(const vtkObjectFactory&);  // Not implemented.
00232   void operator=(const vtkObjectFactory&);  // Not implemented.
00233 };
00234 
00235 // Macro to create an object creation function.
00236 // The name of the function will by vtkObjectFactoryCreateclassname
00237 // where classname is the name of the class being created
00238 #define VTK_CREATE_CREATE_FUNCTION(classname) \
00239 static vtkObject* vtkObjectFactoryCreate##classname() \
00240 { return classname::New(); }
00241 
00242 #endif
00243 
00244 #define VTK_FACTORY_INTERFACE_EXPORT VTKCOMMONCORE_EXPORT
00245 
00246 // Macro to create the interface "C" functions used in
00247 // a dll or shared library that contains a VTK object factory.
00248 // Put this function in the .cxx file of your object factory,
00249 // and pass in the name of the factory sub-class that you want
00250 // the dll to create.
00251 #define VTK_FACTORY_INTERFACE_IMPLEMENT(factoryName)  \
00252 extern "C"                                      \
00253 VTK_FACTORY_INTERFACE_EXPORT                    \
00254 const char* vtkGetFactoryCompilerUsed()         \
00255 {                                               \
00256   return VTK_CXX_COMPILER;                      \
00257 }                                               \
00258 extern "C"                                      \
00259 VTK_FACTORY_INTERFACE_EXPORT                    \
00260 const char* vtkGetFactoryVersion()              \
00261 {                                               \
00262   return VTK_SOURCE_VERSION;                    \
00263 }                                               \
00264 extern "C"                                      \
00265 VTK_FACTORY_INTERFACE_EXPORT                    \
00266 vtkObjectFactory* vtkLoad()                     \
00267 {                                               \
00268   return factoryName ::New();                   \
00269 }
00270 
00271 // Macro to implement the body of the object factory form of the New() method.
00272 #define VTK_OBJECT_FACTORY_NEW_BODY(thisClass) \
00273   vtkObject* ret = vtkObjectFactory::CreateInstance(#thisClass); \
00274   if(ret) \
00275     { \
00276     return static_cast<thisClass*>(ret); \
00277     } \
00278   return new thisClass;
00279 
00280 // Macro to implement the body of the abstract object factory form of the New()
00281 // method, i.e. an abstract base class that can only be instantiated if the
00282 // object factory overrides it.
00283 #define VTK_ABSTRACT_OBJECT_FACTORY_NEW_BODY(thisClass) \
00284   vtkObject* ret = vtkObjectFactory::CreateInstance(#thisClass); \
00285   if(ret) \
00286     { \
00287     return static_cast<thisClass*>(ret); \
00288     } \
00289   vtkGenericWarningMacro("Error: no override found for '" #thisClass "'."); \
00290   return NULL;
00291 
00292 // Macro to implement the body of the standard form of the New() method.
00293 #if defined(VTK_ALL_NEW_OBJECT_FACTORY)
00294 # define VTK_STANDARD_NEW_BODY(thisClass) \
00295   VTK_OBJECT_FACTORY_NEW_BODY(thisClass)
00296 #elif defined(VTK_DEBUG_LEAKS)
00297 # define VTK_STANDARD_NEW_BODY(thisClass) \
00298   vtkObjectFactory::ConstructInstance(#thisClass); \
00299   return new thisClass;
00300 #else
00301 # define VTK_STANDARD_NEW_BODY(thisClass) \
00302   return new thisClass;
00303 #endif
00304 
00305 // Macro to implement the standard form of the New() method.
00306 #define vtkStandardNewMacro(thisClass) \
00307   thisClass* thisClass::New() \
00308   { \
00309   VTK_STANDARD_NEW_BODY(thisClass) \
00310   } \
00311   vtkInstantiatorNewMacro(thisClass)
00312 
00313 // Macro to implement the object factory form of the New() method.
00314 #define vtkObjectFactoryNewMacro(thisClass) \
00315   thisClass* thisClass::New() \
00316   { \
00317   VTK_OBJECT_FACTORY_NEW_BODY(thisClass) \
00318   } \
00319   vtkInstantiatorNewMacro(thisClass)
00320 
00321 // Macro to implement the abstract object factory form of the New() method.
00322 // That is an abstract base class that can only be instantiated if the
00323 // object factory overrides it.
00324 #define vtkAbstractObjectFactoryNewMacro(thisClass) \
00325   thisClass* thisClass::New() \
00326   { \
00327   VTK_ABSTRACT_OBJECT_FACTORY_NEW_BODY(thisClass) \
00328   } \
00329   vtkInstantiatorNewMacro(thisClass)