VTK  9.5.2
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
28
29#ifndef vtkObjectFactory_h
30#define vtkObjectFactory_h
31
32#include "vtkCommonCoreModule.h" // For export macro
33#include "vtkDebugLeaksManager.h" // Must be included before singletons
34#include "vtkFeatures.h" // For VTK_ALL_NEW_OBJECT_FACTORY
35#include "vtkObject.h"
36
37#include <string> // for std::string
38
39VTK_ABI_NAMESPACE_BEGIN
42class vtkCollection;
43
44class VTKCOMMONCORE_EXPORT vtkObjectFactory : public vtkObject
45{
46public:
47 // Class Methods used to interface with the registered factories
48
59 static vtkObject* CreateInstance(const char* vtkclassname, bool isAbstract = false);
60
67 static void CreateAllInstance(const char* vtkclassname, vtkCollection* retList);
72 static void ReHash();
85
91
96 static vtkTypeBool HasOverrideAny(const char* className);
97
103
108 static void SetAllEnableFlags(vtkTypeBool flag, const char* className);
113 static void SetAllEnableFlags(vtkTypeBool flag, const char* className, const char* subclassName);
114
115 // Instance methods to be used on individual instances of vtkObjectFactory
116
117 // Methods from vtkObject
122 void PrintSelf(ostream& os, vtkIndent indent) override;
123
131 virtual const char* GetVTKSourceVersion() VTK_FUTURE_CONST = 0;
132
136 virtual const char* GetDescription() VTK_FUTURE_CONST = 0;
137
141 virtual int GetNumberOfOverrides() VTK_FUTURE_CONST;
142
146 virtual const char* GetClassOverrideName(int index) VTK_FUTURE_CONST;
147
152 virtual const char* GetClassOverrideWithName(int index) VTK_FUTURE_CONST;
153
157 virtual vtkTypeBool GetEnableFlag(int index) VTK_FUTURE_CONST;
158
163 virtual const char* GetOverrideDescription(int index) VTK_FUTURE_CONST;
164
166
170 virtual void SetEnableFlag(vtkTypeBool flag, const char* className, const char* subclassName);
172 const char* className, const char* subclassName) VTK_FUTURE_CONST;
174
178 virtual vtkTypeBool HasOverride(const char* className) VTK_FUTURE_CONST;
182 virtual vtkTypeBool HasOverride(const char* className, const char* subclassName) VTK_FUTURE_CONST;
183
189 virtual void Disable(const char* className);
190
192
197
199
200protected:
204 void RegisterOverride(const char* classOverride, const char* overrideClassName,
205 const char* description, int enableFlag, CreateFunction createFunction);
206
212 virtual vtkObject* CreateObject(const char* vtkclassname);
213
215 ~vtkObjectFactory() override;
216
224
229
230private:
231 void GrowOverrideArray();
232
237 static void Init();
241 static void RegisterDefaults();
245 static void LoadDynamicFactories();
249 static void LoadLibrariesInPath(const std::string&);
250
251 // list of registered factories
252 static vtkObjectFactoryCollection* RegisteredFactories;
253
254 // member variables for a factory set by the base class
255 // at load or register time
256 void* LibraryHandle;
257 char* LibraryVTKVersion;
258 char* LibraryPath;
259
260 vtkObjectFactory(const vtkObjectFactory&) = delete;
261 void operator=(const vtkObjectFactory&) = delete;
262};
263
264// Implementation detail for Schwarz counter idiom.
276
277// Macro to create an object creation function.
278// The name of the function will by vtkObjectFactoryCreateclassname
279// where classname is the name of the class being created
280#define VTK_CREATE_CREATE_FUNCTION(classname) \
281 static vtkObject* vtkObjectFactoryCreate##classname() \
282 { \
283 return classname::New(); \
284 }
285
286VTK_ABI_NAMESPACE_END
287#endif
288
289#define VTK_FACTORY_INTERFACE_EXPORT VTKCOMMONCORE_EXPORT
290
291// Macro to create the interface "C" functions used in
292// a dll or shared library that contains a VTK object factory.
293// Put this function in the .cxx file of your object factory,
294// and pass in the name of the factory sub-class that you want
295// the dll to create.
296#define VTK_FACTORY_INTERFACE_IMPLEMENT(factoryName) \
297 extern "C" VTK_FACTORY_INTERFACE_EXPORT const char* vtkGetFactoryVersion() \
298 { \
299 return VTK_SOURCE_VERSION; \
300 } \
301 extern "C" VTK_FACTORY_INTERFACE_EXPORT vtkObjectFactory* vtkLoad() \
302 { \
303 return factoryName ::New(); \
304 }
305
306// Macro to implement the body of the object factory form of the New() method.
307#define VTK_OBJECT_FACTORY_NEW_BODY(thisClass) \
308 vtkObject* ret = vtkObjectFactory::CreateInstance(#thisClass, false); \
309 if (ret) \
310 { \
311 return static_cast<thisClass*>(ret); \
312 } \
313 auto result = new thisClass; \
314 result->InitializeObjectBase(); \
315 return result
316
317// Macro to implement the body of the abstract object factory form of the New()
318// method, i.e. an abstract base class that can only be instantiated if the
319// object factory overrides it.
320#define VTK_ABSTRACT_OBJECT_FACTORY_NEW_BODY(thisClass) \
321 vtkObject* ret = vtkObjectFactory::CreateInstance(#thisClass, true); \
322 if (ret) \
323 { \
324 return static_cast<thisClass*>(ret); \
325 } \
326 vtkGenericWarningMacro("Error: no override found for '" #thisClass "'."); \
327 return nullptr
328
329// Macro to implement the body of the standard form of the New() method.
330#if defined(VTK_ALL_NEW_OBJECT_FACTORY)
331#define VTK_STANDARD_NEW_BODY(thisClass) VTK_OBJECT_FACTORY_NEW_BODY(thisClass)
332#else
333#define VTK_STANDARD_NEW_BODY(thisClass) \
334 auto result = new thisClass; \
335 result->InitializeObjectBase(); \
336 return result
337#endif
338
339// Macro to implement the standard form of the New() method.
340#define vtkStandardNewMacro(thisClass) \
341 thisClass* thisClass::New() \
342 { \
343 VTK_STANDARD_NEW_BODY(thisClass); \
344 }
345
346// Macro to implement the ExtendedNew() to create an object in a memkind extended memory space. If
347// VTK is not compiled with VTK_USE_MEMKIND this is equivalent to New()
348#define vtkStandardExtendedNewMacro(thisClass) \
349 thisClass* thisClass::ExtendedNew() \
350 { \
351 auto mkhold = vtkMemkindRAII(true); \
352 (void)mkhold; \
353 return thisClass::New(); \
354 }
355
356// Macro to implement the object factory form of the New() method.
357#define vtkObjectFactoryNewMacro(thisClass) \
358 thisClass* thisClass::New() \
359 { \
360 VTK_OBJECT_FACTORY_NEW_BODY(thisClass); \
361 }
362
363// Macro to implement the abstract object factory form of the New() method.
364// That is an abstract base class that can only be instantiated if the
365// object factory overrides it.
366#define vtkAbstractObjectFactoryNewMacro(thisClass) \
367 thisClass* thisClass::New() \
368 { \
369 VTK_ABSTRACT_OBJECT_FACTORY_NEW_BODY(thisClass); \
370 }
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.
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
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 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.
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.
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.
maintain a list of override information objects
int vtkTypeBool
Definition vtkABI.h:64
static vtkObjectFactoryRegistryCleanup vtkObjectFactoryRegistryCleanupInstance
#define VTK_NEWINSTANCE