VTK  9.0.1
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 =========================================================================*/
38 #ifndef vtkObjectFactory_h
39 #define vtkObjectFactory_h
40 
41 #include "vtkCommonCoreModule.h" // For export macro
42 #include "vtkDebugLeaksManager.h" // Must be included before singletons
43 #include "vtkObject.h"
44 
45 #include <string> // for std::string
46 
49 class vtkCollection;
50 
51 class VTKCOMMONCORE_EXPORT vtkObjectFactory : public vtkObject
52 {
53 public:
54  // Class Methods used to interface with the registered factories
55 
66  static vtkObject* CreateInstance(const char* vtkclassname, bool isAbstract = false);
67 
74  static void CreateAllInstance(const char* vtkclassname, vtkCollection* retList);
79  static void ReHash();
83  static void RegisterFactory(vtkObjectFactory*);
87  static void UnRegisterFactory(vtkObjectFactory*);
91  static void UnRegisterAllFactories();
92 
97  static vtkObjectFactoryCollection* GetRegisteredFactories();
98 
103  static int HasOverrideAny(const char* className);
104 
109  static void GetOverrideInformation(const char* name, vtkOverrideInformationCollection*);
110 
115  static void SetAllEnableFlags(vtkTypeBool flag, const char* className);
120  static void SetAllEnableFlags(vtkTypeBool flag, const char* className, const char* subclassName);
121 
122  // Instance methods to be used on individual instances of vtkObjectFactory
123 
124  // Methods from vtkObject
125  vtkTypeMacro(vtkObjectFactory, vtkObject);
129  void PrintSelf(ostream& os, vtkIndent indent) override;
130 
138  virtual const char* GetVTKSourceVersion() = 0;
139 
143  virtual const char* GetDescription() = 0;
144 
148  virtual int GetNumberOfOverrides();
149 
153  virtual const char* GetClassOverrideName(int index);
154 
159  virtual const char* GetClassOverrideWithName(int index);
160 
164  virtual vtkTypeBool GetEnableFlag(int index);
165 
170  virtual const char* GetOverrideDescription(int index);
171 
173 
177  virtual void SetEnableFlag(vtkTypeBool flag, const char* className, const char* subclassName);
178  virtual vtkTypeBool GetEnableFlag(const char* className, const char* subclassName);
180 
184  virtual int HasOverride(const char* className);
188  virtual int HasOverride(const char* className, const char* subclassName);
189 
195  virtual void Disable(const char* className);
196 
198 
201  vtkGetStringMacro(LibraryPath);
203 
204  typedef vtkObject* (*CreateFunction)();
205 
206 protected:
210  void RegisterOverride(const char* classOverride, const char* overrideClassName,
211  const char* description, int enableFlag, CreateFunction createFunction);
212 
218  virtual vtkObject* CreateObject(const char* vtkclassname);
219 
221  ~vtkObjectFactory() override;
222 
224  {
225  char* Description;
228  CreateFunction CreateCallback;
229  };
230 
235 
236 private:
237  void GrowOverrideArray();
238 
243  static void Init();
247  static void RegisterDefaults();
251  static void LoadDynamicFactories();
255  static void LoadLibrariesInPath(const std::string&);
256 
257  // list of registered factories
258  static vtkObjectFactoryCollection* RegisteredFactories;
259 
260  // member variables for a factory set by the base class
261  // at load or register time
262  void* LibraryHandle;
263  char* LibraryVTKVersion;
264  char* LibraryCompilerUsed;
265  char* LibraryPath;
266 
267 private:
268  vtkObjectFactory(const vtkObjectFactory&) = delete;
269  void operator=(const vtkObjectFactory&) = delete;
270 };
271 
272 // Implementation detail for Schwarz counter idiom.
273 class VTKCOMMONCORE_EXPORT vtkObjectFactoryRegistryCleanup
274 {
275 public:
278 
279 private:
282 };
284 
285 // Macro to create an object creation function.
286 // The name of the function will by vtkObjectFactoryCreateclassname
287 // where classname is the name of the class being created
288 #define VTK_CREATE_CREATE_FUNCTION(classname) \
289  static vtkObject* vtkObjectFactoryCreate##classname() { return classname::New(); }
290 
291 #endif
292 
293 #define VTK_FACTORY_INTERFACE_EXPORT VTKCOMMONCORE_EXPORT
294 
295 // Macro to create the interface "C" functions used in
296 // a dll or shared library that contains a VTK object factory.
297 // Put this function in the .cxx file of your object factory,
298 // and pass in the name of the factory sub-class that you want
299 // the dll to create.
300 #define VTK_FACTORY_INTERFACE_IMPLEMENT(factoryName) \
301  extern "C" VTK_FACTORY_INTERFACE_EXPORT const char* vtkGetFactoryCompilerUsed() \
302  { \
303  return VTK_CXX_COMPILER; \
304  } \
305  extern "C" VTK_FACTORY_INTERFACE_EXPORT const char* vtkGetFactoryVersion() \
306  { \
307  return VTK_SOURCE_VERSION; \
308  } \
309  extern "C" VTK_FACTORY_INTERFACE_EXPORT vtkObjectFactory* vtkLoad() \
310  { \
311  return factoryName ::New(); \
312  }
313 
314 // Macro to implement the body of the object factory form of the New() method.
315 #define VTK_OBJECT_FACTORY_NEW_BODY(thisClass) \
316  vtkObject* ret = vtkObjectFactory::CreateInstance(#thisClass, false); \
317  if (ret) \
318  { \
319  return static_cast<thisClass*>(ret); \
320  } \
321  auto result = new thisClass; \
322  result->InitializeObjectBase(); \
323  return result
324 
325 // Macro to implement the body of the abstract object factory form of the New()
326 // method, i.e. an abstract base class that can only be instantiated if the
327 // object factory overrides it.
328 #define VTK_ABSTRACT_OBJECT_FACTORY_NEW_BODY(thisClass) \
329  vtkObject* ret = vtkObjectFactory::CreateInstance(#thisClass, true); \
330  if (ret) \
331  { \
332  return static_cast<thisClass*>(ret); \
333  } \
334  vtkGenericWarningMacro("Error: no override found for '" #thisClass "'."); \
335  return nullptr
336 
337 // Macro to implement the body of the standard form of the New() method.
338 #if defined(VTK_ALL_NEW_OBJECT_FACTORY)
339 #define VTK_STANDARD_NEW_BODY(thisClass) VTK_OBJECT_FACTORY_NEW_BODY(thisClass)
340 #else
341 #define VTK_STANDARD_NEW_BODY(thisClass) \
342  auto result = new thisClass; \
343  result->InitializeObjectBase(); \
344  return result
345 #endif
346 
347 // Macro to implement the standard form of the New() method.
348 #define vtkStandardNewMacro(thisClass) \
349  thisClass* thisClass::New() { VTK_STANDARD_NEW_BODY(thisClass); }
350 
351 // Macro to implement the object factory form of the New() method.
352 #define vtkObjectFactoryNewMacro(thisClass) \
353  thisClass* thisClass::New() { VTK_OBJECT_FACTORY_NEW_BODY(thisClass); }
354 
355 // Macro to implement the abstract object factory form of the New() method.
356 // That is an abstract base class that can only be instantiated if the
357 // object factory overrides it.
358 #define vtkAbstractObjectFactoryNewMacro(thisClass) \
359  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:33
OverrideInformation * OverrideArray
maintain a list of object factories
#define VTK_NEWINSTANCE
create and manipulate ordered lists of objects
Definition: vtkCollection.h:52
abstract base class for vtkObjectFactories