aimsdata  5.0.5
Neuroimaging data handling
defaultItemW.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 /*
35  * Default item writer class
36  */
37 #ifndef AIMS_IO_DEFAULTITEMW_H
38 #define AIMS_IO_DEFAULTITEMW_H
39 
40 
42 #include <aims/def/general.h>
43 #include <aims/vector/vector.h>
44 #include <aims/rgb/rgb.h>
45 #include <aims/io/itemW.h>
46 
47 
48 namespace aims
49 {
50 
57  template <class T>
59  {
60  public:
62  virtual ~DefaultItemWriter() { }
63 
64  virtual void write( std::ostream & os, const T & item ) const
65  { write( os, &item, 1 ); }
66  virtual void write( std::ostream & os, const T* pitem, size_t n ) const;
67  virtual ItemWriter<T>* writer( const std::string & openmode = "binar",
68  bool bswap = false ) const;
69  };
70 
71 
72  template <class T>
74  {
75  public:
78 
79  virtual void write( std::ostream & os, const T& item ) const;
80  virtual void write( std::ostream & os, const T* pitem, size_t n ) const;
81  };
82 
83 
84  template <class T>
86  {
87  public:
90 
91  virtual void write( std::ostream & os, const T& item ) const
92  { write( os, &item, 1 ); }
93  virtual void write( std::ostream & os, const T* pitem, size_t n ) const;
94  };
95 
96 
97 #ifndef DOXYGEN_HIDE_INTERNAL_CLASSES
98 
100  template <class T, int D>
102  : public ItemWriter<AimsVector<T,D> >
103  {
104  public:
106  virtual ~DefaultItemWriter() { }
107 
108  virtual void write( std::ostream & os,
109  const AimsVector<T,D> & item ) const
110  { write( os, &item, 1 ); }
111  virtual void write( std::ostream & os, const AimsVector<T,D> * pitem,
112  size_t n ) const;
113  virtual ItemWriter<AimsVector<T,D> >* writer( const std::string & openmode
114  = "binar",
115  bool bswap = false ) const;
116  };
117 
118 
120  template <class T, int D>
122  : public DefaultItemWriter<AimsVector<T,D> >
123  {
124  public:
127 
128  virtual void write( std::ostream & os,
129  const AimsVector<T,D> & item ) const
130  { write( os, &item, 1 ); }
131  virtual void write( std::ostream & os, const AimsVector<T,D> * pitem,
132  size_t n ) const;
133  };
134 
135 #endif
136 
137 
138  template<class T> ItemWriter<T>*
139  DefaultItemWriter<T>::writer( const std::string & openmode,
140  bool bswap ) const
141  {
142  if( openmode == "ascii" )
143  return( new DefaultAsciiItemWriter<T> );
144  if( bswap )
145  return( new DefaultBSwapItemWriter<T> );
146  return( new DefaultItemWriter<T> );
147  }
148 
149 
150  template <class T> inline void
151  DefaultItemWriter<T>::write( std::ostream & os, const T* pitem,
152  size_t n ) const
153  {
154  os.write( (const char *) pitem, sizeof(T) * n );
155  }
156 
157 
158  template <class T> inline
159  void DefaultAsciiItemWriter<T>::write( std::ostream & os,
160  const T& item ) const
161  {
162  os << item << " ";
163  }
164 
165 
166  template <class T> inline
167  void DefaultAsciiItemWriter<T>::write( std::ostream & os, const T* pitem,
168  size_t n ) const
169  {
170  const T* ptr = pitem;
171  for( size_t i=0; i<n; ++i, ++ptr )
172  os << *ptr << " ";
173  }
174 
175 
176  template <class T> inline
177  void DefaultBSwapItemWriter<T>::write( std::ostream & os, const T* pitem,
178  size_t n ) const
179  {
180  byte *pd = new byte[ n * sizeof( T ) ];
181  const byte *ps = (byte *) pitem;
182  for( size_t k=0; k<n*sizeof(T); k+=sizeof(T) )
183  for( size_t b=0; b<sizeof(T)/2; ++b )
184  pd[k+b] = ps[k+sizeof(T)-1-b];
185  os.write( (const char *) pd, sizeof(T) * n );
186  delete[] pd;
187  }
188 
189 
190  // partial specializations
191 
192  template<class T, int D> ItemWriter<AimsVector<T, D> >*
193  DefaultItemWriter<AimsVector<T, D> >::writer( const std::string & openmode,
194  bool bswap ) const
195  {
196  if( openmode == "ascii" )
198  if( bswap )
200  return( new DefaultItemWriter<AimsVector<T, D> > );
201  }
202 
203 
204  template <class T, int D> inline void
205  DefaultItemWriter<AimsVector<T, D> >::write( std::ostream & os,
206  const AimsVector<T, D>* pitem,
207  size_t n ) const
208  {
210  const AimsVector<T,D> *ptr = pitem;
211 
212  for( size_t p=0; p<n; ++p, ++ptr )
213  ir.write( os, &(*ptr)[0], D );
214  }
215 
216 
217  template <class T, int D> inline void
218  DefaultBSwapItemWriter<AimsVector<T, D> >::write( std::ostream & os,
219  const AimsVector<T, D>
220  * pitem,
221  size_t n ) const
222  {
224  const AimsVector<T,D> *ptr = pitem;
225 
226  for( size_t p=0; p<n; ++p, ++ptr )
227  ir.write( os, &(*ptr)[0], D );
228  }
229 
230 
231  // specializations
232 
233  template <> inline void
234  DefaultItemWriter<Void>::write( std::ostream &, const Void*, size_t ) const
235  {
236  }
237 
238 
239  template <> inline void
240  DefaultAsciiItemWriter<Void>::write( std::ostream & , const Void*,
241  size_t ) const
242  {
243  }
244 
245 
246  template <> inline void
247  DefaultBSwapItemWriter<Void>::write( std::ostream &, const Void*,
248  size_t ) const
249  {
250  }
251 
252 
253  template <> inline void
255  const int8_t* pitem, size_t n ) const
256  {
257  os.write( (const char *) pitem, n );
258  }
259 
260 
261  template <> inline void
263  const uint8_t* pitem,
264  size_t n ) const
265  {
266  os.write( (const char *) pitem, n );
267  }
268 
269 
270  template <> inline void
271  DefaultBSwapItemWriter<bool>::write( std::ostream & os, const bool* pitem,
272  size_t n ) const
273  {
274  os.write( (const char *) pitem, n );
275  }
276 
277 
278  template <> inline void
280  const cfloat* pitem, size_t n ) const
281  {
282  byte *pd = new byte[ n * sizeof( cfloat ) ];
283  const byte *ps = (byte*) pitem;
284  for( size_t k=0; k<n*sizeof(cfloat); k+=sizeof(float) )
285  for( size_t b=0; b<sizeof(float)/2; ++b )
286  pd[k+b] = ps[k+sizeof(float)-1-b];
287  os.write( (const char *) pd, n );
288  delete[] pd;
289  }
290 
291 
292  template <> inline void
294  const cdouble* pitem,
295  size_t n ) const
296  {
297  byte *pd = new byte[ n * sizeof( cdouble ) ];
298  const byte *ps = (byte*) pitem;
299  for( size_t k=0; k<n*sizeof(cdouble); k+=sizeof(float) )
300  for( size_t b=0; b<sizeof(double)/2; ++b )
301  pd[k+b] = ps[k+sizeof(double)-1-b];
302  os.write( (const char *) pd, n );
303  delete[] pd;
304  }
305 
306 
307  // ### remove after everything has been moved to intN_t/uintN_t
308 #if !defined(__sun__) || !defined(_CHAR_IS_SIGNED)
309  template <> inline void
311  const char* pitem,
312  size_t n ) const
313  {
314  os.write( (const char *) pitem, n );
315  }
316 #endif
317 
318 
319 }
320 
321 
322 #endif
Default low-levels writers.
Definition: defaultItemW.h:58
virtual ItemWriter< T > * writer(const std::string &openmode="binar", bool bswap=false) const
Definition: defaultItemW.h:139
#define AIMSDATA_API
virtual void write(std::ostream &os, const T &item) const
Definition: defaultItemW.h:159
The class for EcatSino data write operation.
Definition: border.h:44
std::complex< float > cfloat
Low-level "small item" writer, used by higher-level file readers.
Definition: itemW.h:50
virtual void write(std::ostream &os, const AimsVector< T, D > &item) const
Definition: defaultItemW.h:128
virtual void write(std::ostream &os, const T &item) const
Definition: defaultItemW.h:91
virtual ~DefaultItemWriter()
Definition: defaultItemW.h:62
virtual void write(std::ostream &os, const AimsVector< T, D > &item) const
Definition: defaultItemW.h:108
virtual void write(std::ostream &os, const T &item) const
Definition: defaultItemW.h:64
std::complex< double > cdouble
uint8_t byte