ITK  6.0.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  [[maybe_unused]] static const FactoryRegistration staticFactoryRegistration = [] {
224  RegisterFactoryInternal(TFactory::New());
225  return FactoryRegistration{};
226  }();
227  }
228 
230  static void
231  Initialize();
232 
233 protected:
234  void
235  PrintSelf(std::ostream & os, Indent indent) const override;
236 
238  void
239  RegisterOverride(const char * classOverride,
240  const char * subclass,
241  const char * description,
242  bool enableFlag,
243  CreateObjectFunctionBase * createFunction);
244 
248  virtual LightObject::Pointer
249  CreateObject(const char * itkclassname);
250 
254  virtual std::list<LightObject::Pointer>
255  CreateAllObject(const char * itkclassname);
256 
258  ~ObjectFactoryBase() override;
259 
260 private:
261  // Forward reference because of private implementation
262  class OverrideMap;
263  class ObjectFactoryBasePrivate;
264 
267  static void
268  SynchronizeObjectFactoryBase(void * objectFactoryBasePrivate);
269  itkGetGlobalDeclarationMacro(ObjectFactoryBasePrivate, PimplGlobals);
272  const std::unique_ptr<OverrideMap> m_OverrideMap;
273 
275  static void
276  LoadDynamicFactories();
277 
279  static void
280  LoadLibrariesInPath(const char *);
281 
282  static void
283  DeleteNonInternalFactory(ObjectFactoryBase *);
284 
287  void * m_LibraryHandle{};
288  unsigned long m_LibraryDate{};
289  std::string m_LibraryPath{};
290 
291  static ObjectFactoryBasePrivate * m_PimplGlobals;
292 };
293 } // end namespace itk
294 
295 #endif
itkCreateObjectFunction.h
itk::ObjectFactoryBase
Create instances of classes using an object factory.
Definition: itkObjectFactoryBase.h:59
itkSingletonMacro.h
itk::ObjectFactoryEnums::InsertionPosition
InsertionPosition
Definition: itkCommonEnums.h:273
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: itkAnatomicalOrientation.h:29
itk::ObjectFactoryBase::m_PimplGlobals
static ObjectFactoryBasePrivate * m_PimplGlobals
Definition: itkObjectFactoryBase.h:291
itk::ObjectFactoryBase::m_OverrideMap
const std::unique_ptr< OverrideMap > m_OverrideMap
Definition: itkObjectFactoryBase.h:272
itkCommonEnums.h
itkGetGlobalDeclarationMacro
#define itkGetGlobalDeclarationMacro(Type, VarName)
Definition: itkSingletonMacro.h:34
itk::Object
Base class for most ITK classes.
Definition: itkObject.h:61
New
static Pointer New()
itk::ObjectFactoryBase::RegisterInternalFactoryOnce
static void RegisterInternalFactoryOnce()
Definition: itkObjectFactoryBase.h:217