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