VTK  9.0.1
vtkObjectFactory.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Visualization Toolkit
4  Module: vtkObjectFactory.h
5 
6  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7  All rights reserved.
8  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10  This software is distributed WITHOUT ANY WARRANTY; without even
11  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12  PURPOSE. See the above copyright notice for more information.
13 
14 =========================================================================*/
38 #ifndef vtkObjectFactory_h
39 #define vtkObjectFactory_h
40 
41 #include "vtkCommonCoreModule.h" // For export macro
42 #include "vtkDebugLeaksManager.h" // Must be included before singletons
43 #include "vtkObject.h"
44 
45 #include <string> // for std::string
46 
49 class vtkCollection;
50 
51 class VTKCOMMONCORE_EXPORT vtkObjectFactory : public vtkObject
52 {
53 public:
54  // Class Methods used to interface with the registered factories
55 
66  static vtkObject* CreateInstance(const char* vtkclassname, bool isAbstract = false);
67 
74  static void CreateAllInstance(const char* vtkclassname, vtkCollection* retList);
79  static void ReHash();
91  static void UnRegisterAllFactories();
92 
98 
103  static int HasOverrideAny(const char* className);
104 
110 
115  static void SetAllEnableFlags(vtkTypeBool flag, const char* className);
120  static void SetAllEnableFlags(vtkTypeBool flag, const char* className, const char* subclassName);
121 
122  // Instance methods to be used on individual instances of vtkObjectFactory
123 
124  // Methods from vtkObject
125  vtkTypeMacro(vtkObjectFactory, vtkObject);
129  void PrintSelf(ostream& os, vtkIndent indent) override;
130 
138  virtual const char* GetVTKSourceVersion() = 0;
139 
143  virtual const char* GetDescription() = 0;
144 
148  virtual int GetNumberOfOverrides();
149 
153  virtual const char* GetClassOverrideName(int index);
154 
159  virtual const char* GetClassOverrideWithName(int index);
160 
165 
170  virtual const char* GetOverrideDescription(int index);
171 
173 
177  virtual void SetEnableFlag(vtkTypeBool flag, const char* className, const char* subclassName);
178  virtual vtkTypeBool GetEnableFlag(const char* className, const char* subclassName);
180 
184  virtual int HasOverride(const char* className);
188  virtual int HasOverride(const char* className, const char* subclassName);
189 
195  virtual void Disable(const char* className);
196 
198 
201  vtkGetStringMacro(LibraryPath);
203 
204  typedef vtkObject* (*CreateFunction)();
205 
206 protected:
210  void RegisterOverride(const char* classOverride, const char* overrideClassName,
211  const char* description, int enableFlag, CreateFunction createFunction);
212 
218  virtual vtkObject* CreateObject(const char* vtkclassname);
219 
221  ~vtkObjectFactory() override;
222 
224  {
225  char* Description;
228  CreateFunction CreateCallback;
229  };
230 
235 
236 private:
237  void GrowOverrideArray();
238 
243  static void Init();
247  static void RegisterDefaults();
251  static void LoadDynamicFactories();
255  static void LoadLibrariesInPath(const std::string&);
256 
257  // list of registered factories
258  static vtkObjectFactoryCollection* RegisteredFactories;
259 
260  // member variables for a factory set by the base class
261  // at load or register time
262  void* LibraryHandle;
263  char* LibraryVTKVersion;
264  char* LibraryCompilerUsed;
265  char* LibraryPath;
266 
267 private:
268  vtkObjectFactory(const vtkObjectFactory&) = delete;
269  void operator=(const vtkObjectFactory&) = delete;
270 };
271 
272 // Implementation detail for Schwarz counter idiom.
273 class VTKCOMMONCORE_EXPORT vtkObjectFactoryRegistryCleanup
274 {
275 public:
278 
279 private:
282 };
284 
285 // Macro to create an object creation function.
286 // The name of the function will by vtkObjectFactoryCreateclassname
287 // where classname is the name of the class being created
288 #define VTK_CREATE_CREATE_FUNCTION(classname) \
289  static vtkObject* vtkObjectFactoryCreate##classname() { return classname::New(); }
290 
291 #endif
292 
293 #define VTK_FACTORY_INTERFACE_EXPORT VTKCOMMONCORE_EXPORT
294 
295 // Macro to create the interface "C" functions used in
296 // a dll or shared library that contains a VTK object factory.
297 // Put this function in the .cxx file of your object factory,
298 // and pass in the name of the factory sub-class that you want
299 // the dll to create.
300 #define VTK_FACTORY_INTERFACE_IMPLEMENT(factoryName) \
301  extern "C" VTK_FACTORY_INTERFACE_EXPORT const char* vtkGetFactoryCompilerUsed() \
302  { \
303  return VTK_CXX_COMPILER; \
304  } \
305  extern "C" VTK_FACTORY_INTERFACE_EXPORT const char* vtkGetFactoryVersion() \
306  { \
307  return VTK_SOURCE_VERSION; \
308  } \
309  extern "C" VTK_FACTORY_INTERFACE_EXPORT vtkObjectFactory* vtkLoad() \
310  { \
311  return factoryName ::New(); \
312  }
313 
314 // Macro to implement the body of the object factory form of the New() method.
315 #define VTK_OBJECT_FACTORY_NEW_BODY(thisClass) \
316  vtkObject* ret = vtkObjectFactory::CreateInstance(#thisClass, false); \
317  if (ret) \
318  { \
319  return static_cast<thisClass*>(ret); \
320  } \
321  auto result = new thisClass; \
322  result->InitializeObjectBase(); \
323  return result
324 
325 // Macro to implement the body of the abstract object factory form of the New()
326 // method, i.e. an abstract base class that can only be instantiated if the
327 // object factory overrides it.
328 #define VTK_ABSTRACT_OBJECT_FACTORY_NEW_BODY(thisClass) \
329  vtkObject* ret = vtkObjectFactory::CreateInstance(#thisClass, true); \
330  if (ret) \
331  { \
332  return static_cast<thisClass*>(ret); \
333  } \
334  vtkGenericWarningMacro("Error: no override found for '" #thisClass "'."); \
335  return nullptr
336 
337 // Macro to implement the body of the standard form of the New() method.
338 #if defined(VTK_ALL_NEW_OBJECT_FACTORY)
339 #define VTK_STANDARD_NEW_BODY(thisClass) VTK_OBJECT_FACTORY_NEW_BODY(thisClass)
340 #else
341 #define VTK_STANDARD_NEW_BODY(thisClass) \
342  auto result = new thisClass; \
343  result->InitializeObjectBase(); \
344  return result
345 #endif
346 
347 // Macro to implement the standard form of the New() method.
348 #define vtkStandardNewMacro(thisClass) \
349  thisClass* thisClass::New() { VTK_STANDARD_NEW_BODY(thisClass); }
350 
351 // Macro to implement the object factory form of the New() method.
352 #define vtkObjectFactoryNewMacro(thisClass) \
353  thisClass* thisClass::New() { VTK_OBJECT_FACTORY_NEW_BODY(thisClass); }
354 
355 // Macro to implement the abstract object factory form of the New() method.
356 // That is an abstract base class that can only be instantiated if the
357 // object factory overrides it.
358 #define vtkAbstractObjectFactoryNewMacro(thisClass) \
359  thisClass* thisClass::New() { VTK_ABSTRACT_OBJECT_FACTORY_NEW_BODY(thisClass); }
create and manipulate ordered lists of objects
Definition: vtkCollection.h:53
a simple class to control print indentation
Definition: vtkIndent.h:34
maintain a list of object factories
abstract base class for vtkObjectFactories
virtual const char * GetClassOverrideWithName(int index)
Return the name of the class that will override the class at the given index.
virtual void Disable(const char *className)
Set all enable flags for the given class to 0.
void PrintSelf(ostream &os, vtkIndent indent) override
Print ObjectFactory to stream.
static void SetAllEnableFlags(vtkTypeBool flag, const char *className)
Set the enable flag for a given named class for all registered factories.
virtual vtkTypeBool GetEnableFlag(int index)
Return the enable flag for the class at the given index.
virtual int GetNumberOfOverrides()
Return number of overrides this factory can create.
static void GetOverrideInformation(const char *name, vtkOverrideInformationCollection *)
Fill the given collection with all the overrides for the class with the given name.
virtual vtkObject * CreateObject(const char *vtkclassname)
This method is provided by sub-classes of vtkObjectFactory.
virtual void SetEnableFlag(vtkTypeBool flag, const char *className, const char *subclassName)
Set and Get the Enable flag for the specific override of className.
virtual const char * GetOverrideDescription(int index)
Return the description for a the class override at the given index.
static void CreateAllInstance(const char *vtkclassname, vtkCollection *retList)
Create all possible instances of the named vtk object.
static vtkObjectFactoryCollection * GetRegisteredFactories()
Return the list of all registered factories.
OverrideInformation * OverrideArray
virtual const char * GetDescription()=0
Return a descriptive string describing the factory.
static void UnRegisterFactory(vtkObjectFactory *)
Remove a factory from the list of registered factories.
virtual const char * GetVTKSourceVersion()=0
All sub-classes of vtkObjectFactory should must return the version of VTK they were built with.
void RegisterOverride(const char *classOverride, const char *overrideClassName, const char *description, int enableFlag, CreateFunction createFunction)
Register object creation information with the factory.
virtual vtkTypeBool GetEnableFlag(const char *className, const char *subclassName)
static vtkObject * CreateInstance(const char *vtkclassname, bool isAbstract=false)
Create and return an instance of the named vtk object.
virtual const char * GetClassOverrideName(int index)
Return the name of a class override at the given index.
static int HasOverrideAny(const char *className)
return 1 if one of the registered factories overrides the given class name
virtual int HasOverride(const char *className)
Return 1 if this factory overrides the given class name, 0 otherwise.
static void ReHash()
Re-check the VTK_AUTOLOAD_PATH for new factory libraries.
static void UnRegisterAllFactories()
Unregister all factories.
static void SetAllEnableFlags(vtkTypeBool flag, const char *className, const char *subclassName)
Set the enable flag for a given named class subclass pair for all registered factories.
~vtkObjectFactory() override
static void RegisterFactory(vtkObjectFactory *)
Register a factory so it can be used to create vtk objects.
virtual int HasOverride(const char *className, const char *subclassName)
Return 1 if this factory overrides the given class name, 0 otherwise.
abstract base class for most VTK objects
Definition: vtkObject.h:54
maintain a list of override information objects
@ description
Definition: vtkX3D.h:328
@ name
Definition: vtkX3D.h:225
@ index
Definition: vtkX3D.h:252
@ string
Definition: vtkX3D.h:496
int vtkTypeBool
Definition: vtkABI.h:69
static vtkObjectFactoryRegistryCleanup vtkObjectFactoryRegistryCleanupInstance
#define VTK_NEWINSTANCE