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 =========================================================================*/
41 #ifndef vtkObjectFactory_h
42 #define vtkObjectFactory_h
43 
44 #include "vtkDebugLeaksManager.h" // Must be included before singletons
45 #include "vtkCommonCoreModule.h" // For export macro
46 #include "vtkObject.h"
47 
50 class vtkCollection;
51 
52 class VTKCOMMONCORE_EXPORT vtkObjectFactory : public vtkObject
53 {
54 public:
55  // Class Methods used to interface with the registered factories
56 
67  static vtkObject* CreateInstance(const char* vtkclassname,
68  bool isAbstract = false);
69 
76  static void CreateAllInstance(const char* vtkclassname,
77  vtkCollection* retList);
82  static void ReHash();
86  static void RegisterFactory(vtkObjectFactory* );
90  static void UnRegisterFactory(vtkObjectFactory*);
94  static void UnRegisterAllFactories();
95 
100  static vtkObjectFactoryCollection* GetRegisteredFactories();
101 
106  static int HasOverrideAny(const char* className);
107 
112  static void GetOverrideInformation(const char* name,
114 
119  static void SetAllEnableFlags(vtkTypeBool flag,
120  const char* className);
125  static void SetAllEnableFlags(vtkTypeBool flag,
126  const char* className,
127  const char* subclassName);
128 
129  // Instance methods to be used on individual instances of vtkObjectFactory
130 
131  // Methods from vtkObject
136  void PrintSelf(ostream& os, vtkIndent indent) override;
137 
145  virtual const char* GetVTKSourceVersion() = 0;
146 
150  virtual const char* GetDescription() = 0;
151 
155  virtual int GetNumberOfOverrides();
156 
160  virtual const char* GetClassOverrideName(int index);
161 
166  virtual const char* GetClassOverrideWithName(int index);
167 
171  virtual vtkTypeBool GetEnableFlag(int index);
172 
177  virtual const char* GetOverrideDescription(int index);
178 
180 
184  virtual void SetEnableFlag(vtkTypeBool flag,
185  const char* className,
186  const char* subclassName);
187  virtual vtkTypeBool GetEnableFlag(const char* className,
188  const char* subclassName);
190 
194  virtual int HasOverride(const char* className);
198  virtual int HasOverride(const char* className, const char* subclassName);
199 
205  virtual void Disable(const char* className);
206 
208 
211  vtkGetStringMacro(LibraryPath);
213 
214  typedef vtkObject* (*CreateFunction)();
215 
216 protected:
217 
221  void RegisterOverride(const char* classOverride,
222  const char* overrideClassName,
223  const char* description,
224  int enableFlag,
225  CreateFunction createFunction);
226 
232  virtual vtkObject* CreateObject(const char* vtkclassname );
233 
235  ~vtkObjectFactory() override;
236 
238  {
239  char* Description;
242  CreateFunction CreateCallback;
243  };
244 
249 
250 private:
251  void GrowOverrideArray();
252 
257  static void Init();
261  static void RegisterDefaults();
265  static void LoadDynamicFactories();
269  static void LoadLibrariesInPath( const char*);
270 
271  // list of registered factories
272  static vtkObjectFactoryCollection* RegisteredFactories;
273 
274  // member variables for a factory set by the base class
275  // at load or register time
276  void* LibraryHandle;
277  char* LibraryVTKVersion;
278  char* LibraryCompilerUsed;
279  char* LibraryPath;
280 private:
281  vtkObjectFactory(const vtkObjectFactory&) = delete;
282  void operator=(const vtkObjectFactory&) = delete;
283 };
284 
285 // Implementation detail for Schwartz counter idiom.
286 class VTKCOMMONCORE_EXPORT vtkObjectFactoryRegistryCleanup
287 {
288 public:
291 
292 private:
295 };
297 
298 
299 // Macro to create an object creation function.
300 // The name of the function will by vtkObjectFactoryCreateclassname
301 // where classname is the name of the class being created
302 #define VTK_CREATE_CREATE_FUNCTION(classname) \
303 static vtkObject* vtkObjectFactoryCreate##classname() \
304 { return classname::New(); }
305 
306 #endif
307 
308 #define VTK_FACTORY_INTERFACE_EXPORT VTKCOMMONCORE_EXPORT
309 
310 // Macro to create the interface "C" functions used in
311 // a dll or shared library that contains a VTK object factory.
312 // Put this function in the .cxx file of your object factory,
313 // and pass in the name of the factory sub-class that you want
314 // the dll to create.
315 #define VTK_FACTORY_INTERFACE_IMPLEMENT(factoryName) \
316 extern "C" \
317 VTK_FACTORY_INTERFACE_EXPORT \
318 const char* vtkGetFactoryCompilerUsed() \
319 { \
320  return VTK_CXX_COMPILER; \
321 } \
322 extern "C" \
323 VTK_FACTORY_INTERFACE_EXPORT \
324 const char* vtkGetFactoryVersion() \
325 { \
326  return VTK_SOURCE_VERSION; \
327 } \
328 extern "C" \
329 VTK_FACTORY_INTERFACE_EXPORT \
330 vtkObjectFactory* vtkLoad() \
331 { \
332  return factoryName ::New(); \
333 }
334 
335 // Macro to implement the body of the object factory form of the New() method.
336 #define VTK_OBJECT_FACTORY_NEW_BODY(thisClass) \
337  vtkObject* ret = vtkObjectFactory::CreateInstance(#thisClass, false); \
338  if(ret) \
339  { \
340  return static_cast<thisClass*>(ret); \
341  } \
342  thisClass *result = new thisClass; \
343  result->InitializeObjectBase(); \
344  return result;
345 
346 // Macro to implement the body of the abstract object factory form of the New()
347 // method, i.e. an abstract base class that can only be instantiated if the
348 // object factory overrides it.
349 #define VTK_ABSTRACT_OBJECT_FACTORY_NEW_BODY(thisClass) \
350  vtkObject* ret = vtkObjectFactory::CreateInstance(#thisClass, true); \
351  if(ret) \
352  { \
353  return static_cast<thisClass*>(ret); \
354  } \
355  vtkGenericWarningMacro("Error: no override found for '" #thisClass "'."); \
356  return nullptr;
357 
358 // Macro to implement the body of the standard form of the New() method.
359 #if defined(VTK_ALL_NEW_OBJECT_FACTORY)
360 # define VTK_STANDARD_NEW_BODY(thisClass) \
361  VTK_OBJECT_FACTORY_NEW_BODY(thisClass)
362 #else
363 # define VTK_STANDARD_NEW_BODY(thisClass) \
364  thisClass *result = new thisClass; \
365  result->InitializeObjectBase(); \
366  return result;
367 #endif
368 
369 // Macro to implement the standard form of the New() method.
370 #define vtkStandardNewMacro(thisClass) \
371  thisClass* thisClass::New() \
372  { \
373  VTK_STANDARD_NEW_BODY(thisClass) \
374  }
375 
376 // Macro to implement the object factory form of the New() method.
377 #define vtkObjectFactoryNewMacro(thisClass) \
378  thisClass* thisClass::New() \
379  { \
380  VTK_OBJECT_FACTORY_NEW_BODY(thisClass) \
381  }
382 
383 // Macro to implement the abstract object factory form of the New() method.
384 // That is an abstract base class that can only be instantiated if the
385 // object factory overrides it.
386 #define vtkAbstractObjectFactoryNewMacro(thisClass) \
387  thisClass* thisClass::New() \
388  { \
389  VTK_ABSTRACT_OBJECT_FACTORY_NEW_BODY(thisClass) \
390  }
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