![]() |
MAK RTIspy API Documentation for HLA 1516
|
00001 /********************************************************************* 00002 ** Copyright (c) 2010 MaK Technologies, Inc. 00003 ** All rights reserved. 00004 *********************************************************************/ 00005 /********************************************************************* 00006 ** $RCSfile: encodingImplHelpers.h,v $ $Revision: 1.0 $ $State: Exp $ 00007 *********************************************************************/ 00010 00011 #ifndef encodingImplHelpers_H_ 00012 #define encodingImplHelpers_H_ 00013 00014 #include <typeinfo> 00015 #include <cstring> 00016 00017 namespace rti1516e 00018 { 00020 const int endianChecker = 1; 00021 #define is_bigendian() ( (*(char*)&endianChecker) == 0 ) 00022 00024 inline void swapByteArray( char * byteArray, int theSize ) 00025 { 00026 int theEnd = 0; 00027 for( int i = 0; i < ( theSize >> 1 ); ++i ) 00028 { 00029 theEnd = ( theSize - 1 ) - i; 00030 byteArray[ i ] ^= byteArray[ theEnd ]; 00031 byteArray[ theEnd ] ^= byteArray[ i ]; 00032 byteArray[ i ] ^= byteArray[ theEnd ]; 00033 } 00034 } 00035 00037 inline unsigned short endianSwap16Bit( unsigned short rhs ) 00038 { 00039 unsigned short msbyte = rhs & 0x00ff; 00040 unsigned short lsbyte = ( rhs & 0xff00 ) >> 8; 00041 return ( msbyte << 8 ) | lsbyte; 00042 } 00043 00045 inline unsigned int endianSwap32Bit( unsigned int rhs ) 00046 { 00047 unsigned short msword = endianSwap16Bit( rhs & 0x0000ffff ); 00048 unsigned short lsword = endianSwap16Bit(( rhs & 0xffff0000 ) >> 16 ); 00049 return ( msword << 16 ) | lsword; 00050 } 00051 00053 inline Integer64 endianSwap64Bit( Integer64 rhs ) 00054 { 00056 unsigned int mspart = endianSwap32Bit( static_cast<unsigned int>(rhs & 0xffffffff) ); 00057 unsigned int lspart = endianSwap32Bit(static_cast<unsigned int>(( rhs >> 32 ) & 0xffffffff) ); 00058 return ( Integer64( mspart ) << 32 ) | lspart; 00059 } 00060 00062 inline float endianSwap32BitFloat( float rhs ) 00063 { 00064 union v 00065 { 00066 float f; 00067 unsigned int i; 00068 }; 00069 00070 v val; 00071 val.f = rhs; 00072 val.i = endianSwap32Bit( val.i ); 00073 00074 return val.f; 00075 } 00076 00077 inline double endianSwap64BitDouble( double rhs ) 00078 { 00079 union v 00080 { 00081 double d; 00082 Integer64 i; 00083 }; 00084 00085 v val; 00086 val.d = rhs; 00087 val.i = endianSwap64Bit( val.i ); 00088 return val.d; 00089 } 00090 00092 inline void appendBytes( std::vector<Octet>& wrapper, const char* bytes, size_t length ) 00093 { 00094 // Resize the buffer and then write to it (this is efficient if caller 00095 // has already reserved the space - otherwise buffer may re-alloc) 00096 unsigned int currentBufferSize = wrapper.size(); 00097 wrapper.resize( currentBufferSize + length ); 00098 memcpy( &wrapper[ currentBufferSize ], bytes, length ); 00099 } 00100 00102 inline unsigned int paddingSizeInBytes( unsigned int bufferLength, unsigned int elementOctetBoundary ) 00103 { 00104 if ( bufferLength == 0 ) return 0; 00105 return bufferLength % elementOctetBoundary; 00106 } 00107 00109 inline void insertBytes( std::vector<Octet>& wrapper, size_t length ) 00110 { 00113 unsigned int currentBufferSize = wrapper.size(); 00114 wrapper.resize( currentBufferSize + length ); 00115 } 00116 } 00117 00118 #endif