]> git.saurik.com Git - apple/icu.git/blob - icuSources/i18n/fmtable.cpp
ICU-3.13.tar.gz
[apple/icu.git] / icuSources / i18n / fmtable.cpp
1 /*
2 *******************************************************************************
3 * Copyright (C) 1997-2001, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 *******************************************************************************
6 *
7 * File FMTABLE.CPP
8 *
9 * Modification History:
10 *
11 * Date Name Description
12 * 03/25/97 clhuang Initial Implementation.
13 ********************************************************************************
14 */
15
16 #include "unicode/utypes.h"
17
18 #if !UCONFIG_NO_FORMATTING
19
20 #include "unicode/fmtable.h"
21 #include "cmemory.h"
22
23 // *****************************************************************************
24 // class Formattable
25 // *****************************************************************************
26
27 U_NAMESPACE_BEGIN
28
29 const char Formattable::fgClassID=0;
30
31 // -------------------------------------
32 // default constructor.
33 // Creates a formattable object with a long value 0.
34
35 Formattable::Formattable()
36 : UObject(), fType(kLong)
37 {
38 fValue.fLong = 0;
39 }
40
41 // -------------------------------------
42 // Creates a formattable object with a Date instance.
43
44 Formattable::Formattable(UDate date, ISDATE /*isDate*/)
45 : UObject(), fType(kDate)
46 {
47 fValue.fDate = date;
48 }
49
50 // -------------------------------------
51 // Creates a formattable object with a double value.
52
53 Formattable::Formattable(double value)
54 : UObject(), fType(kDouble)
55 {
56 fValue.fDouble = value;
57 }
58
59 // -------------------------------------
60 // Creates a formattable object with a long value.
61
62 Formattable::Formattable(int32_t value)
63 : UObject(), fType(kLong)
64 {
65 fValue.fLong = value;
66 }
67
68 // -------------------------------------
69 // Creates a formattable object with a char* string.
70
71 Formattable::Formattable(const char* stringToCopy)
72 : UObject(), fType(kString)
73 {
74 fValue.fString = new UnicodeString(stringToCopy);
75 }
76
77 // -------------------------------------
78 // Creates a formattable object with a UnicodeString instance.
79
80 Formattable::Formattable(const UnicodeString& stringToCopy)
81 : UObject(), fType(kString)
82 {
83 fValue.fString = new UnicodeString(stringToCopy);
84 }
85
86 // -------------------------------------
87 // Creates a formattable object with a UnicodeString* value.
88 // (adopting symantics)
89
90 Formattable::Formattable(UnicodeString* stringToAdopt)
91 : UObject(), fType(kString)
92 {
93 fValue.fString = stringToAdopt;
94 }
95
96 // -------------------------------------
97
98 Formattable::Formattable(const Formattable* arrayToCopy, int32_t count)
99 : UObject(), fType(kArray)
100 {
101 fValue.fArrayAndCount.fArray = createArrayCopy(arrayToCopy, count);
102 fValue.fArrayAndCount.fCount = count;
103 }
104
105 // -------------------------------------
106 // copy constructor
107
108 Formattable::Formattable(const Formattable &source)
109 : UObject(source), fType(kLong)
110 {
111 *this = source;
112 }
113
114 // -------------------------------------
115 // assignment operator
116
117 Formattable&
118 Formattable::operator=(const Formattable& source)
119 {
120 if (this != &source)
121 {
122 // Disposes the current formattable value/setting.
123 dispose();
124
125 // Sets the correct data type for this value.
126 fType = source.fType;
127 switch (fType)
128 {
129 case kArray:
130 // Sets each element in the array one by one and records the array count.
131 fValue.fArrayAndCount.fCount = source.fValue.fArrayAndCount.fCount;
132 fValue.fArrayAndCount.fArray = createArrayCopy(source.fValue.fArrayAndCount.fArray,
133 source.fValue.fArrayAndCount.fCount);
134 break;
135 case kString:
136 // Sets the string value.
137 fValue.fString = new UnicodeString(*source.fValue.fString);
138 break;
139 case kDouble:
140 // Sets the double value.
141 fValue.fDouble = source.fValue.fDouble;
142 break;
143 case kLong:
144 // Sets the long value.
145 fValue.fLong = source.fValue.fLong;
146 break;
147 case kDate:
148 // Sets the Date value.
149 fValue.fDate = source.fValue.fDate;
150 break;
151 }
152 }
153 return *this;
154 }
155
156 // -------------------------------------
157
158 UBool
159 Formattable::operator==(const Formattable& that) const
160 {
161 // Checks class ID.
162 if (this == &that) return TRUE;
163
164 // Returns FALSE if the data types are different.
165 if (fType != that.fType) return FALSE;
166
167 // Compares the actual data values.
168 switch (fType) {
169 case kDate:
170 return fValue.fDate == that.fValue.fDate;
171 case kDouble:
172 return fValue.fDouble == that.fValue.fDouble;
173 case kLong:
174 return fValue.fLong == that.fValue.fLong;
175 case kString:
176 return *(fValue.fString) == *(that.fValue.fString);
177 case kArray:
178 if (fValue.fArrayAndCount.fCount != that.fValue.fArrayAndCount.fCount)
179 return FALSE;
180 // Checks each element for equality.
181 for (int32_t i=0; i<fValue.fArrayAndCount.fCount; ++i)
182 if (fValue.fArrayAndCount.fArray[i] != that.fValue.fArrayAndCount.fArray[i])
183 return FALSE;
184 break;
185 }
186 return TRUE;
187 }
188
189 // -------------------------------------
190
191 Formattable::~Formattable()
192 {
193 dispose();
194 }
195
196 // -------------------------------------
197
198 void Formattable::dispose()
199 {
200 // Deletes the data value if necessary.
201 switch (fType) {
202 case kString:
203 delete fValue.fString;
204 break;
205 case kArray:
206 delete[] fValue.fArrayAndCount.fArray;
207 break;
208 case kDate:
209 case kDouble:
210 case kLong:
211 break;
212 }
213 }
214
215 // -------------------------------------
216 // Gets the data type of this Formattable object.
217 Formattable::Type
218 Formattable::getType() const
219 {
220 return fType;
221 }
222
223 // -------------------------------------
224 // Sets the value to a double value d.
225
226 void
227 Formattable::setDouble(double d)
228 {
229 dispose();
230 fType = kDouble;
231 fValue.fDouble = d;
232 }
233
234 // -------------------------------------
235 // Sets the value to a long value l.
236
237 void
238 Formattable::setLong(int32_t l)
239 {
240 dispose();
241 fType = kLong;
242 fValue.fLong = l;
243 }
244
245 // -------------------------------------
246 // Sets the value to a Date instance d.
247
248 void
249 Formattable::setDate(UDate d)
250 {
251 dispose();
252 fType = kDate;
253 fValue.fDate = d;
254 }
255
256 // -------------------------------------
257 // Sets the value to a string value stringToCopy.
258
259 void
260 Formattable::setString(const UnicodeString& stringToCopy)
261 {
262 dispose();
263 fType = kString;
264 fValue.fString = new UnicodeString(stringToCopy);
265 }
266
267 // -------------------------------------
268 // Sets the value to an array of Formattable objects.
269
270 void
271 Formattable::setArray(const Formattable* array, int32_t count)
272 {
273 dispose();
274 fType = kArray;
275 fValue.fArrayAndCount.fArray = createArrayCopy(array, count);
276 fValue.fArrayAndCount.fCount = count;
277 }
278
279 // -------------------------------------
280 // Adopts the stringToAdopt value.
281
282 void
283 Formattable::adoptString(UnicodeString* stringToAdopt)
284 {
285 dispose();
286 fType = kString;
287 fValue.fString = stringToAdopt;
288 }
289
290 // -------------------------------------
291 // Adopts the array value and its count.
292
293 void
294 Formattable::adoptArray(Formattable* array, int32_t count)
295 {
296 dispose();
297 fType = kArray;
298 fValue.fArrayAndCount.fArray = array;
299 fValue.fArrayAndCount.fCount = count;
300 }
301
302 #if 0
303 //----------------------------------------------------
304 // console I/O
305 //----------------------------------------------------
306 #ifdef _DEBUG
307
308 #if U_IOSTREAM_SOURCE >= 199711
309 #include <iostream>
310 using namespace std;
311 #elif U_IOSTREAM_SOURCE >= 198506
312 #include <iostream.h>
313 #endif
314
315 #include "unicode/datefmt.h"
316 #include "unistrm.h"
317
318 class FormattableStreamer /* not : public UObject because all methods are static */ {
319 public:
320 static void streamOut(ostream& stream, const Formattable& obj);
321
322 private:
323 FormattableStreamer() {} // private - forbid instantiation
324 };
325
326 // This is for debugging purposes only. This will send a displayable
327 // form of the Formattable object to the output stream.
328
329 void
330 FormattableStreamer::streamOut(ostream& stream, const Formattable& obj)
331 {
332 static DateFormat *defDateFormat = 0;
333
334 UnicodeString buffer;
335 switch(obj.getType()) {
336 case Formattable::kDate :
337 // Creates a DateFormat instance for formatting the
338 // Date instance.
339 if (defDateFormat == 0) {
340 defDateFormat = DateFormat::createInstance();
341 }
342 defDateFormat->format(obj.getDate(), buffer);
343 stream << buffer;
344 break;
345 case Formattable::kDouble :
346 // Output the double as is.
347 stream << obj.getDouble() << 'D';
348 break;
349 case Formattable::kLong :
350 // Output the double as is.
351 stream << obj.getLong() << 'L';
352 break;
353 case Formattable::kString:
354 // Output the double as is. Please see UnicodeString console
355 // I/O routine for more details.
356 stream << '"' << obj.getString(buffer) << '"';
357 break;
358 case Formattable::kArray:
359 int32_t i, count;
360 const Formattable* array;
361 array = obj.getArray(count);
362 stream << '[';
363 // Recursively calling the console I/O routine for each element in the array.
364 for (i=0; i<count; ++i) {
365 FormattableStreamer::streamOut(stream, array[i]);
366 stream << ( (i==(count-1)) ? "" : ", " );
367 }
368 stream << ']';
369 break;
370 default:
371 // Not a recognizable Formattable object.
372 stream << "INVALID_Formattable";
373 }
374 stream.flush();
375 }
376 #endif
377
378 #endif
379
380 U_NAMESPACE_END
381
382 #endif /* #if !UCONFIG_NO_FORMATTING */
383
384 //eof