Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #ifndef BYTEORDER_H
00027 #define BYTEORDER_H
00028
00029 #include "GpCoreToolsDLLExport.h"
00030
00031
00032
00033
00034 #ifndef BYTE_ORDER
00035 # define BYTE_ORDER LITTLE_ENDIAN
00036 #endif
00037
00038 namespace GpCoreTools {
00039
00040 class GPCORETOOLS_EXPORT ByteOrder
00041 {
00042 public:
00043 static inline int bigEndianToNative(int v);
00044 static inline int littleEndianToNative(int v);
00045
00046 static inline short bigEndianToNative(const unsigned short& v);
00047 static inline short littleEndianToNative(const unsigned short& v);
00048
00049 static int swap(int v);
00050 static short swap(const unsigned short& v);
00051 static inline void swap(char& v1, char& v2);
00052 static inline void swap(unsigned char& v1, unsigned char& v2);
00053 static inline void swap(char * v);
00054 static inline void swap(unsigned char * v);
00055 };
00056
00057 inline int ByteOrder::bigEndianToNative(int v)
00058 {
00059 #if BYTE_ORDER == LITTLE_ENDIAN
00060 return swap(v);
00061 #else
00062 return v;
00063 #endif
00064 }
00065
00066 inline int ByteOrder::littleEndianToNative(int v)
00067 {
00068 #if BYTE_ORDER == BIG_ENDIAN
00069 return swap(v);
00070 #else
00071 return v;
00072 #endif
00073 }
00074
00075 inline short ByteOrder::bigEndianToNative(const unsigned short& v)
00076 {
00077 #if BYTE_ORDER == LITTLE_ENDIAN
00078 return swap(v);
00079 #else
00080 return v;
00081 #endif
00082 }
00083
00084 inline short ByteOrder::littleEndianToNative(const unsigned short& v)
00085 {
00086 #if BYTE_ORDER == BIG_ENDIAN
00087 return swap(v);
00088 #else
00089 return v;
00090 #endif
00091 }
00092
00093 inline void ByteOrder::swap(char& v1, char& v2)
00094 {
00095 char tmp=v1;
00096 v1=v2;
00097 v2=tmp;
00098 }
00099
00100 inline void ByteOrder::swap(unsigned char& v1, unsigned char& v2)
00101 {
00102 char tmp=v1;
00103 v1=v2;
00104 v2=tmp;
00105 }
00106
00107 inline void ByteOrder::swap(char * v)
00108 {
00109 swap(v[0], v[3]);
00110 swap(v[1], v[2]);
00111 }
00112
00113 inline void ByteOrder::swap(unsigned char * v)
00114 {
00115 swap(v[0], v[3]);
00116 swap(v[1], v[2]);
00117 }
00118
00119 }
00120
00121 #endif // BYTEORDER_H