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