VTK  9.6.20260221
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
155
156#ifndef vtkObjectFactory_h
157#define vtkObjectFactory_h
158
159#include "vtkCommonCoreModule.h" // For export macro
160#include "vtkDebugLeaksManager.h" // Must be included before singletons
161#include "vtkFeatures.h" // For VTK_ALL_NEW_OBJECT_FACTORY
162#include "vtkObject.h"
163#include "vtkSmartPointer.h" // For vtkSmartPointer
164
165#include <string> // for std::string
166
167VTK_ABI_NAMESPACE_BEGIN
170class vtkCollection;
172
173class VTKCOMMONCORE_EXPORT vtkObjectFactory : public vtkObject
174{
175public:
176 // Class Methods used to interface with the registered factories
177
188 static vtkObject* CreateInstance(const char* vtkclassname, bool isAbstract = false);
189
196 static void CreateAllInstance(const char* vtkclassname, vtkCollection* retList);
201 static void ReHash();
214
220
225 static vtkTypeBool HasOverrideAny(const char* className);
226
232
237 static void SetAllEnableFlags(vtkTypeBool flag, const char* className);
242 static void SetAllEnableFlags(vtkTypeBool flag, const char* className, const char* subclassName);
243
244 // Instance methods to be used on individual instances of vtkObjectFactory
245
246 // Methods from vtkObject
251 void PrintSelf(ostream& os, vtkIndent indent) override;
252
260 virtual const char* GetVTKSourceVersion() VTK_FUTURE_CONST = 0;
261
265 virtual const char* GetDescription() VTK_FUTURE_CONST = 0;
266
270 virtual int GetNumberOfOverrides() VTK_FUTURE_CONST;
271
275 virtual const char* GetClassOverrideName(int index) VTK_FUTURE_CONST;
276
281 virtual const char* GetClassOverrideWithName(int index) VTK_FUTURE_CONST;
282
286 virtual vtkTypeBool GetEnableFlag(int index) VTK_FUTURE_CONST;
287
292 virtual const char* GetOverrideDescription(int index) VTK_FUTURE_CONST;
293
295
299 virtual void SetEnableFlag(vtkTypeBool flag, const char* className, const char* subclassName);
301 const char* className, const char* subclassName) VTK_FUTURE_CONST;
303
308
312 virtual vtkTypeBool HasOverride(const char* className) VTK_FUTURE_CONST;
316 virtual vtkTypeBool HasOverride(const char* className, const char* subclassName) VTK_FUTURE_CONST;
317
323 virtual void Disable(const char* className);
324
326
331
333
345 static bool InitializePreferencesFromCommandLineArgs(int& argc, char* argv[]);
346
352 static void SetPreferences(std::string preferences);
353 static std::string GetPreferences();
354
355protected:
365 void RegisterOverride(const char* classOverride, const char* overrideClassName,
366 const char* description, int enableFlag, CreateFunction createFunction,
367 vtkOverrideAttribute* attributes = nullptr);
368
374 virtual vtkObject* CreateObject(const char* vtkclassname);
375
377 ~vtkObjectFactory() override;
378
387
392
393private:
394 void GrowOverrideArray();
395
400 static void Init();
404 static void RegisterDefaults();
408 static void LoadDynamicFactories();
412 static void LoadLibrariesInPath(const std::string&);
413
414 // list of registered factories
415 static vtkObjectFactoryCollection* RegisteredFactories;
416
417 // member variables for a factory set by the base class
418 // at load or register time
419 void* LibraryHandle;
420 char* LibraryVTKVersion;
421 char* LibraryPath;
422
423 static std::string Preferences;
424
425 vtkObjectFactory(const vtkObjectFactory&) = delete;
426 void operator=(const vtkObjectFactory&) = delete;
427};
428
429// Implementation detail for Schwarz counter idiom.
441
442// Macro to create an object creation function.
443// The name of the function will by vtkObjectFactoryCreateclassname
444// where classname is the name of the class being created
445#define VTK_CREATE_CREATE_FUNCTION(classname) \
446 static vtkObject* vtkObjectFactoryCreate##classname() \
447 { \
448 return classname::New(); \
449 }
450
451VTK_ABI_NAMESPACE_END
452#endif
453
454#define VTK_FACTORY_INTERFACE_EXPORT VTKCOMMONCORE_EXPORT
455
456// Macro to create the interface "C" functions used in
457// a dll or shared library that contains a VTK object factory.
458// Put this function in the .cxx file of your object factory,
459// and pass in the name of the factory sub-class that you want
460// the dll to create.
461#define VTK_FACTORY_INTERFACE_IMPLEMENT(factoryName) \
462 extern "C" VTK_FACTORY_INTERFACE_EXPORT const char* vtkGetFactoryVersion() \
463 { \
464 return VTK_SOURCE_VERSION; \
465 } \
466 extern "C" VTK_FACTORY_INTERFACE_EXPORT vtkObjectFactory* vtkLoad() \
467 { \
468 return factoryName ::New(); \
469 }
470
471// Macro to implement the body of the object factory form of the New() method.
472#define VTK_OBJECT_FACTORY_NEW_BODY(thisClass) \
473 vtkObject* ret = vtkObjectFactory::CreateInstance(#thisClass, false); \
474 if (ret) \
475 { \
476 return static_cast<thisClass*>(ret); \
477 } \
478 auto result = new thisClass; \
479 result->InitializeObjectBase(); \
480 return result
481
482// Macro to implement the body of the abstract object factory form of the New()
483// method, i.e. an abstract base class that can only be instantiated if the
484// object factory overrides it.
485#define VTK_ABSTRACT_OBJECT_FACTORY_NEW_BODY(thisClass) \
486 vtkObject* ret = vtkObjectFactory::CreateInstance(#thisClass, true); \
487 if (ret) \
488 { \
489 return static_cast<thisClass*>(ret); \
490 } \
491 vtkGenericWarningMacro("Error: no override found for '" #thisClass "'."); \
492 return nullptr
493
494// Macro to implement the body of the standard form of the New() method.
495#if defined(VTK_ALL_NEW_OBJECT_FACTORY)
496#define VTK_STANDARD_NEW_BODY(thisClass) VTK_OBJECT_FACTORY_NEW_BODY(thisClass)
497#else
498#define VTK_STANDARD_NEW_BODY(thisClass) \
499 auto result = new thisClass; \
500 result->InitializeObjectBase(); \
501 return result
502#endif
503
504// Macro to implement the standard form of the New() method.
505#define vtkStandardNewMacro(thisClass) \
506 thisClass* thisClass::New() \
507 { \
508 VTK_STANDARD_NEW_BODY(thisClass); \
509 }
510
511// Macro to implement the ExtendedNew() to create an object in a memkind extended memory space. If
512// VTK is not compiled with VTK_USE_MEMKIND this is equivalent to New()
513#define vtkStandardExtendedNewMacro(thisClass) \
514 thisClass* thisClass::ExtendedNew() \
515 { \
516 auto mkhold = vtkMemkindRAII(true); \
517 (void)mkhold; \
518 return thisClass::New(); \
519 }
520
521// Macro to implement the object factory form of the New() method.
522#define vtkObjectFactoryNewMacro(thisClass) \
523 thisClass* thisClass::New() \
524 { \
525 VTK_OBJECT_FACTORY_NEW_BODY(thisClass); \
526 }
527
528// Macro to implement the abstract object factory form of the New() method.
529// That is an abstract base class that can only be instantiated if the
530// object factory overrides it.
531#define vtkAbstractObjectFactoryNewMacro(thisClass) \
532 thisClass* thisClass::New() \
533 { \
534 VTK_ABSTRACT_OBJECT_FACTORY_NEW_BODY(thisClass); \
535 }
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