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 =========================================================================*/
44 #ifndef vtkObjectFactory_h
45 #define vtkObjectFactory_h
46 
47 #include "vtkDebugLeaksManager.h" // Must be included before singletons
48 #include "vtkCommonCoreModule.h" // For export macro
49 #include "vtkObject.h"
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,
71  bool isAbstract = false);
72 
79  static void CreateAllInstance(const char* vtkclassname,
80  vtkCollection* retList);
85  static void ReHash();
89  static void RegisterFactory(vtkObjectFactory* );
93  static void UnRegisterFactory(vtkObjectFactory*);
97  static void UnRegisterAllFactories();
98 
103  static vtkObjectFactoryCollection* GetRegisteredFactories();
104 
109  static int HasOverrideAny(const char* className);
110 
115  static void GetOverrideInformation(const char* name,
117 
122  static void SetAllEnableFlags(vtkTypeBool flag,
123  const char* className);
128  static void SetAllEnableFlags(vtkTypeBool flag,
129  const char* className,
130  const char* subclassName);
131 
132  // Instance methods to be used on individual instances of vtkObjectFactory
133 
134  // Methods from vtkObject
139  void PrintSelf(ostream& os, vtkIndent indent) override;
140 
148  virtual const char* GetVTKSourceVersion() = 0;
149 
153  virtual const char* GetDescription() = 0;
154 
158  virtual int GetNumberOfOverrides();
159 
163  virtual const char* GetClassOverrideName(int index);
164 
169  virtual const char* GetClassOverrideWithName(int index);
170 
174  virtual vtkTypeBool GetEnableFlag(int index);
175 
180  virtual const char* GetOverrideDescription(int index);
181 
183 
187  virtual void SetEnableFlag(vtkTypeBool flag,
188  const char* className,
189  const char* subclassName);
190  virtual vtkTypeBool GetEnableFlag(const char* className,
191  const char* subclassName);
193 
197  virtual int HasOverride(const char* className);
201  virtual int HasOverride(const char* className, const char* subclassName);
202 
208  virtual void Disable(const char* className);
209 
211 
214  vtkGetStringMacro(LibraryPath);
216 
217  typedef vtkObject* (*CreateFunction)();
218 
219 protected:
220 
224  void RegisterOverride(const char* classOverride,
225  const char* overrideClassName,
226  const char* description,
227  int enableFlag,
228  CreateFunction createFunction);
229 
235  virtual vtkObject* CreateObject(const char* vtkclassname );
236 
238  ~vtkObjectFactory() override;
239 
241  {
242  char* Description;
245  CreateFunction CreateCallback;
246  };
247 
252 
253 private:
254  void GrowOverrideArray();
255 
260  static void Init();
264  static void RegisterDefaults();
268  static void LoadDynamicFactories();
272  static void LoadLibrariesInPath( const char*);
273 
274  // list of registered factories
275  static vtkObjectFactoryCollection* RegisteredFactories;
276 
277  // member variables for a factory set by the base class
278  // at load or register time
279  void* LibraryHandle;
280  char* LibraryVTKVersion;
281  char* LibraryCompilerUsed;
282  char* LibraryPath;
283 private:
284  vtkObjectFactory(const vtkObjectFactory&) = delete;
285  void operator=(const vtkObjectFactory&) = delete;
286 };
287 
288 // Implementation detail for Schwartz counter idiom.
289 class VTKCOMMONCORE_EXPORT vtkObjectFactoryRegistryCleanup
290 {
291 public:
294 
295 private:
298 };
300 
301 
302 // Macro to create an object creation function.
303 // The name of the function will by vtkObjectFactoryCreateclassname
304 // where classname is the name of the class being created
305 #define VTK_CREATE_CREATE_FUNCTION(classname) \
306 static vtkObject* vtkObjectFactoryCreate##classname() \
307 { return classname::New(); }
308 
309 #endif
310 
311 #define VTK_FACTORY_INTERFACE_EXPORT VTKCOMMONCORE_EXPORT
312 
313 // Macro to create the interface "C" functions used in
314 // a dll or shared library that contains a VTK object factory.
315 // Put this function in the .cxx file of your object factory,
316 // and pass in the name of the factory sub-class that you want
317 // the dll to create.
318 #define VTK_FACTORY_INTERFACE_IMPLEMENT(factoryName) \
319 extern "C" \
320 VTK_FACTORY_INTERFACE_EXPORT \
321 const char* vtkGetFactoryCompilerUsed() \
322 { \
323  return VTK_CXX_COMPILER; \
324 } \
325 extern "C" \
326 VTK_FACTORY_INTERFACE_EXPORT \
327 const char* vtkGetFactoryVersion() \
328 { \
329  return VTK_SOURCE_VERSION; \
330 } \
331 extern "C" \
332 VTK_FACTORY_INTERFACE_EXPORT \
333 vtkObjectFactory* vtkLoad() \
334 { \
335  return factoryName ::New(); \
336 }
337 
338 // Macro to implement the body of the object factory form of the New() method.
339 #define VTK_OBJECT_FACTORY_NEW_BODY(thisClass) \
340  vtkObject* ret = vtkObjectFactory::CreateInstance(#thisClass, false); \
341  if(ret) \
342  { \
343  return static_cast<thisClass*>(ret); \
344  } \
345  thisClass *result = new thisClass; \
346  result->InitializeObjectBase(); \
347  return result;
348 
349 // Macro to implement the body of the abstract object factory form of the New()
350 // method, i.e. an abstract base class that can only be instantiated if the
351 // object factory overrides it.
352 #define VTK_ABSTRACT_OBJECT_FACTORY_NEW_BODY(thisClass) \
353  vtkObject* ret = vtkObjectFactory::CreateInstance(#thisClass, true); \
354  if(ret) \
355  { \
356  return static_cast<thisClass*>(ret); \
357  } \
358  vtkGenericWarningMacro("Error: no override found for '" #thisClass "'."); \
359  return nullptr;
360 
361 // Macro to implement the body of the standard form of the New() method.
362 #if defined(VTK_ALL_NEW_OBJECT_FACTORY)
363 # define VTK_STANDARD_NEW_BODY(thisClass) \
364  VTK_OBJECT_FACTORY_NEW_BODY(thisClass)
365 #else
366 # define VTK_STANDARD_NEW_BODY(thisClass) \
367  thisClass *result = new thisClass; \
368  result->InitializeObjectBase(); \
369  return result;
370 #endif
371 
372 // Macro to implement the standard form of the New() method.
373 #define vtkStandardNewMacro(thisClass) \
374  thisClass* thisClass::New() \
375  { \
376  VTK_STANDARD_NEW_BODY(thisClass) \
377  }
378 
379 // Macro to implement the object factory form of the New() method.
380 #define vtkObjectFactoryNewMacro(thisClass) \
381  thisClass* thisClass::New() \
382  { \
383  VTK_OBJECT_FACTORY_NEW_BODY(thisClass) \
384  }
385 
386 // Macro to implement the abstract object factory form of the New() method.
387 // That is an abstract base class that can only be instantiated if the
388 // object factory overrides it.
389 #define vtkAbstractObjectFactoryNewMacro(thisClass) \
390  thisClass* thisClass::New() \
391  { \
392  VTK_ABSTRACT_OBJECT_FACTORY_NEW_BODY(thisClass) \
393  }
maintain a list of override information objects
abstract base class for most VTK objects
Definition: vtkObject.h:59
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
vtkGetStringMacro(ExtensionsString)
Returns a string listing all available extensions.
maintain a list of object factories
#define VTK_NEWINSTANCE
create and manipulate ordered lists of objects
Definition: vtkCollection.h:51
abstract base class for vtkObjectFactories