ITK  5.4.0
Insight Toolkit
itkObjectFactoryBase.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright NumFOCUS
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * https://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *=========================================================================*/
18 /*=========================================================================
19  *
20  * Portions of this file are subject to the VTK Toolkit Version 3 copyright.
21  *
22  * Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
23  *
24  * For complete copyright, license and disclaimer of warranty information
25  * please refer to the NOTICE file at the top of the ITK source tree.
26  *
27  *=========================================================================*/
28 #ifndef itkObjectFactoryBase_h
29 #define itkObjectFactoryBase_h
30 
32 #include "itkSingletonMacro.h"
33 #include "itkCommonEnums.h"
34 #include <list>
35 #include <memory> // For unique_ptr.
36 #include <vector>
37 
38 namespace itk
39 {
59 class ITKCommon_EXPORT ObjectFactoryBase : public Object
60 {
61 public:
62  ITK_DISALLOW_COPY_AND_MOVE(ObjectFactoryBase);
63 
66  using Superclass = Object;
69 
71  itkOverrideGetNameOfClassMacro(ObjectFactoryBase);
72 
82  CreateInstance(const char * itkclassname);
83 
88  static std::list<LightObject::Pointer>
89  CreateAllInstance(const char * itkclassname);
90 
93  static void
94  ReHash();
95 
103  static void
104  RegisterFactoryInternal(ObjectFactoryBase *);
105 
107 #if !defined(ITK_LEGACY_REMOVE)
108  // We need to expose the enum values at the class level
109  // for backwards compatibility
110  static constexpr InsertionPositionEnum INSERT_AT_FRONT = InsertionPositionEnum::INSERT_AT_FRONT;
111  static constexpr InsertionPositionEnum INSERT_AT_BACK = InsertionPositionEnum::INSERT_AT_BACK;
112  static constexpr InsertionPositionEnum INSERT_AT_POSITION = InsertionPositionEnum::INSERT_AT_POSITION;
113 #endif
114 
133  static bool
134  RegisterFactory(ObjectFactoryBase *,
135  InsertionPositionEnum where = InsertionPositionEnum::INSERT_AT_BACK,
136  size_t position = 0);
140  static void
141  UnRegisterFactory(ObjectFactoryBase *);
142 
144  static void
145  UnRegisterAllFactories();
146 
149  static std::list<ObjectFactoryBase *>
150  GetRegisteredFactories();
151 
157  virtual const char *
158  GetITKSourceVersion() const = 0;
159 
165  static void
166  SetStrictVersionChecking(bool);
167  static void
168  StrictVersionCheckingOn();
169  static void
170  StrictVersionCheckingOff();
171  static bool
172  GetStrictVersionChecking();
176  virtual const char *
177  GetDescription() const = 0;
178 
180  virtual std::list<std::string>
181  GetClassOverrideNames();
182 
184  virtual std::list<std::string>
185  GetClassOverrideWithNames();
186 
188  virtual std::list<std::string>
189  GetClassOverrideDescriptions();
190 
192  virtual std::list<bool>
193  GetEnableFlags();
194 
196  virtual void
197  SetEnableFlag(bool flag, const char * className, const char * subclassName);
198 
200  virtual bool
201  GetEnableFlag(const char * className, const char * subclassName);
202 
206  virtual void
207  Disable(const char * className);
208 
210  const char *
211  GetLibraryPath();
212 
215  template <typename TFactory>
216  static void
218  {
219  struct FactoryRegistration
220  {};
221 
222  // Factory registration, made thread-safe by "magic statics" (as introduced with C++11).
223  static const FactoryRegistration staticFactoryRegistration = [] {
224  RegisterFactoryInternal(TFactory::New());
225  return FactoryRegistration{};
226  }();
227 
228  (void)staticFactoryRegistration;
229  }
230 
232  static void
233  Initialize();
234 
235 protected:
236  void
237  PrintSelf(std::ostream & os, Indent indent) const override;
238 
240  void
241  RegisterOverride(const char * classOverride,
242  const char * subclass,
243  const char * description,
244  bool enableFlag,
245  CreateObjectFunctionBase * createFunction);
246 
250  virtual LightObject::Pointer
251  CreateObject(const char * itkclassname);
252 
256  virtual std::list<LightObject::Pointer>
257  CreateAllObject(const char * itkclassname);
258 
260  ~ObjectFactoryBase() override;
261 
262 private:
263  // Forward reference because of private implementation
264  class OverrideMap;
265  class ObjectFactoryBasePrivate;
266 
269  static void
270  SynchronizeObjectFactoryBase(void * objectFactoryBasePrivate);
271  itkGetGlobalDeclarationMacro(ObjectFactoryBasePrivate, PimplGlobals);
274  const std::unique_ptr<OverrideMap> m_OverrideMap;
275 
277  static void
278  LoadDynamicFactories();
279 
281  static void
282  LoadLibrariesInPath(const char *);
283 
284  static void
285  DeleteNonInternalFactory(ObjectFactoryBase *);
286 
289  void * m_LibraryHandle{};
290  unsigned long m_LibraryDate{};
291  std::string m_LibraryPath{};
292 
293  static ObjectFactoryBasePrivate * m_PimplGlobals;
294 };
295 } // end namespace itk
296 
297 #endif
itkCreateObjectFunction.h
itk::ObjectFactoryBase
Create instances of classes using an object factory.
Definition: itkObjectFactoryBase.h:59
itkSingletonMacro.h
itk::SmartPointer< Self >
itk::Indent
Control indentation during Print() invocation.
Definition: itkIndent.h:49
itk::CreateObjectFunctionBase
Define API for object creation callback functions.
Definition: itkCreateObjectFunction.h:31
itk::LightObject
Light weight base class for most itk classes.
Definition: itkLightObject.h:55
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnnulusOperator.h:24
itk::ObjectFactoryBase::m_PimplGlobals
static ObjectFactoryBasePrivate * m_PimplGlobals
Definition: itkObjectFactoryBase.h:293
itk::ObjectFactoryBase::m_OverrideMap
const std::unique_ptr< OverrideMap > m_OverrideMap
Definition: itkObjectFactoryBase.h:274
itkCommonEnums.h
itkGetGlobalDeclarationMacro
#define itkGetGlobalDeclarationMacro(Type, VarName)
Definition: itkSingletonMacro.h:35
itk::Object
Base class for most ITK classes.
Definition: itkObject.h:61
itk::ObjectFactoryEnums::InsertionPosition
InsertionPosition
Definition: itkCommonEnums.h:280
New
static Pointer New()
itk::ObjectFactoryBase::RegisterInternalFactoryOnce
static void RegisterInternalFactoryOnce()
Definition: itkObjectFactoryBase.h:217