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 
48 #include <string> // for std::string
49 
52 class vtkCollection;
53 
54 class VTKCOMMONCORE_EXPORT vtkObjectFactory : public vtkObject
55 {
56 public:
57  // Class Methods used to interface with the registered factories
58 
69  static vtkObject* CreateInstance(const char* vtkclassname,
70  bool isAbstract = false);
71 
78  static void CreateAllInstance(const char* vtkclassname,
79  vtkCollection* retList);
84  static void ReHash();
88  static void RegisterFactory(vtkObjectFactory* );
92  static void UnRegisterFactory(vtkObjectFactory*);
96  static void UnRegisterAllFactories();
97 
102  static vtkObjectFactoryCollection* GetRegisteredFactories();
103 
108  static int HasOverrideAny(const char* className);
109 
114  static void GetOverrideInformation(const char* name,
116 
121  static void SetAllEnableFlags(vtkTypeBool flag,
122  const char* className);
127  static void SetAllEnableFlags(vtkTypeBool flag,
128  const char* className,
129  const char* subclassName);
130 
131  // Instance methods to be used on individual instances of vtkObjectFactory
132 
133  // Methods from vtkObject
138  void PrintSelf(ostream& os, vtkIndent indent) override;
139 
147  virtual const char* GetVTKSourceVersion() = 0;
148 
152  virtual const char* GetDescription() = 0;
153 
157  virtual int GetNumberOfOverrides();
158 
162  virtual const char* GetClassOverrideName(int index);
163 
168  virtual const char* GetClassOverrideWithName(int index);
169 
173  virtual vtkTypeBool GetEnableFlag(int index);
174 
179  virtual const char* GetOverrideDescription(int index);
180 
182 
186  virtual void SetEnableFlag(vtkTypeBool flag,
187  const char* className,
188  const char* subclassName);
189  virtual vtkTypeBool GetEnableFlag(const char* className,
190  const char* subclassName);
192 
196  virtual int HasOverride(const char* className);
200  virtual int HasOverride(const char* className, const char* subclassName);
201 
207  virtual void Disable(const char* className);
208 
210 
213  vtkGetStringMacro(LibraryPath);
215 
216  typedef vtkObject* (*CreateFunction)();
217 
218 protected:
219 
223  void RegisterOverride(const char* classOverride,
224  const char* overrideClassName,
225  const char* description,
226  int enableFlag,
227  CreateFunction createFunction);
228 
234  virtual vtkObject* CreateObject(const char* vtkclassname );
235 
237  ~vtkObjectFactory() override;
238 
240  {
241  char* Description;
244  CreateFunction CreateCallback;
245  };
246 
251 
252 private:
253  void GrowOverrideArray();
254 
259  static void Init();
263  static void RegisterDefaults();
267  static void LoadDynamicFactories();
271  static void LoadLibrariesInPath(const std::string&);
272 
273  // list of registered factories
274  static vtkObjectFactoryCollection* RegisteredFactories;
275 
276  // member variables for a factory set by the base class
277  // at load or register time
278  void* LibraryHandle;
279  char* LibraryVTKVersion;
280  char* LibraryCompilerUsed;
281  char* LibraryPath;
282 private:
283  vtkObjectFactory(const vtkObjectFactory&) = delete;
284  void operator=(const vtkObjectFactory&) = delete;
285 };
286 
287 // Implementation detail for Schwartz counter idiom.
288 class VTKCOMMONCORE_EXPORT vtkObjectFactoryRegistryCleanup
289 {
290 public:
293 
294 private:
297 };
299 
300 
301 // Macro to create an object creation function.
302 // The name of the function will by vtkObjectFactoryCreateclassname
303 // where classname is the name of the class being created
304 #define VTK_CREATE_CREATE_FUNCTION(classname) \
305 static vtkObject* vtkObjectFactoryCreate##classname() \
306 { return classname::New(); }
307 
308 #endif
309 
310 #define VTK_FACTORY_INTERFACE_EXPORT VTKCOMMONCORE_EXPORT
311 
312 // Macro to create the interface "C" functions used in
313 // a dll or shared library that contains a VTK object factory.
314 // Put this function in the .cxx file of your object factory,
315 // and pass in the name of the factory sub-class that you want
316 // the dll to create.
317 #define VTK_FACTORY_INTERFACE_IMPLEMENT(factoryName) \
318 extern "C" \
319 VTK_FACTORY_INTERFACE_EXPORT \
320 const char* vtkGetFactoryCompilerUsed() \
321 { \
322  return VTK_CXX_COMPILER; \
323 } \
324 extern "C" \
325 VTK_FACTORY_INTERFACE_EXPORT \
326 const char* vtkGetFactoryVersion() \
327 { \
328  return VTK_SOURCE_VERSION; \
329 } \
330 extern "C" \
331 VTK_FACTORY_INTERFACE_EXPORT \
332 vtkObjectFactory* vtkLoad() \
333 { \
334  return factoryName ::New(); \
335 }
336 
337 // Macro to implement the body of the object factory form of the New() method.
338 #define VTK_OBJECT_FACTORY_NEW_BODY(thisClass) \
339  vtkObject* ret = vtkObjectFactory::CreateInstance(#thisClass, false); \
340  if(ret) \
341  { \
342  return static_cast<thisClass*>(ret); \
343  } \
344  thisClass *result = new thisClass; \
345  result->InitializeObjectBase(); \
346  return result;
347 
348 // Macro to implement the body of the abstract object factory form of the New()
349 // method, i.e. an abstract base class that can only be instantiated if the
350 // object factory overrides it.
351 #define VTK_ABSTRACT_OBJECT_FACTORY_NEW_BODY(thisClass) \
352  vtkObject* ret = vtkObjectFactory::CreateInstance(#thisClass, true); \
353  if(ret) \
354  { \
355  return static_cast<thisClass*>(ret); \
356  } \
357  vtkGenericWarningMacro("Error: no override found for '" #thisClass "'."); \
358  return nullptr;
359 
360 // Macro to implement the body of the standard form of the New() method.
361 #if defined(VTK_ALL_NEW_OBJECT_FACTORY)
362 # define VTK_STANDARD_NEW_BODY(thisClass) \
363  VTK_OBJECT_FACTORY_NEW_BODY(thisClass)
364 #else
365 # define VTK_STANDARD_NEW_BODY(thisClass) \
366  thisClass *result = new thisClass; \
367  result->InitializeObjectBase(); \
368  return result;
369 #endif
370 
371 // Macro to implement the standard form of the New() method.
372 #define vtkStandardNewMacro(thisClass) \
373  thisClass* thisClass::New() \
374  { \
375  VTK_STANDARD_NEW_BODY(thisClass) \
376  }
377 
378 // Macro to implement the object factory form of the New() method.
379 #define vtkObjectFactoryNewMacro(thisClass) \
380  thisClass* thisClass::New() \
381  { \
382  VTK_OBJECT_FACTORY_NEW_BODY(thisClass) \
383  }
384 
385 // Macro to implement the abstract object factory form of the New() method.
386 // That is an abstract base class that can only be instantiated if the
387 // object factory overrides it.
388 #define vtkAbstractObjectFactoryNewMacro(thisClass) \
389  thisClass* thisClass::New() \
390  { \
391  VTK_ABSTRACT_OBJECT_FACTORY_NEW_BODY(thisClass) \
392  }
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
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