KWStyle - itkObjectFactoryBase.h
 
Matrix View
Description

1 /*=========================================================================
2
3   Program:   Insight Segmentation & Registration Toolkit
4   Module:    $RCSfile: itkObjectFactoryBase.h.html,v $
5   Language:  C++
6   Date:      $Date: 2006/01/17 19:15:43 $
7   Version:   $Revision: 1.4 $
8
9   Copyright (c) Insight Software Consortium. All rights reserved.
10   See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
11
12   Portions of this code are covered under the VTK copyright.
13   See VTKCopyright.txt or http://www.kitware.com/VTKCopyright.htm for details.
14
15      This software is distributed WITHOUT ANY WARRANTY; without even 
16      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
17 IND *****PURPOSE.  See the above copyright notices for more information.
18
19 =========================================================================*/
20 #ifndef __itkObjectFactoryBase_h
21 #define __itkObjectFactoryBase_h
22
23 #include "itkObject.h"
24 #include "itkCreateObjectFunction.h"
25 #include <list>
26 #include <vector>
27
28 namespace itk
29 {
30 /** \class ObjectFactoryBase
31  * \brief Create instances of classes using an object factory.
32  *
33  * ObjectFactoryBase is used to create itk objects. The base class
34  * ObjectFactoryBase contains a static method CreateInstance() that is
35  * used to create itk objects from the list of registerd ObjectFactoryBase
36  * sub-classes.  The first time CreateInstance() is called, all dll's or
37  * shared libraries in the environment variable ITK_AUTOLOAD_PATH are loaded
38  * into the current process.  The C function itkLoad is called on each dll.
39  * itkLoad should return an instance of the factory sub-class implemented in
40  * the shared library. ITK_AUTOLOAD_PATH is an environment variable
41  * containing a colon separated (semi-colon on win32) list of paths.
42  *
43  * This can be use to overide the creation of any object in ITK. 
44  *
45  * \ingroup ITKSystemObjects
46  */
47
48 class OverRideMap;
49
50 class ITKCommon_EXPORT ObjectFactoryBase : public Object
51 {
52 public:  
53   /** Standard class typedefs. */
54   typedef ObjectFactoryBase   Self;
55 TDA   typedef Object  Superclass;
56   typedef SmartPointer<Self>  Pointer;
57 TDA   typedef SmartPointer<const Self>  ConstPointer;
58   
59   /** Run-time type information (and related methods). */
60   itkTypeMacro(ObjectFactoryBase, Object);
61
62   /** Create and return an instance of the named itk object.
63    * Each loaded ObjectFactoryBase will be asked in the order
64    * the factory was in the ITK_AUTOLOAD_PATH.  After the
65    * first factory returns the object no other factories are asked. */
66   static LightObject::Pointer CreateInstance(const char* itkclassname);
67
68   /** Create and return all possible instances of the named itk object.
69    * Each loaded ObjectFactoryBase will be asked in the order
70    * the factory was in the ITK_AUTOLOAD_PATH.  All created objects
71    * will be returned in the list. */
72   static std::list<LightObject::Pointer>
73   CreateAllInstance(const char* itkclassname);
74   
75   /** Re-check the ITK_AUTOLOAD_PATH for new factory libraries.
76    * This calls UnRegisterAll before re-loading. */
77   static void ReHash(); 
78
79   /** Register a factory so it can be used to create itk objects. */
80   static void RegisterFactory(ObjectFactoryBase* );
81
82   /** Remove a factory from the list of registered factories. */
83   static void UnRegisterFactory(ObjectFactoryBase*);
84
85   /** Unregister all factories. */
86   static void UnRegisterAllFactories();
87   
88   /** Return the list of all registered factories.  This is NOT a copy,
89    * do not remove items from this list! */
90   static std::list<ObjectFactoryBase*> GetRegisteredFactories();
91
92   /** All sub-classes of ObjectFactoryBase should must return the version of 
93    * ITK they were built with.  This should be implemented with the macro
94    * ITK_SOURCE_VERSION and NOT a call to Version::GetITKSourceVersion.
95    * As the version needs to be compiled into the file as a string constant.
96 LEN    * This is critical to determine possible incompatible dynamic factory loads. */
97   virtual const char* GetITKSourceVersion(void) const = 0;
98
99   /** Return a descriptive string describing the factory. */
100   virtual const char* GetDescription(void) const = 0;
101
102   /** Return a list of classes that this factory overrides. */
103   virtual std::list<std::string> GetClassOverrideNames();
104
105   /** Return a list of the names of classes that override classes. */
106   virtual std::list<std::string> GetClassOverrideWithNames();
107
108   /** Return a list of descriptions for class overrides. */
109   virtual std::list<std::string> GetClassOverrideDescriptions();
110
111   /** Return a list of enable flags. */
112   virtual std::list<bool> GetEnableFlags();
113
114   /** Set the Enable flag for the specific override of className. */
115   virtual void SetEnableFlag(bool flag,
116            const char* className,
117            const char* subclassName);
118
119   /** Get the Enable flag for the specific override of className. */
120   virtual bool GetEnableFlag(const char* className,
121                              const char* subclassName);
122
123   /** Set all enable flags for the given class to 0.  This will
124    * mean that the factory will stop producing class with the given
125    * name. */
126   virtual void Disable(const char* className);
127   
128   /** This returns the path to a dynamically loaded factory. */
129   const char* GetLibraryPath();
130
131   /** \class OverrideInformation
132    * \brief Internal implementation class for ObjectFactorBase. */
133   struct OverrideInformation
134 IND **{
135     std::string m_Description;
136     std::string m_OverrideWithName;
137     bool m_EnabledFlag;
138     CreateObjectFunctionBase::Pointer m_CreateObject;
139 IND **};
140
141 protected:
142   virtual void PrintSelf(std::ostream& os, Indent indent) const;
143
144   /** Register object creation information with the factory. */
145   void RegisterOverride(const char* classOverride,
146       const char* overrideClassName,
147       const char* description,
148       bool enableFlag,
149       CreateObjectFunctionBase* createFunction);
150     
151   /** This method is provioded by sub-classes of ObjectFactoryBase.
152    * It should create the named itk object or return 0 if that object
153    * is not supported by the factory implementation. */
154   virtual LightObject::Pointer CreateObject(const char* itkclassname );
155   
156   ObjectFactoryBase();
157   virtual ~ObjectFactoryBase();
158
159 private:
160   OverRideMap* m_OverrideMap;
161
162   ObjectFactoryBase(const Self&); //purposely not implemented
163   void operator=(const Self&); //purposely not implemented
164
165   /** Initialize the static members of ObjectFactoryBase.   RegisterDefaults
166    * is called here. */
167   static void Initialize();
168
169   /** Register default factories which are not loaded at run time. */
170   static void RegisterDefaults();
171
172   /** Load dynamic factories from the ITK_AUTOLOAD_PATH */
173   static void LoadDynamicFactories();
174
175   /** Load all dynamic libraries in the given path */
176   static void LoadLibrariesInPath( const char*);
177   
178   /** list of registered factories */
179   static std::list<ObjectFactoryBase*>* m_RegisteredFactories; 
180   
181   /** Member variables for a factory set by the base class
182    * at load or register time */
183   void* m_LibraryHandle;
184   unsigned long m_LibraryDate;
185   std::string m_LibraryPath;
186 IND **};
187
188 // end namespace itk
189
190 #endif
191

Generated by KWStyle 1.0b on Tuesday January,17 at 02:14:37PM
© Kitware Inc.