VTK  9.2.6
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 =========================================================================*/
41 #ifndef vtkObjectFactory_h
42 #define vtkObjectFactory_h
43 
44 #include "vtkCommonCoreModule.h" // For export macro
45 #include "vtkDebugLeaksManager.h" // Must be included before singletons
46 #include "vtkFeatures.h" // For VTK_ALL_NEW_OBJECT_FACTORY
47 #include "vtkObject.h"
48 
49 #include <string> // for std::string
50 
53 class vtkCollection;
54 
55 class VTKCOMMONCORE_EXPORT vtkObjectFactory : public vtkObject
56 {
57 public:
58  // Class Methods used to interface with the registered factories
59 
70  static vtkObject* CreateInstance(const char* vtkclassname, bool isAbstract = false);
71 
78  static void CreateAllInstance(const char* vtkclassname, vtkCollection* retList);
83  static void ReHash();
87  static void RegisterFactory(vtkObjectFactory*);
91  static void UnRegisterFactory(vtkObjectFactory*);
95  static void UnRegisterAllFactories();
96 
101  static vtkObjectFactoryCollection* GetRegisteredFactories();
102 
107  static int HasOverrideAny(const char* className);
108 
113  static void GetOverrideInformation(const char* name, vtkOverrideInformationCollection*);
114 
119  static void SetAllEnableFlags(vtkTypeBool flag, const char* className);
124  static void SetAllEnableFlags(vtkTypeBool flag, const char* className, const char* subclassName);
125 
126  // Instance methods to be used on individual instances of vtkObjectFactory
127 
128  // Methods from vtkObject
129  vtkTypeMacro(vtkObjectFactory, vtkObject);
133  void PrintSelf(ostream& os, vtkIndent indent) override;
134 
142  virtual const char* GetVTKSourceVersion() = 0;
143 
147  virtual const char* GetDescription() = 0;
148 
152  virtual int GetNumberOfOverrides();
153 
157  virtual const char* GetClassOverrideName(int index);
158 
163  virtual const char* GetClassOverrideWithName(int index);
164 
168  virtual vtkTypeBool GetEnableFlag(int index);
169 
174  virtual const char* GetOverrideDescription(int index);
175 
177 
181  virtual void SetEnableFlag(vtkTypeBool flag, const char* className, const char* subclassName);
182  virtual vtkTypeBool GetEnableFlag(const char* className, const char* subclassName);
184 
188  virtual int HasOverride(const char* className);
192  virtual int HasOverride(const char* className, const char* subclassName);
193 
199  virtual void Disable(const char* className);
200 
202 
205  vtkGetFilePathMacro(LibraryPath);
207 
208  typedef vtkObject* (*CreateFunction)();
209 
210 protected:
214  void RegisterOverride(const char* classOverride, const char* overrideClassName,
215  const char* description, int enableFlag, CreateFunction createFunction);
216 
222  virtual vtkObject* CreateObject(const char* vtkclassname);
223 
225  ~vtkObjectFactory() override;
226 
228  {
229  char* Description;
232  CreateFunction CreateCallback;
233  };
234 
239 
240 private:
241  void GrowOverrideArray();
242 
247  static void Init();
251  static void RegisterDefaults();
255  static void LoadDynamicFactories();
259  static void LoadLibrariesInPath(const std::string&);
260 
261  // list of registered factories
262  static vtkObjectFactoryCollection* RegisteredFactories;
263 
264  // member variables for a factory set by the base class
265  // at load or register time
266  void* LibraryHandle;
267  char* LibraryVTKVersion;
268  char* LibraryPath;
269 
270 private:
271  vtkObjectFactory(const vtkObjectFactory&) = delete;
272  void operator=(const vtkObjectFactory&) = delete;
273 };
274 
275 // Implementation detail for Schwarz counter idiom.
276 class VTKCOMMONCORE_EXPORT vtkObjectFactoryRegistryCleanup
277 {
278 public:
281 
282 private:
285 };
287 
288 // Macro to create an object creation function.
289 // The name of the function will by vtkObjectFactoryCreateclassname
290 // where classname is the name of the class being created
291 #define VTK_CREATE_CREATE_FUNCTION(classname) \
292  static vtkObject* vtkObjectFactoryCreate##classname() { return classname::New(); }
293 
294 #endif
295 
296 #define VTK_FACTORY_INTERFACE_EXPORT VTKCOMMONCORE_EXPORT
297 
298 // Macro to create the interface "C" functions used in
299 // a dll or shared library that contains a VTK object factory.
300 // Put this function in the .cxx file of your object factory,
301 // and pass in the name of the factory sub-class that you want
302 // the dll to create.
303 #define VTK_FACTORY_INTERFACE_IMPLEMENT(factoryName) \
304  extern "C" VTK_FACTORY_INTERFACE_EXPORT const char* vtkGetFactoryVersion() \
305  { \
306  return VTK_SOURCE_VERSION; \
307  } \
308  extern "C" VTK_FACTORY_INTERFACE_EXPORT vtkObjectFactory* vtkLoad() \
309  { \
310  return factoryName ::New(); \
311  }
312 
313 // Macro to implement the body of the object factory form of the New() method.
314 #define VTK_OBJECT_FACTORY_NEW_BODY(thisClass) \
315  vtkObject* ret = vtkObjectFactory::CreateInstance(#thisClass, false); \
316  if (ret) \
317  { \
318  return static_cast<thisClass*>(ret); \
319  } \
320  auto result = new thisClass; \
321  result->InitializeObjectBase(); \
322  return result
323 
324 // Macro to implement the body of the abstract object factory form of the New()
325 // method, i.e. an abstract base class that can only be instantiated if the
326 // object factory overrides it.
327 #define VTK_ABSTRACT_OBJECT_FACTORY_NEW_BODY(thisClass) \
328  vtkObject* ret = vtkObjectFactory::CreateInstance(#thisClass, true); \
329  if (ret) \
330  { \
331  return static_cast<thisClass*>(ret); \
332  } \
333  vtkGenericWarningMacro("Error: no override found for '" #thisClass "'."); \
334  return nullptr
335 
336 // Macro to implement the body of the standard form of the New() method.
337 #if defined(VTK_ALL_NEW_OBJECT_FACTORY)
338 #define VTK_STANDARD_NEW_BODY(thisClass) VTK_OBJECT_FACTORY_NEW_BODY(thisClass)
339 #else
340 #define VTK_STANDARD_NEW_BODY(thisClass) \
341  auto result = new thisClass; \
342  result->InitializeObjectBase(); \
343  return result
344 #endif
345 
346 // Macro to implement the standard form of the New() method.
347 #define vtkStandardNewMacro(thisClass) \
348  thisClass* thisClass::New() { VTK_STANDARD_NEW_BODY(thisClass); }
349 
350 // Macro to implement the ExtendedNew() to create an object in a memkind extended memory space. If
351 // VTK is not compiled with VTK_USE_MEMKIND this is equivalent to New()
352 #define vtkStandardExtendedNewMacro(thisClass) \
353  thisClass* thisClass::ExtendedNew() \
354  { \
355  auto mkhold = vtkMemkindRAII(true); \
356  (void)mkhold; \
357  return thisClass::New(); \
358  }
359 
360 // Macro to implement the object factory form of the New() method.
361 #define vtkObjectFactoryNewMacro(thisClass) \
362  thisClass* thisClass::New() { VTK_OBJECT_FACTORY_NEW_BODY(thisClass); }
363 
364 // Macro to implement the abstract object factory form of the New() method.
365 // That is an abstract base class that can only be instantiated if the
366 // object factory overrides it.
367 #define vtkAbstractObjectFactoryNewMacro(thisClass) \
368  thisClass* thisClass::New() { VTK_ABSTRACT_OBJECT_FACTORY_NEW_BODY(thisClass); }
maintain a list of override information objects
abstract base class for most VTK objects
Definition: vtkObject.h:62
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
static vtkObjectFactoryRegistryCleanup vtkObjectFactoryRegistryCleanupInstance
int vtkTypeBool
Definition: vtkABI.h:69
a simple class to control print indentation
Definition: vtkIndent.h:39
OverrideInformation * OverrideArray
maintain a list of object factories
#define VTK_NEWINSTANCE
create and manipulate ordered lists of objects
Definition: vtkCollection.h:55
abstract base class for vtkObjectFactories