34#ifndef CARTOBASE_TYPE_BYTE_ORDER_H
35#define CARTOBASE_TYPE_BYTE_ORDER_H
71 inline bool setSwapped(
bool s ) { _swap = s;
return _swap; }
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;
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;
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;
116 template <
unsigned SIZE>
117 static void _doSwap(
char * );
119 template <
unsigned SIZE>
120 static std::istream & _swappedRead( std::istream &,
char * );
122 template <
unsigned SIZE>
123 static std::ostream & _swappedWrite( std::ostream &,
char * );
130template <
unsigned SIZE>
131void ByteSwapper::_doSwap(
char * )
133 throw std::runtime_error(
"Invalid byte order swap size" );
137template <
unsigned SIZE>
138std::istream &ByteSwapper::_swappedRead( std::istream & s,
char * )
140 throw std::runtime_error(
"Invalid byte order read size" );
145template <
unsigned SIZE>
146std::ostream &ByteSwapper::_swappedWrite( std::ostream & s,
char * )
148 throw std::runtime_error(
"Invalid byte order write size" );
154void ByteSwapper::_doSwap<2>(
char *p );
157void ByteSwapper::_doSwap<4>(
char *p );
160void ByteSwapper::_doSwap<8>(
char *p );
163void ByteSwapper::_doSwap<16>(
char *p );
167std::istream & ByteSwapper::_swappedRead<2>( std::istream &,
char * );
170std::istream & ByteSwapper::_swappedRead<4>( std::istream &,
char * );
173std::istream & ByteSwapper::_swappedRead<8>( std::istream &,
char * );
176std::istream & ByteSwapper::_swappedRead<16>( std::istream &,
char * );
179std::ostream &ByteSwapper::_swappedWrite<2>( std::ostream &,
char * );
182std::ostream &ByteSwapper::_swappedWrite<4>( std::ostream &,
char * );
185std::ostream &ByteSwapper::_swappedWrite<8>( std::ostream &,
char * );
188std::ostream &ByteSwapper::_swappedWrite<16>( std::ostream &,
char * );
205 if ( _swap ) _doSwap< sizeof(p) >(
reinterpret_cast< char *
>( &p ) );
210 if ( _swap ) _doSwap< sizeof(p) >(
reinterpret_cast< char *
>( &p ) );
215 if ( _swap ) _doSwap< sizeof(p) >(
reinterpret_cast< char *
>( &p ) );
220 if ( _swap ) _doSwap< sizeof(p) >(
reinterpret_cast< char *
>( &p ) );
225 if ( _swap ) _doSwap< sizeof(p) >(
reinterpret_cast< char *
>( &p ) );
230 if ( _swap ) _doSwap< sizeof(p) >(
reinterpret_cast< char *
>( &p ) );
235 if ( _swap ) _doSwap< sizeof(p) >(
reinterpret_cast< char *
>( &p ) );
240 if ( _swap ) _doSwap< sizeof(p) >(
reinterpret_cast< char *
>( &p ) );
245 if ( _swap ) _doSwap< sizeof(p) >(
reinterpret_cast< char *
>( &p ) );
250 if ( _swap ) _doSwap< sizeof(p) >(
reinterpret_cast< char *
>( &p ) );
255 if ( _swap ) _doSwap< sizeof(p) >(
reinterpret_cast< char *
>( &p ) );
261 float real = p.real();
264 float imag = p.imag();
273 double real = p.real();
276 double imag = p.imag();
285 long double real = p.real();
288 long double imag = p.imag();
298 if ( _swap ) _swappedRead< sizeof(p) >( in,
299 reinterpret_cast< char *
>( &p ) );
300 else in.read(
reinterpret_cast<char *
>( &p ),
sizeof( p ) );
306 unsigned short &p )
const
308 if ( _swap ) _swappedRead< sizeof(p) >( in,
309 reinterpret_cast< char *
>( &p ) );
310 else in.read(
reinterpret_cast<char *
>( &p ),
sizeof( p ) );
318 if ( _swap ) _swappedRead< sizeof(p) >( in,
319 reinterpret_cast< char *
>( &p ) );
320 else in.read(
reinterpret_cast<char *
>( &p ),
sizeof( p ) );
326 unsigned int &p )
const
328 if ( _swap ) _swappedRead< sizeof(p) >( in,
329 reinterpret_cast< char *
>( &p ) );
330 else in.read(
reinterpret_cast<char *
>( &p ),
sizeof( p ) );
338 if ( _swap ) _swappedRead< sizeof(p) >( in,
339 reinterpret_cast< char *
>( &p ) );
340 else in.read(
reinterpret_cast<char *
>( &p ),
sizeof( p ) );
346 unsigned long &p )
const
348 if ( _swap ) _swappedRead< sizeof(p) >( in,
349 reinterpret_cast< char *
>( &p ) );
350 else in.read(
reinterpret_cast<char *
>( &p ),
sizeof( p ) );
358 if ( _swap ) _swappedRead< sizeof(p) >( in,
359 reinterpret_cast< char *
>( &p ) );
360 else in.read(
reinterpret_cast<char *
>( &p ),
sizeof( p ) );
367 if ( _swap ) _swappedRead< sizeof(p) >( in,
368 reinterpret_cast< char *
>( &p ) );
369 else in.read(
reinterpret_cast<char *
>( &p ),
sizeof( p ) );
376 if ( _swap ) _swappedRead< sizeof(p) >( in,
377 reinterpret_cast< char *
>( &p ) );
378 else in.read(
reinterpret_cast<char *
>( &p ),
sizeof( p ) );
386 if ( _swap ) _swappedRead< sizeof(p) >( in,
387 reinterpret_cast< char *
>( &p ) );
388 else in.read(
reinterpret_cast<char *
>( &p ),
sizeof( p ) );
394 long double &p )
const
396 if ( _swap ) _swappedRead< sizeof(p) >( in,
397 reinterpret_cast< char *
>( &p ) );
398 else in.read(
reinterpret_cast<char *
>( &p ),
sizeof( p ) );
406 if ( _swap ) _swappedWrite< sizeof(p) >( out,
407 reinterpret_cast< char *
>( &p ) );
408 else out.write(
reinterpret_cast<char *
>( &p ),
sizeof( p ) );
414 unsigned short &p )
const
416 if ( _swap ) _swappedWrite< sizeof(p) >( out,
417 reinterpret_cast< char *
>( &p ) );
418 else out.write(
reinterpret_cast<char *
>( &p ),
sizeof( p ) );
426 if ( _swap ) _swappedWrite< sizeof(p) >( out,
427 reinterpret_cast< char *
>( &p ) );
428 else out.write(
reinterpret_cast<char *
>( &p ),
sizeof( p ) );
434 unsigned int &p )
const
436 if ( _swap ) _swappedWrite< sizeof(p) >( out,
437 reinterpret_cast< char *
>( &p ) );
438 else out.write(
reinterpret_cast<char *
>( &p ),
sizeof( p ) );
446 if ( _swap ) _swappedWrite< sizeof(p) >( out,
447 reinterpret_cast< char *
>( &p ) );
448 else out.write(
reinterpret_cast<char *
>( &p ),
sizeof( p ) );
454 unsigned long &p )
const
456 if ( _swap ) _swappedWrite< sizeof(p) >( out,
457 reinterpret_cast< char *
>( &p ) );
458 else out.write(
reinterpret_cast<char *
>( &p ),
sizeof( p ) );
464 unsigned long long &p )
const
466 if ( _swap ) _swappedWrite< sizeof(p) >( out,
467 reinterpret_cast< char *
>( &p ) );
468 else out.write(
reinterpret_cast<char *
>( &p ),
sizeof( p ) );
476 if ( _swap ) _swappedWrite< sizeof(p) >( out,
477 reinterpret_cast< char *
>( &p ) );
478 else out.write(
reinterpret_cast<char *
>( &p ),
sizeof( p ) );
486 if ( _swap ) _swappedWrite< sizeof(p) >( out,
487 reinterpret_cast< char *
>( &p ) );
488 else out.write(
reinterpret_cast<char *
>( &p ),
sizeof( p ) );
496 if ( _swap ) _swappedWrite< sizeof(p) >( out,
497 reinterpret_cast< char *
>( &p ) );
498 else out.write(
reinterpret_cast<char *
>( &p ),
sizeof( p ) );
504 long double &p )
const
506 if ( _swap ) _swappedWrite< sizeof(p) >( out,
507 reinterpret_cast< char *
>( &p ) );
508 else out.write(
reinterpret_cast<char *
>( &p ),
sizeof( p ) );
std::istream & read(std::istream &, short &p) const
ByteSwapper(const std::string &bos)
ByteSwapper(int bo=byteOrder())
std::ostream & write(std::ostream &, short &p) const
void reorder(signed char &p) const
int stringToByteOrder(const std::string &bos)
const char * byteOrderString()