cartobase 6.0.6
byte_order.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 CARTOBASE_TYPE_BYTE_ORDER_H
35#define CARTOBASE_TYPE_BYTE_ORDER_H
36
38#include <complex>
39#include <iostream>
40#include <stdexcept>
41#include <string>
42
43namespace carto {
44
45// Actual machine byte order
47
48// String representing the system byte order :
49// "ABCD" for big endian
50// "DCBA" for little endian
51// "CDAB" for pdpEndian
52const char * byteOrderString();
53
54
55// Convert a string representing byte order
56// to the corresponding constant
57int stringToByteOrder( const std::string &bos );
58
59
60// Class to do byte swapping in memory or from a file
62{
63public:
64
65 // Build a converter from any byte order to
66 // actual machine byte order
67 ByteSwapper( int bo = byteOrder() );
68 ByteSwapper( const std::string &bos );
69
70 inline bool isSwapped() const { return _swap; }
71 inline bool setSwapped( bool s ) { _swap = s; return _swap; }
72
73 inline void reorder( signed char &p ) const;
74 inline void reorder( unsigned char &p ) const;
75 inline void reorder( short &p ) const;
76 inline void reorder( unsigned short &p ) const;
77 inline void reorder( int &p ) const;
78 inline void reorder( unsigned int &p ) const;
79 inline void reorder( long &p ) const;
80 inline void reorder( unsigned long &p ) const;
81 inline void reorder( long long &p ) const;
82 inline void reorder( unsigned long long &p ) const;
83 inline void reorder( float &p ) const;
84 inline void reorder( double &p ) const;
85 inline void reorder( long double &p ) const;
86 inline void reorder( std::complex<float> &p ) const;
87 inline void reorder( std::complex<double> &p ) const;
88 inline void reorder( std::complex<long double> &p ) const;
89
90 inline std::istream &read( std::istream &, short &p ) const;
91 inline std::istream &read( std::istream &, unsigned short &p ) const;
92 inline std::istream &read( std::istream &, int &p ) const;
93 inline std::istream &read( std::istream &, unsigned int &p ) const;
94 inline std::istream &read( std::istream &, long &p ) const;
95 inline std::istream &read( std::istream &, unsigned long &p ) const;
96 inline std::istream &read( std::istream &, long long &p ) const;
97 inline std::istream &read( std::istream &, unsigned long long &p ) const;
98 inline std::istream &read( std::istream &, float &p ) const;
99 inline std::istream &read( std::istream &, double &p ) const;
100 inline std::istream &read( std::istream &, long double &p ) const;
101
102 inline std::ostream &write( std::ostream &, short &p ) const;
103 inline std::ostream &write( std::ostream &, unsigned short &p ) const;
104 inline std::ostream &write( std::ostream &, int &p ) const;
105 inline std::ostream &write( std::ostream &, unsigned int &p ) const;
106 inline std::ostream &write( std::ostream &, long &p ) const;
107 inline std::ostream &write( std::ostream &, unsigned long &p ) const;
108 inline std::ostream &write( std::ostream &, long long &p ) const;
109 inline std::ostream &write( std::ostream &, unsigned long long &p ) const;
110 inline std::ostream &write( std::ostream &, float &p ) const;
111 inline std::ostream &write( std::ostream &, double &p ) const;
112 inline std::ostream &write( std::ostream &, long double &p ) const;
113
114private:
115
116 template <unsigned SIZE>
117 static void _doSwap( char * );
118
119 template <unsigned SIZE>
120 static std::istream & _swappedRead( std::istream &, char * );
121
122 template <unsigned SIZE>
123 static std::ostream & _swappedWrite( std::ostream &, char * );
124
125 bool _swap;
126};
127
128
129// Non specialized swapping is forbidden
130template <unsigned SIZE>
131void ByteSwapper::_doSwap( char * )
132{
133 throw std::runtime_error( "Invalid byte order swap size" );
134}
135
136// Non specialized reading is forbidden
137template <unsigned SIZE>
138std::istream &ByteSwapper::_swappedRead( std::istream & s, char * )
139{
140 throw std::runtime_error( "Invalid byte order read size" );
141 return s;
142}
143
144// Non specialized writing is forbidden
145template <unsigned SIZE>
146std::ostream &ByteSwapper::_swappedWrite( std::ostream & s, char * )
147{
148 throw std::runtime_error( "Invalid byte order write size" );
149 return s;
150}
151
152
153template <>
154void ByteSwapper::_doSwap<2>( char *p );
155
156template <>
157void ByteSwapper::_doSwap<4>( char *p );
158
159template <>
160void ByteSwapper::_doSwap<8>( char *p );
161
162template <>
163void ByteSwapper::_doSwap<16>( char *p );
164
165
166template <>
167std::istream & ByteSwapper::_swappedRead<2>( std::istream &, char * );
168
169template <>
170std::istream & ByteSwapper::_swappedRead<4>( std::istream &, char * );
171
172template <>
173std::istream & ByteSwapper::_swappedRead<8>( std::istream &, char * );
174
175template <>
176std::istream & ByteSwapper::_swappedRead<16>( std::istream &, char * );
177
178template <>
179std::ostream &ByteSwapper::_swappedWrite<2>( std::ostream &, char * );
180
181template <>
182std::ostream &ByteSwapper::_swappedWrite<4>( std::ostream &, char * );
183
184template <>
185std::ostream &ByteSwapper::_swappedWrite<8>( std::ostream &, char * );
186
187template <>
188std::ostream &ByteSwapper::_swappedWrite<16>( std::ostream &, char * );
189
190
191
192
193inline void ByteSwapper::reorder( signed char & ) const
194{
195 // Char is 1 byte, nothing to do
196}
197
198inline void ByteSwapper::reorder( unsigned char & ) const
199{
200 // Char is 1 byte, nothing to do
201}
202
203inline void ByteSwapper::reorder( short &p ) const
204{
205 if ( _swap ) _doSwap< sizeof(p) >( reinterpret_cast< char * >( &p ) );
206}
207
208inline void ByteSwapper::reorder( unsigned short &p ) const
209{
210 if ( _swap ) _doSwap< sizeof(p) >( reinterpret_cast< char * >( &p ) );
211}
212
213inline void ByteSwapper::reorder( int &p ) const
214{
215 if ( _swap ) _doSwap< sizeof(p) >( reinterpret_cast< char * >( &p ) );
216}
217
218inline void ByteSwapper::reorder( unsigned int &p ) const
219{
220 if ( _swap ) _doSwap< sizeof(p) >( reinterpret_cast< char * >( &p ) );
221}
222
223inline void ByteSwapper::reorder( long &p ) const
224{
225 if ( _swap ) _doSwap< sizeof(p) >( reinterpret_cast< char * >( &p ) );
226}
227
228inline void ByteSwapper::reorder( unsigned long &p ) const
229{
230 if ( _swap ) _doSwap< sizeof(p) >( reinterpret_cast< char * >( &p ) );
231}
232
233inline void ByteSwapper::reorder( long long &p ) const
234{
235 if ( _swap ) _doSwap< sizeof(p) >( reinterpret_cast< char * >( &p ) );
236}
237
238inline void ByteSwapper::reorder( unsigned long long &p ) const
239{
240 if ( _swap ) _doSwap< sizeof(p) >( reinterpret_cast< char * >( &p ) );
241}
242
243inline void ByteSwapper::reorder( float &p ) const
244{
245 if ( _swap ) _doSwap< sizeof(p) >( reinterpret_cast< char * >( &p ) );
246}
247
248inline void ByteSwapper::reorder( double &p ) const
249{
250 if ( _swap ) _doSwap< sizeof(p) >( reinterpret_cast< char * >( &p ) );
251}
252
253inline void ByteSwapper::reorder( long double &p ) const
254{
255 if ( _swap ) _doSwap< sizeof(p) >( reinterpret_cast< char * >( &p ) );
256}
257
258inline void ByteSwapper::reorder( std::complex<float> &p ) const
259{
260 if ( _swap ) {
261 float real = p.real();
262 reorder(real);
263 p.real(real);
264 float imag = p.imag();
265 reorder(imag);
266 p.imag(imag);
267 }
268}
269
270inline void ByteSwapper::reorder( std::complex<double> &p ) const
271{
272 if ( _swap ) {
273 double real = p.real();
274 reorder(real);
275 p.real(real);
276 double imag = p.imag();
277 reorder(imag);
278 p.imag(imag);
279 }
280}
281
282inline void ByteSwapper::reorder( std::complex<long double> &p ) const
283{
284 if ( _swap ) {
285 long double real = p.real();
286 reorder(real);
287 p.real(real);
288 long double imag = p.imag();
289 reorder(imag);
290 p.imag(imag);
291 }
292}
293
294
295inline std::istream &ByteSwapper::read( std::istream &in,
296 short &p ) const
297{
298 if ( _swap ) _swappedRead< sizeof(p) >( in,
299 reinterpret_cast< char * >( &p ) );
300 else in.read( reinterpret_cast<char *>( &p ), sizeof( p ) );
301 return in;
302}
303
304
305inline std::istream &ByteSwapper::read( std::istream &in,
306 unsigned short &p ) const
307{
308 if ( _swap ) _swappedRead< sizeof(p) >( in,
309 reinterpret_cast< char * >( &p ) );
310 else in.read( reinterpret_cast<char *>( &p ), sizeof( p ) );
311 return in;
312}
313
314
315inline std::istream &ByteSwapper::read( std::istream &in,
316 int &p ) const
317{
318 if ( _swap ) _swappedRead< sizeof(p) >( in,
319 reinterpret_cast< char * >( &p ) );
320 else in.read( reinterpret_cast<char *>( &p ), sizeof( p ) );
321 return in;
322}
323
324
325inline std::istream &ByteSwapper::read( std::istream &in,
326 unsigned int &p ) const
327{
328 if ( _swap ) _swappedRead< sizeof(p) >( in,
329 reinterpret_cast< char * >( &p ) );
330 else in.read( reinterpret_cast<char *>( &p ), sizeof( p ) );
331 return in;
332}
333
334
335inline std::istream &ByteSwapper::read( std::istream &in,
336 long &p ) const
337{
338 if ( _swap ) _swappedRead< sizeof(p) >( in,
339 reinterpret_cast< char * >( &p ) );
340 else in.read( reinterpret_cast<char *>( &p ), sizeof( p ) );
341 return in;
342}
343
344
345inline std::istream &ByteSwapper::read( std::istream &in,
346 unsigned long &p ) const
347{
348 if ( _swap ) _swappedRead< sizeof(p) >( in,
349 reinterpret_cast< char * >( &p ) );
350 else in.read( reinterpret_cast<char *>( &p ), sizeof( p ) );
351 return in;
352}
353
354
355inline std::istream &ByteSwapper::read( std::istream &in,
356 long long &p ) const
357{
358 if ( _swap ) _swappedRead< sizeof(p) >( in,
359 reinterpret_cast< char * >( &p ) );
360 else in.read( reinterpret_cast<char *>( &p ), sizeof( p ) );
361 return in;
362}
363
364
365inline std::istream &ByteSwapper::read( std::istream &in, unsigned long long &p )
366 const {
367 if ( _swap ) _swappedRead< sizeof(p) >( in,
368 reinterpret_cast< char * >( &p ) );
369 else in.read( reinterpret_cast<char *>( &p ), sizeof( p ) );
370 return in;
371}
372
373
374inline std::istream &ByteSwapper::read( std::istream &in, float &p ) const
375{
376 if ( _swap ) _swappedRead< sizeof(p) >( in,
377 reinterpret_cast< char * >( &p ) );
378 else in.read( reinterpret_cast<char *>( &p ), sizeof( p ) );
379 return in;
380}
381
382
383inline std::istream &ByteSwapper::read( std::istream &in,
384 double &p ) const
385{
386 if ( _swap ) _swappedRead< sizeof(p) >( in,
387 reinterpret_cast< char * >( &p ) );
388 else in.read( reinterpret_cast<char *>( &p ), sizeof( p ) );
389 return in;
390}
391
392
393inline std::istream &ByteSwapper::read( std::istream &in,
394 long double &p ) const
395{
396 if ( _swap ) _swappedRead< sizeof(p) >( in,
397 reinterpret_cast< char * >( &p ) );
398 else in.read( reinterpret_cast<char *>( &p ), sizeof( p ) );
399 return in;
400}
401
402
403inline std::ostream &ByteSwapper::write( std::ostream &out,
404 short &p ) const
405{
406 if ( _swap ) _swappedWrite< sizeof(p) >( out,
407 reinterpret_cast< char * >( &p ) );
408 else out.write( reinterpret_cast<char *>( &p ), sizeof( p ) );
409 return out;
410}
411
412
413inline std::ostream &ByteSwapper::write( std::ostream &out,
414 unsigned short &p ) const
415{
416 if ( _swap ) _swappedWrite< sizeof(p) >( out,
417 reinterpret_cast< char * >( &p ) );
418 else out.write( reinterpret_cast<char *>( &p ), sizeof( p ) );
419 return out;
420}
421
422
423inline std::ostream &ByteSwapper::write( std::ostream &out,
424 int &p ) const
425{
426 if ( _swap ) _swappedWrite< sizeof(p) >( out,
427 reinterpret_cast< char * >( &p ) );
428 else out.write( reinterpret_cast<char *>( &p ), sizeof( p ) );
429 return out;
430}
431
432
433inline std::ostream &ByteSwapper::write( std::ostream &out,
434 unsigned int &p ) const
435{
436 if ( _swap ) _swappedWrite< sizeof(p) >( out,
437 reinterpret_cast< char * >( &p ) );
438 else out.write( reinterpret_cast<char *>( &p ), sizeof( p ) );
439 return out;
440}
441
442
443inline std::ostream &ByteSwapper::write( std::ostream &out,
444 long &p ) const
445{
446 if ( _swap ) _swappedWrite< sizeof(p) >( out,
447 reinterpret_cast< char * >( &p ) );
448 else out.write( reinterpret_cast<char *>( &p ), sizeof( p ) );
449 return out;
450}
451
452
453inline std::ostream &ByteSwapper::write( std::ostream &out,
454 unsigned long &p ) const
455{
456 if ( _swap ) _swappedWrite< sizeof(p) >( out,
457 reinterpret_cast< char * >( &p ) );
458 else out.write( reinterpret_cast<char *>( &p ), sizeof( p ) );
459 return out;
460}
461
462
463inline std::ostream &ByteSwapper::write( std::ostream &out,
464 unsigned long long &p ) const
465{
466 if ( _swap ) _swappedWrite< sizeof(p) >( out,
467 reinterpret_cast< char * >( &p ) );
468 else out.write( reinterpret_cast<char *>( &p ), sizeof( p ) );
469 return out;
470}
471
472
473inline std::ostream &ByteSwapper::write( std::ostream &out,
474 long long &p ) const
475{
476 if ( _swap ) _swappedWrite< sizeof(p) >( out,
477 reinterpret_cast< char * >( &p ) );
478 else out.write( reinterpret_cast<char *>( &p ), sizeof( p ) );
479 return out;
480}
481
482
483inline std::ostream &ByteSwapper::write( std::ostream &out,
484 float &p ) const
485{
486 if ( _swap ) _swappedWrite< sizeof(p) >( out,
487 reinterpret_cast< char * >( &p ) );
488 else out.write( reinterpret_cast<char *>( &p ), sizeof( p ) );
489 return out;
490}
491
492
493inline std::ostream &ByteSwapper::write( std::ostream &out,
494 double &p ) const
495{
496 if ( _swap ) _swappedWrite< sizeof(p) >( out,
497 reinterpret_cast< char * >( &p ) );
498 else out.write( reinterpret_cast<char *>( &p ), sizeof( p ) );
499 return out;
500}
501
502
503inline std::ostream &ByteSwapper::write( std::ostream &out,
504 long double &p ) const
505{
506 if ( _swap ) _swappedWrite< sizeof(p) >( out,
507 reinterpret_cast< char * >( &p ) );
508 else out.write( reinterpret_cast<char *>( &p ), sizeof( p ) );
509 return out;
510}
511
512
513} // namesapce carto
514
515#endif
std::istream & read(std::istream &, short &p) const
Definition byte_order.h:295
ByteSwapper(const std::string &bos)
bool setSwapped(bool s)
Definition byte_order.h:71
ByteSwapper(int bo=byteOrder())
bool isSwapped() const
Definition byte_order.h:70
std::ostream & write(std::ostream &, short &p) const
Definition byte_order.h:403
void reorder(signed char &p) const
Definition byte_order.h:193
int byteOrder()
int stringToByteOrder(const std::string &bos)
const char * byteOrderString()