ICU 52.1  52.1
fmtable.h
Go to the documentation of this file.
1 /*
2 ********************************************************************************
3 * Copyright (C) 1997-2013, 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 
25 #if !UCONFIG_NO_FORMATTING
26 
27 #include "unicode/unistr.h"
28 #include "unicode/stringpiece.h"
29 #include "unicode/uformattable.h"
30 
32 
33 class CharString;
34 class DigitList;
35 
40 #if U_PLATFORM == U_PF_OS400
41 #define UNUM_INTERNAL_STACKARRAY_SIZE 144
42 #else
43 #define UNUM_INTERNAL_STACKARRAY_SIZE 128
44 #endif
45 
66 class U_I18N_API Formattable : public UObject {
67 public:
77  enum ISDATE { kIsDate };
78 
83  Formattable(); // Type kLong, value 0
84 
91  Formattable(UDate d, ISDATE flag);
92 
98  Formattable(double d);
99 
105  Formattable(int32_t l);
106 
112  Formattable(int64_t ll);
113 
114 #if !UCONFIG_NO_CONVERSION
115 
121  Formattable(const char* strToCopy);
122 #endif
123 
137  Formattable(const StringPiece &number, UErrorCode &status);
138 
144  Formattable(const UnicodeString& strToCopy);
145 
151  Formattable(UnicodeString* strToAdopt);
152 
159  Formattable(const Formattable* arrayToCopy, int32_t count);
160 
166  Formattable(UObject* objectToAdopt);
167 
172  Formattable(const Formattable&);
173 
179  Formattable& operator=(const Formattable &rhs);
180 
187  UBool operator==(const Formattable &other) const;
188 
195  UBool operator!=(const Formattable& other) const
196  { return !operator==(other); }
197 
202  virtual ~Formattable();
203 
215  Formattable *clone() const;
216 
223  enum Type {
230 
237 
244 
251 
258 
265 
271  kObject
272  };
273 
279  Type getType(void) const;
280 
287  UBool isNumeric() const;
288 
295  double getDouble(void) const { return fValue.fDouble; }
296 
309  double getDouble(UErrorCode& status) const;
310 
317  int32_t getLong(void) const { return (int32_t)fValue.fInt64; }
318 
335  int32_t getLong(UErrorCode& status) const;
336 
343  int64_t getInt64(void) const { return fValue.fInt64; }
344 
360  int64_t getInt64(UErrorCode& status) const;
361 
368  UDate getDate() const { return fValue.fDate; }
369 
378  UDate getDate(UErrorCode& status) const;
379 
388  { result=*fValue.fString; return result; }
389 
399  UnicodeString& getString(UnicodeString& result, UErrorCode& status) const;
400 
408  inline const UnicodeString& getString(void) const;
409 
418  const UnicodeString& getString(UErrorCode& status) const;
419 
426  inline UnicodeString& getString(void);
427 
436  UnicodeString& getString(UErrorCode& status);
437 
445  const Formattable* getArray(int32_t& count) const
446  { count=fValue.fArrayAndCount.fCount; return fValue.fArrayAndCount.fArray; }
447 
457  const Formattable* getArray(int32_t& count, UErrorCode& status) const;
458 
467  Formattable& operator[](int32_t index) { return fValue.fArrayAndCount.fArray[index]; }
468 
475  const UObject* getObject() const;
476 
495  StringPiece getDecimalNumber(UErrorCode &status);
496 
503  void setDouble(double d);
504 
511  void setLong(int32_t l);
512 
519  void setInt64(int64_t ll);
520 
527  void setDate(UDate d);
528 
535  void setString(const UnicodeString& stringToCopy);
536 
544  void setArray(const Formattable* array, int32_t count);
545 
552  void adoptString(UnicodeString* stringToAdopt);
553 
559  void adoptArray(Formattable* array, int32_t count);
560 
568  void adoptObject(UObject* objectToAdopt);
569 
584  void setDecimalNumber(const StringPiece &numberString,
585  UErrorCode &status);
586 
592  virtual UClassID getDynamicClassID() const;
593 
599  static UClassID U_EXPORT2 getStaticClassID();
600 
601 #ifndef U_HIDE_DRAFT_API
602 
609  static inline Formattable *fromUFormattable(UFormattable *fmt);
610 
618  static inline const Formattable *fromUFormattable(const UFormattable *fmt);
619 
626  inline UFormattable *toUFormattable();
627 
634  inline const UFormattable *toUFormattable() const;
635 #endif /* U_HIDE_DRAFT_API */
636 
637 #ifndef U_HIDE_DEPRECATED_API
638 
644  inline int32_t getLong(UErrorCode* status) const;
645 #endif /* U_HIDE_DEPRECATED_API */
646 
647 #ifndef U_HIDE_INTERNAL_API
648 
656  DigitList *getDigitList() const { return fDecimalNum;}
657 
661  DigitList *getInternalDigitList();
662 
669  void adoptDigitList(DigitList *dl);
670 
677  CharString *internalGetCharString(UErrorCode &status);
678 
679 #endif /* U_HIDE_INTERNAL_API */
680 
681 private:
686  void dispose(void);
687 
691  void init();
692 
693  UnicodeString* getBogus() const;
694 
695  union {
696  UObject* fObject;
697  UnicodeString* fString;
698  double fDouble;
699  int64_t fInt64;
700  UDate fDate;
701  struct {
702  Formattable* fArray;
703  int32_t fCount;
704  } fArrayAndCount;
705  } fValue;
706 
707  CharString *fDecimalStr;
708 
709  DigitList *fDecimalNum;
710 
711  char fStackData[UNUM_INTERNAL_STACKARRAY_SIZE]; // must be big enough for DigitList
712 
713  Type fType;
714  UnicodeString fBogus; // Bogus string when it's needed.
715 };
716 
717 inline UDate Formattable::getDate(UErrorCode& status) const {
718  if (fType != kDate) {
719  if (U_SUCCESS(status)) {
720  status = U_INVALID_FORMAT_ERROR;
721  }
722  return 0;
723  }
724  return fValue.fDate;
725 }
726 
727 inline const UnicodeString& Formattable::getString(void) const {
728  return *fValue.fString;
729 }
730 
731 inline UnicodeString& Formattable::getString(void) {
732  return *fValue.fString;
733 }
734 
735 #ifndef U_HIDE_DEPRECATED_API
736 inline int32_t Formattable::getLong(UErrorCode* status) const {
737  return getLong(*status);
738 }
739 #endif /* U_HIDE_DEPRECATED_API */
740 
741 #ifndef U_HIDE_DRAFT_API
742 inline UFormattable* Formattable::toUFormattable() {
743  return reinterpret_cast<UFormattable*>(this);
744 }
745 
746 inline const UFormattable* Formattable::toUFormattable() const {
747  return reinterpret_cast<const UFormattable*>(this);
748 }
749 
750 inline Formattable* Formattable::fromUFormattable(UFormattable *fmt) {
751  return reinterpret_cast<Formattable *>(fmt);
752 }
753 
754 inline const Formattable* Formattable::fromUFormattable(const UFormattable *fmt) {
755  return reinterpret_cast<const Formattable *>(fmt);
756 }
757 #endif /* U_HIDE_DRAFT_API */
758 
760 
761 #endif /* #if !UCONFIG_NO_FORMATTING */
762 
763 #endif //_FMTABLE
764 //eof