]>
Commit | Line | Data |
---|---|---|
b75a7d8f A |
1 | /* |
2 | ******************************************************************************** | |
3 | * Copyright (C) 1997-2003, International Business Machines | |
4 | * Corporation and others. All Rights Reserved. | |
5 | ******************************************************************************** | |
6 | * | |
7 | * File FMTABLE.H | |
8 | * | |
9 | * Modification History: | |
10 | * | |
11 | * Date Name Description | |
12 | * 02/29/97 aliu Creation. | |
13 | ******************************************************************************** | |
14 | */ | |
15 | #ifndef FMTABLE_H | |
16 | #define FMTABLE_H | |
17 | ||
18 | ||
19 | #include "unicode/utypes.h" | |
20 | ||
21 | #if !UCONFIG_NO_FORMATTING | |
22 | ||
23 | #include "unicode/uobject.h" | |
24 | #include "unicode/unistr.h" | |
25 | ||
26 | U_NAMESPACE_BEGIN | |
27 | ||
28 | /** | |
29 | * Formattable objects can be passed to the Format class or | |
30 | * its subclasses for formatting. Formattable is a thin wrapper | |
31 | * class which interconverts between the primitive numeric types | |
32 | * (double, long, etc.) as well as UDate and UnicodeString. | |
33 | * <P> | |
34 | * Note that this is fundamentally different from the Java behavior, since | |
35 | * in this case the various formattable objects do not occupy a hierarchy, | |
36 | * but are all wrapped within this one class. Formattable encapsulates all | |
37 | * the polymorphism in itself. | |
38 | * <P> | |
39 | * It would be easy to change this so that Formattable was an abstract base | |
40 | * class of a genuine hierarchy, and that would clean up the code that | |
41 | * currently must explicitly check for type, but that seems like overkill at | |
42 | * this point. | |
43 | */ | |
44 | class U_I18N_API Formattable : public UObject { | |
45 | public: | |
46 | /** | |
47 | * This enum is only used to let callers distinguish between | |
48 | * the Formattable(UDate) constructor and the Formattable(double) | |
49 | * constructor; the compiler cannot distinguish the signatures, | |
50 | * since UDate is currently typedefed to be either double or long. | |
51 | * If UDate is changed later to be a bonafide class | |
52 | * or struct, then we no longer need this enum. | |
53 | * @draft ICU 2.4 | |
54 | */ | |
55 | enum ISDATE { kIsDate }; | |
56 | ||
57 | /** | |
58 | * Default constructor | |
59 | * @draft ICU 2.4 | |
60 | */ | |
61 | Formattable(); // Type kLong, value 0 | |
62 | /** | |
63 | * Creates a Formattable object with a UDate instance. | |
64 | * @param d the UDate instance. | |
65 | * @param flag the flag to indicate this is a date. Always set it to kIsDate | |
66 | * @stable ICU 2.0 | |
67 | */ | |
68 | Formattable(UDate d, ISDATE flag); | |
69 | /** | |
70 | * Creates a Formattable object with a double number. | |
71 | * @param d the double number. | |
72 | * @stable ICU 2.0 | |
73 | */ | |
74 | Formattable(double d); | |
75 | /** | |
76 | * Creates a Formattable object with a long number. | |
77 | * @param l the long number. | |
78 | * @stable ICU 2.0 | |
79 | */ | |
80 | Formattable(int32_t l); | |
81 | /** | |
82 | * Creates a Formattable object with a char string pointer. | |
83 | * Assumes that the char string is null terminated. | |
84 | * @param strToCopy the char string. | |
85 | * @stable ICU 2.0 | |
86 | */ | |
87 | Formattable(const char* strToCopy); | |
88 | /** | |
89 | * Creates a Formattable object with a UnicodeString object to copy from. | |
90 | * @param strToCopy the UnicodeString string. | |
91 | * @stable ICU 2.0 | |
92 | */ | |
93 | Formattable(const UnicodeString& strToCopy); | |
94 | /** | |
95 | * Creates a Formattable object with a UnicodeString object to adopt from. | |
96 | * @param strToAdopt the UnicodeString string. | |
97 | * @stable ICU 2.0 | |
98 | */ | |
99 | Formattable(UnicodeString* strToAdopt); | |
100 | /** | |
101 | * Creates a Formattable object with an array of Formattable objects. | |
102 | * @param arrayToCopy the Formattable object array. | |
103 | * @param count the array count. | |
104 | * @stable ICU 2.0 | |
105 | */ | |
106 | Formattable(const Formattable* arrayToCopy, int32_t count); | |
107 | ||
108 | /** | |
109 | * Copy constructor. | |
110 | * @stable ICU 2.0 | |
111 | */ | |
112 | Formattable(const Formattable&); | |
113 | /** | |
114 | * Assignment operator. | |
115 | * @param rhs The Formattable object to copy into this object. | |
116 | * @stable ICU 2.0 | |
117 | */ | |
118 | Formattable& operator=(const Formattable &rhs); | |
119 | /** | |
120 | * Equality comparison. | |
121 | * @param other the object to be compared with. | |
122 | * @return TRUE if other are equal to this, FALSE otherwise. | |
123 | * @stable ICU 2.0 | |
124 | */ | |
125 | UBool operator==(const Formattable &other) const; | |
126 | ||
127 | /** | |
128 | * Equality operator. | |
129 | * @param other the object to be compared with. | |
130 | * @return TRUE if other are unequal to this, FALSE otherwise. | |
131 | * @stable ICU 2.0 | |
132 | */ | |
133 | UBool operator!=(const Formattable& other) const | |
134 | { return !operator==(other); } | |
135 | ||
136 | /** | |
137 | * Destructor. | |
138 | * @stable ICU 2.0 | |
139 | */ | |
140 | virtual ~Formattable(); | |
141 | ||
142 | /** | |
143 | * The list of possible data types of this Formattable object. | |
144 | * @draft ICU 2.4 | |
145 | */ | |
146 | enum Type { | |
147 | kDate, // Date | |
148 | kDouble, // double | |
149 | kLong, // long | |
150 | kString, // UnicodeString | |
151 | kArray // Formattable[] | |
152 | }; | |
153 | ||
154 | /** | |
155 | * Gets the data type of this Formattable object. | |
156 | * @return the data type of this Formattable object. | |
157 | * @stable ICU 2.0 | |
158 | */ | |
159 | Type getType(void) const; | |
160 | ||
161 | /** | |
162 | * Gets the double value of this object. | |
163 | * @return the double value of this object. | |
164 | * @stable ICU 2.0 | |
165 | */ | |
166 | double getDouble(void) const { return fValue.fDouble; } | |
167 | /** | |
168 | * Gets the long value of this object. | |
169 | * @return the long value of this object. | |
170 | * @stable ICU 2.0 | |
171 | */ | |
172 | int32_t getLong(void) const { return fValue.fLong; } | |
173 | /** | |
174 | * Gets the Date value of this object. | |
175 | * @return the Date value of this object. | |
176 | * @stable ICU 2.0 | |
177 | */ | |
178 | UDate getDate(void) const { return fValue.fDate; } | |
179 | ||
180 | /** | |
181 | * Gets the string value of this object. | |
182 | * @param result Output param to receive the Date value of this object. | |
183 | * @return A reference to 'result'. | |
184 | * @stable ICU 2.0 | |
185 | */ | |
186 | UnicodeString& getString(UnicodeString& result) const | |
187 | { result=*fValue.fString; return result; } | |
188 | ||
189 | /** | |
190 | * Gets a const reference to the string value of this object. | |
191 | * @return a const reference to the string value of this object. | |
192 | * @stable ICU 2.0 | |
193 | */ | |
194 | inline const UnicodeString& getString(void) const; | |
195 | ||
196 | /** | |
197 | * Gets a reference to the string value of this object. | |
198 | * @return a reference to the string value of this object. | |
199 | * @stable ICU 2.0 | |
200 | */ | |
201 | inline UnicodeString& getString(void); | |
202 | ||
203 | /** | |
204 | * Gets the array value and count of this object. | |
205 | * @param count fill-in with the count of this object. | |
206 | * @return the array value of this object. | |
207 | * @stable ICU 2.0 | |
208 | */ | |
209 | const Formattable* getArray(int32_t& count) const | |
210 | { count=fValue.fArrayAndCount.fCount; return fValue.fArrayAndCount.fArray; } | |
211 | ||
212 | /** | |
213 | * Accesses the specified element in the array value of this Formattable object. | |
214 | * @param index the specified index. | |
215 | * @return the accessed element in the array. | |
216 | * @stable ICU 2.0 | |
217 | */ | |
218 | Formattable& operator[](int32_t index) { return fValue.fArrayAndCount.fArray[index]; } | |
219 | ||
220 | /** | |
221 | * Sets the double value of this object. | |
222 | * @param d the new double value to be set. | |
223 | * @stable ICU 2.0 | |
224 | */ | |
225 | void setDouble(double d); | |
226 | /** | |
227 | * Sets the long value of this object. | |
228 | * @param l the new long value to be set. | |
229 | * @stable ICU 2.0 | |
230 | */ | |
231 | void setLong(int32_t l); | |
232 | /** | |
233 | * Sets the Date value of this object. | |
234 | * @param d the new Date value to be set. | |
235 | * @stable ICU 2.0 | |
236 | */ | |
237 | void setDate(UDate d); | |
238 | /** | |
239 | * Sets the string value of this object. | |
240 | * @param stringToCopy the new string value to be set. | |
241 | * @stable ICU 2.0 | |
242 | */ | |
243 | void setString(const UnicodeString& stringToCopy); | |
244 | /** | |
245 | * Sets the array value and count of this object. | |
246 | * @param array the array value. | |
247 | * @param count the number of array elements to be copied. | |
248 | * @stable ICU 2.0 | |
249 | */ | |
250 | void setArray(const Formattable* array, int32_t count); | |
251 | /** | |
252 | * Sets and adopts the string value and count of this object. | |
253 | * @param stringToAdopt the new string value to be adopted. | |
254 | * @stable ICU 2.0 | |
255 | */ | |
256 | void adoptString(UnicodeString* stringToAdopt); | |
257 | /** | |
258 | * Sets and adopts the array value and count of this object. | |
259 | * @stable ICU 2.0 | |
260 | */ | |
261 | void adoptArray(Formattable* array, int32_t count); | |
262 | ||
263 | /** | |
264 | * ICU "poor man's RTTI", returns a UClassID for the actual class. | |
265 | * | |
266 | * @draft ICU 2.2 | |
267 | */ | |
268 | virtual inline UClassID getDynamicClassID() const; | |
269 | ||
270 | /** | |
271 | * ICU "poor man's RTTI", returns a UClassID for this class. | |
272 | * | |
273 | * @draft ICU 2.2 | |
274 | */ | |
275 | static inline UClassID getStaticClassID(); | |
276 | ||
277 | private: | |
278 | /** | |
279 | * Cleans up the memory for unwanted values. For example, the adopted | |
280 | * string or array objects. | |
281 | */ | |
282 | void dispose(void); | |
283 | ||
284 | /** | |
285 | * Creates a new Formattable array and copies the values from the specified | |
286 | * original. | |
287 | * @param array the original array | |
288 | * @param count the original array count | |
289 | * @return the new Formattable array. | |
290 | */ | |
291 | static Formattable* createArrayCopy(const Formattable* array, int32_t count); | |
292 | ||
293 | // Note: For now, we do not handle unsigned long and unsigned | |
294 | // double types. Smaller unsigned types, such as unsigned | |
295 | // short, can fit within a long. | |
296 | union { | |
297 | UnicodeString* fString; | |
298 | double fDouble; | |
299 | int32_t fLong; | |
300 | UDate fDate; | |
301 | struct | |
302 | { | |
303 | Formattable* fArray; | |
304 | int32_t fCount; | |
305 | } fArrayAndCount; | |
306 | } fValue; | |
307 | ||
308 | Type fType; | |
309 | ||
310 | /** | |
311 | * The address of this static class variable serves as this class's ID | |
312 | * for ICU "poor man's RTTI". | |
313 | */ | |
314 | static const char fgClassID; | |
315 | }; | |
316 | ||
317 | inline UClassID Formattable::getStaticClassID() | |
318 | { return (UClassID)&fgClassID; } | |
319 | ||
320 | inline UClassID Formattable::getDynamicClassID() const | |
321 | { return Formattable::getStaticClassID(); } | |
322 | ||
323 | inline Formattable* | |
324 | Formattable::createArrayCopy(const Formattable* array, int32_t count) | |
325 | { | |
326 | Formattable *result = new Formattable[count]; | |
327 | for (int32_t i=0; i<count; ++i) result[i] = array[i]; // Don't memcpy! | |
328 | return result; | |
329 | } | |
330 | ||
331 | inline const UnicodeString& Formattable::getString(void) const { | |
332 | return *fValue.fString; | |
333 | } | |
334 | ||
335 | inline UnicodeString& Formattable::getString(void) { | |
336 | return *fValue.fString; | |
337 | } | |
338 | ||
339 | U_NAMESPACE_END | |
340 | ||
341 | #endif /* #if !UCONFIG_NO_FORMATTING */ | |
342 | ||
343 | #endif //_FMTABLE | |
344 | //eof | |
345 |