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
29 #ifndef vtkObjectFactory_h
30 #define vtkObjectFactory_h
31 
32 #include "vtkCommonCoreModule.h" // For export macro
33 #include "vtkDebugLeaksManager.h" // Must be included before singletons
34 #include "vtkFeatures.h" // For VTK_ALL_NEW_OBJECT_FACTORY
35 #include "vtkObject.h"
36 
37 #include <string> // for std::string
38 
39 VTK_ABI_NAMESPACE_BEGIN
42 class vtkCollection;
43 
44 class VTKCOMMONCORE_EXPORT vtkObjectFactory : public vtkObject
45 {
46 public:
47  // Class Methods used to interface with the registered factories
48 
59  static vtkObject* CreateInstance(const char* vtkclassname, bool isAbstract = false);
60 
67  static void CreateAllInstance(const char* vtkclassname, vtkCollection* retList);
72  static void ReHash();
84  static void UnRegisterAllFactories();
85 
91 
96  static vtkTypeBool HasOverrideAny(const char* className);
97 
103 
108  static void SetAllEnableFlags(vtkTypeBool flag, const char* className);
113  static void SetAllEnableFlags(vtkTypeBool flag, const char* className, const char* subclassName);
114 
115  // Instance methods to be used on individual instances of vtkObjectFactory
116 
117  // Methods from vtkObject
118  vtkTypeMacro(vtkObjectFactory, vtkObject);
122  void PrintSelf(ostream& os, vtkIndent indent) override;
123 
131  virtual const char* GetVTKSourceVersion() = 0;
132 
136  virtual const char* GetDescription() = 0;
137 
141  virtual int GetNumberOfOverrides();
142 
146  virtual const char* GetClassOverrideName(int index);
147 
152  virtual const char* GetClassOverrideWithName(int index);
153 
158 
163  virtual const char* GetOverrideDescription(int index);
164 
166 
170  virtual void SetEnableFlag(vtkTypeBool flag, const char* className, const char* subclassName);
171  virtual vtkTypeBool GetEnableFlag(const char* className, const char* subclassName);
173 
177  virtual vtkTypeBool HasOverride(const char* className);
181  virtual vtkTypeBool HasOverride(const char* className, const char* subclassName);
182 
188  virtual void Disable(const char* className);
189 
191 
194  vtkGetFilePathMacro(LibraryPath);
196 
197  typedef vtkObject* (*CreateFunction)();
198 
199 protected:
203  void RegisterOverride(const char* classOverride, const char* overrideClassName,
204  const char* description, int enableFlag, CreateFunction createFunction);
205 
211  virtual vtkObject* CreateObject(const char* vtkclassname);
212 
214  ~vtkObjectFactory() override;
215 
217  {
218  char* Description;
221  CreateFunction CreateCallback;
222  };
223 
228 
229 private:
230  void GrowOverrideArray();
231 
236  static void Init();
240  static void RegisterDefaults();
244  static void LoadDynamicFactories();
248  static void LoadLibrariesInPath(const std::string&);
249 
250  // list of registered factories
251  static vtkObjectFactoryCollection* RegisteredFactories;
252 
253  // member variables for a factory set by the base class
254  // at load or register time
255  void* LibraryHandle;
256  char* LibraryVTKVersion;
257  char* LibraryPath;
258 
259  vtkObjectFactory(const vtkObjectFactory&) = delete;
260  void operator=(const vtkObjectFactory&) = delete;
261 };
262 
263 // Implementation detail for Schwarz counter idiom.
264 class VTKCOMMONCORE_EXPORT vtkObjectFactoryRegistryCleanup
265 {
266 public:
269 
270 private:
273 };
275 
276 // Macro to create an object creation function.
277 // The name of the function will by vtkObjectFactoryCreateclassname
278 // where classname is the name of the class being created
279 #define VTK_CREATE_CREATE_FUNCTION(classname) \
280  static vtkObject* vtkObjectFactoryCreate##classname() { return classname::New(); }
281 
282 VTK_ABI_NAMESPACE_END
283 #endif
284 
285 #define VTK_FACTORY_INTERFACE_EXPORT VTKCOMMONCORE_EXPORT
286 
287 // Macro to create the interface "C" functions used in
288 // a dll or shared library that contains a VTK object factory.
289 // Put this function in the .cxx file of your object factory,
290 // and pass in the name of the factory sub-class that you want
291 // the dll to create.
292 #define VTK_FACTORY_INTERFACE_IMPLEMENT(factoryName) \
293  extern "C" VTK_FACTORY_INTERFACE_EXPORT const char* vtkGetFactoryVersion() \
294  { \
295  return VTK_SOURCE_VERSION; \
296  } \
297  extern "C" VTK_FACTORY_INTERFACE_EXPORT vtkObjectFactory* vtkLoad() \
298  { \
299  return factoryName ::New(); \
300  }
301 
302 // Macro to implement the body of the object factory form of the New() method.
303 #define VTK_OBJECT_FACTORY_NEW_BODY(thisClass) \
304  vtkObject* ret = vtkObjectFactory::CreateInstance(#thisClass, false); \
305  if (ret) \
306  { \
307  return static_cast<thisClass*>(ret); \
308  } \
309  auto result = new thisClass; \
310  result->InitializeObjectBase(); \
311  return result
312 
313 // Macro to implement the body of the abstract object factory form of the New()
314 // method, i.e. an abstract base class that can only be instantiated if the
315 // object factory overrides it.
316 #define VTK_ABSTRACT_OBJECT_FACTORY_NEW_BODY(thisClass) \
317  vtkObject* ret = vtkObjectFactory::CreateInstance(#thisClass, true); \
318  if (ret) \
319  { \
320  return static_cast<thisClass*>(ret); \
321  } \
322  vtkGenericWarningMacro("Error: no override found for '" #thisClass "'."); \
323  return nullptr
324 
325 // Macro to implement the body of the standard form of the New() method.
326 #if defined(VTK_ALL_NEW_OBJECT_FACTORY)
327 #define VTK_STANDARD_NEW_BODY(thisClass) VTK_OBJECT_FACTORY_NEW_BODY(thisClass)
328 #else
329 #define VTK_STANDARD_NEW_BODY(thisClass) \
330  auto result = new thisClass; \
331  result->InitializeObjectBase(); \
332  return result
333 #endif
334 
335 // Macro to implement the standard form of the New() method.
336 #define vtkStandardNewMacro(thisClass) \
337  thisClass* thisClass::New() { VTK_STANDARD_NEW_BODY(thisClass); }
338 
339 // Macro to implement the ExtendedNew() to create an object in a memkind extended memory space. If
340 // VTK is not compiled with VTK_USE_MEMKIND this is equivalent to New()
341 #define vtkStandardExtendedNewMacro(thisClass) \
342  thisClass* thisClass::ExtendedNew() \
343  { \
344  auto mkhold = vtkMemkindRAII(true); \
345  (void)mkhold; \
346  return thisClass::New(); \
347  }
348 
349 // Macro to implement the object factory form of the New() method.
350 #define vtkObjectFactoryNewMacro(thisClass) \
351  thisClass* thisClass::New() { VTK_OBJECT_FACTORY_NEW_BODY(thisClass); }
352 
353 // Macro to implement the abstract object factory form of the New() method.
354 // That is an abstract base class that can only be instantiated if the
355 // object factory overrides it.
356 #define vtkAbstractObjectFactoryNewMacro(thisClass) \
357  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:29
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:52
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