os/textandloc/fontservices/textshaperplugin/IcuSource/layout/LESwaps.h
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
sl@0
     2
/*
sl@0
     3
 *
sl@0
     4
 * (C) Copyright IBM Corp. 1998-2005 - All Rights Reserved
sl@0
     5
 *
sl@0
     6
 */
sl@0
     7
sl@0
     8
#ifndef __LESWAPS_H
sl@0
     9
#define __LESWAPS_H
sl@0
    10
sl@0
    11
#include "LETypes.h"
sl@0
    12
sl@0
    13
/**
sl@0
    14
 * \file 
sl@0
    15
 * \brief C++ API: Endian independent access to data for LayoutEngine
sl@0
    16
 */
sl@0
    17
sl@0
    18
U_NAMESPACE_BEGIN
sl@0
    19
sl@0
    20
/**
sl@0
    21
 * A convenience macro which invokes the swapWord member function
sl@0
    22
 * from a concise call.
sl@0
    23
 *
sl@0
    24
 * @stable ICU 2.8
sl@0
    25
 */
sl@0
    26
#if defined(U_IS_BIG_ENDIAN)
sl@0
    27
    #if U_IS_BIG_ENDIAN
sl@0
    28
        #define SWAPW(value) (value)
sl@0
    29
    #else
sl@0
    30
        #define SWAPW(value) LESwaps::swapWord(value)
sl@0
    31
    #endif
sl@0
    32
#else
sl@0
    33
    #define SWAPW(value) (LESwaps::isBigEndian() ? (value) : LESwaps::swapWord(value))
sl@0
    34
#endif
sl@0
    35
sl@0
    36
/**
sl@0
    37
 * A convenience macro which invokes the swapLong member function
sl@0
    38
 * from a concise call.
sl@0
    39
 *
sl@0
    40
 * @stable ICU 2.8
sl@0
    41
 */
sl@0
    42
#if defined(U_IS_BIG_ENDIAN)
sl@0
    43
    #if U_IS_BIG_ENDIAN
sl@0
    44
        #define SWAPL(value) (value)
sl@0
    45
    #else
sl@0
    46
        #define SWAPL(value) LESwaps::swapLong(value)
sl@0
    47
    #endif
sl@0
    48
#else
sl@0
    49
    #define SWAPL(value) (LESwaps::isBigEndian() ? (value) : LESwaps::swapLong(value))
sl@0
    50
#endif
sl@0
    51
sl@0
    52
/**
sl@0
    53
 * This class is used to access data which stored in big endian order
sl@0
    54
 * regardless of the conventions of the platform. It has been designed
sl@0
    55
 * to automatically detect the endian-ness of the platform, so that a
sl@0
    56
 * compilation flag is not needed.
sl@0
    57
 *
sl@0
    58
 * All methods are static and inline in an attempt to induce the compiler
sl@0
    59
 * to do most of the calculations at compile time.
sl@0
    60
 *
sl@0
    61
 * @stable ICU 2.8
sl@0
    62
 */
sl@0
    63
class U_LAYOUT_API LESwaps /* not : public UObject because all methods are static */ {
sl@0
    64
public:
sl@0
    65
sl@0
    66
#if !defined(U_IS_BIG_ENDIAN)
sl@0
    67
    /**
sl@0
    68
     * This method detects the endian-ness of the platform by
sl@0
    69
     * casting a pointer to a word to a pointer to a byte. On
sl@0
    70
     * big endian platforms the FF will be in the byte with the
sl@0
    71
     * lowest address. On little endian platforms, the FF will
sl@0
    72
     * be in the byte with the highest address.
sl@0
    73
     *
sl@0
    74
     * @return TRUE if the platform is big endian
sl@0
    75
     *
sl@0
    76
     * @stable ICU 2.8
sl@0
    77
     */
sl@0
    78
    static le_uint8 isBigEndian()
sl@0
    79
    {
sl@0
    80
        const le_uint16 word = 0xFF00;
sl@0
    81
sl@0
    82
        return *((le_uint8 *) &word);
sl@0
    83
    };
sl@0
    84
#endif
sl@0
    85
sl@0
    86
    /**
sl@0
    87
     * This method does the byte swap required on little endian platforms
sl@0
    88
     * to correctly access a (16-bit) word.
sl@0
    89
     *
sl@0
    90
     * @param value - the word to be byte swapped
sl@0
    91
     *
sl@0
    92
     * @return the byte swapped word
sl@0
    93
     *
sl@0
    94
     * @stable ICU 2.8
sl@0
    95
     */
sl@0
    96
    static le_uint16 swapWord(le_uint16 value)
sl@0
    97
    {
sl@0
    98
        return (((le_uint8) (value >> 8)) | (value << 8));
sl@0
    99
    };
sl@0
   100
sl@0
   101
    /**
sl@0
   102
     * This method does the byte swapping required on little endian platforms
sl@0
   103
     * to correctly access a (32-bit) long.
sl@0
   104
     *
sl@0
   105
     * @param value - the long to be byte swapped
sl@0
   106
     *
sl@0
   107
     * @return the byte swapped long
sl@0
   108
     *
sl@0
   109
     * @stable ICU 2.8
sl@0
   110
     */
sl@0
   111
    static le_uint32 swapLong(le_uint32 value)
sl@0
   112
    {
sl@0
   113
        return swapWord((le_uint16) (value >> 16)) | (swapWord((le_uint16) value) << 16);
sl@0
   114
    };
sl@0
   115
sl@0
   116
private:
sl@0
   117
    LESwaps() {} // private - forbid instantiation
sl@0
   118
};
sl@0
   119
sl@0
   120
U_NAMESPACE_END
sl@0
   121
#endif