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  VTK_LEGACY(static void ConstructInstance(const char* vtkclassname));
77 
84  static void CreateAllInstance(const char* vtkclassname,
85  vtkCollection* retList);
90  static void ReHash();
94  static void RegisterFactory(vtkObjectFactory* );
98  static void UnRegisterFactory(vtkObjectFactory*);
102  static void UnRegisterAllFactories();
103 
108  static vtkObjectFactoryCollection* GetRegisteredFactories();
109 
114  static int HasOverrideAny(const char* className);
115 
120  static void GetOverrideInformation(const char* name,
122 
127  static void SetAllEnableFlags(vtkTypeBool flag,
128  const char* className);
133  static void SetAllEnableFlags(vtkTypeBool flag,
134  const char* className,
135  const char* subclassName);
136 
137  // Instance methods to be used on individual instances of vtkObjectFactory
138 
139  // Methods from vtkObject
144  void PrintSelf(ostream& os, vtkIndent indent) VTK_OVERRIDE;
145 
153  virtual const char* GetVTKSourceVersion() = 0;
154 
158  virtual const char* GetDescription() = 0;
159 
163  virtual int GetNumberOfOverrides();
164 
168  virtual const char* GetClassOverrideName(int index);
169 
174  virtual const char* GetClassOverrideWithName(int index);
175 
179  virtual vtkTypeBool GetEnableFlag(int index);
180 
185  virtual const char* GetOverrideDescription(int index);
186 
188 
192  virtual void SetEnableFlag(vtkTypeBool flag,
193  const char* className,
194  const char* subclassName);
195  virtual vtkTypeBool GetEnableFlag(const char* className,
196  const char* subclassName);
198 
202  virtual int HasOverride(const char* className);
206  virtual int HasOverride(const char* className, const char* subclassName);
207 
213  virtual void Disable(const char* className);
214 
216 
219  vtkGetStringMacro(LibraryPath);
221 
222  typedef vtkObject* (*CreateFunction)();
223 
224 protected:
225 
229  void RegisterOverride(const char* classOverride,
230  const char* overrideClassName,
231  const char* description,
232  int enableFlag,
233  CreateFunction createFunction);
234 
240  virtual vtkObject* CreateObject(const char* vtkclassname );
241 
243  ~vtkObjectFactory() VTK_OVERRIDE;
244 
246  {
247  char* Description;
250  CreateFunction CreateCallback;
251  };
252 
257 
258 private:
259  void GrowOverrideArray();
260 
265  static void Init();
269  static void RegisterDefaults();
273  static void LoadDynamicFactories();
277  static void LoadLibrariesInPath( const char*);
278 
279  // list of registered factories
280  static vtkObjectFactoryCollection* RegisteredFactories;
281 
282  // member variables for a factory set by the base class
283  // at load or register time
284  void* LibraryHandle;
285  char* LibraryVTKVersion;
286  char* LibraryCompilerUsed;
287  char* LibraryPath;
288 private:
289  vtkObjectFactory(const vtkObjectFactory&) VTK_DELETE_FUNCTION;
290  void operator=(const vtkObjectFactory&) VTK_DELETE_FUNCTION;
291 };
292 
293 // Implementation detail for Schwartz counter idiom.
294 class VTKCOMMONCORE_EXPORT vtkObjectFactoryRegistryCleanup
295 {
296 public:
297  vtkObjectFactoryRegistryCleanup();
298  ~vtkObjectFactoryRegistryCleanup();
299 
300 private:
301  vtkObjectFactoryRegistryCleanup(const vtkObjectFactoryRegistryCleanup& other) VTK_DELETE_FUNCTION;
302  vtkObjectFactoryRegistryCleanup& operator=(const vtkObjectFactoryRegistryCleanup& rhs) VTK_DELETE_FUNCTION;
303 };
305 
306 
307 // Macro to create an object creation function.
308 // The name of the function will by vtkObjectFactoryCreateclassname
309 // where classname is the name of the class being created
310 #define VTK_CREATE_CREATE_FUNCTION(classname) \
311 static vtkObject* vtkObjectFactoryCreate##classname() \
312 { return classname::New(); }
313 
314 #endif
315 
316 #define VTK_FACTORY_INTERFACE_EXPORT VTKCOMMONCORE_EXPORT
317 
318 // Macro to create the interface "C" functions used in
319 // a dll or shared library that contains a VTK object factory.
320 // Put this function in the .cxx file of your object factory,
321 // and pass in the name of the factory sub-class that you want
322 // the dll to create.
323 #define VTK_FACTORY_INTERFACE_IMPLEMENT(factoryName) \
324 extern "C" \
325 VTK_FACTORY_INTERFACE_EXPORT \
326 const char* vtkGetFactoryCompilerUsed() \
327 { \
328  return VTK_CXX_COMPILER; \
329 } \
330 extern "C" \
331 VTK_FACTORY_INTERFACE_EXPORT \
332 const char* vtkGetFactoryVersion() \
333 { \
334  return VTK_SOURCE_VERSION; \
335 } \
336 extern "C" \
337 VTK_FACTORY_INTERFACE_EXPORT \
338 vtkObjectFactory* vtkLoad() \
339 { \
340  return factoryName ::New(); \
341 }
342 
343 // Macro to implement the body of the object factory form of the New() method.
344 #define VTK_OBJECT_FACTORY_NEW_BODY(thisClass) \
345  vtkObject* ret = vtkObjectFactory::CreateInstance(#thisClass, false); \
346  if(ret) \
347  { \
348  return static_cast<thisClass*>(ret); \
349  } \
350  thisClass *result = new thisClass; \
351  result->InitializeObjectBase(); \
352  return result;
353 
354 // Macro to implement the body of the abstract object factory form of the New()
355 // method, i.e. an abstract base class that can only be instantiated if the
356 // object factory overrides it.
357 #define VTK_ABSTRACT_OBJECT_FACTORY_NEW_BODY(thisClass) \
358  vtkObject* ret = vtkObjectFactory::CreateInstance(#thisClass, true); \
359  if(ret) \
360  { \
361  return static_cast<thisClass*>(ret); \
362  } \
363  vtkGenericWarningMacro("Error: no override found for '" #thisClass "'."); \
364  return NULL;
365 
366 // Macro to implement the body of the standard form of the New() method.
367 #if defined(VTK_ALL_NEW_OBJECT_FACTORY)
368 # define VTK_STANDARD_NEW_BODY(thisClass) \
369  VTK_OBJECT_FACTORY_NEW_BODY(thisClass)
370 #else
371 # define VTK_STANDARD_NEW_BODY(thisClass) \
372  thisClass *result = new thisClass; \
373  result->InitializeObjectBase(); \
374  return result;
375 #endif
376 
377 // Macro to implement the standard form of the New() method.
378 #define vtkStandardNewMacro(thisClass) \
379  thisClass* thisClass::New() \
380  { \
381  VTK_STANDARD_NEW_BODY(thisClass) \
382  }
383 
384 // Macro to implement the object factory form of the New() method.
385 #define vtkObjectFactoryNewMacro(thisClass) \
386  thisClass* thisClass::New() \
387  { \
388  VTK_OBJECT_FACTORY_NEW_BODY(thisClass) \
389  }
390 
391 // Macro to implement the abstract object factory form of the New() method.
392 // That is an abstract base class that can only be instantiated if the
393 // object factory overrides it.
394 #define vtkAbstractObjectFactoryNewMacro(thisClass) \
395  thisClass* thisClass::New() \
396  { \
397  VTK_ABSTRACT_OBJECT_FACTORY_NEW_BODY(thisClass) \
398  }
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 unsorted lists of objects
Definition: vtkCollection.h:51
abstract base class for vtkObjectFactories