ImageWriterFactory.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_ImageWriterFactory_H
21 #define MIRTK_ImageWriterFactory_H
22 
23 #include "mirtk/ObjectFactory.h" // New<BaseType, ObjectType>()
24 #include "mirtk/ImageWriter.h"
25 
26 #include "mirtk/Array.h"
27 #include "mirtk/UnorderedMap.h"
28 
29 
30 
31 namespace mirtk {
32 
33 
34 /**
35  * Factory for instantiation of image writers
36  */
38 {
39  // ---------------------------------------------------------------------------
40  // Types
41 public:
42 
43  /// Type of object creator
44  typedef ImageWriter *(*ImageWriterCreator)();
45 
46  // ---------------------------------------------------------------------------
47  // Singleton
48 private:
49 
50  /// Constructor
52 
53  /// Destructor
55 
56  /// Copy constructor. Intentionally not implemented.
58 
59  /// Assignment operator. Intentionally not implemented.
60  void operator =(const ImageWriterFactory &);
61 
62 public:
63 
64  /// Singleton instance
65  /// \attention This function is not thread-safe!
66  static ImageWriterFactory &Instance();
67 
68 
69 private:
70 
71  /// Type of associative map
72  typedef UnorderedMap<string, ImageWriterCreator> Associations;
73 
74  /// Associates file name extensions with image writer creators
75  Associations _Associations;
76 
77 public:
78 
79  /// Register new image writer
80  ///
81  /// \param[in] exts File name extensions.
82  /// \param[in] creator Image writer instantiation function.
83  bool Register(const Array<string> &exts, ImageWriterCreator creator);
84 
85  /// Construct new image writer for given output image file name
86  ///
87  /// \returns Image writer which is able to write an image in the format
88  /// corresponding to the given file name extension or nullptr
89  /// when no such writer is registered.
90  ImageWriter *New(const char *fname) const;
91 
92 };
93 
94 // -----------------------------------------------------------------------------
95 /// Register image reader with factory singleton
96 #define mirtkRegisterImageWriterMacro(type) \
97  mirtk::ImageWriterFactory::Instance() \
98  .Register(type::Extensions(), mirtk::New<mirtk::ImageWriter, type>)
99 
100 // -----------------------------------------------------------------------------
101 /// Register image reader with factory singleton at static initialization time
102 #ifdef MIRTK_AUTO_REGISTER
103  #define mirtkAutoRegisterImageWriterMacro(type) \
104  namespace { \
105  static auto _##type##Registered = \
106  mirtk::ImageWriterFactory::Instance() \
107  .Register(type::Extensions(), mirtk::New<mirtk::ImageWriter, type>); \
108  }
109 #else // MIRTK_AUTO_REGISTER
110  #define mirtkAutoRegisterImageWriterMacro(type)
111 #endif // MIRTK_AUTO_REGISTER
112 
113 
114 
115 } // namespace mirtk
116 
117 #endif // MIRTK_ImageWriterFactory_H
static ImageWriterFactory & Instance()
Definition: IOConfig.h:41
bool Register(const Array< string > &exts, ImageWriterCreator creator)
ImageWriter * New(const char *fname) const
ImageWriter *(* ImageWriterCreator)()
Type of object creator.