1 2 /* 3 * 4 * (C) Copyright IBM Corp. 1998-2008 - All Rights Reserved 5 * 6 */ 7 8 #ifndef __LESWAPS_H 9 #define __LESWAPS_H 10 11 #include "LETypes.h" 12 13 /** 14 * \file 15 * \brief C++ API: Endian independent access to data for LayoutEngine 16 */ 17 18 U_NAMESPACE_BEGIN 19 20 /** 21 * A convenience macro which invokes the swapWord member function 22 * from a concise call. 23 * 24 * @stable ICU 2.8 25 */ 26 #define SWAPW(value) LESwaps::swapWord((const le_uint16 &) (value)) 27 28 /** 29 * A convenience macro which invokes the swapLong member function 30 * from a concise call. 31 * 32 * @stable ICU 2.8 33 */ 34 #define SWAPL(value) LESwaps::swapLong((const le_uint32 &) (value)) 35 36 /** 37 * This class is used to access data which stored in big endian order 38 * regardless of the conventions of the platform. 39 * 40 * All methods are static and inline in an attempt to induce the compiler 41 * to do most of the calculations at compile time. 42 * 43 * @stable ICU 2.8 44 */ 45 class U_LAYOUT_API LESwaps /* not : public UObject because all methods are static */ { 46 public: 47 48 /** 49 * This method does the byte swap required on little endian platforms 50 * to correctly access a (16-bit) word. 51 * 52 * @param value - the word to be byte swapped 53 * 54 * @return the byte swapped word 55 * 56 * @stable ICU 2.8 57 */ swapWord(const le_uint16 & value)58 static le_uint16 swapWord(const le_uint16 &value) 59 { 60 const le_uint8 *p = (const le_uint8 *) &value; 61 62 return ((p[0] << 8) + p[1]); 63 }; 64 65 /** 66 * This method does the byte swapping required on little endian platforms 67 * to correctly access a (32-bit) long. 68 * 69 * @param value - the long to be byte swapped 70 * 71 * @return the byte swapped long 72 * 73 * @stable ICU 2.8 74 */ swapLong(const le_uint32 & value)75 static le_uint32 swapLong(const le_uint32 &value) 76 { 77 const le_uint8 *p = (const le_uint8 *) &value; 78 79 return ((p[0] << 24) + (p[1] << 16) + (p[2] << 8) + p[3]); 80 }; 81 82 private: LESwaps()83 LESwaps() {} // private - forbid instantiation 84 }; 85 86 U_NAMESPACE_END 87 #endif 88