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=========================================================================*/
47#ifndef vtkObjectFactory_h
48#define vtkObjectFactory_h
49
50#include "vtkCommonCoreModule.h" // For export macro
51#include "vtkDebugLeaksManager.h" // Must be included before singletons
52#include "vtkFeatures.h" // For VTK_ALL_NEW_OBJECT_FACTORY
53#include "vtkObject.h"
54
55#include <string> // for std::string
56
59class vtkCollection;
60
61class VTKCOMMONCORE_EXPORT vtkObjectFactory : public vtkObject
62{
63public:
64 // Class Methods used to interface with the registered factories
65
76 static vtkObject* CreateInstance(const char* vtkclassname, bool isAbstract = false);
77
84 static void CreateAllInstance(const char* vtkclassname, vtkCollection* retList);
89 static void ReHash();
102
108
113 static int HasOverrideAny(const char* className);
114
120
125 static void SetAllEnableFlags(vtkTypeBool flag, const char* className);
130 static void SetAllEnableFlags(vtkTypeBool flag, const char* className, const char* subclassName);
131
132 // Instance methods to be used on individual instances of vtkObjectFactory
133
134 // Methods from vtkObject
139 void PrintSelf(ostream& os, vtkIndent indent) override;
140
148 virtual const char* GetVTKSourceVersion() = 0;
149
153 virtual const char* GetDescription() = 0;
154
158 virtual int GetNumberOfOverrides();
159
163 virtual const char* GetClassOverrideName(int index);
164
169 virtual const char* GetClassOverrideWithName(int index);
170
175
180 virtual const char* GetOverrideDescription(int index);
181
183
187 virtual void SetEnableFlag(vtkTypeBool flag, const char* className, const char* subclassName);
188 virtual vtkTypeBool GetEnableFlag(const char* className, const char* subclassName);
190
194 virtual int HasOverride(const char* className);
198 virtual int HasOverride(const char* className, const char* subclassName);
199
205 virtual void Disable(const char* className);
206
208
213
214 typedef vtkObject* (*CreateFunction)();
215
216protected:
220 void RegisterOverride(const char* classOverride, const char* overrideClassName,
221 const char* description, int enableFlag, CreateFunction createFunction);
222
228 virtual vtkObject* CreateObject(const char* vtkclassname);
229
232
234 {
238 CreateFunction CreateCallback;
239 };
240
245
246private:
247 void GrowOverrideArray();
248
253 static void Init();
257 static void RegisterDefaults();
261 static void LoadDynamicFactories();
265 static void LoadLibrariesInPath(const std::string&);
266
267 // list of registered factories
268 static vtkObjectFactoryCollection* RegisteredFactories;
269
270 // member variables for a factory set by the base class
271 // at load or register time
272 void* LibraryHandle;
273 char* LibraryVTKVersion;
274 char* LibraryPath;
275
276private:
277 vtkObjectFactory(const vtkObjectFactory&) = delete;
278 void operator=(const vtkObjectFactory&) = delete;
279};
280
281// Implementation detail for Schwarz counter idiom.
282class VTKCOMMONCORE_EXPORT vtkObjectFactoryRegistryCleanup
283{
284public:
287
288private:
291};
293
294// Macro to create an object creation function.
295// The name of the function will by vtkObjectFactoryCreateclassname
296// where classname is the name of the class being created
297#define VTK_CREATE_CREATE_FUNCTION(classname) \
298 static vtkObject* vtkObjectFactoryCreate##classname() { return classname::New(); }
299
300#endif
301
302#define VTK_FACTORY_INTERFACE_EXPORT VTKCOMMONCORE_EXPORT
303
304// Macro to create the interface "C" functions used in
305// a dll or shared library that contains a VTK object factory.
306// Put this function in the .cxx file of your object factory,
307// and pass in the name of the factory sub-class that you want
308// the dll to create.
309#define VTK_FACTORY_INTERFACE_IMPLEMENT(factoryName) \
310 extern "C" VTK_FACTORY_INTERFACE_EXPORT const char* vtkGetFactoryVersion() \
311 { \
312 return VTK_SOURCE_VERSION; \
313 } \
314 extern "C" VTK_FACTORY_INTERFACE_EXPORT vtkObjectFactory* vtkLoad() \
315 { \
316 return factoryName ::New(); \
317 }
318
319// Macro to implement the body of the object factory form of the New() method.
320#define VTK_OBJECT_FACTORY_NEW_BODY(thisClass) \
321 vtkObject* ret = vtkObjectFactory::CreateInstance(#thisClass, false); \
322 if (ret) \
323 { \
324 return static_cast<thisClass*>(ret); \
325 } \
326 auto result = new thisClass; \
327 result->InitializeObjectBase(); \
328 return result
329
330// Macro to implement the body of the abstract object factory form of the New()
331// method, i.e. an abstract base class that can only be instantiated if the
332// object factory overrides it.
333#define VTK_ABSTRACT_OBJECT_FACTORY_NEW_BODY(thisClass) \
334 vtkObject* ret = vtkObjectFactory::CreateInstance(#thisClass, true); \
335 if (ret) \
336 { \
337 return static_cast<thisClass*>(ret); \
338 } \
339 vtkGenericWarningMacro("Error: no override found for '" #thisClass "'."); \
340 return nullptr
341
342// Macro to implement the body of the standard form of the New() method.
343#if defined(VTK_ALL_NEW_OBJECT_FACTORY)
344#define VTK_STANDARD_NEW_BODY(thisClass) VTK_OBJECT_FACTORY_NEW_BODY(thisClass)
345#else
346#define VTK_STANDARD_NEW_BODY(thisClass) \
347 auto result = new thisClass; \
348 result->InitializeObjectBase(); \
349 return result
350#endif
351
352// Macro to implement the standard form of the New() method.
353#define vtkStandardNewMacro(thisClass) \
354 thisClass* thisClass::New() { VTK_STANDARD_NEW_BODY(thisClass); }
355
356// Macro to implement the ExtendedNew() to create an object in a memkind extended memory space. If
357// VTK is not compiled with VTK_USE_MEMKIND this is equivalent to New()
358#define vtkStandardExtendedNewMacro(thisClass) \
359 thisClass* thisClass::ExtendedNew() \
360 { \
361 auto mkhold = vtkMemkindRAII(true); \
362 (void)mkhold; \
363 return thisClass::New(); \
364 }
365
366// Macro to implement the object factory form of the New() method.
367#define vtkObjectFactoryNewMacro(thisClass) \
368 thisClass* thisClass::New() { VTK_OBJECT_FACTORY_NEW_BODY(thisClass); }
369
370// Macro to implement the abstract object factory form of the New() method.
371// That is an abstract base class that can only be instantiated if the
372// object factory overrides it.
373#define vtkAbstractObjectFactoryNewMacro(thisClass) \
374 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:43
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 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
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.
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