ICU 50.1.2  50.1.2
fmtable.h
Go to the documentation of this file.
1 /*
2 ********************************************************************************
3 * Copyright (C) 1997-2012, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 ********************************************************************************
6 *
7 * File FMTABLE.H
8 *
9 * Modification History:
10 *
11 * Date Name Description
12 * 02/29/97 aliu Creation.
13 ********************************************************************************
14 */
15 #ifndef FMTABLE_H
16 #define FMTABLE_H
17 
18 #include "unicode/utypes.h"
19 #include "unicode/unistr.h"
20 #include "unicode/stringpiece.h"
21 
27 #if !UCONFIG_NO_FORMATTING
28 
30 
31 class CharString;
32 class DigitList;
33 
38 #if U_PLATFORM == U_PF_OS400
39 #define UNUM_INTERNAL_STACKARRAY_SIZE 144
40 #else
41 #define UNUM_INTERNAL_STACKARRAY_SIZE 128
42 #endif
43 
62 class U_I18N_API Formattable : public UObject {
63 public:
73  enum ISDATE { kIsDate };
74 
79  Formattable(); // Type kLong, value 0
80 
87  Formattable(UDate d, ISDATE flag);
88 
94  Formattable(double d);
95 
101  Formattable(int32_t l);
102 
108  Formattable(int64_t ll);
109 
110 #if !UCONFIG_NO_CONVERSION
111 
117  Formattable(const char* strToCopy);
118 #endif
119 
133  Formattable(const StringPiece &number, UErrorCode &status);
134 
140  Formattable(const UnicodeString& strToCopy);
141 
147  Formattable(UnicodeString* strToAdopt);
148 
155  Formattable(const Formattable* arrayToCopy, int32_t count);
156 
162  Formattable(UObject* objectToAdopt);
163 
168  Formattable(const Formattable&);
169 
175  Formattable& operator=(const Formattable &rhs);
176 
183  UBool operator==(const Formattable &other) const;
184 
191  UBool operator!=(const Formattable& other) const
192  { return !operator==(other); }
193 
198  virtual ~Formattable();
199 
211  Formattable *clone() const;
212 
219  enum Type {
226 
233 
240 
247 
254 
261 
267  kObject
268  };
269 
275  Type getType(void) const;
276 
283  UBool isNumeric() const;
284 
291  double getDouble(void) const { return fValue.fDouble; }
292 
305  double getDouble(UErrorCode& status) const;
306 
313  int32_t getLong(void) const { return (int32_t)fValue.fInt64; }
314 
331  int32_t getLong(UErrorCode& status) const;
332 
339  int64_t getInt64(void) const { return fValue.fInt64; }
340 
356  int64_t getInt64(UErrorCode& status) const;
357 
364  UDate getDate() const { return fValue.fDate; }
365 
374  UDate getDate(UErrorCode& status) const;
375 
383  UnicodeString& getString(UnicodeString& result) const
384  { result=*fValue.fString; return result; }
385 
395  UnicodeString& getString(UnicodeString& result, UErrorCode& status) const;
396 
404  inline const UnicodeString& getString(void) const;
405 
414  const UnicodeString& getString(UErrorCode& status) const;
415 
422  inline UnicodeString& getString(void);
423 
432  UnicodeString& getString(UErrorCode& status);
433 
441  const Formattable* getArray(int32_t& count) const
442  { count=fValue.fArrayAndCount.fCount; return fValue.fArrayAndCount.fArray; }
443 
453  const Formattable* getArray(int32_t& count, UErrorCode& status) const;
454 
463  Formattable& operator[](int32_t index) { return fValue.fArrayAndCount.fArray[index]; }
464 
471  const UObject* getObject() const;
472 
491  StringPiece getDecimalNumber(UErrorCode &status);
492 
499  void setDouble(double d);
500 
507  void setLong(int32_t l);
508 
515  void setInt64(int64_t ll);
516 
523  void setDate(UDate d);
524 
531  void setString(const UnicodeString& stringToCopy);
532 
540  void setArray(const Formattable* array, int32_t count);
541 
548  void adoptString(UnicodeString* stringToAdopt);
549 
555  void adoptArray(Formattable* array, int32_t count);
556 
564  void adoptObject(UObject* objectToAdopt);
565 
580  void setDecimalNumber(const StringPiece &numberString,
581  UErrorCode &status);
582 
588  virtual UClassID getDynamicClassID() const;
589 
595  static UClassID U_EXPORT2 getStaticClassID();
596 
597 #ifndef U_HIDE_DEPRECATED_API
598 
604  inline int32_t getLong(UErrorCode* status) const;
605 #endif /* U_HIDE_DEPRECATED_API */
606 
607 #ifndef U_HIDE_INTERNAL_API
608 
616  DigitList *getDigitList() const { return fDecimalNum;}
617 
621  DigitList *getInternalDigitList();
622 
629  void adoptDigitList(DigitList *dl);
630 #endif /* U_HIDE_INTERNAL_API */
631 
632 private:
637  void dispose(void);
638 
642  void init();
643 
644  UnicodeString* getBogus() const;
645 
646  union {
647  UObject* fObject;
648  UnicodeString* fString;
649  double fDouble;
650  int64_t fInt64;
651  UDate fDate;
652  struct {
653  Formattable* fArray;
654  int32_t fCount;
655  } fArrayAndCount;
656  } fValue;
657 
658  CharString *fDecimalStr;
659 
660  DigitList *fDecimalNum;
661 
662  char fStackData[UNUM_INTERNAL_STACKARRAY_SIZE]; // must be big enough for DigitList
663 
664  Type fType;
665  UnicodeString fBogus; // Bogus string when it's needed.
666 };
667 
668 inline UDate Formattable::getDate(UErrorCode& status) const {
669  if (fType != kDate) {
670  if (U_SUCCESS(status)) {
671  status = U_INVALID_FORMAT_ERROR;
672  }
673  return 0;
674  }
675  return fValue.fDate;
676 }
677 
678 inline const UnicodeString& Formattable::getString(void) const {
679  return *fValue.fString;
680 }
681 
682 inline UnicodeString& Formattable::getString(void) {
683  return *fValue.fString;
684 }
685 
686 #ifndef U_HIDE_DEPRECATED_API
687 inline int32_t Formattable::getLong(UErrorCode* status) const {
688  return getLong(*status);
689 }
690 #endif
691 
692 
694 
695 #endif /* #if !UCONFIG_NO_FORMATTING */
696 
697 #endif //_FMTABLE
698 //eof