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 
51 #include <string> // for std::string
52 
55 class vtkCollection;
56 
57 class VTKCOMMONCORE_EXPORT vtkObjectFactory : public vtkObject
58 {
59 public:
60  // Class Methods used to interface with the registered factories
61 
72  static vtkObject* CreateInstance(const char* vtkclassname,
73  bool isAbstract = false);
74 
81  static void CreateAllInstance(const char* vtkclassname,
82  vtkCollection* retList);
87  static void ReHash();
91  static void RegisterFactory(vtkObjectFactory* );
95  static void UnRegisterFactory(vtkObjectFactory*);
99  static void UnRegisterAllFactories();
100 
105  static vtkObjectFactoryCollection* GetRegisteredFactories();
106 
111  static int HasOverrideAny(const char* className);
112 
117  static void GetOverrideInformation(const char* name,
119 
124  static void SetAllEnableFlags(vtkTypeBool flag,
125  const char* className);
130  static void SetAllEnableFlags(vtkTypeBool flag,
131  const char* className,
132  const char* subclassName);
133 
134  // Instance methods to be used on individual instances of vtkObjectFactory
135 
136  // Methods from vtkObject
141  void PrintSelf(ostream& os, vtkIndent indent) override;
142 
150  virtual const char* GetVTKSourceVersion() = 0;
151 
155  virtual const char* GetDescription() = 0;
156 
160  virtual int GetNumberOfOverrides();
161 
165  virtual const char* GetClassOverrideName(int index);
166 
171  virtual const char* GetClassOverrideWithName(int index);
172 
176  virtual vtkTypeBool GetEnableFlag(int index);
177 
182  virtual const char* GetOverrideDescription(int index);
183 
185 
189  virtual void SetEnableFlag(vtkTypeBool flag,
190  const char* className,
191  const char* subclassName);
192  virtual vtkTypeBool GetEnableFlag(const char* className,
193  const char* subclassName);
195 
199  virtual int HasOverride(const char* className);
203  virtual int HasOverride(const char* className, const char* subclassName);
204 
210  virtual void Disable(const char* className);
211 
213 
216  vtkGetStringMacro(LibraryPath);
218 
219  typedef vtkObject* (*CreateFunction)();
220 
221 protected:
222 
226  void RegisterOverride(const char* classOverride,
227  const char* overrideClassName,
228  const char* description,
229  int enableFlag,
230  CreateFunction createFunction);
231 
237  virtual vtkObject* CreateObject(const char* vtkclassname );
238 
240  ~vtkObjectFactory() override;
241 
243  {
244  char* Description;
247  CreateFunction CreateCallback;
248  };
249 
254 
255 private:
256  void GrowOverrideArray();
257 
262  static void Init();
266  static void RegisterDefaults();
270  static void LoadDynamicFactories();
274  static void LoadLibrariesInPath(const std::string&);
275 
276  // list of registered factories
277  static vtkObjectFactoryCollection* RegisteredFactories;
278 
279  // member variables for a factory set by the base class
280  // at load or register time
281  void* LibraryHandle;
282  char* LibraryVTKVersion;
283  char* LibraryCompilerUsed;
284  char* LibraryPath;
285 private:
286  vtkObjectFactory(const vtkObjectFactory&) = delete;
287  void operator=(const vtkObjectFactory&) = delete;
288 };
289 
290 // Implementation detail for Schwartz counter idiom.
291 class VTKCOMMONCORE_EXPORT vtkObjectFactoryRegistryCleanup
292 {
293 public:
296 
297 private:
300 };
302 
303 
304 // Macro to create an object creation function.
305 // The name of the function will by vtkObjectFactoryCreateclassname
306 // where classname is the name of the class being created
307 #define VTK_CREATE_CREATE_FUNCTION(classname) \
308 static vtkObject* vtkObjectFactoryCreate##classname() \
309 { return classname::New(); }
310 
311 #endif
312 
313 #define VTK_FACTORY_INTERFACE_EXPORT VTKCOMMONCORE_EXPORT
314 
315 // Macro to create the interface "C" functions used in
316 // a dll or shared library that contains a VTK object factory.
317 // Put this function in the .cxx file of your object factory,
318 // and pass in the name of the factory sub-class that you want
319 // the dll to create.
320 #define VTK_FACTORY_INTERFACE_IMPLEMENT(factoryName) \
321 extern "C" \
322 VTK_FACTORY_INTERFACE_EXPORT \
323 const char* vtkGetFactoryCompilerUsed() \
324 { \
325  return VTK_CXX_COMPILER; \
326 } \
327 extern "C" \
328 VTK_FACTORY_INTERFACE_EXPORT \
329 const char* vtkGetFactoryVersion() \
330 { \
331  return VTK_SOURCE_VERSION; \
332 } \
333 extern "C" \
334 VTK_FACTORY_INTERFACE_EXPORT \
335 vtkObjectFactory* vtkLoad() \
336 { \
337  return factoryName ::New(); \
338 }
339 
340 // Macro to implement the body of the object factory form of the New() method.
341 #define VTK_OBJECT_FACTORY_NEW_BODY(thisClass) \
342  vtkObject* ret = vtkObjectFactory::CreateInstance(#thisClass, false); \
343  if(ret) \
344  { \
345  return static_cast<thisClass*>(ret); \
346  } \
347  thisClass *result = new thisClass; \
348  result->InitializeObjectBase(); \
349  return result;
350 
351 // Macro to implement the body of the abstract object factory form of the New()
352 // method, i.e. an abstract base class that can only be instantiated if the
353 // object factory overrides it.
354 #define VTK_ABSTRACT_OBJECT_FACTORY_NEW_BODY(thisClass) \
355  vtkObject* ret = vtkObjectFactory::CreateInstance(#thisClass, true); \
356  if(ret) \
357  { \
358  return static_cast<thisClass*>(ret); \
359  } \
360  vtkGenericWarningMacro("Error: no override found for '" #thisClass "'."); \
361  return nullptr;
362 
363 // Macro to implement the body of the standard form of the New() method.
364 #if defined(VTK_ALL_NEW_OBJECT_FACTORY)
365 # define VTK_STANDARD_NEW_BODY(thisClass) \
366  VTK_OBJECT_FACTORY_NEW_BODY(thisClass)
367 #else
368 # define VTK_STANDARD_NEW_BODY(thisClass) \
369  thisClass *result = new thisClass; \
370  result->InitializeObjectBase(); \
371  return result;
372 #endif
373 
374 // Macro to implement the standard form of the New() method.
375 #define vtkStandardNewMacro(thisClass) \
376  thisClass* thisClass::New() \
377  { \
378  VTK_STANDARD_NEW_BODY(thisClass) \
379  }
380 
381 // Macro to implement the object factory form of the New() method.
382 #define vtkObjectFactoryNewMacro(thisClass) \
383  thisClass* thisClass::New() \
384  { \
385  VTK_OBJECT_FACTORY_NEW_BODY(thisClass) \
386  }
387 
388 // Macro to implement the abstract object factory form of the New() method.
389 // That is an abstract base class that can only be instantiated if the
390 // object factory overrides it.
391 #define vtkAbstractObjectFactoryNewMacro(thisClass) \
392  thisClass* thisClass::New() \
393  { \
394  VTK_ABSTRACT_OBJECT_FACTORY_NEW_BODY(thisClass) \
395  }
vtkOverrideInformationCollection
maintain a list of override information objects
Definition: vtkOverrideInformationCollection.h:36
vtkObjectFactory::SizeOverrideArray
int SizeOverrideArray
Definition: vtkObjectFactory.h:252
vtkObjectFactoryRegistryCleanupInstance
static vtkObjectFactoryRegistryCleanup vtkObjectFactoryRegistryCleanupInstance
Definition: vtkObjectFactory.h:301
vtkObjectFactory::OverrideInformation::CreateCallback
CreateFunction CreateCallback
Definition: vtkObjectFactory.h:247
vtkObject
abstract base class for most VTK objects
Definition: vtkObject.h:59
vtkObjectFactoryRegistryCleanup
Definition: vtkObjectFactory.h:291
vtkObjectFactory::OverrideClassNames
char ** OverrideClassNames
Definition: vtkObjectFactory.h:251
vtkObjectFactory::OverrideArrayLength
int OverrideArrayLength
Definition: vtkObjectFactory.h:253
vtkObjectFactory::OverrideInformation::Description
char * Description
Definition: vtkObjectFactory.h:244
vtkDebugLeaksManager.h
vtkCollection
create and manipulate ordered lists of objects
Definition: vtkCollection.h:51
vtkObjectFactory
abstract base class for vtkObjectFactories
Definition: vtkObjectFactory.h:57
vtkObjectFactory::OverrideInformation::OverrideWithName
char * OverrideWithName
Definition: vtkObjectFactory.h:245
vtkIndent
a simple class to control print indentation
Definition: vtkIndent.h:39
vtkObjectFactory::OverrideInformation::EnabledFlag
vtkTypeBool EnabledFlag
Definition: vtkObjectFactory.h:246
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:40
vtkX3D::name
Definition: vtkX3D.h:219
vtkObject.h
vtkX3D::string
Definition: vtkX3D.h:490
vtkX3D::description
Definition: vtkX3D.h:322
vtkObjectFactory::OverrideInformation
Definition: vtkObjectFactory.h:242
vtkObjectFactory::OverrideArray
OverrideInformation * OverrideArray
Definition: vtkObjectFactory.h:250
VTK_NEWINSTANCE
#define VTK_NEWINSTANCE
Definition: vtkWrappingHints.h:39
vtkX3D::index
Definition: vtkX3D.h:246
vtkTypeBool
int vtkTypeBool
Definition: vtkABI.h:69