]>
git.saurik.com Git - apple/icu.git/blob - icuSources/i18n/fmtable.cpp
2 *******************************************************************************
3 * Copyright (C) 1997-2001, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 *******************************************************************************
9 * Modification History:
11 * Date Name Description
12 * 03/25/97 clhuang Initial Implementation.
13 ********************************************************************************
16 #include "unicode/utypes.h"
18 #if !UCONFIG_NO_FORMATTING
20 #include "unicode/fmtable.h"
23 // *****************************************************************************
25 // *****************************************************************************
29 const char Formattable::fgClassID
=0;
31 // -------------------------------------
32 // default constructor.
33 // Creates a formattable object with a long value 0.
35 Formattable::Formattable()
36 : UObject(), fType(kLong
)
41 // -------------------------------------
42 // Creates a formattable object with a Date instance.
44 Formattable::Formattable(UDate date
, ISDATE
/*isDate*/)
45 : UObject(), fType(kDate
)
50 // -------------------------------------
51 // Creates a formattable object with a double value.
53 Formattable::Formattable(double value
)
54 : UObject(), fType(kDouble
)
56 fValue
.fDouble
= value
;
59 // -------------------------------------
60 // Creates a formattable object with a long value.
62 Formattable::Formattable(int32_t value
)
63 : UObject(), fType(kLong
)
68 // -------------------------------------
69 // Creates a formattable object with a char* string.
71 Formattable::Formattable(const char* stringToCopy
)
72 : UObject(), fType(kString
)
74 fValue
.fString
= new UnicodeString(stringToCopy
);
77 // -------------------------------------
78 // Creates a formattable object with a UnicodeString instance.
80 Formattable::Formattable(const UnicodeString
& stringToCopy
)
81 : UObject(), fType(kString
)
83 fValue
.fString
= new UnicodeString(stringToCopy
);
86 // -------------------------------------
87 // Creates a formattable object with a UnicodeString* value.
88 // (adopting symantics)
90 Formattable::Formattable(UnicodeString
* stringToAdopt
)
91 : UObject(), fType(kString
)
93 fValue
.fString
= stringToAdopt
;
96 // -------------------------------------
98 Formattable::Formattable(const Formattable
* arrayToCopy
, int32_t count
)
99 : UObject(), fType(kArray
)
101 fValue
.fArrayAndCount
.fArray
= createArrayCopy(arrayToCopy
, count
);
102 fValue
.fArrayAndCount
.fCount
= count
;
105 // -------------------------------------
108 Formattable::Formattable(const Formattable
&source
)
109 : UObject(source
), fType(kLong
)
114 // -------------------------------------
115 // assignment operator
118 Formattable::operator=(const Formattable
& source
)
122 // Disposes the current formattable value/setting.
125 // Sets the correct data type for this value.
126 fType
= source
.fType
;
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
);
136 // Sets the string value.
137 fValue
.fString
= new UnicodeString(*source
.fValue
.fString
);
140 // Sets the double value.
141 fValue
.fDouble
= source
.fValue
.fDouble
;
144 // Sets the long value.
145 fValue
.fLong
= source
.fValue
.fLong
;
148 // Sets the Date value.
149 fValue
.fDate
= source
.fValue
.fDate
;
156 // -------------------------------------
159 Formattable::operator==(const Formattable
& that
) const
162 if (this == &that
) return TRUE
;
164 // Returns FALSE if the data types are different.
165 if (fType
!= that
.fType
) return FALSE
;
167 // Compares the actual data values.
170 return fValue
.fDate
== that
.fValue
.fDate
;
172 return fValue
.fDouble
== that
.fValue
.fDouble
;
174 return fValue
.fLong
== that
.fValue
.fLong
;
176 return *(fValue
.fString
) == *(that
.fValue
.fString
);
178 if (fValue
.fArrayAndCount
.fCount
!= that
.fValue
.fArrayAndCount
.fCount
)
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
])
189 // -------------------------------------
191 Formattable::~Formattable()
196 // -------------------------------------
198 void Formattable::dispose()
200 // Deletes the data value if necessary.
203 delete fValue
.fString
;
206 delete[] fValue
.fArrayAndCount
.fArray
;
215 // -------------------------------------
216 // Gets the data type of this Formattable object.
218 Formattable::getType() const
223 // -------------------------------------
224 // Sets the value to a double value d.
227 Formattable::setDouble(double d
)
234 // -------------------------------------
235 // Sets the value to a long value l.
238 Formattable::setLong(int32_t l
)
245 // -------------------------------------
246 // Sets the value to a Date instance d.
249 Formattable::setDate(UDate d
)
256 // -------------------------------------
257 // Sets the value to a string value stringToCopy.
260 Formattable::setString(const UnicodeString
& stringToCopy
)
264 fValue
.fString
= new UnicodeString(stringToCopy
);
267 // -------------------------------------
268 // Sets the value to an array of Formattable objects.
271 Formattable::setArray(const Formattable
* array
, int32_t count
)
275 fValue
.fArrayAndCount
.fArray
= createArrayCopy(array
, count
);
276 fValue
.fArrayAndCount
.fCount
= count
;
279 // -------------------------------------
280 // Adopts the stringToAdopt value.
283 Formattable::adoptString(UnicodeString
* stringToAdopt
)
287 fValue
.fString
= stringToAdopt
;
290 // -------------------------------------
291 // Adopts the array value and its count.
294 Formattable::adoptArray(Formattable
* array
, int32_t count
)
298 fValue
.fArrayAndCount
.fArray
= array
;
299 fValue
.fArrayAndCount
.fCount
= count
;
303 //----------------------------------------------------
305 //----------------------------------------------------
308 #if U_IOSTREAM_SOURCE >= 199711
311 #elif U_IOSTREAM_SOURCE >= 198506
312 #include <iostream.h>
315 #include "unicode/datefmt.h"
318 class FormattableStreamer
/* not : public UObject because all methods are static */ {
320 static void streamOut(ostream
& stream
, const Formattable
& obj
);
323 FormattableStreamer() {} // private - forbid instantiation
326 // This is for debugging purposes only. This will send a displayable
327 // form of the Formattable object to the output stream.
330 FormattableStreamer::streamOut(ostream
& stream
, const Formattable
& obj
)
332 static DateFormat
*defDateFormat
= 0;
334 UnicodeString buffer
;
335 switch(obj
.getType()) {
336 case Formattable::kDate
:
337 // Creates a DateFormat instance for formatting the
339 if (defDateFormat
== 0) {
340 defDateFormat
= DateFormat::createInstance();
342 defDateFormat
->format(obj
.getDate(), buffer
);
345 case Formattable::kDouble
:
346 // Output the double as is.
347 stream
<< obj
.getDouble() << 'D';
349 case Formattable::kLong
:
350 // Output the double as is.
351 stream
<< obj
.getLong() << 'L';
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
) << '"';
358 case Formattable::kArray
:
360 const Formattable
* array
;
361 array
= obj
.getArray(count
);
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)) ? "" : ", " );
371 // Not a recognizable Formattable object.
372 stream
<< "INVALID_Formattable";
382 #endif /* #if !UCONFIG_NO_FORMATTING */