VTK  9.7.1
vtkObjectFactory.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
2 // SPDX-License-Identifier: BSD-3-Clause
47 #ifndef vtkObjectFactory_h
48 #define vtkObjectFactory_h
49 
50 #include "vtkCommonCoreModule.h" // For export macro
51 #include "vtkDebugLeaksManager.h" // Must be included before singletons
52 #include "vtkFeatures.h" // For VTK_ALL_NEW_OBJECT_FACTORY
53 #include "vtkObject.h"
54 #include "vtkSmartPointer.h" // For vtkSmartPointer
55 
56 #include <string> // for std::string
57 
58 VTK_ABI_NAMESPACE_BEGIN
61 class vtkCollection;
63 
64 class VTKCOMMONCORE_EXPORT vtkObjectFactory : public vtkObject
65 {
66 public:
67  // Class Methods used to interface with the registered factories
68 
79  static vtkObject* CreateInstance(const char* vtkclassname, bool isAbstract = false);
80 
87  static void CreateAllInstance(const char* vtkclassname, vtkCollection* retList);
92  static void ReHash();
96  static void RegisterFactory(vtkObjectFactory*);
100  static void UnRegisterFactory(vtkObjectFactory*);
104  static void UnRegisterAllFactories();
105 
110  static vtkObjectFactoryCollection* GetRegisteredFactories();
111 
116  static vtkTypeBool HasOverrideAny(const char* className);
117 
122  static void GetOverrideInformation(const char* name, vtkOverrideInformationCollection*);
123 
128  static void SetAllEnableFlags(vtkTypeBool flag, const char* className);
133  static void SetAllEnableFlags(vtkTypeBool flag, const char* className, const char* subclassName);
134 
135  // Instance methods to be used on individual instances of vtkObjectFactory
136 
137  // Methods from vtkObject
138  vtkTypeMacro(vtkObjectFactory, vtkObject);
142  void PrintSelf(ostream& os, vtkIndent indent) override;
143 
151  virtual const char* GetVTKSourceVersion() VTK_FUTURE_CONST = 0;
152 
156  virtual const char* GetDescription() VTK_FUTURE_CONST = 0;
157 
161  virtual int GetNumberOfOverrides() VTK_FUTURE_CONST;
162 
166  virtual const char* GetClassOverrideName(int index) VTK_FUTURE_CONST;
167 
172  virtual const char* GetClassOverrideWithName(int index) VTK_FUTURE_CONST;
173 
177  virtual vtkTypeBool GetEnableFlag(int index) VTK_FUTURE_CONST;
178 
183  virtual const char* GetOverrideDescription(int index) VTK_FUTURE_CONST;
184 
186 
190  virtual void SetEnableFlag(vtkTypeBool flag, const char* className, const char* subclassName);
191  virtual vtkTypeBool GetEnableFlag(
192  const char* className, const char* subclassName) VTK_FUTURE_CONST;
194 
198  vtkOverrideAttribute* GetOverrideAttributes(int index) const;
199 
203  virtual vtkTypeBool HasOverride(const char* className) VTK_FUTURE_CONST;
207  virtual vtkTypeBool HasOverride(const char* className, const char* subclassName) VTK_FUTURE_CONST;
208 
214  virtual void Disable(const char* className);
215 
217 
220  vtkGetFilePathMacro(LibraryPath);
222 
223  typedef vtkObject* (*CreateFunction)();
224 
236  static bool InitializePreferencesFromCommandLineArgs(int& argc, char* argv[]);
237 
243  static void SetPreferences(std::string preferences);
244  static std::string GetPreferences();
245 
246 protected:
256  void RegisterOverride(const char* classOverride, const char* overrideClassName,
257  const char* description, int enableFlag, CreateFunction createFunction,
258  vtkOverrideAttribute* attributes = nullptr);
259 
265  virtual vtkObject* CreateObject(const char* vtkclassname);
266 
268  ~vtkObjectFactory() override;
269 
271  {
272  char* Description;
275  CreateFunction CreateCallback;
277  };
278 
283 
284 private:
285  void GrowOverrideArray();
286 
291  static void Init();
295  static void RegisterDefaults();
299  static void LoadDynamicFactories();
303  static void LoadLibrariesInPath(const std::string&);
304 
305  // list of registered factories
306  static vtkObjectFactoryCollection* RegisteredFactories;
307 
308  // member variables for a factory set by the base class
309  // at load or register time
310  void* LibraryHandle;
311  char* LibraryVTKVersion;
312  char* LibraryPath;
313 
314  static std::string Preferences;
315 
316  vtkObjectFactory(const vtkObjectFactory&) = delete;
317  void operator=(const vtkObjectFactory&) = delete;
318 };
319 
320 // Implementation detail for Schwarz counter idiom.
321 class VTKCOMMONCORE_EXPORT vtkObjectFactoryRegistryCleanup
322 {
323 public:
326 
327 private:
330 };
332 
333 // Macro to create an object creation function.
334 // The name of the function will by vtkObjectFactoryCreateclassname
335 // where classname is the name of the class being created
336 #define VTK_CREATE_CREATE_FUNCTION(classname) \
337  static vtkObject* vtkObjectFactoryCreate##classname() \
338  { \
339  return classname::New(); \
340  }
341 
342 VTK_ABI_NAMESPACE_END
343 #endif
344 
345 #define VTK_FACTORY_INTERFACE_EXPORT VTKCOMMONCORE_EXPORT
346 
347 // Macro to create the interface "C" functions used in
348 // a dll or shared library that contains a VTK object factory.
349 // Put this function in the .cxx file of your object factory,
350 // and pass in the name of the factory sub-class that you want
351 // the dll to create.
352 #define VTK_FACTORY_INTERFACE_IMPLEMENT(factoryName) \
353  extern "C" VTK_FACTORY_INTERFACE_EXPORT const char* vtkGetFactoryVersion() \
354  { \
355  return VTK_SOURCE_VERSION; \
356  } \
357  extern "C" VTK_FACTORY_INTERFACE_EXPORT vtkObjectFactory* vtkLoad() \
358  { \
359  return factoryName ::New(); \
360  }
361 
362 // Macro to implement the body of the object factory form of the New() method.
363 #define VTK_OBJECT_FACTORY_NEW_BODY(thisClass) \
364  vtkObject* ret = vtkObjectFactory::CreateInstance(#thisClass, false); \
365  if (ret) \
366  { \
367  return static_cast<thisClass*>(ret); \
368  } \
369  auto result = new thisClass; \
370  result->InitializeObjectBase(); \
371  return result
372 
373 // Macro to implement the body of the abstract object factory form of the New()
374 // method, i.e. an abstract base class that can only be instantiated if the
375 // object factory overrides it.
376 #define VTK_ABSTRACT_OBJECT_FACTORY_NEW_BODY(thisClass) \
377  vtkObject* ret = vtkObjectFactory::CreateInstance(#thisClass, true); \
378  if (ret) \
379  { \
380  return static_cast<thisClass*>(ret); \
381  } \
382  vtkGenericWarningMacro("Error: no override found for '" #thisClass "'."); \
383  return nullptr
384 
385 // Macro to implement the body of the standard form of the New() method.
386 #if defined(VTK_ALL_NEW_OBJECT_FACTORY)
387 #define VTK_STANDARD_NEW_BODY(thisClass) VTK_OBJECT_FACTORY_NEW_BODY(thisClass)
388 #else
389 #define VTK_STANDARD_NEW_BODY(thisClass) \
390  auto result = new thisClass; \
391  result->InitializeObjectBase(); \
392  return result
393 #endif
394 
395 // Macro to implement the standard form of the New() method.
396 #define vtkStandardNewMacro(thisClass) \
397  thisClass* thisClass::New() \
398  { \
399  VTK_STANDARD_NEW_BODY(thisClass); \
400  }
401 
402 // Macro to implement the ExtendedNew() to create an object in a memkind extended memory space. If
403 // VTK is not compiled with VTK_USE_MEMKIND this is equivalent to New()
404 #define vtkStandardExtendedNewMacro(thisClass) \
405  thisClass* thisClass::ExtendedNew() \
406  { \
407  auto mkhold = vtkMemkindRAII(true); \
408  (void)mkhold; \
409  return thisClass::New(); \
410  }
411 
412 // Macro to implement the object factory form of the New() method.
413 #define vtkObjectFactoryNewMacro(thisClass) \
414  thisClass* thisClass::New() \
415  { \
416  VTK_OBJECT_FACTORY_NEW_BODY(thisClass); \
417  }
418 
419 // Macro to implement the abstract object factory form of the New() method.
420 // That is an abstract base class that can only be instantiated if the
421 // object factory overrides it.
422 #define vtkAbstractObjectFactoryNewMacro(thisClass) \
423  thisClass* thisClass::New() \
424  { \
425  VTK_ABSTRACT_OBJECT_FACTORY_NEW_BODY(thisClass); \
426  }
vtkSmartPointer< vtkOverrideAttribute > Attributes
Attribute for vtkObjectFactory overrides.
maintain a list of override information objects
abstract base class for most VTK objects
Definition: vtkObject.h:52
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:64
a simple class to control print indentation
Definition: vtkIndent.h:28
OverrideInformation * OverrideArray
maintain a list of object factories
#define VTK_NEWINSTANCE
create and manipulate ordered lists of objects
Definition: vtkCollection.h:39
abstract base class for vtkObjectFactories