STIR  6.2.0
InputFileFormatRegistry.h
Go to the documentation of this file.
1 #ifndef __stir_IO_InputFileFormatRegistry_h__
2 #define __stir_IO_InputFileFormatRegistry_h__
3 /*
4  Copyright (C) 2006-2008, Hammersmith Imanet Ltd
5  This file is part of STIR.
6  SPDX-License-Identifier: Apache-2.0
7 
8  See STIR/LICENSE.txt for details
9 */
18 #include "stir/shared_ptr.h"
19 #include <map>
20 #include <fstream>
21 #include <string>
22 #include <memory>
23 
24 START_NAMESPACE_STIR
25 
26 class FileSignature;
27 
29 
41 template <class DataT>
43 {
44 public:
45  typedef DataT data_type;
46  // In the future, maybe we'll have a factory type, and let InputFileFormat be derived from it
47  // So we will use a typedef to prepare for that.
49  typedef shared_ptr<Factory> FactorySPtr;
51 
53 
61  static shared_ptr<self_type>& default_sptr();
62 
65 
66  inline ~InputFileFormatRegistry() {}
67 
71  void add_to_registry(FactorySPtr const& factory, const unsigned ranking);
72 
74  void remove_from_registry(const Factory& factory);
75 
77 
84  Factory const& find_factory(const FileSignature& signature, std::istream& input) const;
85 
87 
94  Factory const& find_factory(const FileSignature& signature, const std::string& filename) const;
95 
97  Factory const& find_factory(const std::string& filename) const;
98 
100  Factory const& find_factory(std::istream& input) const;
101 
103 
104  void list_registered_names(std::ostream& stream) const;
105 
106 private:
107  typedef typename std::multimap<unsigned, FactorySPtr> _registry_type;
108  typedef typename _registry_type::const_iterator const_iterator;
109  typedef typename _registry_type::iterator iterator;
110 
111  _registry_type _registry;
112 
113  bool _valid(const_iterator iter) const { return iter != this->_registry.end(); }
114 
115  // File can be either string or istream
116  template <class File>
117  inline const_iterator _actual_find_factory(const FileSignature& signature, File& input) const
118  {
119  const_iterator iter = this->_registry.begin();
120  const_iterator const end = this->_registry.end();
121  while (iter != end)
122  {
123  if (iter->second->can_read(signature, input))
124  return iter;
125  ++iter;
126  }
127  return end;
128  }
129 };
130 
132 
139 template <class Format>
141 {
142  typedef typename Format::data_type data_type;
144  explicit RegisterInputFileFormat(const unsigned ranking)
145  {
146  shared_ptr<InputFileFormat<data_type>> format_sptr(new Format);
148  }
149 
153  {
154  Format format;
156  }
157 };
158 
159 END_NAMESPACE_STIR
160 
161 //#include "stir/IO/InputFileFormatRegistry.inl"
162 
163 #endif
A class to read/store the file signature.
Definition: FileSignature.h:34
InputFileFormatRegistry()
Default constructor without defaults (see find_factory())
Definition: InputFileFormatRegistry.h:64
Declaration of class stir::InputFileFormat.
RegisterInputFileFormat(const unsigned ranking)
constructor adds the Format to the registry with the given ranking.
Definition: InputFileFormatRegistry.h:144
void add_to_registry(FactorySPtr const &factory, const unsigned ranking)
Add a file-format to the registry with given ranking Ranking 0 is the &#39;highest&#39;, so will be found fir...
Import of std::shared_ptr, std::dynamic_pointer_cast and std::static_pointer_cast (or corresponding b...
~RegisterInputFileFormat()
Destructor remove the format from the registry.
Definition: InputFileFormatRegistry.h:152
Base-class for file-formats for reading.
Definition: InputFileFormat.h:39
A helper class to allow automatic registration to the default InputFileFormatRegistry.
Definition: InputFileFormatRegistry.h:140
void remove_from_registry(const Factory &factory)
Remove a pair from the registry.
A class for registering (and finding) all input file formats.
Definition: InputFileFormatRegistry.h:42