VTK  9.2.6
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 =========================================================================*/
150 #ifndef vtkObjectFactory_h
151 #define vtkObjectFactory_h
152 
153 #include "vtkCommonCoreModule.h" // For export macro
154 #include "vtkDebugLeaksManager.h" // Must be included before singletons
155 #include "vtkFeatures.h" // For VTK_ALL_NEW_OBJECT_FACTORY
156 #include "vtkObject.h"
157 
158 #include <string> // for std::string
159 
162 class vtkCollection;
163 
164 class VTKCOMMONCORE_EXPORT vtkObjectFactory : public vtkObject
165 {
166 public:
167  // Class Methods used to interface with the registered factories
168 
179  static vtkObject* CreateInstance(const char* vtkclassname, bool isAbstract = false);
180 
187  static void CreateAllInstance(const char* vtkclassname, vtkCollection* retList);
192  static void ReHash();
204  static void UnRegisterAllFactories();
205 
211 
216  static int HasOverrideAny(const char* className);
217 
223 
228  static void SetAllEnableFlags(vtkTypeBool flag, const char* className);
233  static void SetAllEnableFlags(vtkTypeBool flag, const char* className, const char* subclassName);
234 
235  // Instance methods to be used on individual instances of vtkObjectFactory
236 
237  // Methods from vtkObject
238  vtkTypeMacro(vtkObjectFactory, vtkObject);
242  void PrintSelf(ostream& os, vtkIndent indent) override;
243 
251  virtual const char* GetVTKSourceVersion() = 0;
252 
256  virtual const char* GetDescription() = 0;
257 
261  virtual int GetNumberOfOverrides();
262 
266  virtual const char* GetClassOverrideName(int index);
267 
272  virtual const char* GetClassOverrideWithName(int index);
273 
278 
283  virtual const char* GetOverrideDescription(int index);
284 
286 
290  virtual void SetEnableFlag(vtkTypeBool flag, const char* className, const char* subclassName);
291  virtual vtkTypeBool GetEnableFlag(const char* className, const char* subclassName);
293 
297  virtual int HasOverride(const char* className);
301  virtual int HasOverride(const char* className, const char* subclassName);
302 
308  virtual void Disable(const char* className);
309 
311 
314  vtkGetFilePathMacro(LibraryPath);
316 
317  typedef vtkObject* (*CreateFunction)();
318 
319 protected:
323  void RegisterOverride(const char* classOverride, const char* overrideClassName,
324  const char* description, int enableFlag, CreateFunction createFunction);
325 
331  virtual vtkObject* CreateObject(const char* vtkclassname);
332 
334  ~vtkObjectFactory() override;
335 
337  {
338  char* Description;
341  CreateFunction CreateCallback;
342  };
343 
348 
349 private:
350  void GrowOverrideArray();
351 
356  static void Init();
360  static void RegisterDefaults();
364  static void LoadDynamicFactories();
368  static void LoadLibrariesInPath(const std::string&);
369 
370  // list of registered factories
371  static vtkObjectFactoryCollection* RegisteredFactories;
372 
373  // member variables for a factory set by the base class
374  // at load or register time
375  void* LibraryHandle;
376  char* LibraryVTKVersion;
377  char* LibraryPath;
378 
379 private:
380  vtkObjectFactory(const vtkObjectFactory&) = delete;
381  void operator=(const vtkObjectFactory&) = delete;
382 };
383 
384 // Implementation detail for Schwarz counter idiom.
385 class VTKCOMMONCORE_EXPORT vtkObjectFactoryRegistryCleanup
386 {
387 public:
390 
391 private:
394 };
396 
397 // Macro to create an object creation function.
398 // The name of the function will by vtkObjectFactoryCreateclassname
399 // where classname is the name of the class being created
400 #define VTK_CREATE_CREATE_FUNCTION(classname) \
401  static vtkObject* vtkObjectFactoryCreate##classname() { return classname::New(); }
402 
403 #endif
404 
405 #define VTK_FACTORY_INTERFACE_EXPORT VTKCOMMONCORE_EXPORT
406 
407 // Macro to create the interface "C" functions used in
408 // a dll or shared library that contains a VTK object factory.
409 // Put this function in the .cxx file of your object factory,
410 // and pass in the name of the factory sub-class that you want
411 // the dll to create.
412 #define VTK_FACTORY_INTERFACE_IMPLEMENT(factoryName) \
413  extern "C" VTK_FACTORY_INTERFACE_EXPORT const char* vtkGetFactoryVersion() \
414  { \
415  return VTK_SOURCE_VERSION; \
416  } \
417  extern "C" VTK_FACTORY_INTERFACE_EXPORT vtkObjectFactory* vtkLoad() \
418  { \
419  return factoryName ::New(); \
420  }
421 
422 // Macro to implement the body of the object factory form of the New() method.
423 #define VTK_OBJECT_FACTORY_NEW_BODY(thisClass) \
424  vtkObject* ret = vtkObjectFactory::CreateInstance(#thisClass, false); \
425  if (ret) \
426  { \
427  return static_cast<thisClass*>(ret); \
428  } \
429  auto result = new thisClass; \
430  result->InitializeObjectBase(); \
431  return result
432 
433 // Macro to implement the body of the abstract object factory form of the New()
434 // method, i.e. an abstract base class that can only be instantiated if the
435 // object factory overrides it.
436 #define VTK_ABSTRACT_OBJECT_FACTORY_NEW_BODY(thisClass) \
437  vtkObject* ret = vtkObjectFactory::CreateInstance(#thisClass, true); \
438  if (ret) \
439  { \
440  return static_cast<thisClass*>(ret); \
441  } \
442  vtkGenericWarningMacro("Error: no override found for '" #thisClass "'."); \
443  return nullptr
444 
445 // Macro to implement the body of the standard form of the New() method.
446 #if defined(VTK_ALL_NEW_OBJECT_FACTORY)
447 #define VTK_STANDARD_NEW_BODY(thisClass) VTK_OBJECT_FACTORY_NEW_BODY(thisClass)
448 #else
449 #define VTK_STANDARD_NEW_BODY(thisClass) \
450  auto result = new thisClass; \
451  result->InitializeObjectBase(); \
452  return result
453 #endif
454 
455 // Macro to implement the standard form of the New() method.
456 #define vtkStandardNewMacro(thisClass) \
457  thisClass* thisClass::New() { VTK_STANDARD_NEW_BODY(thisClass); }
458 
459 // Macro to implement the ExtendedNew() to create an object in a memkind extended memory space. If
460 // VTK is not compiled with VTK_USE_MEMKIND this is equivalent to New()
461 #define vtkStandardExtendedNewMacro(thisClass) \
462  thisClass* thisClass::ExtendedNew() \
463  { \
464  auto mkhold = vtkMemkindRAII(true); \
465  (void)mkhold; \
466  return thisClass::New(); \
467  }
468 
469 // Macro to implement the object factory form of the New() method.
470 #define vtkObjectFactoryNewMacro(thisClass) \
471  thisClass* thisClass::New() { VTK_OBJECT_FACTORY_NEW_BODY(thisClass); }
472 
473 // Macro to implement the abstract object factory form of the New() method.
474 // That is an abstract base class that can only be instantiated if the
475 // object factory overrides it.
476 #define vtkAbstractObjectFactoryNewMacro(thisClass) \
477  thisClass* thisClass::New() { VTK_ABSTRACT_OBJECT_FACTORY_NEW_BODY(thisClass); }
create and manipulate ordered lists of objects
Definition: vtkCollection.h:56
a simple class to control print indentation
Definition: vtkIndent.h:119
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)
Set and Get the Enable flag for the specific override of className.
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.
vtkGetFilePathMacro(LibraryPath)
This returns the path to a dynamically loaded factory.
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:82
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