00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #ifndef FMTABLE_H
00016 #define FMTABLE_H
00017
00018 #include "unicode/utypes.h"
00019
00025 #if !UCONFIG_NO_FORMATTING
00026
00027 #include "unicode/unistr.h"
00028 #include "unicode/stringpiece.h"
00029 #include "unicode/uformattable.h"
00030
00031 U_NAMESPACE_BEGIN
00032
00033 class CharString;
00034 class DigitList;
00035
00040 #if U_PLATFORM == U_PF_OS400
00041 #define UNUM_INTERNAL_STACKARRAY_SIZE 144
00042 #else
00043 #define UNUM_INTERNAL_STACKARRAY_SIZE 128
00044 #endif
00045
00066 class U_I18N_API Formattable : public UObject {
00067 public:
00077 enum ISDATE { kIsDate };
00078
00083 Formattable();
00084
00091 Formattable(UDate d, ISDATE flag);
00092
00098 Formattable(double d);
00099
00105 Formattable(int32_t l);
00106
00112 Formattable(int64_t ll);
00113
00114 #if !UCONFIG_NO_CONVERSION
00115
00121 Formattable(const char* strToCopy);
00122 #endif
00123
00137 Formattable(const StringPiece &number, UErrorCode &status);
00138
00144 Formattable(const UnicodeString& strToCopy);
00145
00151 Formattable(UnicodeString* strToAdopt);
00152
00159 Formattable(const Formattable* arrayToCopy, int32_t count);
00160
00166 Formattable(UObject* objectToAdopt);
00167
00172 Formattable(const Formattable&);
00173
00179 Formattable& operator=(const Formattable &rhs);
00180
00187 UBool operator==(const Formattable &other) const;
00188
00195 UBool operator!=(const Formattable& other) const
00196 { return !operator==(other); }
00197
00202 virtual ~Formattable();
00203
00215 Formattable *clone() const;
00216
00223 enum Type {
00229 kDate,
00230
00236 kDouble,
00237
00243 kLong,
00244
00250 kString,
00251
00257 kArray,
00258
00264 kInt64,
00265
00271 kObject
00272 };
00273
00279 Type getType(void) const;
00280
00287 UBool isNumeric() const;
00288
00295 double getDouble(void) const { return fValue.fDouble; }
00296
00309 double getDouble(UErrorCode& status) const;
00310
00317 int32_t getLong(void) const { return (int32_t)fValue.fInt64; }
00318
00335 int32_t getLong(UErrorCode& status) const;
00336
00343 int64_t getInt64(void) const { return fValue.fInt64; }
00344
00360 int64_t getInt64(UErrorCode& status) const;
00361
00368 UDate getDate() const { return fValue.fDate; }
00369
00378 UDate getDate(UErrorCode& status) const;
00379
00387 UnicodeString& getString(UnicodeString& result) const
00388 { result=*fValue.fString; return result; }
00389
00399 UnicodeString& getString(UnicodeString& result, UErrorCode& status) const;
00400
00408 inline const UnicodeString& getString(void) const;
00409
00418 const UnicodeString& getString(UErrorCode& status) const;
00419
00426 inline UnicodeString& getString(void);
00427
00436 UnicodeString& getString(UErrorCode& status);
00437
00445 const Formattable* getArray(int32_t& count) const
00446 { count=fValue.fArrayAndCount.fCount; return fValue.fArrayAndCount.fArray; }
00447
00457 const Formattable* getArray(int32_t& count, UErrorCode& status) const;
00458
00467 Formattable& operator[](int32_t index) { return fValue.fArrayAndCount.fArray[index]; }
00468
00475 const UObject* getObject() const;
00476
00495 StringPiece getDecimalNumber(UErrorCode &status);
00496
00503 void setDouble(double d);
00504
00511 void setLong(int32_t l);
00512
00519 void setInt64(int64_t ll);
00520
00527 void setDate(UDate d);
00528
00535 void setString(const UnicodeString& stringToCopy);
00536
00544 void setArray(const Formattable* array, int32_t count);
00545
00552 void adoptString(UnicodeString* stringToAdopt);
00553
00559 void adoptArray(Formattable* array, int32_t count);
00560
00568 void adoptObject(UObject* objectToAdopt);
00569
00584 void setDecimalNumber(const StringPiece &numberString,
00585 UErrorCode &status);
00586
00592 virtual UClassID getDynamicClassID() const;
00593
00599 static UClassID U_EXPORT2 getStaticClassID();
00600
00608 static inline Formattable *fromUFormattable(UFormattable *fmt);
00609
00617 static inline const Formattable *fromUFormattable(const UFormattable *fmt);
00618
00625 inline UFormattable *toUFormattable();
00626
00633 inline const UFormattable *toUFormattable() const;
00634
00635 #ifndef U_HIDE_DEPRECATED_API
00636
00642 inline int32_t getLong(UErrorCode* status) const;
00643 #endif
00644
00645 #ifndef U_HIDE_INTERNAL_API
00646
00654 DigitList *getDigitList() const { return fDecimalNum;}
00655
00659 DigitList *getInternalDigitList();
00660
00667 void adoptDigitList(DigitList *dl);
00668
00675 CharString *internalGetCharString(UErrorCode &status);
00676
00677 #endif
00678
00679 private:
00684 void dispose(void);
00685
00689 void init();
00690
00691 UnicodeString* getBogus() const;
00692
00693 union {
00694 UObject* fObject;
00695 UnicodeString* fString;
00696 double fDouble;
00697 int64_t fInt64;
00698 UDate fDate;
00699 struct {
00700 Formattable* fArray;
00701 int32_t fCount;
00702 } fArrayAndCount;
00703 } fValue;
00704
00705 CharString *fDecimalStr;
00706
00707 DigitList *fDecimalNum;
00708
00709 char fStackData[UNUM_INTERNAL_STACKARRAY_SIZE];
00710
00711 Type fType;
00712 UnicodeString fBogus;
00713 };
00714
00715 inline UDate Formattable::getDate(UErrorCode& status) const {
00716 if (fType != kDate) {
00717 if (U_SUCCESS(status)) {
00718 status = U_INVALID_FORMAT_ERROR;
00719 }
00720 return 0;
00721 }
00722 return fValue.fDate;
00723 }
00724
00725 inline const UnicodeString& Formattable::getString(void) const {
00726 return *fValue.fString;
00727 }
00728
00729 inline UnicodeString& Formattable::getString(void) {
00730 return *fValue.fString;
00731 }
00732
00733 #ifndef U_HIDE_DEPRECATED_API
00734 inline int32_t Formattable::getLong(UErrorCode* status) const {
00735 return getLong(*status);
00736 }
00737 #endif
00738
00739 inline UFormattable* Formattable::toUFormattable() {
00740 return reinterpret_cast<UFormattable*>(this);
00741 }
00742
00743 inline const UFormattable* Formattable::toUFormattable() const {
00744 return reinterpret_cast<const UFormattable*>(this);
00745 }
00746
00747 inline Formattable* Formattable::fromUFormattable(UFormattable *fmt) {
00748 return reinterpret_cast<Formattable *>(fmt);
00749 }
00750
00751 inline const Formattable* Formattable::fromUFormattable(const UFormattable *fmt) {
00752 return reinterpret_cast<const Formattable *>(fmt);
00753 }
00754
00755 U_NAMESPACE_END
00756
00757 #endif
00758
00759 #endif //_FMTABLE
00760