VTK  9.1.0
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 =========================================================================*/
147 #ifndef vtkObjectFactory_h
148 #define vtkObjectFactory_h
149 
150 #include "vtkCommonCoreModule.h" // For export macro
151 #include "vtkDebugLeaksManager.h" // Must be included before singletons
152 #include "vtkFeatures.h" // For VTK_ALL_NEW_OBJECT_FACTORY
153 #include "vtkObject.h"
154 
155 #include <string> // for std::string
156 
159 class vtkCollection;
160 
161 class VTKCOMMONCORE_EXPORT vtkObjectFactory : public vtkObject
162 {
163 public:
164  // Class Methods used to interface with the registered factories
165 
176  static vtkObject* CreateInstance(const char* vtkclassname, bool isAbstract = false);
177 
184  static void CreateAllInstance(const char* vtkclassname, vtkCollection* retList);
189  static void ReHash();
201  static void UnRegisterAllFactories();
202 
208 
213  static int HasOverrideAny(const char* className);
214 
220 
225  static void SetAllEnableFlags(vtkTypeBool flag, const char* className);
230  static void SetAllEnableFlags(vtkTypeBool flag, const char* className, const char* subclassName);
231 
232  // Instance methods to be used on individual instances of vtkObjectFactory
233 
234  // Methods from vtkObject
235  vtkTypeMacro(vtkObjectFactory, vtkObject);
239  void PrintSelf(ostream& os, vtkIndent indent) override;
240 
248  virtual const char* GetVTKSourceVersion() = 0;
249 
253  virtual const char* GetDescription() = 0;
254 
258  virtual int GetNumberOfOverrides();
259 
263  virtual const char* GetClassOverrideName(int index);
264 
269  virtual const char* GetClassOverrideWithName(int index);
270 
275 
280  virtual const char* GetOverrideDescription(int index);
281 
283 
287  virtual void SetEnableFlag(vtkTypeBool flag, const char* className, const char* subclassName);
288  virtual vtkTypeBool GetEnableFlag(const char* className, const char* subclassName);
290 
294  virtual int HasOverride(const char* className);
298  virtual int HasOverride(const char* className, const char* subclassName);
299 
305  virtual void Disable(const char* className);
306 
308 
311  vtkGetFilePathMacro(LibraryPath);
313 
314  typedef vtkObject* (*CreateFunction)();
315 
316 protected:
320  void RegisterOverride(const char* classOverride, const char* overrideClassName,
321  const char* description, int enableFlag, CreateFunction createFunction);
322 
328  virtual vtkObject* CreateObject(const char* vtkclassname);
329 
331  ~vtkObjectFactory() override;
332 
334  {
335  char* Description;
338  CreateFunction CreateCallback;
339  };
340 
345 
346 private:
347  void GrowOverrideArray();
348 
353  static void Init();
357  static void RegisterDefaults();
361  static void LoadDynamicFactories();
365  static void LoadLibrariesInPath(const std::string&);
366 
367  // list of registered factories
368  static vtkObjectFactoryCollection* RegisteredFactories;
369 
370  // member variables for a factory set by the base class
371  // at load or register time
372  void* LibraryHandle;
373  char* LibraryVTKVersion;
374  char* LibraryPath;
375 
376 private:
377  vtkObjectFactory(const vtkObjectFactory&) = delete;
378  void operator=(const vtkObjectFactory&) = delete;
379 };
380 
381 // Implementation detail for Schwarz counter idiom.
382 class VTKCOMMONCORE_EXPORT vtkObjectFactoryRegistryCleanup
383 {
384 public:
387 
388 private:
391 };
393 
394 // Macro to create an object creation function.
395 // The name of the function will by vtkObjectFactoryCreateclassname
396 // where classname is the name of the class being created
397 #define VTK_CREATE_CREATE_FUNCTION(classname) \
398  static vtkObject* vtkObjectFactoryCreate##classname() { return classname::New(); }
399 
400 #endif
401 
402 #define VTK_FACTORY_INTERFACE_EXPORT VTKCOMMONCORE_EXPORT
403 
404 // Macro to create the interface "C" functions used in
405 // a dll or shared library that contains a VTK object factory.
406 // Put this function in the .cxx file of your object factory,
407 // and pass in the name of the factory sub-class that you want
408 // the dll to create.
409 #define VTK_FACTORY_INTERFACE_IMPLEMENT(factoryName) \
410  extern "C" VTK_FACTORY_INTERFACE_EXPORT const char* vtkGetFactoryVersion() \
411  { \
412  return VTK_SOURCE_VERSION; \
413  } \
414  extern "C" VTK_FACTORY_INTERFACE_EXPORT vtkObjectFactory* vtkLoad() \
415  { \
416  return factoryName ::New(); \
417  }
418 
419 // Macro to implement the body of the object factory form of the New() method.
420 #define VTK_OBJECT_FACTORY_NEW_BODY(thisClass) \
421  vtkObject* ret = vtkObjectFactory::CreateInstance(#thisClass, false); \
422  if (ret) \
423  { \
424  return static_cast<thisClass*>(ret); \
425  } \
426  auto result = new thisClass; \
427  result->InitializeObjectBase(); \
428  return result
429 
430 // Macro to implement the body of the abstract object factory form of the New()
431 // method, i.e. an abstract base class that can only be instantiated if the
432 // object factory overrides it.
433 #define VTK_ABSTRACT_OBJECT_FACTORY_NEW_BODY(thisClass) \
434  vtkObject* ret = vtkObjectFactory::CreateInstance(#thisClass, true); \
435  if (ret) \
436  { \
437  return static_cast<thisClass*>(ret); \
438  } \
439  vtkGenericWarningMacro("Error: no override found for '" #thisClass "'."); \
440  return nullptr
441 
442 // Macro to implement the body of the standard form of the New() method.
443 #if defined(VTK_ALL_NEW_OBJECT_FACTORY)
444 #define VTK_STANDARD_NEW_BODY(thisClass) VTK_OBJECT_FACTORY_NEW_BODY(thisClass)
445 #else
446 #define VTK_STANDARD_NEW_BODY(thisClass) \
447  auto result = new thisClass; \
448  result->InitializeObjectBase(); \
449  return result
450 #endif
451 
452 // Macro to implement the standard form of the New() method.
453 #define vtkStandardNewMacro(thisClass) \
454  thisClass* thisClass::New() { VTK_STANDARD_NEW_BODY(thisClass); }
455 
456 // Macro to implement the ExtendedNew() to create an object in a memkind extended memory space. If
457 // VTK is not compiled with VTK_USE_MEMKIND this is equivalent to New()
458 #define vtkStandardExtendedNewMacro(thisClass) \
459  thisClass* thisClass::ExtendedNew() \
460  { \
461  auto mkhold = vtkMemkindRAII(true); \
462  (void)mkhold; \
463  return thisClass::New(); \
464  }
465 
466 // Macro to implement the object factory form of the New() method.
467 #define vtkObjectFactoryNewMacro(thisClass) \
468  thisClass* thisClass::New() { VTK_OBJECT_FACTORY_NEW_BODY(thisClass); }
469 
470 // Macro to implement the abstract object factory form of the New() method.
471 // That is an abstract base class that can only be instantiated if the
472 // object factory overrides it.
473 #define vtkAbstractObjectFactoryNewMacro(thisClass) \
474  thisClass* thisClass::New() { VTK_ABSTRACT_OBJECT_FACTORY_NEW_BODY(thisClass); }
vtkOverrideInformationCollection
maintain a list of override information objects
Definition: vtkOverrideInformationCollection.h:34
vtkObjectFactory::CreateObject
virtual vtkObject * CreateObject(const char *vtkclassname)
This method is provided by sub-classes of vtkObjectFactory.
vtkObjectFactory::CreateAllInstance
static void CreateAllInstance(const char *vtkclassname, vtkCollection *retList)
Create all possible instances of the named vtk object.
vtkObjectFactory::RegisterFactory
static void RegisterFactory(vtkObjectFactory *)
Register a factory so it can be used to create vtk objects.
vtkObjectFactory::vtkObjectFactory
vtkObjectFactory()
vtkObjectFactory::GetOverrideDescription
virtual const char * GetOverrideDescription(int index)
Return the description for a the class override at the given index.
vtkObjectFactory::SizeOverrideArray
int SizeOverrideArray
Definition: vtkObjectFactory.h:343
vtkObjectFactoryRegistryCleanupInstance
static vtkObjectFactoryRegistryCleanup vtkObjectFactoryRegistryCleanupInstance
Definition: vtkObjectFactory.h:392
vtkObjectFactory::GetOverrideInformation
static void GetOverrideInformation(const char *name, vtkOverrideInformationCollection *)
Fill the given collection with all the overrides for the class with the given name.
vtkObjectFactory::OverrideInformation::CreateCallback
CreateFunction CreateCallback
Definition: vtkObjectFactory.h:338
vtkObjectFactory::SetEnableFlag
virtual void SetEnableFlag(vtkTypeBool flag, const char *className, const char *subclassName)
Set and Get the Enable flag for the specific override of className.
vtkObjectFactory::HasOverride
virtual int HasOverride(const char *className)
Return 1 if this factory overrides the given class name, 0 otherwise.
vtkObject
abstract base class for most VTK objects
Definition: vtkObject.h:82
vtkObjectFactoryRegistryCleanup
Definition: vtkObjectFactory.h:383
vtkObjectFactory::GetEnableFlag
virtual vtkTypeBool GetEnableFlag(int index)
Return the enable flag for the class at the given index.
vtkObjectFactory::ReHash
static void ReHash()
Re-check the VTK_AUTOLOAD_PATH for new factory libraries.
vtkObjectFactory::Disable
virtual void Disable(const char *className)
Set all enable flags for the given class to 0.
vtkObjectFactory::GetClassOverrideName
virtual const char * GetClassOverrideName(int index)
Return the name of a class override at the given index.
vtkObjectFactory::GetNumberOfOverrides
virtual int GetNumberOfOverrides()
Return number of overrides this factory can create.
vtkObjectFactory::UnRegisterFactory
static void UnRegisterFactory(vtkObjectFactory *)
Remove a factory from the list of registered factories.
vtkObjectFactory::RegisterOverride
void RegisterOverride(const char *classOverride, const char *overrideClassName, const char *description, int enableFlag, CreateFunction createFunction)
Register object creation information with the factory.
vtkObjectFactory::OverrideClassNames
char ** OverrideClassNames
Definition: vtkObjectFactory.h:342
vtkObjectFactory::OverrideArrayLength
int OverrideArrayLength
Definition: vtkObjectFactory.h:344
vtkObjectFactoryRegistryCleanup::vtkObjectFactoryRegistryCleanup
vtkObjectFactoryRegistryCleanup()
vtkObjectFactoryRegistryCleanup::~vtkObjectFactoryRegistryCleanup
~vtkObjectFactoryRegistryCleanup()
vtkObjectFactory::OverrideInformation::Description
char * Description
Definition: vtkObjectFactory.h:335
vtkDebugLeaksManager.h
vtkCollection
create and manipulate ordered lists of objects
Definition: vtkCollection.h:53
vtkObjectFactory
abstract base class for vtkObjectFactories
Definition: vtkObjectFactory.h:162
vtkObjectFactory::HasOverrideAny
static int HasOverrideAny(const char *className)
return 1 if one of the registered factories overrides the given class name
vtkObjectFactory::GetRegisteredFactories
static vtkObjectFactoryCollection * GetRegisteredFactories()
Return the list of all registered factories.
vtkObjectFactory::OverrideInformation::OverrideWithName
char * OverrideWithName
Definition: vtkObjectFactory.h:336
vtkIndent
a simple class to control print indentation
Definition: vtkIndent.h:113
vtkObjectFactory::UnRegisterAllFactories
static void UnRegisterAllFactories()
Unregister all factories.
vtkObjectFactory::HasOverride
virtual int HasOverride(const char *className, const char *subclassName)
Return 1 if this factory overrides the given class name, 0 otherwise.
vtkObjectFactory::OverrideInformation::EnabledFlag
vtkTypeBool EnabledFlag
Definition: vtkObjectFactory.h:337
vtkObjectFactory::SetAllEnableFlags
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::CreateInstance
static vtkObject * CreateInstance(const char *vtkclassname, bool isAbstract=false)
Create and return an instance of the named vtk object.
vtkObjectFactoryCollection
maintain a list of object factories
Definition: vtkObjectFactoryCollection.h:35
vtkX3D::name
@ name
Definition: vtkX3D.h:225
vtkObject.h
vtkObjectFactory::SetAllEnableFlags
static void SetAllEnableFlags(vtkTypeBool flag, const char *className)
Set the enable flag for a given named class for all registered factories.
vtkObjectFactory::PrintSelf
void PrintSelf(ostream &os, vtkIndent indent) override
Print ObjectFactory to stream.
vtkX3D::string
@ string
Definition: vtkX3D.h:496
vtkX3D::description
@ description
Definition: vtkX3D.h:328
vtkObjectFactory::OverrideInformation
Definition: vtkObjectFactory.h:334
vtkObjectFactory::~vtkObjectFactory
~vtkObjectFactory() override
vtkObjectFactory::OverrideArray
OverrideInformation * OverrideArray
Definition: vtkObjectFactory.h:341
VTK_NEWINSTANCE
#define VTK_NEWINSTANCE
Definition: vtkWrappingHints.h:44
vtkObjectFactory::GetDescription
virtual const char * GetDescription()=0
Return a descriptive string describing the factory.
vtkObjectFactory::vtkGetFilePathMacro
vtkGetFilePathMacro(LibraryPath)
This returns the path to a dynamically loaded factory.
vtkX3D::index
@ index
Definition: vtkX3D.h:252
vtkObjectFactory::GetClassOverrideWithName
virtual const char * GetClassOverrideWithName(int index)
Return the name of the class that will override the class at the given index.
vtkTypeBool
int vtkTypeBool
Definition: vtkABI.h:69
vtkObjectFactory::GetVTKSourceVersion
virtual const char * GetVTKSourceVersion()=0
All sub-classes of vtkObjectFactory should must return the version of VTK they were built with.
vtkObjectFactory::GetEnableFlag
virtual vtkTypeBool GetEnableFlag(const char *className, const char *subclassName)
Set and Get the Enable flag for the specific override of className.