soma-io  5.1.2
itemwriter.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 SOMAIO_WRITER_ITEMWRITER_H
35 #define SOMAIO_WRITER_ITEMWRITER_H
36 //--- soma-io ----------------------------------------------------------------
40 //----------------------------------------------------------------------------
41 #include <iostream>
42 
43 namespace soma
44 {
45 
48  template<typename T>
49  class ItemWriter
50  {
51  public:
52  virtual ~ItemWriter() {}
53 
55  virtual ItemWriter<T>* writer( bool binary = true,
56  bool bswap = false ) const = 0;
57 
58  bool write( DataSource & ds, const T & item ) const
59  { return write( ds, &item, 1 ) == 1; }
60  virtual long write( DataSource & ds, const T* pitem,
61  size_t n ) const = 0;
62  };
63 
64 
70  template <typename T>
71  class DefaultItemWriter : public ItemWriter<T>
72  {
73  public:
75  virtual ~DefaultItemWriter() { }
76 
77  virtual long write( DataSource & ds, const T* pitem, size_t n ) const;
78  virtual ItemWriter<T>* writer( bool binary = true,
79  bool bswap = false ) const;
80  };
81 
82 
83  template <typename T>
85  {
86  public:
89 
90  virtual long write( DataSource & ds, const T* pitem, size_t n ) const;
91  };
92 
93 
94  template <typename T>
96  {
97  public:
100 
101  virtual long write( DataSource & ds, const T* pitem, size_t n ) const;
102  };
103 
104 
105  template<typename T> ItemWriter<T>*
107  bool bswap ) const
108  {
109  if( !binary )
110  return( new DefaultAsciiItemWriter<T> );
111  if( bswap )
112  return( new DefaultBSwapItemWriter<T> );
113  return( new DefaultItemWriter<T> );
114  }
115 
116 
117  template <typename T> inline long
118  DefaultItemWriter<T>::write( DataSource & ds, const T* pitem,
119  size_t n ) const
120  {
121  long len = ds.writeBlock( (const char *) pitem, sizeof(T) * n );
122  return len / sizeof( T );
123  }
124 
125 
126  template <typename T> inline
127  long DefaultAsciiItemWriter<T>::write( DataSource & ds, const T* pitem,
128  size_t n ) const
129  {
130  const T* ptr = pitem;
131  size_t i;
132  for( i=0; i<n && ds.isOpen(); ++i, ++ptr )
133  ds << *ptr << " ";
134  return (long) i;
135  }
136 
137 
138  template <typename T> inline
139  long DefaultBSwapItemWriter<T>::write( DataSource & ds, const T* pitem,
140  size_t n ) const
141  {
142  std::vector<uint8_t> pd( n * sizeof(T) );
143  const uint8_t *ps = (uint8_t *) pitem;
144  for( size_t k=0; k<n*sizeof(T); k+=sizeof(T) ) {
145  for( size_t b=0; b<sizeof(T)/2; ++b ) {
146  pd[k+b] = ps[k+sizeof(T)-1-b];
147  pd[k+sizeof(T)-1-b] = ps[k+b];
148  }
149  }
150  return ds.writeBlock( (const char *) &pd[0], sizeof(T) * n ) / sizeof(T);
151  }
152 
153 
154  // specializations
155 
156  template <> inline long
157  DefaultItemWriter<Void>::write( DataSource &, const Void*, size_t n ) const
158  {
159  return (long) n;
160  }
161 
162 
163  template <> inline long
165  size_t n ) const
166  {
167  return (long) n;
168  }
169 
170 
171  template <> inline long
173  size_t n ) const
174  {
175  return (long) n;
176  }
177 
178 }
179 
180 
181 #endif
Abstraction layer for various data sources (file, buffer, socket...).
Definition: datasource.h:65
virtual long writeBlock(const char *data, unsigned long len)=0
virtual bool isOpen() const =0
virtual long write(DataSource &ds, const T *pitem, size_t n) const
Definition: itemwriter.h:127
virtual long write(DataSource &ds, const T *pitem, size_t n) const
Definition: itemwriter.h:139
Default low-levels writers.
Definition: itemwriter.h:72
virtual ItemWriter< T > * writer(bool binary=true, bool bswap=false) const
Factory function.
Definition: itemwriter.h:106
virtual ~DefaultItemWriter()
Definition: itemwriter.h:75
virtual long write(DataSource &ds, const T *pitem, size_t n) const
Definition: itemwriter.h:118
Low-level "small item" writer, used by higher-level file writers.
Definition: itemwriter.h:50
virtual ~ItemWriter()
Definition: itemwriter.h:52
virtual ItemWriter< T > * writer(bool binary=true, bool bswap=false) const =0
Factory function.
bool write(DataSource &ds, const T &item) const
Definition: itemwriter.h:58
virtual long write(DataSource &ds, const T *pitem, size_t n) const =0
Definition: allocator.h:49