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 namespace number {
00037 namespace impl {
00038 class DecimalQuantity;
00039 }
00040 }
00041
00062 class U_I18N_API Formattable : public UObject {
00063 public:
00073 enum ISDATE { kIsDate };
00074
00079 Formattable();
00080
00087 Formattable(UDate d, ISDATE flag);
00088
00094 Formattable(double d);
00095
00101 Formattable(int32_t l);
00102
00108 Formattable(int64_t ll);
00109
00110 #if !UCONFIG_NO_CONVERSION
00111
00117 Formattable(const char* strToCopy);
00118 #endif
00119
00133 Formattable(StringPiece number, UErrorCode &status);
00134
00140 Formattable(const UnicodeString& strToCopy);
00141
00147 Formattable(UnicodeString* strToAdopt);
00148
00155 Formattable(const Formattable* arrayToCopy, int32_t count);
00156
00162 Formattable(UObject* objectToAdopt);
00163
00168 Formattable(const Formattable&);
00169
00175 Formattable& operator=(const Formattable &rhs);
00176
00183 UBool operator==(const Formattable &other) const;
00184
00191 UBool operator!=(const Formattable& other) const
00192 { return !operator==(other); }
00193
00198 virtual ~Formattable();
00199
00211 Formattable *clone() const;
00212
00219 enum Type {
00225 kDate,
00226
00232 kDouble,
00233
00239 kLong,
00240
00246 kString,
00247
00253 kArray,
00254
00260 kInt64,
00261
00267 kObject
00268 };
00269
00275 Type getType(void) const;
00276
00283 UBool isNumeric() const;
00284
00291 double getDouble(void) const { return fValue.fDouble; }
00292
00305 double getDouble(UErrorCode& status) const;
00306
00313 int32_t getLong(void) const { return (int32_t)fValue.fInt64; }
00314
00331 int32_t getLong(UErrorCode& status) const;
00332
00339 int64_t getInt64(void) const { return fValue.fInt64; }
00340
00356 int64_t getInt64(UErrorCode& status) const;
00357
00364 UDate getDate() const { return fValue.fDate; }
00365
00374 UDate getDate(UErrorCode& status) const;
00375
00383 UnicodeString& getString(UnicodeString& result) const
00384 { result=*fValue.fString; return result; }
00385
00395 UnicodeString& getString(UnicodeString& result, UErrorCode& status) const;
00396
00404 inline const UnicodeString& getString(void) const;
00405
00414 const UnicodeString& getString(UErrorCode& status) const;
00415
00422 inline UnicodeString& getString(void);
00423
00432 UnicodeString& getString(UErrorCode& status);
00433
00441 const Formattable* getArray(int32_t& count) const
00442 { count=fValue.fArrayAndCount.fCount; return fValue.fArrayAndCount.fArray; }
00443
00453 const Formattable* getArray(int32_t& count, UErrorCode& status) const;
00454
00463 Formattable& operator[](int32_t index) { return fValue.fArrayAndCount.fArray[index]; }
00464
00471 const UObject* getObject() const;
00472
00491 StringPiece getDecimalNumber(UErrorCode &status);
00492
00499 void setDouble(double d);
00500
00507 void setLong(int32_t l);
00508
00515 void setInt64(int64_t ll);
00516
00523 void setDate(UDate d);
00524
00531 void setString(const UnicodeString& stringToCopy);
00532
00540 void setArray(const Formattable* array, int32_t count);
00541
00548 void adoptString(UnicodeString* stringToAdopt);
00549
00555 void adoptArray(Formattable* array, int32_t count);
00556
00564 void adoptObject(UObject* objectToAdopt);
00565
00580 void setDecimalNumber(StringPiece numberString,
00581 UErrorCode &status);
00582
00588 virtual UClassID getDynamicClassID() const;
00589
00595 static UClassID U_EXPORT2 getStaticClassID();
00596
00604 static inline Formattable *fromUFormattable(UFormattable *fmt);
00605
00613 static inline const Formattable *fromUFormattable(const UFormattable *fmt);
00614
00621 inline UFormattable *toUFormattable();
00622
00629 inline const UFormattable *toUFormattable() const;
00630
00631 #ifndef U_HIDE_DEPRECATED_API
00632
00638 inline int32_t getLong(UErrorCode* status) const;
00639 #endif
00640
00641 #ifndef U_HIDE_INTERNAL_API
00642
00650 number::impl::DecimalQuantity *getDecimalQuantity() const { return fDecimalQuantity;}
00651
00656 void populateDecimalQuantity(number::impl::DecimalQuantity& output, UErrorCode& status) const;
00657
00664 void adoptDecimalQuantity(number::impl::DecimalQuantity *dq);
00665
00672 CharString *internalGetCharString(UErrorCode &status);
00673
00674 #endif
00675
00676 private:
00681 void dispose(void);
00682
00686 void init();
00687
00688 UnicodeString* getBogus() const;
00689
00690 union {
00691 UObject* fObject;
00692 UnicodeString* fString;
00693 double fDouble;
00694 int64_t fInt64;
00695 UDate fDate;
00696 struct {
00697 Formattable* fArray;
00698 int32_t fCount;
00699 } fArrayAndCount;
00700 } fValue;
00701
00702 CharString *fDecimalStr;
00703
00704 number::impl::DecimalQuantity *fDecimalQuantity;
00705
00706 Type fType;
00707 UnicodeString fBogus;
00708 };
00709
00710 inline UDate Formattable::getDate(UErrorCode& status) const {
00711 if (fType != kDate) {
00712 if (U_SUCCESS(status)) {
00713 status = U_INVALID_FORMAT_ERROR;
00714 }
00715 return 0;
00716 }
00717 return fValue.fDate;
00718 }
00719
00720 inline const UnicodeString& Formattable::getString(void) const {
00721 return *fValue.fString;
00722 }
00723
00724 inline UnicodeString& Formattable::getString(void) {
00725 return *fValue.fString;
00726 }
00727
00728 #ifndef U_HIDE_DEPRECATED_API
00729 inline int32_t Formattable::getLong(UErrorCode* status) const {
00730 return getLong(*status);
00731 }
00732 #endif
00733
00734 inline UFormattable* Formattable::toUFormattable() {
00735 return reinterpret_cast<UFormattable*>(this);
00736 }
00737
00738 inline const UFormattable* Formattable::toUFormattable() const {
00739 return reinterpret_cast<const UFormattable*>(this);
00740 }
00741
00742 inline Formattable* Formattable::fromUFormattable(UFormattable *fmt) {
00743 return reinterpret_cast<Formattable *>(fmt);
00744 }
00745
00746 inline const Formattable* Formattable::fromUFormattable(const UFormattable *fmt) {
00747 return reinterpret_cast<const Formattable *>(fmt);
00748 }
00749
00750 U_NAMESPACE_END
00751
00752 #endif
00753
00754 #endif //_FMTABLE
00755