00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef FMTABLE_H
00018 #define FMTABLE_H
00019
00020 #include "unicode/utypes.h"
00021
00027 #if !UCONFIG_NO_FORMATTING
00028
00029 #include "unicode/unistr.h"
00030 #include "unicode/stringpiece.h"
00031 #include "unicode/uformattable.h"
00032
00033 U_NAMESPACE_BEGIN
00034
00035 class CharString;
00036 class DigitList;
00037
00042 #if U_PLATFORM == U_PF_OS400
00043 #define UNUM_INTERNAL_STACKARRAY_SIZE 144
00044 #else
00045 #define UNUM_INTERNAL_STACKARRAY_SIZE 128
00046 #endif
00047
00068 class U_I18N_API Formattable : public UObject {
00069 public:
00079 enum ISDATE { kIsDate };
00080
00085 Formattable();
00086
00093 Formattable(UDate d, ISDATE flag);
00094
00100 Formattable(double d);
00101
00107 Formattable(int32_t l);
00108
00114 Formattable(int64_t ll);
00115
00116 #if !UCONFIG_NO_CONVERSION
00117
00123 Formattable(const char* strToCopy);
00124 #endif
00125
00139 Formattable(StringPiece number, UErrorCode &status);
00140
00146 Formattable(const UnicodeString& strToCopy);
00147
00153 Formattable(UnicodeString* strToAdopt);
00154
00161 Formattable(const Formattable* arrayToCopy, int32_t count);
00162
00168 Formattable(UObject* objectToAdopt);
00169
00174 Formattable(const Formattable&);
00175
00181 Formattable& operator=(const Formattable &rhs);
00182
00189 UBool operator==(const Formattable &other) const;
00190
00197 UBool operator!=(const Formattable& other) const
00198 { return !operator==(other); }
00199
00204 virtual ~Formattable();
00205
00217 Formattable *clone() const;
00218
00225 enum Type {
00231 kDate,
00232
00238 kDouble,
00239
00245 kLong,
00246
00252 kString,
00253
00259 kArray,
00260
00266 kInt64,
00267
00273 kObject
00274 };
00275
00281 Type getType(void) const;
00282
00289 UBool isNumeric() const;
00290
00297 double getDouble(void) const { return fValue.fDouble; }
00298
00311 double getDouble(UErrorCode& status) const;
00312
00319 int32_t getLong(void) const { return (int32_t)fValue.fInt64; }
00320
00337 int32_t getLong(UErrorCode& status) const;
00338
00345 int64_t getInt64(void) const { return fValue.fInt64; }
00346
00362 int64_t getInt64(UErrorCode& status) const;
00363
00370 UDate getDate() const { return fValue.fDate; }
00371
00380 UDate getDate(UErrorCode& status) const;
00381
00389 UnicodeString& getString(UnicodeString& result) const
00390 { result=*fValue.fString; return result; }
00391
00401 UnicodeString& getString(UnicodeString& result, UErrorCode& status) const;
00402
00410 inline const UnicodeString& getString(void) const;
00411
00420 const UnicodeString& getString(UErrorCode& status) const;
00421
00428 inline UnicodeString& getString(void);
00429
00438 UnicodeString& getString(UErrorCode& status);
00439
00447 const Formattable* getArray(int32_t& count) const
00448 { count=fValue.fArrayAndCount.fCount; return fValue.fArrayAndCount.fArray; }
00449
00459 const Formattable* getArray(int32_t& count, UErrorCode& status) const;
00460
00469 Formattable& operator[](int32_t index) { return fValue.fArrayAndCount.fArray[index]; }
00470
00477 const UObject* getObject() const;
00478
00497 StringPiece getDecimalNumber(UErrorCode &status);
00498
00505 void setDouble(double d);
00506
00513 void setLong(int32_t l);
00514
00521 void setInt64(int64_t ll);
00522
00529 void setDate(UDate d);
00530
00537 void setString(const UnicodeString& stringToCopy);
00538
00546 void setArray(const Formattable* array, int32_t count);
00547
00554 void adoptString(UnicodeString* stringToAdopt);
00555
00561 void adoptArray(Formattable* array, int32_t count);
00562
00570 void adoptObject(UObject* objectToAdopt);
00571
00586 void setDecimalNumber(StringPiece numberString,
00587 UErrorCode &status);
00588
00594 virtual UClassID getDynamicClassID() const;
00595
00601 static UClassID U_EXPORT2 getStaticClassID();
00602
00610 static inline Formattable *fromUFormattable(UFormattable *fmt);
00611
00619 static inline const Formattable *fromUFormattable(const UFormattable *fmt);
00620
00627 inline UFormattable *toUFormattable();
00628
00635 inline const UFormattable *toUFormattable() const;
00636
00637 #ifndef U_HIDE_DEPRECATED_API
00638
00644 inline int32_t getLong(UErrorCode* status) const;
00645 #endif
00646
00647 #ifndef U_HIDE_INTERNAL_API
00648
00656 DigitList *getDigitList() const { return fDecimalNum;}
00657
00661 DigitList *getInternalDigitList();
00662
00669 void adoptDigitList(DigitList *dl);
00670
00677 CharString *internalGetCharString(UErrorCode &status);
00678
00679 #endif
00680
00681 private:
00686 void dispose(void);
00687
00691 void init();
00692
00693 UnicodeString* getBogus() const;
00694
00695 union {
00696 UObject* fObject;
00697 UnicodeString* fString;
00698 double fDouble;
00699 int64_t fInt64;
00700 UDate fDate;
00701 struct {
00702 Formattable* fArray;
00703 int32_t fCount;
00704 } fArrayAndCount;
00705 } fValue;
00706
00707 CharString *fDecimalStr;
00708
00709 DigitList *fDecimalNum;
00710
00711 char fStackData[UNUM_INTERNAL_STACKARRAY_SIZE];
00712
00713 Type fType;
00714 UnicodeString fBogus;
00715 };
00716
00717 inline UDate Formattable::getDate(UErrorCode& status) const {
00718 if (fType != kDate) {
00719 if (U_SUCCESS(status)) {
00720 status = U_INVALID_FORMAT_ERROR;
00721 }
00722 return 0;
00723 }
00724 return fValue.fDate;
00725 }
00726
00727 inline const UnicodeString& Formattable::getString(void) const {
00728 return *fValue.fString;
00729 }
00730
00731 inline UnicodeString& Formattable::getString(void) {
00732 return *fValue.fString;
00733 }
00734
00735 #ifndef U_HIDE_DEPRECATED_API
00736 inline int32_t Formattable::getLong(UErrorCode* status) const {
00737 return getLong(*status);
00738 }
00739 #endif
00740
00741 inline UFormattable* Formattable::toUFormattable() {
00742 return reinterpret_cast<UFormattable*>(this);
00743 }
00744
00745 inline const UFormattable* Formattable::toUFormattable() const {
00746 return reinterpret_cast<const UFormattable*>(this);
00747 }
00748
00749 inline Formattable* Formattable::fromUFormattable(UFormattable *fmt) {
00750 return reinterpret_cast<Formattable *>(fmt);
00751 }
00752
00753 inline const Formattable* Formattable::fromUFormattable(const UFormattable *fmt) {
00754 return reinterpret_cast<const Formattable *>(fmt);
00755 }
00756
00757 U_NAMESPACE_END
00758
00759 #endif
00760
00761 #endif //_FMTABLE
00762