/home/aherms/Code/git/awds-routing/src/awds/toArray.h

00001 #ifndef _TOARRAY_H__
00002 #define _TOARRAY_H__
00003 
00004 #include <cstddef> // for size_t
00005 
00006 
00007 
00008 
00013 template <typename T>  
00014 void toArray(T d, char *array) {
00015     
00016     for (size_t i = 0; i < sizeof(T); ++i) {
00017         array[sizeof(T) - 1 - i] = (char)(unsigned char)d;
00018         d >>= 8;
00019     }
00020 }
00021 
00022 template <typename T> 
00023 T fromArray( const char *array) {
00024     T d = 0;
00025     for (size_t i = 0; i < sizeof(T); ++i) {
00026         d <<= 8;
00027         d |= (T)(unsigned char)array[i];
00028     }
00029     return d;
00030 }
00031 
00032 /* define optimized version for i386 platform */
00033 #if defined(__GNUC__) && defined(__i386)
00034 
00035 #include <byteswap.h>
00036 
00037 template <>
00038 static unsigned short fromArray<unsigned short>(const char *array) {
00039     return bswap_16( *(unsigned short *)array );
00040 }
00041 
00042 template <>
00043 static unsigned long fromArray<unsigned long>(const char *array) {
00044     return bswap_32( *(unsigned long *)array );
00045 }
00046 
00047 template <>
00048 static void toArray<unsigned short>(unsigned short v, char *array) {
00049     *(unsigned short *)array = bswap_16(v);
00050 }
00051 
00052 template <>
00053 static void toArray<unsigned long>(unsigned long v, char *array) {
00054     *(unsigned long *)array = bswap_32(v);
00055 }
00056 
00057 #endif
00058 
00059 
00060 
00061 
00062 
00063 
00064 
00065 
00066 #endif //TOARRAY_H__
00067 /* This stuff is for emacs
00068  * Local variables:
00069  * mode:c++
00070  * c-basic-offset: 4
00071  * End:
00072  */

Generated on Tue Dec 11 17:58:48 2007 for AWDS by  doxygen 1.5.3-20071008