aimsdata  5.1.2
Neuroimaging data handling
fileFormat_d.h
Go to the documentation of this file.
1 /* This software and supporting documentation are distributed by
2  * Institut Federatif de Recherche 49
3  * CEA/NeuroSpin, Batiment 145,
4  * 91191 Gif-sur-Yvette cedex
5  * France
6  *
7  * This software is governed by the CeCILL-B license under
8  * French law and abiding by the rules of distribution of free software.
9  * You can use, modify and/or redistribute the software under the
10  * terms of the CeCILL-B license as circulated by CEA, CNRS
11  * and INRIA at the following URL "http://www.cecill.info".
12  *
13  * As a counterpart to the access to the source code and rights to copy,
14  * modify and redistribute granted by the license, users are provided only
15  * with a limited warranty and the software's author, the holder of the
16  * economic rights, and the successive licensors have only limited
17  * liability.
18  *
19  * In this respect, the user's attention is drawn to the risks associated
20  * with loading, using, modifying and/or developing or reproducing the
21  * software by the user in light of its specific status of free software,
22  * that may mean that it is complicated to manipulate, and that also
23  * therefore means that it is reserved for developers and experienced
24  * professionals having in-depth computer knowledge. Users are therefore
25  * encouraged to load and test the software's suitability as regards their
26  * requirements in conditions enabling the security of their systems and/or
27  * data to be ensured and, more generally, to use and operate it in the
28  * same conditions as regards security.
29  *
30  * The fact that you are presently reading this means that you have had
31  * knowledge of the CeCILL-B license and that you accept its terms.
32  */
33 
34 #ifndef AIMS_IO_FILEFORMAT_D_H
35 #define AIMS_IO_FILEFORMAT_D_H
36 
37 
38 #include <aims/io/fileFormat.h>
39 #include <cartobase/thread/mutex.h>
41 
42 namespace aims
43 {
44 
45  template<typename T>
47  {
48  }
49 
50 
51  template<typename T> T*
52  FileFormat<T>::read( const std::string & filename,
53  const carto::AllocatorContext & context,
54  carto::Object options )
55  {
56  T *object = new T;
57  try
58  {
59  if( read( filename, *object, context, options ) )
60  return object;
61  }
62  catch( std::exception & )
63  {
64  delete object;
65  throw;
66  }
67  delete object;
68  return 0;
69  }
70 
71 
72  template <typename T>
73  bool FileFormat<T>::write( const std::string &, const T &, carto::Object )
74  {
75  return false;
76  }
77 
78 
79  template<class T> std::map<std::string, FileFormat<T>*>
81  {
82  static std::map<std::string, FileFormat<T>*> form;
83  return form;
84  }
85 
86  template<class T> std::map<std::string, std::list<std::string> >
88  {
89  static std::map<std::string, std::list<std::string> > ext;
90  return ext;
91  }
92 
93 
94  template<class T>
95  carto::Mutex & FileFormatDictionary<T>::mutex()
96  {
97  // Must be initialized (generally in main thread) before using concurrently
98  static carto::Mutex mutex( carto::Mutex::Recursive );
99  return mutex;
100  }
101 
102 
103  template<class T>
104  const std::map<std::string, std::list<std::string> > &
106  {
107  init();
108  return _extensions();
109  }
110 
111  template<class T>
113  {
114  static bool initialized = false;
115  if( !initialized )
116  {
117  initialized = true;
120  dtc.dataType(), &formats );
121  registerBaseFormats();
122  }
123  }
124 
125  template<class T> std::string
126  FileFormatDictionary<T>::fileExtension( const std::string & filename )
127  {
128  return( carto::FileUtil::extension(filename) );
129  }
130 
131  template<class T> void
132  FileFormatDictionary<T>::registerFormat( const std::string & format,
133  FileFormat<T>* formatObj,
134  const std::vector<std::string>
135  & extensions,
136  const std::string & before )
137  {
138  init();
139 
140  FileFormat<T> *oldr = fileFormat( format );
141  delete oldr;
142  _formats()[ format ] = formatObj;
143 
144  std::vector<std::string>::const_iterator ie, ee = extensions.end();
145  std::list<std::string>::iterator ie2, ee2;
146  for( ie=extensions.begin(); ie!=ee; ++ie )
147  {
148  std::list<std::string> & ext = _extensions()[ *ie ];
149  if( before.empty() )
150  ext.push_back( format );
151  else
152  {
153  for( ie2=ext.begin(), ee2=ext.end(); ie2!=ee2; ++ie2 )
154  if( *ie2 == before )
155  break;
156  ext.insert( ie2, format );
157  }
158  }
159  }
160 
161 
162  template<class T> void
163  FileFormatDictionary<T>::unregisterFormat( const std::string & format )
164  {
165  typename std::map<std::string, FileFormat<T>*>::iterator
166  ir = _formats().find( format);
167 
168  if( ir != _formats().end() )
169  _formats().erase( ir );
170 
171  std::map<std::string, std::list<std::string> >::iterator
172  ie = _extensions().begin(), je, ee = _extensions().end();
173  std::list<std::string>::iterator il, jl, el;
174 
175  while( ie != ee )
176  {
177  je = ie;
178  ++ie;
179  il = je->second.begin();
180  el = je->second.end();
181  while( il != el )
182  {
183  jl = il;
184  ++il;
185 
186  if( (il != el) && (*il == format ) )
187  {
188  je->second.erase( il );
189  el = je->second.end();
190  if( je->second.empty() )
191  _extensions().erase( je );
192  }
193  }
194  }
195  }
196 
197 
198  template<class T> FileFormat<T> *
199  FileFormatDictionary<T>::fileFormat( const std::string & format )
200  {
201  init();
202 
203  typename std::map<std::string, FileFormat<T>*>::const_iterator i
204  = _formats().find( format );
205  if( i == _formats().end() )
206  return( 0 );
207  return( (*i).second );
208  }
209 
210 
211  template<class T> std::set<std::string> FileFormatDictionary<T>::formats()
212  {
213  std::set<std::string> f;
214  typename std::map<std::string, FileFormat<T>*>::const_iterator
215  i, e = _formats().end();
216  for( i=_formats().begin(); i!=e; ++i )
217  f.insert( i->first );
218  return( f );
219  }
220 
221 }
222 
223 
224 #endif
Formats holder for each format of Aims object, used by Reader and Writer.
Definition: fileFormat.h:140
static FileFormat< T > * fileFormat(const std::string &format)
Definition: fileFormat_d.h:199
static std::set< std::string > formats()
Definition: fileFormat_d.h:211
static std::string fileExtension(const std::string &filename)
Definition: fileFormat_d.h:126
static const std::map< std::string, std::list< std::string > > & extensions()
Definition: fileFormat_d.h:105
static void registerFormat(const std::string &formatID, FileFormat< T > *format, const std::vector< std::string > &extensions, const std::string &before="")
Definition: fileFormat_d.h:132
static void unregisterFormat(const std::string &formatID)
Definition: fileFormat_d.h:163
Low-level object IO format: each specific format has such a reader / writer.
Definition: fileFormat.h:62
virtual bool write(const std::string &, const T &, carto::Object options=carto::none())
the base class has a default implementation which only returns false so if your new format only suppo...
Definition: fileFormat_d.h:73
virtual bool read(const std::string &filename, T &obj, const carto::AllocatorContext &context, carto::Object options)=0
virtual ~FileFormat()
Definition: fileFormat_d.h:46
static void registerType(const std::string &objtype, const std::string &datatype, FormatInfo info)
std::string objectType()
std::string dataType()
static std::string extension(const std::string &)
The class for EcatSino data write operation.
Definition: borderfiller.h:13