- Timestamp:
- Mar 2, 2006, 9:07:47 PM (19 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/mesh/byte_order.h
r265 r266 1 1 #ifndef __RP_BYTEORDER_H__ 2 2 #define __RP_BYTEORDER_H__ 3 4 #include <string.h> 3 5 4 6 template<class ValType> class ByteOrder { … … 8 10 9 11 // returns 1 if byte order is big endian 10 static int IsBigEndian(); 12 static int IsBigEndian() 13 { 14 unsigned x = 1; 15 return !(*(char *)(&x)); 16 }; 11 17 12 18 // copy value from src to dst adjusting for endianess 13 static void OrderCopy(const ValType& src, ValType& dst); 19 // If the machine calling this function uses big endian byte order, 20 // bytes are flipped. 21 static void OrderCopy(const ValType& src_val, ValType& dst_val) 22 { 23 unsigned char* src = (unsigned char *)&src_val; 24 unsigned char* dst = (unsigned char *)&dst_val; 25 26 // our software will use little endian 27 // swap bytes if reading on a big endian machine 28 // 29 if (IsBigEndian()) { 30 switch(sizeof(ValType)) { 31 case 1: 32 dst[0] = src[0]; 33 break; 34 case 2: 35 dst[1] = src[0]; 36 dst[0] = src[1]; 37 break; 38 case 4: 39 dst[3] = src[0]; 40 dst[2] = src[1]; 41 dst[1] = src[2]; 42 dst[0] = src[3]; 43 default: 44 dst += sizeof(ValType)-1; 45 for(int i=sizeof(ValType); i>0; i-- ) 46 *(dst--) = *(src++); 47 } 48 } 49 else { 50 // LE: straight copy 51 unsigned int i; 52 for (i=0; i<sizeof(ValType); i++) 53 memcpy((void*)&dst, (void*)&src, sizeof(ValType)); 54 } 55 }; 14 56 15 57 // swap bytes in place, src altered for BE machine. 16 static void Flip(ValType& src); 58 static void Flip(ValType& src_val) 59 { 60 ValType val; 61 Copy(src_val, val); 62 memcpy((void*)&src_val, (void*)&val, sizeof(ValType)); 63 }; 17 64 18 //operator ValType () const; 19 //ValType operator=(ValType val); 65 operator ValType () const 66 { 67 ValType l_value; 68 OrderCopy(m_val, l_value); 69 return l_value; 70 }; 71 72 ValType operator=(ValType in_val) 73 { 74 OrderCopy(in_val, m_val); 75 return in_val; 76 }; 20 77 21 78 private:
Note: See TracChangeset
for help on using the changeset viewer.