ImageReaderFactory.h
1 /*
2  * Medical Image Registration ToolKit (MIRTK)
3  *
4  * Copyright 2013-2015 Imperial College London
5  * Copyright 2013-2015 Andreas Schuh
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 
20 #ifndef MIRTK_ImageReaderFactory_H
21 #define MIRTK_ImageReaderFactory_H
22 
23 #include "mirtk/ObjectFactory.h" // New<BaseType, ObjectType>()
24 #include "mirtk/ImageReader.h"
25 #include "mirtk/List.h"
26 
27 
28 namespace mirtk {
29 
30 
31 /**
32  * Factory for instantiation of image readers
33  */
35 {
36  // ---------------------------------------------------------------------------
37  // Types
38 public:
39 
40  /// Type of object creator
41  typedef ImageReader *(*ImageReaderCreator)();
42 
43  // ---------------------------------------------------------------------------
44  // Singleton
45 private:
46 
47  /// Constructor
49 
50  /// Destructor
52 
53  /// Copy constructor. Intentionally not implemented.
55 
56  /// Assignment operator. Intentionally not implemented.
57  void operator =(const ImageReaderFactory &);
58 
59 public:
60 
61  /// Singleton instance
62  /// \attention This function is not thread-safe!
63  static ImageReaderFactory &Instance();
64 
65  // ---------------------------------------------------------------------------
66  // Object creation
67 private:
68 
69  /// Type of associative map
70  typedef List<ImageReaderCreator> Creators;
71 
72  /// Registered object type creators
73  Creators _Creators;
74 
75 public:
76 
77  /// Register new object creator
78  ///
79  /// \param[in] creator Object creator function.
80  bool Register(ImageReaderCreator creator);
81 
82  /// Construct new image reader for given image file
83  ///
84  /// \param[in] fname Input image file path/name incl. file name extension.
85  ///
86  /// \returns First found image reader which is able to read the given image file
87  /// or nullptr when file cannot be read by any registered reader.
88  ImageReader *New(const char *fname) const;
89 
90 };
91 
92 // -----------------------------------------------------------------------------
93 /// Register image reader with factory singleton
94 #define mirtkRegisterImageReaderMacro(type) \
95  mirtk::ImageReaderFactory::Instance() \
96  .Register(mirtk::New<mirtk::ImageReader, type>)
97 
98 // -----------------------------------------------------------------------------
99 /// Register image reader with factory singleton at static initialization time
100 #ifdef MIRTK_AUTO_REGISTER
101  #define mirtkAutoRegisterImageReaderMacro(type) \
102  namespace { \
103  static auto _##type##Registered = \
104  mirtk::ImageReaderFactory::Instance() \
105  .Register(mirtk::New<mirtk::ImageReader, type>); \
106  }
107 #else // MIRTK_AUTO_REGISTER
108  #define mirtkAutoRegisterImageReaderMacro(type)
109 #endif // MIRTK_AUTO_REGISTER
110 
111 
112 } // namespace mirtk
113 
114 #endif // MIRTK_ImageReaderFactory_H
static ImageReaderFactory & Instance()
ImageReader *(* ImageReaderCreator)()
Type of object creator.
ImageReader * New(const char *fname) const
Definition: IOConfig.h:41
bool Register(ImageReaderCreator creator)