VTK  9.7.20260805
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
46
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#include "vtkSmartPointer.h" // For vtkSmartPointer
55
56#include <string> // for std::string
57
58VTK_ABI_NAMESPACE_BEGIN
61class 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 vtkTypeBool 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() VTK_FUTURE_CONST = 0;
152
156 virtual const char* GetDescription() VTK_FUTURE_CONST = 0;
157
161 virtual int GetNumberOfOverrides() VTK_FUTURE_CONST;
162
166 virtual const char* GetClassOverrideName(int index) VTK_FUTURE_CONST;
167
172 virtual const char* GetClassOverrideWithName(int index) VTK_FUTURE_CONST;
173
177 virtual vtkTypeBool GetEnableFlag(int index) VTK_FUTURE_CONST;
178
183 virtual const char* GetOverrideDescription(int index) VTK_FUTURE_CONST;
184
186
190 virtual void SetEnableFlag(vtkTypeBool flag, const char* className, const char* subclassName);
192 const char* className, const char* subclassName) VTK_FUTURE_CONST;
194
199
203 virtual vtkTypeBool HasOverride(const char* className) VTK_FUTURE_CONST;
207 virtual vtkTypeBool HasOverride(const char* className, const char* subclassName) VTK_FUTURE_CONST;
208
214 virtual void Disable(const char* className);
215
217
222
224
236 static bool InitializePreferencesFromCommandLineArgs(int& argc, char* argv[]);
237
243 static void SetPreferences(std::string preferences);
244 static std::string GetPreferences();
245
246protected:
256 void RegisterOverride(const char* classOverride, const char* overrideClassName,
257 const char* description, int enableFlag, CreateFunction createFunction,
258 vtkOverrideAttribute* attributes = nullptr);
259
265 virtual vtkObject* CreateObject(const char* vtkclassname);
266
268 ~vtkObjectFactory() override;
269
278
283
284private:
285 void GrowOverrideArray();
286
291 static void Init();
295 static void RegisterDefaults();
299 static void LoadDynamicFactories();
303 static void LoadLibrariesInPath(const std::string&);
304
305 // list of registered factories
306 static vtkObjectFactoryCollection* RegisteredFactories;
307
308 // member variables for a factory set by the base class
309 // at load or register time
310 void* LibraryHandle;
311 char* LibraryVTKVersion;
312 char* LibraryPath;
313
314 static std::string Preferences;
315
316 vtkObjectFactory(const vtkObjectFactory&) = delete;
317 void operator=(const vtkObjectFactory&) = delete;
318};
319
320// Implementation detail for Schwarz counter idiom.
332
333// Macro to create an object creation function.
334// The name of the function will by vtkObjectFactoryCreateclassname
335// where classname is the name of the class being created
336#define VTK_CREATE_CREATE_FUNCTION(classname) \
337 static vtkObject* vtkObjectFactoryCreate##classname() \
338 { \
339 return classname::New(); \
340 }
341
342VTK_ABI_NAMESPACE_END
343#endif
344
345#define VTK_FACTORY_INTERFACE_EXPORT VTKCOMMONCORE_EXPORT
346
347// Macro to create the interface "C" functions used in
348// a dll or shared library that contains a VTK object factory.
349// Put this function in the .cxx file of your object factory,
350// and pass in the name of the factory sub-class that you want
351// the dll to create.
352#define VTK_FACTORY_INTERFACE_IMPLEMENT(factoryName) \
353 extern "C" VTK_FACTORY_INTERFACE_EXPORT const char* vtkGetFactoryVersion() \
354 { \
355 return VTK_SOURCE_VERSION; \
356 } \
357 extern "C" VTK_FACTORY_INTERFACE_EXPORT vtkObjectFactory* vtkLoad() \
358 { \
359 return factoryName ::New(); \
360 }
361
362// Macro to implement the body of the object factory form of the New() method.
363#define VTK_OBJECT_FACTORY_NEW_BODY(thisClass) \
364 vtkObject* ret = vtkObjectFactory::CreateInstance(#thisClass, false); \
365 if (ret) \
366 { \
367 return static_cast<thisClass*>(ret); \
368 } \
369 auto result = new thisClass; \
370 result->InitializeObjectBase(); \
371 return result
372
373// Macro to implement the body of the abstract object factory form of the New()
374// method, i.e. an abstract base class that can only be instantiated if the
375// object factory overrides it.
376#define VTK_ABSTRACT_OBJECT_FACTORY_NEW_BODY(thisClass) \
377 vtkObject* ret = vtkObjectFactory::CreateInstance(#thisClass, true); \
378 if (ret) \
379 { \
380 return static_cast<thisClass*>(ret); \
381 } \
382 vtkGenericWarningMacro("Error: no override found for '" #thisClass "'."); \
383 return nullptr
384
385// Macro to implement the body of the standard form of the New() method.
386#if defined(VTK_ALL_NEW_OBJECT_FACTORY)
387#define VTK_STANDARD_NEW_BODY(thisClass) VTK_OBJECT_FACTORY_NEW_BODY(thisClass)
388#else
389#define VTK_STANDARD_NEW_BODY(thisClass) \
390 auto result = new thisClass; \
391 result->InitializeObjectBase(); \
392 return result
393#endif
394
395// Macro to implement the standard form of the New() method.
396#define vtkStandardNewMacro(thisClass) \
397 thisClass* thisClass::New() \
398 { \
399 VTK_STANDARD_NEW_BODY(thisClass); \
400 }
401
402// Macro to implement the ExtendedNew() to create an object in a memkind extended memory space. If
403// VTK is not compiled with VTK_USE_MEMKIND this is equivalent to New()
404#define vtkStandardExtendedNewMacro(thisClass) \
405 thisClass* thisClass::ExtendedNew() \
406 { \
407 auto mkhold = vtkMemkindRAII(true); \
408 (void)mkhold; \
409 return thisClass::New(); \
410 }
411
412// Macro to implement the object factory form of the New() method.
413#define vtkObjectFactoryNewMacro(thisClass) \
414 thisClass* thisClass::New() \
415 { \
416 VTK_OBJECT_FACTORY_NEW_BODY(thisClass); \
417 }
418
419// Macro to implement the abstract object factory form of the New() method.
420// That is an abstract base class that can only be instantiated if the
421// object factory overrides it.
422#define vtkAbstractObjectFactoryNewMacro(thisClass) \
423 thisClass* thisClass::New() \
424 { \
425 VTK_ABSTRACT_OBJECT_FACTORY_NEW_BODY(thisClass); \
426 }
create and manipulate ordered lists of objects
a simple class to control print indentation
Definition vtkIndent.h:29
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