VTK
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 "vtkDebugLeaksManager.h" // Must be included before singletons
42 #include "vtkCommonCoreModule.h" // For export macro
43 #include "vtkObject.h"
44 
47 class vtkCollection;
48 
49 class VTKCOMMONCORE_EXPORT vtkObjectFactory : public vtkObject
50 {
51 public:
52  // Class Methods used to interface with the registered factories
53 
64  static vtkObject* CreateInstance(const char* vtkclassname,
65  bool isAbstract = false);
66 
73  static void CreateAllInstance(const char* vtkclassname,
74  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,
111 
116  static void SetAllEnableFlags(vtkTypeBool flag,
117  const char* className);
122  static void SetAllEnableFlags(vtkTypeBool flag,
123  const char* className,
124  const char* subclassName);
125 
126  // Instance methods to be used on individual instances of vtkObjectFactory
127 
128  // Methods from 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,
182  const char* className,
183  const char* subclassName);
184  virtual vtkTypeBool GetEnableFlag(const char* className,
185  const char* subclassName);
187 
191  virtual int HasOverride(const char* className);
195  virtual int HasOverride(const char* className, const char* subclassName);
196 
202  virtual void Disable(const char* className);
203 
205 
208  vtkGetStringMacro(LibraryPath);
210 
211  typedef vtkObject* (*CreateFunction)();
212 
213 protected:
214 
218  void RegisterOverride(const char* classOverride,
219  const char* overrideClassName,
220  const char* description,
221  int enableFlag,
222  CreateFunction createFunction);
223 
229  virtual vtkObject* CreateObject(const char* vtkclassname );
230 
232  ~vtkObjectFactory() override;
233 
235  {
236  char* Description;
239  CreateFunction CreateCallback;
240  };
241 
246 
247 private:
248  void GrowOverrideArray();
249 
254  static void Init();
258  static void RegisterDefaults();
262  static void LoadDynamicFactories();
266  static void LoadLibrariesInPath( const char*);
267 
268  // list of registered factories
269  static vtkObjectFactoryCollection* RegisteredFactories;
270 
271  // member variables for a factory set by the base class
272  // at load or register time
273  void* LibraryHandle;
274  char* LibraryVTKVersion;
275  char* LibraryCompilerUsed;
276  char* LibraryPath;
277 private:
278  vtkObjectFactory(const vtkObjectFactory&) = delete;
279  void operator=(const vtkObjectFactory&) = delete;
280 };
281 
282 // Implementation detail for Schwartz counter idiom.
283 class VTKCOMMONCORE_EXPORT vtkObjectFactoryRegistryCleanup
284 {
285 public:
288 
289 private:
292 };
294 
295 
296 // Macro to create an object creation function.
297 // The name of the function will by vtkObjectFactoryCreateclassname
298 // where classname is the name of the class being created
299 #define VTK_CREATE_CREATE_FUNCTION(classname) \
300 static vtkObject* vtkObjectFactoryCreate##classname() \
301 { return classname::New(); }
302 
303 #endif
304 
305 #define VTK_FACTORY_INTERFACE_EXPORT VTKCOMMONCORE_EXPORT
306 
307 // Macro to create the interface "C" functions used in
308 // a dll or shared library that contains a VTK object factory.
309 // Put this function in the .cxx file of your object factory,
310 // and pass in the name of the factory sub-class that you want
311 // the dll to create.
312 #define VTK_FACTORY_INTERFACE_IMPLEMENT(factoryName) \
313 extern "C" \
314 VTK_FACTORY_INTERFACE_EXPORT \
315 const char* vtkGetFactoryCompilerUsed() \
316 { \
317  return VTK_CXX_COMPILER; \
318 } \
319 extern "C" \
320 VTK_FACTORY_INTERFACE_EXPORT \
321 const char* vtkGetFactoryVersion() \
322 { \
323  return VTK_SOURCE_VERSION; \
324 } \
325 extern "C" \
326 VTK_FACTORY_INTERFACE_EXPORT \
327 vtkObjectFactory* vtkLoad() \
328 { \
329  return factoryName ::New(); \
330 }
331 
332 // Macro to implement the body of the object factory form of the New() method.
333 #define VTK_OBJECT_FACTORY_NEW_BODY(thisClass) \
334  vtkObject* ret = vtkObjectFactory::CreateInstance(#thisClass, false); \
335  if(ret) \
336  { \
337  return static_cast<thisClass*>(ret); \
338  } \
339  thisClass *result = new thisClass; \
340  result->InitializeObjectBase(); \
341  return result;
342 
343 // Macro to implement the body of the abstract object factory form of the New()
344 // method, i.e. an abstract base class that can only be instantiated if the
345 // object factory overrides it.
346 #define VTK_ABSTRACT_OBJECT_FACTORY_NEW_BODY(thisClass) \
347  vtkObject* ret = vtkObjectFactory::CreateInstance(#thisClass, true); \
348  if(ret) \
349  { \
350  return static_cast<thisClass*>(ret); \
351  } \
352  vtkGenericWarningMacro("Error: no override found for '" #thisClass "'."); \
353  return nullptr;
354 
355 // Macro to implement the body of the standard form of the New() method.
356 #if defined(VTK_ALL_NEW_OBJECT_FACTORY)
357 # define VTK_STANDARD_NEW_BODY(thisClass) \
358  VTK_OBJECT_FACTORY_NEW_BODY(thisClass)
359 #else
360 # define VTK_STANDARD_NEW_BODY(thisClass) \
361  thisClass *result = new thisClass; \
362  result->InitializeObjectBase(); \
363  return result;
364 #endif
365 
366 // Macro to implement the standard form of the New() method.
367 #define vtkStandardNewMacro(thisClass) \
368  thisClass* thisClass::New() \
369  { \
370  VTK_STANDARD_NEW_BODY(thisClass) \
371  }
372 
373 // Macro to implement the object factory form of the New() method.
374 #define vtkObjectFactoryNewMacro(thisClass) \
375  thisClass* thisClass::New() \
376  { \
377  VTK_OBJECT_FACTORY_NEW_BODY(thisClass) \
378  }
379 
380 // Macro to implement the abstract object factory form of the New() method.
381 // That is an abstract base class that can only be instantiated if the
382 // object factory overrides it.
383 #define vtkAbstractObjectFactoryNewMacro(thisClass) \
384  thisClass* thisClass::New() \
385  { \
386  VTK_ABSTRACT_OBJECT_FACTORY_NEW_BODY(thisClass) \
387  }
vtkOverrideInformationCollection
maintain a list of override information objects
Definition: vtkOverrideInformationCollection.h:33
vtkObjectFactory::SizeOverrideArray
int SizeOverrideArray
Definition: vtkObjectFactory.h:244
vtkObjectFactoryRegistryCleanupInstance
static vtkObjectFactoryRegistryCleanup vtkObjectFactoryRegistryCleanupInstance
Definition: vtkObjectFactory.h:293
vtkObjectFactory::OverrideInformation::CreateCallback
CreateFunction CreateCallback
Definition: vtkObjectFactory.h:239
vtkObject
abstract base class for most VTK objects
Definition: vtkObject.h:53
vtkObjectFactoryRegistryCleanup
Definition: vtkObjectFactory.h:283
vtkObjectFactory::OverrideClassNames
char ** OverrideClassNames
Definition: vtkObjectFactory.h:243
vtkObjectFactory::OverrideArrayLength
int OverrideArrayLength
Definition: vtkObjectFactory.h:245
vtkObjectFactory::OverrideInformation::Description
char * Description
Definition: vtkObjectFactory.h:236
vtkDebugLeaksManager.h
vtkCollection
create and manipulate ordered lists of objects
Definition: vtkCollection.h:48
vtkObjectFactory
abstract base class for vtkObjectFactories
Definition: vtkObjectFactory.h:49
vtkObjectFactory::OverrideInformation::OverrideWithName
char * OverrideWithName
Definition: vtkObjectFactory.h:237
vtkIndent
a simple class to control print indentation
Definition: vtkIndent.h:33
vtkObjectFactory::OverrideInformation::EnabledFlag
vtkTypeBool EnabledFlag
Definition: vtkObjectFactory.h:238
vtkObject::PrintSelf
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
vtkObjectFactoryCollection
maintain a list of object factories
Definition: vtkObjectFactoryCollection.h:34
vtkX3D::name
Definition: vtkX3D.h:219
vtkObject.h
vtkX3D::description
Definition: vtkX3D.h:322
vtkObjectFactory::OverrideInformation
Definition: vtkObjectFactory.h:234
vtkObjectFactory::OverrideArray
OverrideInformation * OverrideArray
Definition: vtkObjectFactory.h:242
VTK_NEWINSTANCE
#define VTK_NEWINSTANCE
Definition: vtkWrappingHints.h:39
vtkX3D::index
Definition: vtkX3D.h:246
vtkTypeBool
int vtkTypeBool
Definition: vtkABI.h:69