VTK  9.1.0
vtkObjectFactory.h
Go to the documentation of this file.
1/*=========================================================================
2
3 Program: Visualization Toolkit
4 Module: vtkObjectFactory.h
5
6 Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7 All rights reserved.
8 See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9
10 This software is distributed WITHOUT ANY WARRANTY; without even
11 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12 PURPOSE. See the above copyright notice for more information.
13
14=========================================================================*/
147#ifndef vtkObjectFactory_h
148#define vtkObjectFactory_h
149
150#include "vtkCommonCoreModule.h" // For export macro
151#include "vtkDebugLeaksManager.h" // Must be included before singletons
152#include "vtkFeatures.h" // For VTK_ALL_NEW_OBJECT_FACTORY
153#include "vtkObject.h"
154
155#include <string> // for std::string
156
159class vtkCollection;
160
161class VTKCOMMONCORE_EXPORT vtkObjectFactory : public vtkObject
162{
163public:
164 // Class Methods used to interface with the registered factories
165
176 static vtkObject* CreateInstance(const char* vtkclassname, bool isAbstract = false);
177
184 static void CreateAllInstance(const char* vtkclassname, vtkCollection* retList);
189 static void ReHash();
202
208
213 static int HasOverrideAny(const char* className);
214
220
225 static void SetAllEnableFlags(vtkTypeBool flag, const char* className);
230 static void SetAllEnableFlags(vtkTypeBool flag, const char* className, const char* subclassName);
231
232 // Instance methods to be used on individual instances of vtkObjectFactory
233
234 // Methods from vtkObject
239 void PrintSelf(ostream& os, vtkIndent indent) override;
240
248 virtual const char* GetVTKSourceVersion() = 0;
249
253 virtual const char* GetDescription() = 0;
254
258 virtual int GetNumberOfOverrides();
259
263 virtual const char* GetClassOverrideName(int index);
264
269 virtual const char* GetClassOverrideWithName(int index);
270
275
280 virtual const char* GetOverrideDescription(int index);
281
283
287 virtual void SetEnableFlag(vtkTypeBool flag, const char* className, const char* subclassName);
288 virtual vtkTypeBool GetEnableFlag(const char* className, const char* subclassName);
290
294 virtual int HasOverride(const char* className);
298 virtual int HasOverride(const char* className, const char* subclassName);
299
305 virtual void Disable(const char* className);
306
308
313
314 typedef vtkObject* (*CreateFunction)();
315
316protected:
320 void RegisterOverride(const char* classOverride, const char* overrideClassName,
321 const char* description, int enableFlag, CreateFunction createFunction);
322
328 virtual vtkObject* CreateObject(const char* vtkclassname);
329
332
334 {
338 CreateFunction CreateCallback;
339 };
340
345
346private:
347 void GrowOverrideArray();
348
353 static void Init();
357 static void RegisterDefaults();
361 static void LoadDynamicFactories();
365 static void LoadLibrariesInPath(const std::string&);
366
367 // list of registered factories
368 static vtkObjectFactoryCollection* RegisteredFactories;
369
370 // member variables for a factory set by the base class
371 // at load or register time
372 void* LibraryHandle;
373 char* LibraryVTKVersion;
374 char* LibraryPath;
375
376private:
377 vtkObjectFactory(const vtkObjectFactory&) = delete;
378 void operator=(const vtkObjectFactory&) = delete;
379};
380
381// Implementation detail for Schwarz counter idiom.
382class VTKCOMMONCORE_EXPORT vtkObjectFactoryRegistryCleanup
383{
384public:
387
388private:
391};
393
394// Macro to create an object creation function.
395// The name of the function will by vtkObjectFactoryCreateclassname
396// where classname is the name of the class being created
397#define VTK_CREATE_CREATE_FUNCTION(classname) \
398 static vtkObject* vtkObjectFactoryCreate##classname() { return classname::New(); }
399
400#endif
401
402#define VTK_FACTORY_INTERFACE_EXPORT VTKCOMMONCORE_EXPORT
403
404// Macro to create the interface "C" functions used in
405// a dll or shared library that contains a VTK object factory.
406// Put this function in the .cxx file of your object factory,
407// and pass in the name of the factory sub-class that you want
408// the dll to create.
409#define VTK_FACTORY_INTERFACE_IMPLEMENT(factoryName) \
410 extern "C" VTK_FACTORY_INTERFACE_EXPORT const char* vtkGetFactoryVersion() \
411 { \
412 return VTK_SOURCE_VERSION; \
413 } \
414 extern "C" VTK_FACTORY_INTERFACE_EXPORT vtkObjectFactory* vtkLoad() \
415 { \
416 return factoryName ::New(); \
417 }
418
419// Macro to implement the body of the object factory form of the New() method.
420#define VTK_OBJECT_FACTORY_NEW_BODY(thisClass) \
421 vtkObject* ret = vtkObjectFactory::CreateInstance(#thisClass, false); \
422 if (ret) \
423 { \
424 return static_cast<thisClass*>(ret); \
425 } \
426 auto result = new thisClass; \
427 result->InitializeObjectBase(); \
428 return result
429
430// Macro to implement the body of the abstract object factory form of the New()
431// method, i.e. an abstract base class that can only be instantiated if the
432// object factory overrides it.
433#define VTK_ABSTRACT_OBJECT_FACTORY_NEW_BODY(thisClass) \
434 vtkObject* ret = vtkObjectFactory::CreateInstance(#thisClass, true); \
435 if (ret) \
436 { \
437 return static_cast<thisClass*>(ret); \
438 } \
439 vtkGenericWarningMacro("Error: no override found for '" #thisClass "'."); \
440 return nullptr
441
442// Macro to implement the body of the standard form of the New() method.
443#if defined(VTK_ALL_NEW_OBJECT_FACTORY)
444#define VTK_STANDARD_NEW_BODY(thisClass) VTK_OBJECT_FACTORY_NEW_BODY(thisClass)
445#else
446#define VTK_STANDARD_NEW_BODY(thisClass) \
447 auto result = new thisClass; \
448 result->InitializeObjectBase(); \
449 return result
450#endif
451
452// Macro to implement the standard form of the New() method.
453#define vtkStandardNewMacro(thisClass) \
454 thisClass* thisClass::New() { VTK_STANDARD_NEW_BODY(thisClass); }
455
456// Macro to implement the ExtendedNew() to create an object in a memkind extended memory space. If
457// VTK is not compiled with VTK_USE_MEMKIND this is equivalent to New()
458#define vtkStandardExtendedNewMacro(thisClass) \
459 thisClass* thisClass::ExtendedNew() \
460 { \
461 auto mkhold = vtkMemkindRAII(true); \
462 (void)mkhold; \
463 return thisClass::New(); \
464 }
465
466// Macro to implement the object factory form of the New() method.
467#define vtkObjectFactoryNewMacro(thisClass) \
468 thisClass* thisClass::New() { VTK_OBJECT_FACTORY_NEW_BODY(thisClass); }
469
470// Macro to implement the abstract object factory form of the New() method.
471// That is an abstract base class that can only be instantiated if the
472// object factory overrides it.
473#define vtkAbstractObjectFactoryNewMacro(thisClass) \
474 thisClass* thisClass::New() { VTK_ABSTRACT_OBJECT_FACTORY_NEW_BODY(thisClass); }
create and manipulate ordered lists of objects
Definition: vtkCollection.h:53
a simple class to control print indentation
Definition: vtkIndent.h:113
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 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.
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 vtkTypeBool GetEnableFlag(const char *className, const char *subclassName)
Set and Get the Enable flag for the specific override of className.
static int HasOverrideAny(const char *className)
return 1 if one of the registered factories overrides the given class name
virtual vtkObject * CreateObject(const char *vtkclassname)
This method is provided by sub-classes of vtkObjectFactory.
virtual int HasOverride(const char *className)
Return 1 if this factory overrides the given class name, 0 otherwise.
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.
virtual int HasOverride(const char *className, const char *subclassName)
Return 1 if this factory overrides the given class name, 0 otherwise.
abstract base class for most VTK objects
Definition: vtkObject.h:73
maintain a list of override information objects
@ description
Definition: vtkX3D.h:328
@ name
Definition: vtkX3D.h:225
@ index
Definition: vtkX3D.h:252
@ string
Definition: vtkX3D.h:496
int vtkTypeBool
Definition: vtkABI.h:69
static vtkObjectFactoryRegistryCleanup vtkObjectFactoryRegistryCleanupInstance
#define VTK_NEWINSTANCE