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=========================================================================*/
50#ifndef vtkObjectFactory_h
51#define vtkObjectFactory_h
52
53#include "vtkCommonCoreModule.h" // For export macro
54#include "vtkDebugLeaksManager.h" // Must be included before singletons
55#include "vtkFeatures.h" // For VTK_ALL_NEW_OBJECT_FACTORY
56#include "vtkObject.h"
57
58#include <string> // for std::string
59
62class vtkCollection;
63
64class VTKCOMMONCORE_EXPORT vtkObjectFactory : public vtkObject
65{
66public:
67 // Class Methods used to interface with the registered factories
68
79 static vtkObject* CreateInstance(const char* vtkclassname, bool isAbstract = false);
80
87 static void CreateAllInstance(const char* vtkclassname, vtkCollection* retList);
92 static void ReHash();
105
111
116 static int HasOverrideAny(const char* className);
117
123
128 static void SetAllEnableFlags(vtkTypeBool flag, const char* className);
133 static void SetAllEnableFlags(vtkTypeBool flag, const char* className, const char* subclassName);
134
135 // Instance methods to be used on individual instances of vtkObjectFactory
136
137 // Methods from vtkObject
142 void PrintSelf(ostream& os, vtkIndent indent) override;
143
151 virtual const char* GetVTKSourceVersion() = 0;
152
156 virtual const char* GetDescription() = 0;
157
161 virtual int GetNumberOfOverrides();
162
166 virtual const char* GetClassOverrideName(int index);
167
172 virtual const char* GetClassOverrideWithName(int index);
173
177 virtual vtkTypeBool GetEnableFlag(int index);
178
183 virtual const char* GetOverrideDescription(int index);
184
186
190 virtual void SetEnableFlag(vtkTypeBool flag, const char* className, const char* subclassName);
191 virtual vtkTypeBool GetEnableFlag(const char* className, const char* subclassName);
193
197 virtual int HasOverride(const char* className);
201 virtual int HasOverride(const char* className, const char* subclassName);
202
208 virtual void Disable(const char* className);
209
211
216
217 typedef vtkObject* (*CreateFunction)();
218
219protected:
223 void RegisterOverride(const char* classOverride, const char* overrideClassName,
224 const char* description, int enableFlag, CreateFunction createFunction);
225
231 virtual vtkObject* CreateObject(const char* vtkclassname);
232
235
237 {
241 CreateFunction CreateCallback;
242 };
243
248
249private:
250 void GrowOverrideArray();
251
256 static void Init();
260 static void RegisterDefaults();
264 static void LoadDynamicFactories();
268 static void LoadLibrariesInPath(const std::string&);
269
270 // list of registered factories
271 static vtkObjectFactoryCollection* RegisteredFactories;
272
273 // member variables for a factory set by the base class
274 // at load or register time
275 void* LibraryHandle;
276 char* LibraryVTKVersion;
277 char* LibraryPath;
278
279private:
280 vtkObjectFactory(const vtkObjectFactory&) = delete;
281 void operator=(const vtkObjectFactory&) = delete;
282};
283
284// Implementation detail for Schwarz counter idiom.
285class VTKCOMMONCORE_EXPORT vtkObjectFactoryRegistryCleanup
286{
287public:
290
291private:
294};
296
297// Macro to create an object creation function.
298// The name of the function will by vtkObjectFactoryCreateclassname
299// where classname is the name of the class being created
300#define VTK_CREATE_CREATE_FUNCTION(classname) \
301 static vtkObject* vtkObjectFactoryCreate##classname() { return classname::New(); }
302
303#endif
304
305#define VTK_FACTORY_INTERFACE_EXPORT VTKCOMMONCORE_EXPORT
306
307// Macro to create the interface "C" functions used in
308// a dll or shared library that contains a VTK object factory.
309// Put this function in the .cxx file of your object factory,
310// and pass in the name of the factory sub-class that you want
311// the dll to create.
312#define VTK_FACTORY_INTERFACE_IMPLEMENT(factoryName) \
313 extern "C" VTK_FACTORY_INTERFACE_EXPORT const char* vtkGetFactoryVersion() \
314 { \
315 return VTK_SOURCE_VERSION; \
316 } \
317 extern "C" VTK_FACTORY_INTERFACE_EXPORT vtkObjectFactory* vtkLoad() \
318 { \
319 return factoryName ::New(); \
320 }
321
322// Macro to implement the body of the object factory form of the New() method.
323#define VTK_OBJECT_FACTORY_NEW_BODY(thisClass) \
324 vtkObject* ret = vtkObjectFactory::CreateInstance(#thisClass, false); \
325 if (ret) \
326 { \
327 return static_cast<thisClass*>(ret); \
328 } \
329 auto result = new thisClass; \
330 result->InitializeObjectBase(); \
331 return result
332
333// Macro to implement the body of the abstract object factory form of the New()
334// method, i.e. an abstract base class that can only be instantiated if the
335// object factory overrides it.
336#define VTK_ABSTRACT_OBJECT_FACTORY_NEW_BODY(thisClass) \
337 vtkObject* ret = vtkObjectFactory::CreateInstance(#thisClass, true); \
338 if (ret) \
339 { \
340 return static_cast<thisClass*>(ret); \
341 } \
342 vtkGenericWarningMacro("Error: no override found for '" #thisClass "'."); \
343 return nullptr
344
345// Macro to implement the body of the standard form of the New() method.
346#if defined(VTK_ALL_NEW_OBJECT_FACTORY)
347#define VTK_STANDARD_NEW_BODY(thisClass) VTK_OBJECT_FACTORY_NEW_BODY(thisClass)
348#else
349#define VTK_STANDARD_NEW_BODY(thisClass) \
350 auto result = new thisClass; \
351 result->InitializeObjectBase(); \
352 return result
353#endif
354
355// Macro to implement the standard form of the New() method.
356#define vtkStandardNewMacro(thisClass) \
357 thisClass* thisClass::New() { VTK_STANDARD_NEW_BODY(thisClass); }
358
359// Macro to implement the ExtendedNew() to create an object in a memkind extended memory space. If
360// VTK is not compiled with VTK_USE_MEMKIND this is equivalent to New()
361#define vtkStandardExtendedNewMacro(thisClass) \
362 thisClass* thisClass::ExtendedNew() \
363 { \
364 auto mkhold = vtkMemkindRAII(true); \
365 (void)mkhold; \
366 return thisClass::New(); \
367 }
368
369// Macro to implement the object factory form of the New() method.
370#define vtkObjectFactoryNewMacro(thisClass) \
371 thisClass* thisClass::New() { VTK_OBJECT_FACTORY_NEW_BODY(thisClass); }
372
373// Macro to implement the abstract object factory form of the New() method.
374// That is an abstract base class that can only be instantiated if the
375// object factory overrides it.
376#define vtkAbstractObjectFactoryNewMacro(thisClass) \
377 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:49
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:72
maintain a list of override information objects
int vtkTypeBool
Definition vtkABI.h:69
static vtkObjectFactoryRegistryCleanup vtkObjectFactoryRegistryCleanupInstance
#define VTK_NEWINSTANCE