VTK  9.6.20260108
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
152
153#ifndef vtkObjectFactory_h
154#define vtkObjectFactory_h
155
156#include "vtkCommonCoreModule.h" // For export macro
157#include "vtkDebugLeaksManager.h" // Must be included before singletons
158#include "vtkFeatures.h" // For VTK_ALL_NEW_OBJECT_FACTORY
159#include "vtkObject.h"
160#include "vtkSmartPointer.h" // For vtkSmartPointer
161
162#include <string> // for std::string
163
164VTK_ABI_NAMESPACE_BEGIN
167class vtkCollection;
169
170class VTKCOMMONCORE_EXPORT vtkObjectFactory : public vtkObject
171{
172public:
173 // Class Methods used to interface with the registered factories
174
185 static vtkObject* CreateInstance(const char* vtkclassname, bool isAbstract = false);
186
193 static void CreateAllInstance(const char* vtkclassname, vtkCollection* retList);
198 static void ReHash();
211
217
222 static vtkTypeBool HasOverrideAny(const char* className);
223
229
234 static void SetAllEnableFlags(vtkTypeBool flag, const char* className);
239 static void SetAllEnableFlags(vtkTypeBool flag, const char* className, const char* subclassName);
240
241 // Instance methods to be used on individual instances of vtkObjectFactory
242
243 // Methods from vtkObject
248 void PrintSelf(ostream& os, vtkIndent indent) override;
249
257 virtual const char* GetVTKSourceVersion() VTK_FUTURE_CONST = 0;
258
262 virtual const char* GetDescription() VTK_FUTURE_CONST = 0;
263
267 virtual int GetNumberOfOverrides() VTK_FUTURE_CONST;
268
272 virtual const char* GetClassOverrideName(int index) VTK_FUTURE_CONST;
273
278 virtual const char* GetClassOverrideWithName(int index) VTK_FUTURE_CONST;
279
283 virtual vtkTypeBool GetEnableFlag(int index) VTK_FUTURE_CONST;
284
289 virtual const char* GetOverrideDescription(int index) VTK_FUTURE_CONST;
290
292
296 virtual void SetEnableFlag(vtkTypeBool flag, const char* className, const char* subclassName);
298 const char* className, const char* subclassName) VTK_FUTURE_CONST;
300
305
309 virtual vtkTypeBool HasOverride(const char* className) VTK_FUTURE_CONST;
313 virtual vtkTypeBool HasOverride(const char* className, const char* subclassName) VTK_FUTURE_CONST;
314
320 virtual void Disable(const char* className);
321
323
328
330
342 static bool InitializePreferencesFromCommandLineArgs(int& argc, char* argv[]);
343
349 static void SetPreferences(std::string preferences);
350 static std::string GetPreferences();
351
352protected:
362 void RegisterOverride(const char* classOverride, const char* overrideClassName,
363 const char* description, int enableFlag, CreateFunction createFunction,
364 vtkOverrideAttribute* attributes = nullptr);
365
371 virtual vtkObject* CreateObject(const char* vtkclassname);
372
374 ~vtkObjectFactory() override;
375
384
389
390private:
391 void GrowOverrideArray();
392
397 static void Init();
401 static void RegisterDefaults();
405 static void LoadDynamicFactories();
409 static void LoadLibrariesInPath(const std::string&);
410
411 // list of registered factories
412 static vtkObjectFactoryCollection* RegisteredFactories;
413
414 // member variables for a factory set by the base class
415 // at load or register time
416 void* LibraryHandle;
417 char* LibraryVTKVersion;
418 char* LibraryPath;
419
420 static std::string Preferences;
421
422 vtkObjectFactory(const vtkObjectFactory&) = delete;
423 void operator=(const vtkObjectFactory&) = delete;
424};
425
426// Implementation detail for Schwarz counter idiom.
438
439// Macro to create an object creation function.
440// The name of the function will by vtkObjectFactoryCreateclassname
441// where classname is the name of the class being created
442#define VTK_CREATE_CREATE_FUNCTION(classname) \
443 static vtkObject* vtkObjectFactoryCreate##classname() \
444 { \
445 return classname::New(); \
446 }
447
448VTK_ABI_NAMESPACE_END
449#endif
450
451#define VTK_FACTORY_INTERFACE_EXPORT VTKCOMMONCORE_EXPORT
452
453// Macro to create the interface "C" functions used in
454// a dll or shared library that contains a VTK object factory.
455// Put this function in the .cxx file of your object factory,
456// and pass in the name of the factory sub-class that you want
457// the dll to create.
458#define VTK_FACTORY_INTERFACE_IMPLEMENT(factoryName) \
459 extern "C" VTK_FACTORY_INTERFACE_EXPORT const char* vtkGetFactoryVersion() \
460 { \
461 return VTK_SOURCE_VERSION; \
462 } \
463 extern "C" VTK_FACTORY_INTERFACE_EXPORT vtkObjectFactory* vtkLoad() \
464 { \
465 return factoryName ::New(); \
466 }
467
468// Macro to implement the body of the object factory form of the New() method.
469#define VTK_OBJECT_FACTORY_NEW_BODY(thisClass) \
470 vtkObject* ret = vtkObjectFactory::CreateInstance(#thisClass, false); \
471 if (ret) \
472 { \
473 return static_cast<thisClass*>(ret); \
474 } \
475 auto result = new thisClass; \
476 result->InitializeObjectBase(); \
477 return result
478
479// Macro to implement the body of the abstract object factory form of the New()
480// method, i.e. an abstract base class that can only be instantiated if the
481// object factory overrides it.
482#define VTK_ABSTRACT_OBJECT_FACTORY_NEW_BODY(thisClass) \
483 vtkObject* ret = vtkObjectFactory::CreateInstance(#thisClass, true); \
484 if (ret) \
485 { \
486 return static_cast<thisClass*>(ret); \
487 } \
488 vtkGenericWarningMacro("Error: no override found for '" #thisClass "'."); \
489 return nullptr
490
491// Macro to implement the body of the standard form of the New() method.
492#if defined(VTK_ALL_NEW_OBJECT_FACTORY)
493#define VTK_STANDARD_NEW_BODY(thisClass) VTK_OBJECT_FACTORY_NEW_BODY(thisClass)
494#else
495#define VTK_STANDARD_NEW_BODY(thisClass) \
496 auto result = new thisClass; \
497 result->InitializeObjectBase(); \
498 return result
499#endif
500
501// Macro to implement the standard form of the New() method.
502#define vtkStandardNewMacro(thisClass) \
503 thisClass* thisClass::New() \
504 { \
505 VTK_STANDARD_NEW_BODY(thisClass); \
506 }
507
508// Macro to implement the ExtendedNew() to create an object in a memkind extended memory space. If
509// VTK is not compiled with VTK_USE_MEMKIND this is equivalent to New()
510#define vtkStandardExtendedNewMacro(thisClass) \
511 thisClass* thisClass::ExtendedNew() \
512 { \
513 auto mkhold = vtkMemkindRAII(true); \
514 (void)mkhold; \
515 return thisClass::New(); \
516 }
517
518// Macro to implement the object factory form of the New() method.
519#define vtkObjectFactoryNewMacro(thisClass) \
520 thisClass* thisClass::New() \
521 { \
522 VTK_OBJECT_FACTORY_NEW_BODY(thisClass); \
523 }
524
525// Macro to implement the abstract object factory form of the New() method.
526// That is an abstract base class that can only be instantiated if the
527// object factory overrides it.
528#define vtkAbstractObjectFactoryNewMacro(thisClass) \
529 thisClass* thisClass::New() \
530 { \
531 VTK_ABSTRACT_OBJECT_FACTORY_NEW_BODY(thisClass); \
532 }
create and manipulate ordered lists of objects
a simple class to control print indentation
Definition vtkIndent.h:108
maintain a list of object factories
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.
static vtkObjectFactoryCollection * GetRegisteredFactories()
Return the list of all registered factories.
static void GetOverrideInformation(const char *name, vtkOverrideInformationCollection *)
Fill the given collection with all the overrides for the class with the given name.
virtual vtkTypeBool HasOverride(const char *className) VTK_FUTURE_CONST
Return 1 if this factory overrides the given class name, 0 otherwise.
virtual void SetEnableFlag(vtkTypeBool flag, const char *className, const char *subclassName)
Set and Get the Enable flag for the specific override of className.
vtkOverrideAttribute * GetOverrideAttributes(int index) const
Get override attributes if any for the factory at the given index.
virtual vtkTypeBool GetEnableFlag(int index) VTK_FUTURE_CONST
Return the enable flag for the class 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
virtual const char * GetClassOverrideWithName(int index) VTK_FUTURE_CONST
Return the name of the class that will override the class at the given index.
OverrideInformation * OverrideArray
void RegisterOverride(const char *classOverride, const char *overrideClassName, const char *description, int enableFlag, CreateFunction createFunction, vtkOverrideAttribute *attributes=nullptr)
Register object creation information with the factory.
static void UnRegisterFactory(vtkObjectFactory *)
Remove a factory from the list of registered factories.
static std::string GetPreferences()
virtual const char * GetOverrideDescription(int index) VTK_FUTURE_CONST
Return the description for a the class override at the given index.
virtual const char * GetDescription() VTK_FUTURE_CONST=0
Return a descriptive string describing the factory.
vtkObject *(* CreateFunction)()
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 SetPreferences(std::string preferences)
Set preferences string.
virtual int GetNumberOfOverrides() VTK_FUTURE_CONST
Return number of overrides this factory can create.
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.
static bool InitializePreferencesFromCommandLineArgs(int &argc, char *argv[])
Initialize preferences from command line arguments.
virtual const char * GetClassOverrideName(int index) VTK_FUTURE_CONST
Return the name of a class override at the given index.
static void RegisterFactory(vtkObjectFactory *)
Register a factory so it can be used to create vtk objects.
virtual const char * GetVTKSourceVersion() VTK_FUTURE_CONST=0
All sub-classes of vtkObjectFactory should must return the version of VTK they were built with.
vtkGetFilePathMacro(LibraryPath)
This returns the path to a dynamically loaded factory.
Attribute for vtkObjectFactory overrides.
maintain a list of override information objects
Hold a reference to a vtkObjectBase instance.
vtkSmartPointer< vtkOverrideAttribute > Attributes
int vtkTypeBool
Definition vtkABI.h:64
static vtkObjectFactoryRegistryCleanup vtkObjectFactoryRegistryCleanupInstance
#define VTK_NEWINSTANCE