]> git.saurik.com Git - apple/icu.git/blob - icuSources/layout/LESwaps.h
ICU-3.13.tar.gz
[apple/icu.git] / icuSources / layout / LESwaps.h
1
2 /*
3 * @(#)LESwaps.h 1.3 00/03/15
4 *
5 * (C) Copyright IBM Corp. 1998-2003 - All Rights Reserved
6 *
7 */
8
9 #ifndef __LESWAPS_H
10 #define __LESWAPS_H
11
12 #include "LETypes.h"
13
14 U_NAMESPACE_BEGIN
15
16 /**
17 * A convenience macro which invokes the swapWord member function
18 * from a concise call.
19 *
20 * @draft ICU 2.2
21 */
22 #define SWAPW(value) (LESwaps::isBigEndian() ? (value) : LESwaps::swapWord(value))
23
24
25 /**
26 * A convenience macro which invokes the swapLong member function
27 * from a concise call.
28 *
29 * @draft ICU 2.2
30 */
31 #define SWAPL(value) (LESwaps::isBigEndian() ? (value) : LESwaps::swapLong(value))
32
33 /**
34 * This class is used to access data which stored in big endian order
35 * regardless of the conventions of the platform. It has been designed
36 * to automatically detect the endian-ness of the platform, so that a
37 * compilation flag is not needed.
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 * @draft ICU 2.2
43 */
44 class U_LAYOUT_API LESwaps /* not : public UObject because all methods are static */ {
45 public:
46
47 /**
48 * This method detects the endian-ness of the platform by
49 * casting a pointer to a word to a pointer to a byte. On
50 * big endian platforms the FF will be in the byte with the
51 * lowest address. On little endian platforms, the FF will
52 * be in the byte with the highest address.
53 *
54 * @return true if the platform is big endian
55 *
56 * @draft ICU 2.2
57 */
58 static le_bool isBigEndian()
59 {
60 const le_uint16 word = 0xFF00;
61
62 return *((le_uint8 *) &word);
63 };
64
65 /**
66 * This method does the byte swap required on little endian platforms
67 * to correctly access a (16-bit) word.
68 *
69 * @param value - the word to be byte swapped
70 *
71 * @return the byte swapped word
72 *
73 * @draft ICU 2.2
74 */
75 static le_uint16 swapWord(le_uint16 value)
76 {
77 return (((le_uint8) (value >> 8)) | (value << 8));
78 };
79
80 /**
81 * This method does the byte swapping required on little endian platforms
82 * to correctly access a (32-bit) long.
83 *
84 * @param value - the long to be byte swapped
85 *
86 * @return the byte swapped long
87 *
88 * @draft ICU 2.2
89 */
90 static le_uint32 swapLong(le_uint32 value)
91 {
92 return swapWord((le_uint16) (value >> 16)) | (swapWord((le_uint16) value) << 16);
93 };
94
95 private:
96 LESwaps() {} // private - forbid instantiation
97 };
98
99 U_NAMESPACE_END
100 #endif