ICU 73.2  73.2
fmtable.h
Go to the documentation of this file.
00001 // © 2016 and later: Unicode, Inc. and others.
00002 // License & terms of use: http://www.unicode.org/copyright.html
00003 /*
00004 ********************************************************************************
00005 *   Copyright (C) 1997-2014, International Business Machines
00006 *   Corporation and others.  All Rights Reserved.
00007 ********************************************************************************
00008 *
00009 * File FMTABLE.H
00010 *
00011 * Modification History:
00012 *
00013 *   Date        Name        Description
00014 *   02/29/97    aliu        Creation.
00015 ********************************************************************************
00016 */
00017 #ifndef FMTABLE_H
00018 #define FMTABLE_H
00019 
00020 #include "unicode/utypes.h"
00021 
00022 #if U_SHOW_CPLUSPLUS_API
00023 
00029 #if !UCONFIG_NO_FORMATTING
00030 
00031 #include "unicode/unistr.h"
00032 #include "unicode/stringpiece.h"
00033 #include "unicode/uformattable.h"
00034 
00035 U_NAMESPACE_BEGIN
00036 
00037 class CharString;
00038 namespace number {
00039 namespace impl {
00040 class DecimalQuantity;
00041 }
00042 }
00043 
00064 class U_I18N_API Formattable : public UObject {
00065 public:
00075     enum ISDATE { kIsDate };
00076 
00081     Formattable(); // Type kLong, value 0
00082 
00089     Formattable(UDate d, ISDATE flag);
00090 
00096     Formattable(double d);
00097 
00103     Formattable(int32_t l);
00104 
00110     Formattable(int64_t ll);
00111 
00112 #if !UCONFIG_NO_CONVERSION
00113 
00119     Formattable(const char* strToCopy);
00120 #endif
00121 
00135     Formattable(StringPiece number, UErrorCode &status);
00136 
00142     Formattable(const UnicodeString& strToCopy);
00143 
00149     Formattable(UnicodeString* strToAdopt);
00150 
00157     Formattable(const Formattable* arrayToCopy, int32_t count);
00158 
00164     Formattable(UObject* objectToAdopt);
00165 
00170     Formattable(const Formattable&);
00171 
00177     Formattable&    operator=(const Formattable &rhs);
00178 
00185     bool           operator==(const Formattable &other) const;
00186 
00193     bool           operator!=(const Formattable& other) const
00194       { return !operator==(other); }
00195 
00200     virtual         ~Formattable();
00201 
00213     Formattable *clone() const;
00214 
00221     enum Type {
00227         kDate,
00228 
00234         kDouble,
00235 
00241         kLong,
00242 
00248         kString,
00249 
00255         kArray,
00256 
00262         kInt64,
00263 
00269         kObject
00270    };
00271 
00277     Type            getType(void) const;
00278 
00285     UBool           isNumeric() const;
00286 
00293     double          getDouble(void) const { return fValue.fDouble; }
00294 
00307     double          getDouble(UErrorCode& status) const;
00308 
00315     int32_t         getLong(void) const { return (int32_t)fValue.fInt64; }
00316 
00333     int32_t         getLong(UErrorCode& status) const;
00334 
00341     int64_t         getInt64(void) const { return fValue.fInt64; }
00342 
00358     int64_t         getInt64(UErrorCode& status) const;
00359 
00366     UDate           getDate() const { return fValue.fDate; }
00367 
00376      UDate          getDate(UErrorCode& status) const;
00377 
00385     UnicodeString&  getString(UnicodeString& result) const
00386       { result=*fValue.fString; return result; }
00387 
00397     UnicodeString&  getString(UnicodeString& result, UErrorCode& status) const;
00398 
00406     inline const UnicodeString& getString(void) const;
00407 
00416     const UnicodeString& getString(UErrorCode& status) const;
00417 
00424     inline UnicodeString& getString(void);
00425 
00434     UnicodeString& getString(UErrorCode& status);
00435 
00443     const Formattable* getArray(int32_t& count) const
00444       { count=fValue.fArrayAndCount.fCount; return fValue.fArrayAndCount.fArray; }
00445 
00455     const Formattable* getArray(int32_t& count, UErrorCode& status) const;
00456 
00465     Formattable&    operator[](int32_t index) { return fValue.fArrayAndCount.fArray[index]; }
00466 
00473     const UObject*  getObject() const;
00474 
00493     StringPiece getDecimalNumber(UErrorCode &status);
00494 
00501     void            setDouble(double d);
00502 
00509     void            setLong(int32_t l);
00510 
00517     void            setInt64(int64_t ll);
00518 
00525     void            setDate(UDate d);
00526 
00533     void            setString(const UnicodeString& stringToCopy);
00534 
00542     void            setArray(const Formattable* array, int32_t count);
00543 
00550     void            adoptString(UnicodeString* stringToAdopt);
00551 
00557     void            adoptArray(Formattable* array, int32_t count);
00558 
00566     void            adoptObject(UObject* objectToAdopt);
00567 
00582     void             setDecimalNumber(StringPiece numberString,
00583                                       UErrorCode &status);
00584 
00590     virtual UClassID getDynamicClassID() const override;
00591 
00597     static UClassID U_EXPORT2 getStaticClassID();
00598 
00606     static inline Formattable *fromUFormattable(UFormattable *fmt);
00607 
00615     static inline const Formattable *fromUFormattable(const UFormattable *fmt);
00616 
00623     inline UFormattable *toUFormattable();
00624 
00631     inline const UFormattable *toUFormattable() const;
00632 
00633 #ifndef U_HIDE_DEPRECATED_API
00634 
00640     inline int32_t getLong(UErrorCode* status) const;
00641 #endif  /* U_HIDE_DEPRECATED_API */
00642 
00643 #ifndef U_HIDE_INTERNAL_API
00644 
00652     number::impl::DecimalQuantity *getDecimalQuantity() const { return fDecimalQuantity;}
00653 
00658     void populateDecimalQuantity(number::impl::DecimalQuantity& output, UErrorCode& status) const;
00659 
00666     void adoptDecimalQuantity(number::impl::DecimalQuantity *dq);
00667 
00674     CharString *internalGetCharString(UErrorCode &status);
00675 
00676 #endif  /* U_HIDE_INTERNAL_API */
00677 
00678 private:
00683     void            dispose(void);
00684 
00688     void            init();
00689 
00690     UnicodeString* getBogus() const;
00691 
00692     union {
00693         UObject*        fObject;
00694         UnicodeString*  fString;
00695         double          fDouble;
00696         int64_t         fInt64;
00697         UDate           fDate;
00698         struct {
00699           Formattable*  fArray;
00700           int32_t       fCount;
00701         }               fArrayAndCount;
00702     } fValue;
00703 
00704     CharString           *fDecimalStr;
00705 
00706     number::impl::DecimalQuantity *fDecimalQuantity;
00707 
00708     Type                fType;
00709     UnicodeString       fBogus; // Bogus string when it's needed.
00710 };
00711 
00712 inline UDate Formattable::getDate(UErrorCode& status) const {
00713     if (fType != kDate) {
00714         if (U_SUCCESS(status)) {
00715             status = U_INVALID_FORMAT_ERROR;
00716         }
00717         return 0;
00718     }
00719     return fValue.fDate;
00720 }
00721 
00722 inline const UnicodeString& Formattable::getString(void) const {
00723     return *fValue.fString;
00724 }
00725 
00726 inline UnicodeString& Formattable::getString(void) {
00727     return *fValue.fString;
00728 }
00729 
00730 #ifndef U_HIDE_DEPRECATED_API
00731 inline int32_t Formattable::getLong(UErrorCode* status) const {
00732     return getLong(*status);
00733 }
00734 #endif  /* U_HIDE_DEPRECATED_API */
00735 
00736 inline UFormattable* Formattable::toUFormattable() {
00737   return reinterpret_cast<UFormattable*>(this);
00738 }
00739 
00740 inline const UFormattable* Formattable::toUFormattable() const {
00741   return reinterpret_cast<const UFormattable*>(this);
00742 }
00743 
00744 inline Formattable* Formattable::fromUFormattable(UFormattable *fmt) {
00745   return reinterpret_cast<Formattable *>(fmt);
00746 }
00747 
00748 inline const Formattable* Formattable::fromUFormattable(const UFormattable *fmt) {
00749   return reinterpret_cast<const Formattable *>(fmt);
00750 }
00751 
00752 U_NAMESPACE_END
00753 
00754 #endif /* #if !UCONFIG_NO_FORMATTING */
00755 
00756 #endif /* U_SHOW_CPLUSPLUS_API */
00757 
00758 #endif //_FMTABLE
00759 //eof