00001 /* 00002 ******************************************************************************** 00003 * Copyright (C) 1997-1999, International Business Machines 00004 * Corporation and others. All Rights Reserved. 00005 ******************************************************************************** 00006 * 00007 * File FMTABLE.H 00008 * 00009 * Modification History: 00010 * 00011 * Date Name Description 00012 * 02/29/97 aliu Creation. 00013 ******************************************************************************** 00014 */ 00015 #ifndef FMTABLE_H 00016 #define FMTABLE_H 00017 00018 00019 #include "unicode/utypes.h" 00020 #include "unicode/unistr.h" 00021 00038 class U_I18N_API Formattable { 00039 public: 00048 enum ISDATE { kIsDate }; 00049 00050 Formattable(); // Type kLong, value 0 00057 Formattable(UDate d, ISDATE); 00063 Formattable(double d); 00069 Formattable(int32_t l); 00076 Formattable(const char* strToCopy); 00082 Formattable(const UnicodeString& stringToCopy); 00088 Formattable(UnicodeString* stringToAdopt); 00095 Formattable(const Formattable* arrayToCopy, int32_t count); 00096 00101 Formattable(const Formattable&); 00106 Formattable& operator=(const Formattable&); 00111 UBool operator==(const Formattable&) const; 00112 UBool operator!=(const Formattable& other) const 00113 { return !operator==(other); } 00114 00119 virtual ~Formattable(); 00120 00124 enum Type { 00125 kDate, // Date 00126 kDouble, // double 00127 kLong, // long 00128 kString, // UnicodeString 00129 kArray // Formattable[] 00130 }; 00131 00136 Type getType(void) const; 00137 00142 double getDouble(void) const { return fValue.fDouble; } 00147 int32_t getLong(void) const { return fValue.fLong; } 00152 UDate getDate(void) const { return fValue.fDate; } 00153 00158 UnicodeString& getString(UnicodeString& result) const 00159 { result=*fValue.fString; return result; } 00160 00165 const Formattable* getArray(int32_t& count) const 00166 { count=fValue.fArrayAndCount.fCount; return fValue.fArrayAndCount.fArray; } 00167 00174 Formattable& operator[](int32_t index) { return fValue.fArrayAndCount.fArray[index]; } 00175 00180 void setDouble(double d); 00185 void setLong(int32_t l); 00190 void setDate(UDate d); 00195 void setString(const UnicodeString& stringToCopy); 00200 void setArray(const Formattable* array, int32_t count); 00205 void adoptString(UnicodeString* stringToAdopt); 00210 void adoptArray(Formattable* array, int32_t count); 00211 00212 private: 00217 void dispose(void); 00218 00226 static Formattable* createArrayCopy(const Formattable* array, int32_t count); 00227 00228 // Note: For now, we do not handle unsigned long and unsigned 00229 // double types. Smaller unsigned types, such as unsigned 00230 // short, can fit within a long. 00231 union { 00232 UnicodeString* fString; 00233 double fDouble; 00234 int32_t fLong; 00235 UDate fDate; 00236 struct 00237 { 00238 Formattable* fArray; 00239 int32_t fCount; 00240 } fArrayAndCount; 00241 } fValue; 00242 00243 Type fType; 00244 }; 00245 00246 inline Formattable* 00247 Formattable::createArrayCopy(const Formattable* array, int32_t count) 00248 { 00249 Formattable *result = new Formattable[count]; 00250 for (int32_t i=0; i<count; ++i) result[i] = array[i]; // Don't memcpy! 00251 return result; 00252 } 00253 00254 #endif //_FMTABLE 00255 //eof 00256