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=========================================================================*/
40
41#ifndef vtkObjectFactory_h
42#define vtkObjectFactory_h
43
44#include "vtkCommonCoreModule.h" // For export macro
45#include "vtkDebugLeaksManager.h" // Must be included before singletons
46#include "vtkFeatures.h" // For VTK_ALL_NEW_OBJECT_FACTORY
47#include "vtkObject.h"
48
49#include <string> // for std::string
50
53class vtkCollection;
54
55class VTKCOMMONCORE_EXPORT vtkObjectFactory : public vtkObject
56{
57public:
58 // Class Methods used to interface with the registered factories
59
70 static vtkObject* CreateInstance(const char* vtkclassname, bool isAbstract = false);
71
78 static void CreateAllInstance(const char* vtkclassname, vtkCollection* retList);
83 static void ReHash();
96
102
107 static int HasOverrideAny(const char* className);
108
114
119 static void SetAllEnableFlags(vtkTypeBool flag, const char* className);
124 static void SetAllEnableFlags(vtkTypeBool flag, const char* className, const char* subclassName);
125
126 // Instance methods to be used on individual instances of vtkObjectFactory
127
128 // Methods from vtkObject
133 void PrintSelf(ostream& os, vtkIndent indent) override;
134
142 virtual const char* GetVTKSourceVersion() = 0;
143
147 virtual const char* GetDescription() = 0;
148
152 virtual int GetNumberOfOverrides();
153
157 virtual const char* GetClassOverrideName(int index);
158
163 virtual const char* GetClassOverrideWithName(int index);
164
168 virtual vtkTypeBool GetEnableFlag(int index);
169
174 virtual const char* GetOverrideDescription(int index);
175
177
181 virtual void SetEnableFlag(vtkTypeBool flag, const char* className, const char* subclassName);
182 virtual vtkTypeBool GetEnableFlag(const char* className, const char* subclassName);
184
188 virtual int HasOverride(const char* className);
192 virtual int HasOverride(const char* className, const char* subclassName);
193
199 virtual void Disable(const char* className);
200
202
207
208 typedef vtkObject* (*CreateFunction)();
209
210protected:
214 void RegisterOverride(const char* classOverride, const char* overrideClassName,
215 const char* description, int enableFlag, CreateFunction createFunction);
216
222 virtual vtkObject* CreateObject(const char* vtkclassname);
223
226
234
239
240private:
241 void GrowOverrideArray();
242
247 static void Init();
251 static void RegisterDefaults();
255 static void LoadDynamicFactories();
259 static void LoadLibrariesInPath(const std::string&);
260
261 // list of registered factories
262 static vtkObjectFactoryCollection* RegisteredFactories;
263
264 // member variables for a factory set by the base class
265 // at load or register time
266 void* LibraryHandle;
267 char* LibraryVTKVersion;
268 char* LibraryPath;
269
270private:
271 vtkObjectFactory(const vtkObjectFactory&) = delete;
272 void operator=(const vtkObjectFactory&) = delete;
273};
274
275// Implementation detail for Schwarz counter idiom.
287
288// Macro to create an object creation function.
289// The name of the function will by vtkObjectFactoryCreateclassname
290// where classname is the name of the class being created
291#define VTK_CREATE_CREATE_FUNCTION(classname) \
292 static vtkObject* vtkObjectFactoryCreate##classname() { return classname::New(); }
293
294#endif
295
296#define VTK_FACTORY_INTERFACE_EXPORT VTKCOMMONCORE_EXPORT
297
298// Macro to create the interface "C" functions used in
299// a dll or shared library that contains a VTK object factory.
300// Put this function in the .cxx file of your object factory,
301// and pass in the name of the factory sub-class that you want
302// the dll to create.
303#define VTK_FACTORY_INTERFACE_IMPLEMENT(factoryName) \
304 extern "C" VTK_FACTORY_INTERFACE_EXPORT const char* vtkGetFactoryVersion() \
305 { \
306 return VTK_SOURCE_VERSION; \
307 } \
308 extern "C" VTK_FACTORY_INTERFACE_EXPORT vtkObjectFactory* vtkLoad() \
309 { \
310 return factoryName ::New(); \
311 }
312
313// Macro to implement the body of the object factory form of the New() method.
314#define VTK_OBJECT_FACTORY_NEW_BODY(thisClass) \
315 vtkObject* ret = vtkObjectFactory::CreateInstance(#thisClass, false); \
316 if (ret) \
317 { \
318 return static_cast<thisClass*>(ret); \
319 } \
320 auto result = new thisClass; \
321 result->InitializeObjectBase(); \
322 return result
323
324// Macro to implement the body of the abstract object factory form of the New()
325// method, i.e. an abstract base class that can only be instantiated if the
326// object factory overrides it.
327#define VTK_ABSTRACT_OBJECT_FACTORY_NEW_BODY(thisClass) \
328 vtkObject* ret = vtkObjectFactory::CreateInstance(#thisClass, true); \
329 if (ret) \
330 { \
331 return static_cast<thisClass*>(ret); \
332 } \
333 vtkGenericWarningMacro("Error: no override found for '" #thisClass "'."); \
334 return nullptr
335
336// Macro to implement the body of the standard form of the New() method.
337#if defined(VTK_ALL_NEW_OBJECT_FACTORY)
338#define VTK_STANDARD_NEW_BODY(thisClass) VTK_OBJECT_FACTORY_NEW_BODY(thisClass)
339#else
340#define VTK_STANDARD_NEW_BODY(thisClass) \
341 auto result = new thisClass; \
342 result->InitializeObjectBase(); \
343 return result
344#endif
345
346// Macro to implement the standard form of the New() method.
347#define vtkStandardNewMacro(thisClass) \
348 thisClass* thisClass::New() { VTK_STANDARD_NEW_BODY(thisClass); }
349
350// Macro to implement the ExtendedNew() to create an object in a memkind extended memory space. If
351// VTK is not compiled with VTK_USE_MEMKIND this is equivalent to New()
352#define vtkStandardExtendedNewMacro(thisClass) \
353 thisClass* thisClass::ExtendedNew() \
354 { \
355 auto mkhold = vtkMemkindRAII(true); \
356 (void)mkhold; \
357 return thisClass::New(); \
358 }
359
360// Macro to implement the object factory form of the New() method.
361#define vtkObjectFactoryNewMacro(thisClass) \
362 thisClass* thisClass::New() { VTK_OBJECT_FACTORY_NEW_BODY(thisClass); }
363
364// Macro to implement the abstract object factory form of the New() method.
365// That is an abstract base class that can only be instantiated if the
366// object factory overrides it.
367#define vtkAbstractObjectFactoryNewMacro(thisClass) \
368 thisClass* thisClass::New() { VTK_ABSTRACT_OBJECT_FACTORY_NEW_BODY(thisClass); }
create and manipulate ordered lists of objects
a simple class to control print indentation
Definition vtkIndent.h:40
maintain a list of object factories
virtual const char * GetClassOverrideWithName(int index)
Return the name of the class that will override the class at the given index.
virtual const char * GetDescription()=0
Return a descriptive string describing the factory.
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.
static vtkObject * CreateInstance(const char *vtkclassname, bool isAbstract=false)
Create and return an instance of the named vtk object.
virtual vtkTypeBool GetEnableFlag(int index)
Return the enable flag for the class at the given index.
virtual const char * GetVTKSourceVersion()=0
All sub-classes of vtkObjectFactory should must return the version of VTK they were built with.
virtual const char * GetClassOverrideName(int index)
Return the name of a class override at the given index.
static vtkObjectFactoryCollection * GetRegisteredFactories()
Return the list of all registered factories.
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 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.
OverrideInformation * OverrideArray
static void UnRegisterFactory(vtkObjectFactory *)
Remove a factory from the list of registered factories.
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 int HasOverrideAny(const char *className)
return 1 if one of the registered factories overrides the given class name
vtkObject *(* CreateFunction)()
virtual vtkObject * CreateObject(const char *vtkclassname)
This method is provided by sub-classes of vtkObjectFactory.
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.
maintain a list of override information objects
int vtkTypeBool
Definition vtkABI.h:69
static vtkObjectFactoryRegistryCleanup vtkObjectFactoryRegistryCleanupInstance
#define VTK_NEWINSTANCE