STIR 6.4.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
24START_NAMESPACE_STIR
25
26class FileSignature;
27
29
41template <class DataT>
43{
44public:
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.
48 typedef InputFileFormat<DataT> Factory;
49 typedef shared_ptr<Factory> FactorySPtr;
50 typedef InputFileFormatRegistry<DataT> self_type;
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
106private:
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
139template <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);
147 InputFileFormatRegistry<data_type>::default_sptr()->add_to_registry(format_sptr, ranking);
148 }
149
153 {
154 Format format;
155 InputFileFormatRegistry<data_type>::default_sptr()->remove_from_registry(format);
156 }
157};
158
159END_NAMESPACE_STIR
160
161//#include "stir/IO/InputFileFormatRegistry.inl"
162
163#endif
Declaration of class stir::InputFileFormat.
A class to read/store the file signature.
Definition FileSignature.h:35
A class for registering (and finding) all input file formats.
Definition InputFileFormatRegistry.h:43
void list_registered_names(std::ostream &stream) const
List all possible registered names to the stream.
Factory const & find_factory(std::istream &input) const
Find a factory that can handle a particular stream.
Factory const & find_factory(const FileSignature &signature, std::istream &input) const
Find a factory that can handle a particular stream.
void remove_from_registry(const Factory &factory)
Remove a pair from the registry.
static shared_ptr< self_type > & default_sptr()
A function to return the default registry.
Factory const & find_factory(const FileSignature &signature, const std::string &filename) const
Find a factory that can handle a particular filename.
void add_to_registry(FactorySPtr const &factory, const unsigned ranking)
Add a file-format to the registry with given ranking Ranking 0 is the 'highest', so will be found fir...
Factory const & find_factory(const std::string &filename) const
Find a factory that can handle a particular filename.
InputFileFormatRegistry()
Default constructor without defaults (see find_factory())
Definition InputFileFormatRegistry.h:64
Base-class for file-formats for reading.
Definition InputFileFormat.h:40
Import of std::shared_ptr, std::dynamic_pointer_cast and std::static_pointer_cast into the stir names...
~RegisterInputFileFormat()
Destructor remove the format from the registry.
Definition InputFileFormatRegistry.h:152
RegisterInputFileFormat(const unsigned ranking)
constructor adds the Format to the registry with the given ranking.
Definition InputFileFormatRegistry.h:144