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 =========================================================================*/
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 
50 class vtkCollection;
51 
52 class VTKCOMMONCORE_EXPORT vtkObjectFactory : public vtkObject
53 {
54 public:
55  // Class Methods used to interface with the registered factories
56 
67  static vtkObject* CreateInstance(const char* vtkclassname, bool isAbstract = false);
68 
75  static void CreateAllInstance(const char* vtkclassname, vtkCollection* retList);
80  static void ReHash();
92  static void UnRegisterAllFactories();
93 
99 
104  static int HasOverrideAny(const char* className);
105 
111 
116  static void SetAllEnableFlags(vtkTypeBool flag, const char* className);
121  static void SetAllEnableFlags(vtkTypeBool flag, const char* className, const char* subclassName);
122 
123  // Instance methods to be used on individual instances of vtkObjectFactory
124 
125  // Methods from vtkObject
126  vtkTypeMacro(vtkObjectFactory, vtkObject);
130  void PrintSelf(ostream& os, vtkIndent indent) override;
131 
139  virtual const char* GetVTKSourceVersion() = 0;
140 
144  virtual const char* GetDescription() = 0;
145 
149  virtual int GetNumberOfOverrides();
150 
154  virtual const char* GetClassOverrideName(int index);
155 
160  virtual const char* GetClassOverrideWithName(int index);
161 
166 
171  virtual const char* GetOverrideDescription(int index);
172 
174 
178  virtual void SetEnableFlag(vtkTypeBool flag, const char* className, const char* subclassName);
179  virtual vtkTypeBool GetEnableFlag(const char* className, const char* subclassName);
181 
185  virtual int HasOverride(const char* className);
189  virtual int HasOverride(const char* className, const char* subclassName);
190 
196  virtual void Disable(const char* className);
197 
199 
202  vtkGetFilePathMacro(LibraryPath);
204 
205  typedef vtkObject* (*CreateFunction)();
206 
207 protected:
211  void RegisterOverride(const char* classOverride, const char* overrideClassName,
212  const char* description, int enableFlag, CreateFunction createFunction);
213 
219  virtual vtkObject* CreateObject(const char* vtkclassname);
220 
222  ~vtkObjectFactory() override;
223 
225  {
226  char* Description;
229  CreateFunction CreateCallback;
230  };
231 
236 
237 private:
238  void GrowOverrideArray();
239 
244  static void Init();
248  static void RegisterDefaults();
252  static void LoadDynamicFactories();
256  static void LoadLibrariesInPath(const std::string&);
257 
258  // list of registered factories
259  static vtkObjectFactoryCollection* RegisteredFactories;
260 
261  // member variables for a factory set by the base class
262  // at load or register time
263  void* LibraryHandle;
264  char* LibraryVTKVersion;
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* vtkGetFactoryVersion() \
302  { \
303  return VTK_SOURCE_VERSION; \
304  } \
305  extern "C" VTK_FACTORY_INTERFACE_EXPORT vtkObjectFactory* vtkLoad() \
306  { \
307  return factoryName ::New(); \
308  }
309 
310 // Macro to implement the body of the object factory form of the New() method.
311 #define VTK_OBJECT_FACTORY_NEW_BODY(thisClass) \
312  vtkObject* ret = vtkObjectFactory::CreateInstance(#thisClass, false); \
313  if (ret) \
314  { \
315  return static_cast<thisClass*>(ret); \
316  } \
317  auto result = new thisClass; \
318  result->InitializeObjectBase(); \
319  return result
320 
321 // Macro to implement the body of the abstract object factory form of the New()
322 // method, i.e. an abstract base class that can only be instantiated if the
323 // object factory overrides it.
324 #define VTK_ABSTRACT_OBJECT_FACTORY_NEW_BODY(thisClass) \
325  vtkObject* ret = vtkObjectFactory::CreateInstance(#thisClass, true); \
326  if (ret) \
327  { \
328  return static_cast<thisClass*>(ret); \
329  } \
330  vtkGenericWarningMacro("Error: no override found for '" #thisClass "'."); \
331  return nullptr
332 
333 // Macro to implement the body of the standard form of the New() method.
334 #if defined(VTK_ALL_NEW_OBJECT_FACTORY)
335 #define VTK_STANDARD_NEW_BODY(thisClass) VTK_OBJECT_FACTORY_NEW_BODY(thisClass)
336 #else
337 #define VTK_STANDARD_NEW_BODY(thisClass) \
338  auto result = new thisClass; \
339  result->InitializeObjectBase(); \
340  return result
341 #endif
342 
343 // Macro to implement the standard form of the New() method.
344 #define vtkStandardNewMacro(thisClass) \
345  thisClass* thisClass::New() { VTK_STANDARD_NEW_BODY(thisClass); }
346 
347 // Macro to implement the ExtendedNew() to create an object in a memkind extended memory space. If
348 // VTK is not compiled with VTK_USE_MEMKIND this is equivalent to New()
349 #define vtkStandardExtendedNewMacro(thisClass) \
350  thisClass* thisClass::ExtendedNew() \
351  { \
352  auto mkhold = vtkMemkindRAII(true); \
353  (void)mkhold; \
354  return thisClass::New(); \
355  }
356 
357 // Macro to implement the object factory form of the New() method.
358 #define vtkObjectFactoryNewMacro(thisClass) \
359  thisClass* thisClass::New() { VTK_OBJECT_FACTORY_NEW_BODY(thisClass); }
360 
361 // Macro to implement the abstract object factory form of the New() method.
362 // That is an abstract base class that can only be instantiated if the
363 // object factory overrides it.
364 #define vtkAbstractObjectFactoryNewMacro(thisClass) \
365  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)
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:63
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