vsl_block_binary.h
Go to the documentation of this file.
1 // This is core/vsl/vsl_block_binary.h
2 #ifndef vsl_block_binary_io_h_
3 #define vsl_block_binary_io_h_
4 //:
5 // \file
6 // \brief Set of functions to do binary IO on a block of values.
7 // \author Ian Scott, ISBE Manchester, Feb 2003
8 
9 #include <vsl/vsl_binary_io.h>
11 
12 //: Error checking.
14 
15 //: Write a block of values to a vsl_b_ostream, potentially very efficiently for fundamental types.
16 template <class T>
17 inline void vsl_block_binary_write(vsl_b_ostream &os, const T* begin, std::size_t nelems);
18 
19 //: Read a block of values from a vsl_b_ostream, potentially very efficiently for fundamental types.
20 template <class T>
21 inline void vsl_block_binary_read(vsl_b_istream &is, T* begin, std::size_t nelems);
22 
23 
24 /////////////////////////////////////////////////////////////////////////
25 
26 // Internal implementation
27 template <class T>
28  void vsl_block_binary_write_float_impl(vsl_b_ostream &os, const T* begin, std::size_t nelems);
29 
30 // Internal implementation
31 template <class T>
32  void vsl_block_binary_read_float_impl(vsl_b_istream &is, T* begin, std::size_t nelems);
33 
34 
35 /////////////////////////////////////////////////////////////////////////
36 //: Write a block of doubles to a vsl_b_ostream
37 // This function is very speed efficient, but
38 // temporarily allocates a block of memory the size of the
39 // block being read.
40 template <>
41 inline void vsl_block_binary_write(vsl_b_ostream &os, const double* begin, std::size_t nelems)
42 {
43  vsl_block_binary_write_float_impl(os, begin, nelems);
44 }
45 
46 //: Read a block of doubles from a vsl_b_istream
47 // This function is very speed efficient.
48 template <>
49 inline void vsl_block_binary_read(vsl_b_istream &is, double* begin, std::size_t nelems)
50 {
51  vsl_block_binary_read_float_impl(is, begin, nelems);
52 }
53 
54 /////////////////////////////////////////////////////////////////////////
55 
56 //: Write a block of floats to a vsl_b_ostream
57 // This function is very speed efficient, but
58 // temporarily allocates a block of memory the size of the
59 // block being read.
60 template <>
61 inline void vsl_block_binary_write(vsl_b_ostream &os, const float* begin, std::size_t nelems)
62 {
63  vsl_block_binary_write_float_impl(os, begin, nelems);
64 }
65 
66 //: Read a block of floats from a vsl_b_istream
67 // This function is very speed efficient.
68 template <>
69 inline void vsl_block_binary_read(vsl_b_istream &is, float* begin, std::size_t nelems)
70 {
71  vsl_block_binary_read_float_impl(is, begin, nelems);
72 }
73 
74 /////////////////////////////////////////////////////////////////////////
75 
76 // Internal implementation
77 template <class T>
78  void vsl_block_binary_write_int_impl(vsl_b_ostream &os, const T* begin, std::size_t nelems);
79 
80 // Internal implementation
81 template <class T>
82  void vsl_block_binary_read_int_impl(vsl_b_istream &is, T* begin, std::size_t nelems);
83 
84 /////////////////////////////////////////////////////////////////////////
85 
86 //: Write a block of signed ints to a vsl_b_ostream
87 // This function is very speed efficient, but
88 // temporarily allocates a block of memory the about 1.2 times
89 // size of the block being read.
90 template <>
91 inline void vsl_block_binary_write(vsl_b_ostream &os, const int* begin, std::size_t nelems)
92 {
93  vsl_block_binary_write_int_impl(os, begin, nelems);
94 }
95 
96 //: Read a block of signed ints from a vsl_b_istream
97 // This function is very speed efficient, but
98 // temporarily allocates a block of memory the about 1.2 times
99 // size of the block being read.
100 template <>
101 inline void vsl_block_binary_read(vsl_b_istream &is, int* begin, std::size_t nelems)
102 {
103  vsl_block_binary_read_int_impl(is, begin, nelems);
104 }
105 
106 /////////////////////////////////////////////////////////////////////////
107 
108 //: Write a block of unsigned ints to a vsl_b_ostream
109 // This function is very speed efficient, but
110 // temporarily allocates a block of memory the about 1.2 times
111 // size of the block being read.
112 template <>
113 inline void vsl_block_binary_write(vsl_b_ostream &os, const unsigned int* begin, std::size_t nelems)
114 {
115  vsl_block_binary_write_int_impl(os, begin, nelems);
116 }
117 
118 //: Read a block of unsigned ints from a vsl_b_istream
119 // This function is very speed efficient, but
120 // temporarily allocates a block of memory the about 1.2 times
121 // size of the block being read.
122 template <>
123 inline void vsl_block_binary_read(vsl_b_istream &is, unsigned int* begin, std::size_t nelems)
124 {
125  vsl_block_binary_read_int_impl(is, begin, nelems);
126 }
127 
128 
129 /////////////////////////////////////////////////////////////////////////
130 
131 //: Write a block of signed shorts to a vsl_b_ostream
132 // This function is very speed efficient, but
133 // temporarily allocates a block of memory the about 1.2 times
134 // size of the block being read.
135 template <>
136 inline void vsl_block_binary_write(vsl_b_ostream &os, const short* begin, std::size_t nelems)
137 {
138  vsl_block_binary_write_int_impl(os, begin, nelems);
139 }
140 
141 //: Read a block of signed shorts from a vsl_b_istream
142 // This function is very speed efficient, but
143 // temporarily allocates a block of memory the about 1.2 times
144 // size of the block being read.
145 template <>
146 inline void vsl_block_binary_read(vsl_b_istream &is, short* begin, std::size_t nelems)
147 {
148  vsl_block_binary_read_int_impl(is, begin, nelems);
149 }
150 
151 
152 /////////////////////////////////////////////////////////////////////////
153 
154 //: Write a block of unsigned shorts to a vsl_b_ostream
155 // This function is very speed efficient, but
156 // temporarily allocates a block of memory the about 1.2 times
157 // size of the block being read.
158 template <>
159 inline void vsl_block_binary_write(vsl_b_ostream &os, const unsigned short* begin, std::size_t nelems)
160 {
161  vsl_block_binary_write_int_impl(os, begin, nelems);
162 }
163 
164 //: Read a block of unsigned shorts from a vsl_b_istream
165 // This function is very speed efficient, but
166 // temporarily allocates a block of memory the about 1.2 times
167 // size of the block being read.
168 template <>
169 inline void vsl_block_binary_read(vsl_b_istream &is, unsigned short* begin, std::size_t nelems)
170 {
171  vsl_block_binary_read_int_impl(is, begin, nelems);
172 }
173 
174 
175 /////////////////////////////////////////////////////////////////////////
176 
177 //: Write a block of signed longs to a vsl_b_ostream
178 // This function is very speed efficient, but
179 // temporarily allocates a block of memory the about 1.2 times
180 // size of the block being read.
181 template <>
182 inline void vsl_block_binary_write(vsl_b_ostream &os, const long* begin, std::size_t nelems)
183 {
184  vsl_block_binary_write_int_impl(os, begin, nelems);
185 }
186 
187 //: Read a block of signed longs from a vsl_b_istream
188 // This function is very speed efficient, but
189 // temporarily allocates a block of memory the about 1.2 times
190 // size of the block being read.
191 template <>
192 inline void vsl_block_binary_read(vsl_b_istream &is, long* begin, std::size_t nelems)
193 {
194  vsl_block_binary_read_int_impl(is, begin, nelems);
195 }
196 
197 
198 /////////////////////////////////////////////////////////////////////////
199 
200 //: Write a block of unsigned longs to a vsl_b_ostream
201 // This function is very speed efficient, but
202 // temporarily allocates a block of memory the about 1.2 times
203 // size of the block being read.
204 template <>
205 inline void vsl_block_binary_write(vsl_b_ostream &os, const unsigned long* begin, std::size_t nelems)
206 {
207  vsl_block_binary_write_int_impl(os, begin, nelems);
208 }
209 
210 //: Read a block of unsigned longs from a vsl_b_istream
211 // This function is very speed efficient, but
212 // temporarily allocates a block of memory the about 1.2 times
213 // size of the block being read.
214 template <>
215 inline void vsl_block_binary_read(vsl_b_istream &is, unsigned long* begin, std::size_t nelems)
216 {
217  vsl_block_binary_read_int_impl(is, begin, nelems);
218 }
219 
220 /////////////////////////////////////////////////////////////////////////
221 #if VXL_HAS_INT_64 && !VXL_INT_64_IS_LONG
222 
223 //: Write a block of signed longs to a vsl_b_ostream
224 // This function is very speed efficient, but
225 // temporarily allocates a block of memory the about 1.2 times
226 // size of the block being read.
227 template <>
228 inline void vsl_block_binary_write(vsl_b_ostream &os, const vxl_int_64* begin, std::size_t nelems)
229 {
230  vsl_block_binary_write_int_impl(os, begin, nelems);
231 }
232 
233 //: Read a block of signed longs from a vsl_b_istream
234 // This function is very speed efficient, but
235 // temporarily allocates a block of memory the about 1.2 times
236 // size of the block being read.
237 template <>
238 inline void vsl_block_binary_read(vsl_b_istream &is, vxl_int_64* begin, std::size_t nelems)
239 {
240  vsl_block_binary_read_int_impl(is, begin, nelems);
241 }
242 
243 
244 /////////////////////////////////////////////////////////////////////////
245 
246 //: Write a block of unsigned longs to a vsl_b_ostream
247 // This function is very speed efficient, but
248 // temporarily allocates a block of memory the about 1.2 times
249 // size of the block being read.
250 template <>
251 inline void vsl_block_binary_write(vsl_b_ostream &os, const vxl_uint_64* begin, std::size_t nelems)
252 {
253  vsl_block_binary_write_int_impl(os, begin, nelems);
254 }
255 
256 //: Read a block of unsigned longs from a vsl_b_istream
257 // This function is very speed efficient, but
258 // temporarily allocates a block of memory the about 1.2 times
259 // size of the block being read.
260 template <>
261 inline void vsl_block_binary_read(vsl_b_istream &is, vxl_uint_64* begin, std::size_t nelems)
262 {
263  vsl_block_binary_read_int_impl(is, begin, nelems);
264 }
265 #endif //VXL_HAS_INT_64 && !VXL_INT_64_IS_LONG
266 
267 /////////////////////////////////////////////////////////////////////////
268 
269 // Internal implementation
270 template <class T>
271  void vsl_block_binary_write_byte_impl(vsl_b_ostream &os, const T* begin, std::size_t nelems);
272 
273 // Internal implementation
274 template <class T>
275  void vsl_block_binary_read_byte_impl(vsl_b_istream &is, T* begin, std::size_t nelems);
276 
277  /////////////////////////////////////////////////////////////////////////
278 
279 //: Write a block of unsigned chars to a vsl_b_ostream
280 // This function is very speed and space efficient.
281 template <>
282 inline void vsl_block_binary_write(vsl_b_ostream &os, const unsigned char* begin, std::size_t nelems)
283 {
284  vsl_block_binary_write_byte_impl(os, begin, nelems);
285 }
286 
287 //: Read a block of unsigned chars from a vsl_b_istream
288 // This function is very speed and space efficient.
289 template <>
290 inline void vsl_block_binary_read(vsl_b_istream &is, unsigned char* begin, std::size_t nelems)
291 {
292  vsl_block_binary_read_byte_impl(is, begin, nelems);
293 }
294 
295 /////////////////////////////////////////////////////////////////////////
296 
297 //: Write a block of signed chars to a vsl_b_ostream
298 // This function is very speed and space efficient.
299 template <>
300 inline void vsl_block_binary_write(vsl_b_ostream &os, const signed char* begin, std::size_t nelems)
301 {
302  vsl_block_binary_write_byte_impl(os, begin, nelems);
303 }
304 
305 //: Read a block of signed chars from a vsl_b_istream
306 // This function is very speed and space efficient.
307 template <>
308 inline void vsl_block_binary_read(vsl_b_istream &is, signed char* begin, std::size_t nelems)
309 {
310  vsl_block_binary_read_byte_impl(is, begin, nelems);
311 }
312 
313 /////////////////////////////////////////////////////////////////////////
314 
315 
316 //: Write a block of values to a vsl_b_ostream
317 // If you want to output a block of fundamental data types very efficiently,
318 // then just #include <vsl_binary_explicit_io.h>
319 template <class T>
320 inline void vsl_block_binary_write(vsl_b_ostream &os, const T* begin, std::size_t nelems)
321 {
322  vsl_b_write(os, false); // Error check that this is a specialised version
323  while (nelems--)
324  vsl_b_write(os, *(begin++));
325 }
326 
327 //: Read a block of values from a vsl_b_istream
328 // If you want to output a block of fundamental data types very efficiently,
329 // then just #include <vsl_binary_explicit_io.h>
330 template <class T>
331 inline void vsl_block_binary_read(vsl_b_istream &is, T* begin, std::size_t nelems)
332 {
334  if (!is) return;
335  while (nelems--)
336  vsl_b_read(is, *(begin++));
337 }
338 
339 #endif // vsl_block_binary_io_h_
A binary output adaptor for any std::ostream.
Definition: vsl_binary_io.h:37
void vsl_b_write(vsl_b_ostream &os, char n)
Write char to vsl_b_ostream.
void vsl_block_binary_write_int_impl(vsl_b_ostream &os, const T *begin, std::size_t nelems)
Write a block of signed ints to a vsl_b_ostream.
void vsl_block_binary_read(vsl_b_istream &is, T *begin, std::size_t nelems)
Read a block of values from a vsl_b_ostream, potentially very efficiently for fundamental types.
Byte-swapping, arbitrary length integer conversion, and explicit I/O.
void vsl_block_binary_write_float_impl(vsl_b_ostream &os, const T *begin, std::size_t nelems)
Write a block of floats to a vsl_b_ostream.
void vsl_block_binary_read_byte_impl(vsl_b_istream &is, T *begin, std::size_t nelems)
Read a block of bytes from a vsl_b_ostream.
void vsl_b_read(vsl_b_istream &is, char &n)
Read char from vsl_b_istream.
void vsl_block_binary_write_byte_impl(vsl_b_ostream &os, const T *begin, std::size_t nelems)
Write a block of bytes to a vsl_b_ostream.
An adaptor for any std::istream to make it suitable for binary input.
void vsl_block_binary_write(vsl_b_ostream &os, const T *begin, std::size_t nelems)
Write a block of values to a vsl_b_ostream, potentially very efficiently for fundamental types.
void vsl_block_binary_read_confirm_specialisation(vsl_b_istream &is, bool specialised)
Error checking.
Set of functions, and objects to perform binary IO.
void vsl_block_binary_read_int_impl(vsl_b_istream &is, T *begin, std::size_t nelems)
Read a block of signed ints from a vsl_b_istream.
void vsl_block_binary_read_float_impl(vsl_b_istream &is, T *begin, std::size_t nelems)
Read a block of floats from a vsl_b_ostream.