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