VTK  9.3.20240424
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
138#ifndef vtkObjectFactory_h
139#define vtkObjectFactory_h
140
141#include "vtkCommonCoreModule.h" // For export macro
142#include "vtkDebugLeaksManager.h" // Must be included before singletons
143#include "vtkFeatures.h" // For VTK_ALL_NEW_OBJECT_FACTORY
144#include "vtkObject.h"
145
146#include <string> // for std::string
147
148VTK_ABI_NAMESPACE_BEGIN
151class vtkCollection;
152
153class VTKCOMMONCORE_EXPORT vtkObjectFactory : public vtkObject
154{
155public:
156 // Class Methods used to interface with the registered factories
157
168 static vtkObject* CreateInstance(const char* vtkclassname, bool isAbstract = false);
169
176 static void CreateAllInstance(const char* vtkclassname, vtkCollection* retList);
181 static void ReHash();
194
200
205 static vtkTypeBool HasOverrideAny(const char* className);
206
212
217 static void SetAllEnableFlags(vtkTypeBool flag, const char* className);
222 static void SetAllEnableFlags(vtkTypeBool flag, const char* className, const char* subclassName);
223
224 // Instance methods to be used on individual instances of vtkObjectFactory
225
226 // Methods from vtkObject
231 void PrintSelf(ostream& os, vtkIndent indent) override;
232
240 virtual const char* GetVTKSourceVersion() = 0;
241
245 virtual const char* GetDescription() = 0;
246
250 virtual int GetNumberOfOverrides();
251
255 virtual const char* GetClassOverrideName(int index);
256
261 virtual const char* GetClassOverrideWithName(int index);
262
266 virtual vtkTypeBool GetEnableFlag(int index);
267
272 virtual const char* GetOverrideDescription(int index);
273
275
279 virtual void SetEnableFlag(vtkTypeBool flag, const char* className, const char* subclassName);
280 virtual vtkTypeBool GetEnableFlag(const char* className, const char* subclassName);
282
286 virtual vtkTypeBool HasOverride(const char* className);
290 virtual vtkTypeBool HasOverride(const char* className, const char* subclassName);
291
297 virtual void Disable(const char* className);
298
300
305
306 typedef vtkObject* (*CreateFunction)();
307
308protected:
312 void RegisterOverride(const char* classOverride, const char* overrideClassName,
313 const char* description, int enableFlag, CreateFunction createFunction);
314
320 virtual vtkObject* CreateObject(const char* vtkclassname);
321
324
326 {
330 CreateFunction CreateCallback;
331 };
332
337
338private:
339 void GrowOverrideArray();
340
345 static void Init();
349 static void RegisterDefaults();
353 static void LoadDynamicFactories();
357 static void LoadLibrariesInPath(const std::string&);
358
359 // list of registered factories
360 static vtkObjectFactoryCollection* RegisteredFactories;
361
362 // member variables for a factory set by the base class
363 // at load or register time
364 void* LibraryHandle;
365 char* LibraryVTKVersion;
366 char* LibraryPath;
367
368 vtkObjectFactory(const vtkObjectFactory&) = delete;
369 void operator=(const vtkObjectFactory&) = delete;
370};
371
372// Implementation detail for Schwarz counter idiom.
373class VTKCOMMONCORE_EXPORT vtkObjectFactoryRegistryCleanup
374{
375public:
378
379private:
382};
384
385// Macro to create an object creation function.
386// The name of the function will by vtkObjectFactoryCreateclassname
387// where classname is the name of the class being created
388#define VTK_CREATE_CREATE_FUNCTION(classname) \
389 static vtkObject* vtkObjectFactoryCreate##classname() { return classname::New(); }
390
391VTK_ABI_NAMESPACE_END
392#endif
393
394#define VTK_FACTORY_INTERFACE_EXPORT VTKCOMMONCORE_EXPORT
395
396// Macro to create the interface "C" functions used in
397// a dll or shared library that contains a VTK object factory.
398// Put this function in the .cxx file of your object factory,
399// and pass in the name of the factory sub-class that you want
400// the dll to create.
401#define VTK_FACTORY_INTERFACE_IMPLEMENT(factoryName) \
402 extern "C" VTK_FACTORY_INTERFACE_EXPORT const char* vtkGetFactoryVersion() \
403 { \
404 return VTK_SOURCE_VERSION; \
405 } \
406 extern "C" VTK_FACTORY_INTERFACE_EXPORT vtkObjectFactory* vtkLoad() \
407 { \
408 return factoryName ::New(); \
409 }
410
411// Macro to implement the body of the object factory form of the New() method.
412#define VTK_OBJECT_FACTORY_NEW_BODY(thisClass) \
413 vtkObject* ret = vtkObjectFactory::CreateInstance(#thisClass, false); \
414 if (ret) \
415 { \
416 return static_cast<thisClass*>(ret); \
417 } \
418 auto result = new thisClass; \
419 result->InitializeObjectBase(); \
420 return result
421
422// Macro to implement the body of the abstract object factory form of the New()
423// method, i.e. an abstract base class that can only be instantiated if the
424// object factory overrides it.
425#define VTK_ABSTRACT_OBJECT_FACTORY_NEW_BODY(thisClass) \
426 vtkObject* ret = vtkObjectFactory::CreateInstance(#thisClass, true); \
427 if (ret) \
428 { \
429 return static_cast<thisClass*>(ret); \
430 } \
431 vtkGenericWarningMacro("Error: no override found for '" #thisClass "'."); \
432 return nullptr
433
434// Macro to implement the body of the standard form of the New() method.
435#if defined(VTK_ALL_NEW_OBJECT_FACTORY)
436#define VTK_STANDARD_NEW_BODY(thisClass) VTK_OBJECT_FACTORY_NEW_BODY(thisClass)
437#else
438#define VTK_STANDARD_NEW_BODY(thisClass) \
439 auto result = new thisClass; \
440 result->InitializeObjectBase(); \
441 return result
442#endif
443
444// Macro to implement the standard form of the New() method.
445#define vtkStandardNewMacro(thisClass) \
446 thisClass* thisClass::New() { VTK_STANDARD_NEW_BODY(thisClass); }
447
448// Macro to implement the ExtendedNew() to create an object in a memkind extended memory space. If
449// VTK is not compiled with VTK_USE_MEMKIND this is equivalent to New()
450#define vtkStandardExtendedNewMacro(thisClass) \
451 thisClass* thisClass::ExtendedNew() \
452 { \
453 auto mkhold = vtkMemkindRAII(true); \
454 (void)mkhold; \
455 return thisClass::New(); \
456 }
457
458// Macro to implement the object factory form of the New() method.
459#define vtkObjectFactoryNewMacro(thisClass) \
460 thisClass* thisClass::New() { VTK_OBJECT_FACTORY_NEW_BODY(thisClass); }
461
462// Macro to implement the abstract object factory form of the New() method.
463// That is an abstract base class that can only be instantiated if the
464// object factory overrides it.
465#define vtkAbstractObjectFactoryNewMacro(thisClass) \
466 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:108
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 vtkTypeBool HasOverride(const char *className)
Return 1 if this factory overrides the given class name, 0 otherwise.
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.
static vtkTypeBool HasOverrideAny(const char *className)
return 1 if one of the registered factories overrides the given class name
OverrideInformation * OverrideArray
virtual vtkTypeBool HasOverride(const char *className, const char *subclassName)
Return 1 if this factory overrides the given class name, 0 otherwise.
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.
virtual vtkObject * CreateObject(const char *vtkclassname)
This method is provided by sub-classes of vtkObjectFactory.
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:162
maintain a list of override information objects
int vtkTypeBool
Definition vtkABI.h:64
static vtkObjectFactoryRegistryCleanup vtkObjectFactoryRegistryCleanupInstance
#define VTK_NEWINSTANCE