sl@0: 
sl@0: /*
sl@0:  *
sl@0:  * (C) Copyright IBM Corp. 1998-2005 - All Rights Reserved
sl@0:  *
sl@0:  */
sl@0: 
sl@0: #ifndef __LESWAPS_H
sl@0: #define __LESWAPS_H
sl@0: 
sl@0: #include "LETypes.h"
sl@0: 
sl@0: /**
sl@0:  * \file 
sl@0:  * \brief C++ API: Endian independent access to data for LayoutEngine
sl@0:  */
sl@0: 
sl@0: U_NAMESPACE_BEGIN
sl@0: 
sl@0: /**
sl@0:  * A convenience macro which invokes the swapWord member function
sl@0:  * from a concise call.
sl@0:  *
sl@0:  * @stable ICU 2.8
sl@0:  */
sl@0: #if defined(U_IS_BIG_ENDIAN)
sl@0:     #if U_IS_BIG_ENDIAN
sl@0:         #define SWAPW(value) (value)
sl@0:     #else
sl@0:         #define SWAPW(value) LESwaps::swapWord(value)
sl@0:     #endif
sl@0: #else
sl@0:     #define SWAPW(value) (LESwaps::isBigEndian() ? (value) : LESwaps::swapWord(value))
sl@0: #endif
sl@0: 
sl@0: /**
sl@0:  * A convenience macro which invokes the swapLong member function
sl@0:  * from a concise call.
sl@0:  *
sl@0:  * @stable ICU 2.8
sl@0:  */
sl@0: #if defined(U_IS_BIG_ENDIAN)
sl@0:     #if U_IS_BIG_ENDIAN
sl@0:         #define SWAPL(value) (value)
sl@0:     #else
sl@0:         #define SWAPL(value) LESwaps::swapLong(value)
sl@0:     #endif
sl@0: #else
sl@0:     #define SWAPL(value) (LESwaps::isBigEndian() ? (value) : LESwaps::swapLong(value))
sl@0: #endif
sl@0: 
sl@0: /**
sl@0:  * This class is used to access data which stored in big endian order
sl@0:  * regardless of the conventions of the platform. It has been designed
sl@0:  * to automatically detect the endian-ness of the platform, so that a
sl@0:  * compilation flag is not needed.
sl@0:  *
sl@0:  * All methods are static and inline in an attempt to induce the compiler
sl@0:  * to do most of the calculations at compile time.
sl@0:  *
sl@0:  * @stable ICU 2.8
sl@0:  */
sl@0: class U_LAYOUT_API LESwaps /* not : public UObject because all methods are static */ {
sl@0: public:
sl@0: 
sl@0: #if !defined(U_IS_BIG_ENDIAN)
sl@0:     /**
sl@0:      * This method detects the endian-ness of the platform by
sl@0:      * casting a pointer to a word to a pointer to a byte. On
sl@0:      * big endian platforms the FF will be in the byte with the
sl@0:      * lowest address. On little endian platforms, the FF will
sl@0:      * be in the byte with the highest address.
sl@0:      *
sl@0:      * @return TRUE if the platform is big endian
sl@0:      *
sl@0:      * @stable ICU 2.8
sl@0:      */
sl@0:     static le_uint8 isBigEndian()
sl@0:     {
sl@0:         const le_uint16 word = 0xFF00;
sl@0: 
sl@0:         return *((le_uint8 *) &word);
sl@0:     };
sl@0: #endif
sl@0: 
sl@0:     /**
sl@0:      * This method does the byte swap required on little endian platforms
sl@0:      * to correctly access a (16-bit) word.
sl@0:      *
sl@0:      * @param value - the word to be byte swapped
sl@0:      *
sl@0:      * @return the byte swapped word
sl@0:      *
sl@0:      * @stable ICU 2.8
sl@0:      */
sl@0:     static le_uint16 swapWord(le_uint16 value)
sl@0:     {
sl@0:         return (((le_uint8) (value >> 8)) | (value << 8));
sl@0:     };
sl@0: 
sl@0:     /**
sl@0:      * This method does the byte swapping required on little endian platforms
sl@0:      * to correctly access a (32-bit) long.
sl@0:      *
sl@0:      * @param value - the long to be byte swapped
sl@0:      *
sl@0:      * @return the byte swapped long
sl@0:      *
sl@0:      * @stable ICU 2.8
sl@0:      */
sl@0:     static le_uint32 swapLong(le_uint32 value)
sl@0:     {
sl@0:         return swapWord((le_uint16) (value >> 16)) | (swapWord((le_uint16) value) << 16);
sl@0:     };
sl@0: 
sl@0: private:
sl@0:     LESwaps() {} // private - forbid instantiation
sl@0: };
sl@0: 
sl@0: U_NAMESPACE_END
sl@0: #endif