VTK  9.3.0
vtkObjectFactory.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
2 // SPDX-License-Identifier: BSD-3-Clause
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 "vtkFeatures.h" // For VTK_ALL_NEW_OBJECT_FACTORY
44 #include "vtkObject.h"
45 
46 #include <string> // for std::string
47 
48 VTK_ABI_NAMESPACE_BEGIN
51 class vtkCollection;
52 
53 class VTKCOMMONCORE_EXPORT vtkObjectFactory : public vtkObject
54 {
55 public:
56  // Class Methods used to interface with the registered factories
57 
68  static vtkObject* CreateInstance(const char* vtkclassname, bool isAbstract = false);
69 
76  static void CreateAllInstance(const char* vtkclassname, vtkCollection* retList);
81  static void ReHash();
93  static void UnRegisterAllFactories();
94 
100 
105  static vtkTypeBool HasOverrideAny(const char* className);
106 
112 
117  static void SetAllEnableFlags(vtkTypeBool flag, const char* className);
122  static void SetAllEnableFlags(vtkTypeBool flag, const char* className, const char* subclassName);
123 
124  // Instance methods to be used on individual instances of vtkObjectFactory
125 
126  // Methods from vtkObject
127  vtkTypeMacro(vtkObjectFactory, vtkObject);
131  void PrintSelf(ostream& os, vtkIndent indent) override;
132 
140  virtual const char* GetVTKSourceVersion() = 0;
141 
145  virtual const char* GetDescription() = 0;
146 
150  virtual int GetNumberOfOverrides();
151 
155  virtual const char* GetClassOverrideName(int index);
156 
161  virtual const char* GetClassOverrideWithName(int index);
162 
167 
172  virtual const char* GetOverrideDescription(int index);
173 
175 
179  virtual void SetEnableFlag(vtkTypeBool flag, const char* className, const char* subclassName);
180  virtual vtkTypeBool GetEnableFlag(const char* className, const char* subclassName);
182 
186  virtual vtkTypeBool HasOverride(const char* className);
190  virtual vtkTypeBool HasOverride(const char* className, const char* subclassName);
191 
197  virtual void Disable(const char* className);
198 
200 
203  vtkGetFilePathMacro(LibraryPath);
205 
206  typedef vtkObject* (*CreateFunction)();
207 
208 protected:
212  void RegisterOverride(const char* classOverride, const char* overrideClassName,
213  const char* description, int enableFlag, CreateFunction createFunction);
214 
220  virtual vtkObject* CreateObject(const char* vtkclassname);
221 
223  ~vtkObjectFactory() override;
224 
226  {
227  char* Description;
230  CreateFunction CreateCallback;
231  };
232 
237 
238 private:
239  void GrowOverrideArray();
240 
245  static void Init();
249  static void RegisterDefaults();
253  static void LoadDynamicFactories();
257  static void LoadLibrariesInPath(const std::string&);
258 
259  // list of registered factories
260  static vtkObjectFactoryCollection* RegisteredFactories;
261 
262  // member variables for a factory set by the base class
263  // at load or register time
264  void* LibraryHandle;
265  char* LibraryVTKVersion;
266  char* LibraryPath;
267 
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 VTK_ABI_NAMESPACE_END
292 #endif
293 
294 #define VTK_FACTORY_INTERFACE_EXPORT VTKCOMMONCORE_EXPORT
295 
296 // Macro to create the interface "C" functions used in
297 // a dll or shared library that contains a VTK object factory.
298 // Put this function in the .cxx file of your object factory,
299 // and pass in the name of the factory sub-class that you want
300 // the dll to create.
301 #define VTK_FACTORY_INTERFACE_IMPLEMENT(factoryName) \
302  extern "C" VTK_FACTORY_INTERFACE_EXPORT const char* vtkGetFactoryVersion() \
303  { \
304  return VTK_SOURCE_VERSION; \
305  } \
306  extern "C" VTK_FACTORY_INTERFACE_EXPORT vtkObjectFactory* vtkLoad() \
307  { \
308  return factoryName ::New(); \
309  }
310 
311 // Macro to implement the body of the object factory form of the New() method.
312 #define VTK_OBJECT_FACTORY_NEW_BODY(thisClass) \
313  vtkObject* ret = vtkObjectFactory::CreateInstance(#thisClass, false); \
314  if (ret) \
315  { \
316  return static_cast<thisClass*>(ret); \
317  } \
318  auto result = new thisClass; \
319  result->InitializeObjectBase(); \
320  return result
321 
322 // Macro to implement the body of the abstract object factory form of the New()
323 // method, i.e. an abstract base class that can only be instantiated if the
324 // object factory overrides it.
325 #define VTK_ABSTRACT_OBJECT_FACTORY_NEW_BODY(thisClass) \
326  vtkObject* ret = vtkObjectFactory::CreateInstance(#thisClass, true); \
327  if (ret) \
328  { \
329  return static_cast<thisClass*>(ret); \
330  } \
331  vtkGenericWarningMacro("Error: no override found for '" #thisClass "'."); \
332  return nullptr
333 
334 // Macro to implement the body of the standard form of the New() method.
335 #if defined(VTK_ALL_NEW_OBJECT_FACTORY)
336 #define VTK_STANDARD_NEW_BODY(thisClass) VTK_OBJECT_FACTORY_NEW_BODY(thisClass)
337 #else
338 #define VTK_STANDARD_NEW_BODY(thisClass) \
339  auto result = new thisClass; \
340  result->InitializeObjectBase(); \
341  return result
342 #endif
343 
344 // Macro to implement the standard form of the New() method.
345 #define vtkStandardNewMacro(thisClass) \
346  thisClass* thisClass::New() { VTK_STANDARD_NEW_BODY(thisClass); }
347 
348 // Macro to implement the ExtendedNew() to create an object in a memkind extended memory space. If
349 // VTK is not compiled with VTK_USE_MEMKIND this is equivalent to New()
350 #define vtkStandardExtendedNewMacro(thisClass) \
351  thisClass* thisClass::ExtendedNew() \
352  { \
353  auto mkhold = vtkMemkindRAII(true); \
354  (void)mkhold; \
355  return thisClass::New(); \
356  }
357 
358 // Macro to implement the object factory form of the New() method.
359 #define vtkObjectFactoryNewMacro(thisClass) \
360  thisClass* thisClass::New() { VTK_OBJECT_FACTORY_NEW_BODY(thisClass); }
361 
362 // Macro to implement the abstract object factory form of the New() method.
363 // That is an abstract base class that can only be instantiated if the
364 // object factory overrides it.
365 #define vtkAbstractObjectFactoryNewMacro(thisClass) \
366  thisClass* thisClass::New() { VTK_ABSTRACT_OBJECT_FACTORY_NEW_BODY(thisClass); }
create and manipulate ordered lists of objects
Definition: vtkCollection.h:45
a simple class to control print indentation
Definition: vtkIndent.h:38
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 vtkTypeBool HasOverride(const char *className)
Return 1 if this factory overrides the given class name, 0 otherwise.
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 vtkTypeBool HasOverrideAny(const char *className)
return 1 if one of the registered factories overrides the given class name
static vtkObjectFactoryCollection * GetRegisteredFactories()
Return the list of all registered factories.
OverrideInformation * OverrideArray
virtual vtkTypeBool HasOverride(const char *className, const char *subclassName)
Return 1 if this factory overrides the given class name, 0 otherwise.
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 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.
abstract base class for most VTK objects
Definition: vtkObject.h:61
maintain a list of override information objects
@ description
Definition: vtkX3D.h:322
@ name
Definition: vtkX3D.h:219
@ index
Definition: vtkX3D.h:246
@ string
Definition: vtkX3D.h:490
int vtkTypeBool
Definition: vtkABI.h:64
static vtkObjectFactoryRegistryCleanup vtkObjectFactoryRegistryCleanupInstance
#define VTK_NEWINSTANCE