os/textandloc/fontservices/textshaperplugin/IcuSource/layout/LESwaps.h
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/textandloc/fontservices/textshaperplugin/IcuSource/layout/LESwaps.h	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,121 @@
     1.4 +
     1.5 +/*
     1.6 + *
     1.7 + * (C) Copyright IBM Corp. 1998-2005 - All Rights Reserved
     1.8 + *
     1.9 + */
    1.10 +
    1.11 +#ifndef __LESWAPS_H
    1.12 +#define __LESWAPS_H
    1.13 +
    1.14 +#include "LETypes.h"
    1.15 +
    1.16 +/**
    1.17 + * \file 
    1.18 + * \brief C++ API: Endian independent access to data for LayoutEngine
    1.19 + */
    1.20 +
    1.21 +U_NAMESPACE_BEGIN
    1.22 +
    1.23 +/**
    1.24 + * A convenience macro which invokes the swapWord member function
    1.25 + * from a concise call.
    1.26 + *
    1.27 + * @stable ICU 2.8
    1.28 + */
    1.29 +#if defined(U_IS_BIG_ENDIAN)
    1.30 +    #if U_IS_BIG_ENDIAN
    1.31 +        #define SWAPW(value) (value)
    1.32 +    #else
    1.33 +        #define SWAPW(value) LESwaps::swapWord(value)
    1.34 +    #endif
    1.35 +#else
    1.36 +    #define SWAPW(value) (LESwaps::isBigEndian() ? (value) : LESwaps::swapWord(value))
    1.37 +#endif
    1.38 +
    1.39 +/**
    1.40 + * A convenience macro which invokes the swapLong member function
    1.41 + * from a concise call.
    1.42 + *
    1.43 + * @stable ICU 2.8
    1.44 + */
    1.45 +#if defined(U_IS_BIG_ENDIAN)
    1.46 +    #if U_IS_BIG_ENDIAN
    1.47 +        #define SWAPL(value) (value)
    1.48 +    #else
    1.49 +        #define SWAPL(value) LESwaps::swapLong(value)
    1.50 +    #endif
    1.51 +#else
    1.52 +    #define SWAPL(value) (LESwaps::isBigEndian() ? (value) : LESwaps::swapLong(value))
    1.53 +#endif
    1.54 +
    1.55 +/**
    1.56 + * This class is used to access data which stored in big endian order
    1.57 + * regardless of the conventions of the platform. It has been designed
    1.58 + * to automatically detect the endian-ness of the platform, so that a
    1.59 + * compilation flag is not needed.
    1.60 + *
    1.61 + * All methods are static and inline in an attempt to induce the compiler
    1.62 + * to do most of the calculations at compile time.
    1.63 + *
    1.64 + * @stable ICU 2.8
    1.65 + */
    1.66 +class U_LAYOUT_API LESwaps /* not : public UObject because all methods are static */ {
    1.67 +public:
    1.68 +
    1.69 +#if !defined(U_IS_BIG_ENDIAN)
    1.70 +    /**
    1.71 +     * This method detects the endian-ness of the platform by
    1.72 +     * casting a pointer to a word to a pointer to a byte. On
    1.73 +     * big endian platforms the FF will be in the byte with the
    1.74 +     * lowest address. On little endian platforms, the FF will
    1.75 +     * be in the byte with the highest address.
    1.76 +     *
    1.77 +     * @return TRUE if the platform is big endian
    1.78 +     *
    1.79 +     * @stable ICU 2.8
    1.80 +     */
    1.81 +    static le_uint8 isBigEndian()
    1.82 +    {
    1.83 +        const le_uint16 word = 0xFF00;
    1.84 +
    1.85 +        return *((le_uint8 *) &word);
    1.86 +    };
    1.87 +#endif
    1.88 +
    1.89 +    /**
    1.90 +     * This method does the byte swap required on little endian platforms
    1.91 +     * to correctly access a (16-bit) word.
    1.92 +     *
    1.93 +     * @param value - the word to be byte swapped
    1.94 +     *
    1.95 +     * @return the byte swapped word
    1.96 +     *
    1.97 +     * @stable ICU 2.8
    1.98 +     */
    1.99 +    static le_uint16 swapWord(le_uint16 value)
   1.100 +    {
   1.101 +        return (((le_uint8) (value >> 8)) | (value << 8));
   1.102 +    };
   1.103 +
   1.104 +    /**
   1.105 +     * This method does the byte swapping required on little endian platforms
   1.106 +     * to correctly access a (32-bit) long.
   1.107 +     *
   1.108 +     * @param value - the long to be byte swapped
   1.109 +     *
   1.110 +     * @return the byte swapped long
   1.111 +     *
   1.112 +     * @stable ICU 2.8
   1.113 +     */
   1.114 +    static le_uint32 swapLong(le_uint32 value)
   1.115 +    {
   1.116 +        return swapWord((le_uint16) (value >> 16)) | (swapWord((le_uint16) value) << 16);
   1.117 +    };
   1.118 +
   1.119 +private:
   1.120 +    LESwaps() {} // private - forbid instantiation
   1.121 +};
   1.122 +
   1.123 +U_NAMESPACE_END
   1.124 +#endif