34 #ifndef CARTOBASE_TYPE_BYTE_ORDER_H
35 #define CARTOBASE_TYPE_BYTE_ORDER_H
70 inline bool setSwapped(
bool s ) { _swap = s;
return _swap; }
72 inline void reorder(
short &p )
const;
73 inline void reorder(
unsigned short &p )
const;
74 inline void reorder(
int &p )
const;
75 inline void reorder(
unsigned int &p )
const;
76 inline void reorder(
long &p )
const;
77 inline void reorder(
unsigned long &p )
const;
78 inline void reorder(
long long &p )
const;
79 inline void reorder(
unsigned long long &p )
const;
80 inline void reorder(
float &p )
const;
81 inline void reorder(
double &p )
const;
82 inline void reorder(
long double &p )
const;
84 inline std::istream &
read( std::istream &,
short &p )
const;
85 inline std::istream &
read( std::istream &,
unsigned short &p )
const;
86 inline std::istream &
read( std::istream &,
int &p )
const;
87 inline std::istream &
read( std::istream &,
unsigned int &p )
const;
88 inline std::istream &
read( std::istream &,
long &p )
const;
89 inline std::istream &
read( std::istream &,
unsigned long &p )
const;
90 inline std::istream &
read( std::istream &,
long long &p )
const;
91 inline std::istream &
read( std::istream &,
unsigned long long &p )
const;
92 inline std::istream &
read( std::istream &,
float &p )
const;
93 inline std::istream &
read( std::istream &,
double &p )
const;
94 inline std::istream &
read( std::istream &,
long double &p )
const;
96 inline std::ostream &
write( std::ostream &,
short &p )
const;
97 inline std::ostream &
write( std::ostream &,
unsigned short &p )
const;
98 inline std::ostream &
write( std::ostream &,
int &p )
const;
99 inline std::ostream &
write( std::ostream &,
unsigned int &p )
const;
100 inline std::ostream &
write( std::ostream &,
long &p )
const;
101 inline std::ostream &
write( std::ostream &,
unsigned long &p )
const;
102 inline std::ostream &
write( std::ostream &,
long long &p )
const;
103 inline std::ostream &
write( std::ostream &,
unsigned long long &p )
const;
104 inline std::ostream &
write( std::ostream &,
float &p )
const;
105 inline std::ostream &
write( std::ostream &,
double &p )
const;
106 inline std::ostream &
write( std::ostream &,
long double &p )
const;
110 template <
unsigned SIZE>
111 static void _doSwap(
char * );
113 template <
unsigned SIZE>
114 static std::istream & _swappedRead( std::istream &,
char * );
116 template <
unsigned SIZE>
117 static std::ostream & _swappedWrite( std::ostream &,
char * );
124 template <
unsigned SIZE>
125 void ByteSwapper::_doSwap(
char * )
127 throw std::runtime_error(
"Invalid byte order swap size" );
131 template <
unsigned SIZE>
132 std::istream &ByteSwapper::_swappedRead( std::istream & s,
char * )
134 throw std::runtime_error(
"Invalid byte order read size" );
139 template <
unsigned SIZE>
140 std::ostream &ByteSwapper::_swappedWrite( std::ostream & s,
char * )
142 throw std::runtime_error(
"Invalid byte order write size" );
148 void ByteSwapper::_doSwap<2>(
char *p );
151 void ByteSwapper::_doSwap<4>(
char *p );
154 void ByteSwapper::_doSwap<8>(
char *p );
157 void ByteSwapper::_doSwap<16>(
char *p );
161 std::istream & ByteSwapper::_swappedRead<2>( std::istream &,
char * );
164 std::istream & ByteSwapper::_swappedRead<4>( std::istream &,
char * );
167 std::istream & ByteSwapper::_swappedRead<8>( std::istream &,
char * );
170 std::istream & ByteSwapper::_swappedRead<16>( std::istream &,
char * );
173 std::ostream &ByteSwapper::_swappedWrite<2>( std::ostream &,
char * );
176 std::ostream &ByteSwapper::_swappedWrite<4>( std::ostream &,
char * );
179 std::ostream &ByteSwapper::_swappedWrite<8>( std::ostream &,
char * );
182 std::ostream &ByteSwapper::_swappedWrite<16>( std::ostream &,
char * );
189 if ( _swap ) _doSwap< sizeof(p) >(
reinterpret_cast< char *
>( &p ) );
194 if ( _swap ) _doSwap< sizeof(p) >(
reinterpret_cast< char *
>( &p ) );
199 if ( _swap ) _doSwap< sizeof(p) >(
reinterpret_cast< char *
>( &p ) );
204 if ( _swap ) _doSwap< sizeof(p) >(
reinterpret_cast< char *
>( &p ) );
209 if ( _swap ) _doSwap< sizeof(p) >(
reinterpret_cast< char *
>( &p ) );
214 if ( _swap ) _doSwap< sizeof(p) >(
reinterpret_cast< char *
>( &p ) );
219 if ( _swap ) _doSwap< sizeof(p) >(
reinterpret_cast< char *
>( &p ) );
224 if ( _swap ) _doSwap< sizeof(p) >(
reinterpret_cast< char *
>( &p ) );
229 if ( _swap ) _doSwap< sizeof(p) >(
reinterpret_cast< char *
>( &p ) );
234 if ( _swap ) _doSwap< sizeof(p) >(
reinterpret_cast< char *
>( &p ) );
239 if ( _swap ) _doSwap< sizeof(p) >(
reinterpret_cast< char *
>( &p ) );
246 if ( _swap ) _swappedRead< sizeof(p) >( in,
247 reinterpret_cast< char *
>( &p ) );
248 else in.read(
reinterpret_cast<char *
>( &p ),
sizeof( p ) );
254 unsigned short &p )
const
256 if ( _swap ) _swappedRead< sizeof(p) >( in,
257 reinterpret_cast< char *
>( &p ) );
258 else in.read(
reinterpret_cast<char *
>( &p ),
sizeof( p ) );
266 if ( _swap ) _swappedRead< sizeof(p) >( in,
267 reinterpret_cast< char *
>( &p ) );
268 else in.read(
reinterpret_cast<char *
>( &p ),
sizeof( p ) );
274 unsigned int &p )
const
276 if ( _swap ) _swappedRead< sizeof(p) >( in,
277 reinterpret_cast< char *
>( &p ) );
278 else in.read(
reinterpret_cast<char *
>( &p ),
sizeof( p ) );
286 if ( _swap ) _swappedRead< sizeof(p) >( in,
287 reinterpret_cast< char *
>( &p ) );
288 else in.read(
reinterpret_cast<char *
>( &p ),
sizeof( p ) );
294 unsigned long &p )
const
296 if ( _swap ) _swappedRead< sizeof(p) >( in,
297 reinterpret_cast< char *
>( &p ) );
298 else in.read(
reinterpret_cast<char *
>( &p ),
sizeof( p ) );
306 if ( _swap ) _swappedRead< sizeof(p) >( in,
307 reinterpret_cast< char *
>( &p ) );
308 else in.read(
reinterpret_cast<char *
>( &p ),
sizeof( p ) );
315 if ( _swap ) _swappedRead< sizeof(p) >( in,
316 reinterpret_cast< char *
>( &p ) );
317 else in.read(
reinterpret_cast<char *
>( &p ),
sizeof( p ) );
324 if ( _swap ) _swappedRead< sizeof(p) >( in,
325 reinterpret_cast< char *
>( &p ) );
326 else in.read(
reinterpret_cast<char *
>( &p ),
sizeof( p ) );
334 if ( _swap ) _swappedRead< sizeof(p) >( in,
335 reinterpret_cast< char *
>( &p ) );
336 else in.read(
reinterpret_cast<char *
>( &p ),
sizeof( p ) );
342 long double &p )
const
344 if ( _swap ) _swappedRead< sizeof(p) >( in,
345 reinterpret_cast< char *
>( &p ) );
346 else in.read(
reinterpret_cast<char *
>( &p ),
sizeof( p ) );
354 if ( _swap ) _swappedWrite< sizeof(p) >( out,
355 reinterpret_cast< char *
>( &p ) );
356 else out.write(
reinterpret_cast<char *
>( &p ),
sizeof( p ) );
362 unsigned short &p )
const
364 if ( _swap ) _swappedWrite< sizeof(p) >( out,
365 reinterpret_cast< char *
>( &p ) );
366 else out.write(
reinterpret_cast<char *
>( &p ),
sizeof( p ) );
374 if ( _swap ) _swappedWrite< sizeof(p) >( out,
375 reinterpret_cast< char *
>( &p ) );
376 else out.write(
reinterpret_cast<char *
>( &p ),
sizeof( p ) );
382 unsigned int &p )
const
384 if ( _swap ) _swappedWrite< sizeof(p) >( out,
385 reinterpret_cast< char *
>( &p ) );
386 else out.write(
reinterpret_cast<char *
>( &p ),
sizeof( p ) );
394 if ( _swap ) _swappedWrite< sizeof(p) >( out,
395 reinterpret_cast< char *
>( &p ) );
396 else out.write(
reinterpret_cast<char *
>( &p ),
sizeof( p ) );
402 unsigned long &p )
const
404 if ( _swap ) _swappedWrite< sizeof(p) >( out,
405 reinterpret_cast< char *
>( &p ) );
406 else out.write(
reinterpret_cast<char *
>( &p ),
sizeof( p ) );
412 unsigned long long &p )
const
414 if ( _swap ) _swappedWrite< sizeof(p) >( out,
415 reinterpret_cast< char *
>( &p ) );
416 else out.write(
reinterpret_cast<char *
>( &p ),
sizeof( p ) );
424 if ( _swap ) _swappedWrite< sizeof(p) >( out,
425 reinterpret_cast< char *
>( &p ) );
426 else out.write(
reinterpret_cast<char *
>( &p ),
sizeof( p ) );
434 if ( _swap ) _swappedWrite< sizeof(p) >( out,
435 reinterpret_cast< char *
>( &p ) );
436 else out.write(
reinterpret_cast<char *
>( &p ),
sizeof( p ) );
444 if ( _swap ) _swappedWrite< sizeof(p) >( out,
445 reinterpret_cast< char *
>( &p ) );
446 else out.write(
reinterpret_cast<char *
>( &p ),
sizeof( p ) );
452 long double &p )
const
454 if ( _swap ) _swappedWrite< sizeof(p) >( out,
455 reinterpret_cast< char *
>( &p ) );
456 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())
void reorder(short &p) const
std::ostream & write(std::ostream &, short &p) const
int stringToByteOrder(const std::string &bos)
const char * byteOrderString()