00001
00002
00003
00004
00005
00006
00007
00008
00010
00011
00012 #ifndef __LIDAR_ENDIAN_H__
00013 #define __LIDAR_ENDIAN_H__
00014
00015 #include "lidar/Base.h"
00016
00017 LT_BEGIN_LIDAR_NAMESPACE
00018
00020 #define HOST_IS_BIG_ENDIAN (Endian().isHostBigEndian())
00021
00022 #define HOST_IS_LITTLE_ENDIAN (Endian().isHostLittleEndian())
00023
00030 class Endian
00031 {
00032 public:
00033 Endian(void) : word(1) {}
00034
00041 bool isHostBigEndian(void) const { return byte[sizeof(int) - 1] == 1; }
00048 bool isHostLittleEndian(void) const { return byte[0] == 1; }
00049
00050
00059 template<size_t size> static void swap(void *buffer)
00060 {
00061 unsigned char *head = static_cast<unsigned char *>(buffer);
00062 unsigned char *tail = head + size - 1;
00063 while(head < tail)
00064 {
00065 unsigned char temp = *head;
00066 *head++ = *tail;
00067 *tail-- = temp;
00068 }
00069 }
00070
00079 static void swap(void *buffer, size_t size)
00080 {
00081 unsigned char *head = static_cast<unsigned char *>(buffer);
00082 unsigned char *tail = head + size - 1;
00083 while(head < tail)
00084 {
00085 unsigned char temp = *head;
00086 *head++ = *tail;
00087 *tail-- = temp;
00088 }
00089 }
00090
00091 protected:
00092 const union { int word; char byte[sizeof(int)]; };
00093 };
00094
00095
00096 LT_END_LIDAR_NAMESPACE
00097 #endif // __LIDAR_ENDIAN_H__