ICU 51.2  51.2
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 #include "unicode/unistr.h"
20 #include "unicode/stringpiece.h"
21 
27 #if !UCONFIG_NO_FORMATTING
28 
30 
31 class CharString;
32 class DigitList;
33 
34 #ifndef U_HIDE_INTERNAL_API
35 
39 #if U_PLATFORM == U_PF_OS400
40 #define UNUM_INTERNAL_STACKARRAY_SIZE 144
41 #else
42 #define UNUM_INTERNAL_STACKARRAY_SIZE 128
43 #endif
44 #endif /* U_HIDE_INTERNAL_API */
45 
64 class U_I18N_API Formattable : public UObject {
65 public:
75  enum ISDATE { kIsDate };
76 
81  Formattable(); // Type kLong, value 0
82 
89  Formattable(UDate d, ISDATE flag);
90 
96  Formattable(double d);
97 
103  Formattable(int32_t l);
104 
110  Formattable(int64_t ll);
111 
112 #if !UCONFIG_NO_CONVERSION
113 
119  Formattable(const char* strToCopy);
120 #endif
121 
135  Formattable(const StringPiece &number, UErrorCode &status);
136 
142  Formattable(const UnicodeString& strToCopy);
143 
149  Formattable(UnicodeString* strToAdopt);
150 
157  Formattable(const Formattable* arrayToCopy, int32_t count);
158 
164  Formattable(UObject* objectToAdopt);
165 
170  Formattable(const Formattable&);
171 
177  Formattable& operator=(const Formattable &rhs);
178 
185  UBool operator==(const Formattable &other) const;
186 
193  UBool operator!=(const Formattable& other) const
194  { return !operator==(other); }
195 
200  virtual ~Formattable();
201 
213  Formattable *clone() const;
214 
221  enum Type {
228 
235 
242 
249 
256 
263 
269  kObject
270  };
271 
277  Type getType(void) const;
278 
285  UBool isNumeric() const;
286 
293  double getDouble(void) const { return fValue.fDouble; }
294 
307  double getDouble(UErrorCode& status) const;
308 
315  int32_t getLong(void) const { return (int32_t)fValue.fInt64; }
316 
333  int32_t getLong(UErrorCode& status) const;
334 
341  int64_t getInt64(void) const { return fValue.fInt64; }
342 
358  int64_t getInt64(UErrorCode& status) const;
359 
366  UDate getDate() const { return fValue.fDate; }
367 
376  UDate getDate(UErrorCode& status) const;
377 
385  UnicodeString& getString(UnicodeString& result) const
386  { result=*fValue.fString; return result; }
387 
397  UnicodeString& getString(UnicodeString& result, UErrorCode& status) const;
398 
406  inline const UnicodeString& getString(void) const;
407 
416  const UnicodeString& getString(UErrorCode& status) const;
417 
424  inline UnicodeString& getString(void);
425 
434  UnicodeString& getString(UErrorCode& status);
435 
443  const Formattable* getArray(int32_t& count) const
444  { count=fValue.fArrayAndCount.fCount; return fValue.fArrayAndCount.fArray; }
445 
455  const Formattable* getArray(int32_t& count, UErrorCode& status) const;
456 
465  Formattable& operator[](int32_t index) { return fValue.fArrayAndCount.fArray[index]; }
466 
473  const UObject* getObject() const;
474 
493  StringPiece getDecimalNumber(UErrorCode &status);
494 
501  void setDouble(double d);
502 
509  void setLong(int32_t l);
510 
517  void setInt64(int64_t ll);
518 
525  void setDate(UDate d);
526 
533  void setString(const UnicodeString& stringToCopy);
534 
542  void setArray(const Formattable* array, int32_t count);
543 
550  void adoptString(UnicodeString* stringToAdopt);
551 
557  void adoptArray(Formattable* array, int32_t count);
558 
566  void adoptObject(UObject* objectToAdopt);
567 
582  void setDecimalNumber(const StringPiece &numberString,
583  UErrorCode &status);
584 
590  virtual UClassID getDynamicClassID() const;
591 
597  static UClassID U_EXPORT2 getStaticClassID();
598 
599 #ifndef U_HIDE_DEPRECATED_API
600 
606  inline int32_t getLong(UErrorCode* status) const;
607 #endif /* U_HIDE_DEPRECATED_API */
608 
609 #ifndef U_HIDE_INTERNAL_API
610 
618  DigitList *getDigitList() const { return fDecimalNum;}
619 
623  DigitList *getInternalDigitList();
624 
631  void adoptDigitList(DigitList *dl);
632 #endif /* U_HIDE_INTERNAL_API */
633 
634 private:
639  void dispose(void);
640 
644  void init();
645 
646  UnicodeString* getBogus() const;
647 
648  union {
649  UObject* fObject;
650  UnicodeString* fString;
651  double fDouble;
652  int64_t fInt64;
653  UDate fDate;
654  struct {
655  Formattable* fArray;
656  int32_t fCount;
657  } fArrayAndCount;
658  } fValue;
659 
660  CharString *fDecimalStr;
661 
662  DigitList *fDecimalNum;
663 
664  char fStackData[UNUM_INTERNAL_STACKARRAY_SIZE]; // must be big enough for DigitList
665 
666  Type fType;
667  UnicodeString fBogus; // Bogus string when it's needed.
668 };
669 
670 inline UDate Formattable::getDate(UErrorCode& status) const {
671  if (fType != kDate) {
672  if (U_SUCCESS(status)) {
673  status = U_INVALID_FORMAT_ERROR;
674  }
675  return 0;
676  }
677  return fValue.fDate;
678 }
679 
680 inline const UnicodeString& Formattable::getString(void) const {
681  return *fValue.fString;
682 }
683 
684 inline UnicodeString& Formattable::getString(void) {
685  return *fValue.fString;
686 }
687 
688 inline int32_t Formattable::getLong(UErrorCode* status) const {
689  return getLong(*status);
690 }
691 
692 
694 
695 #endif /* #if !UCONFIG_NO_FORMATTING */
696 
697 #endif //_FMTABLE
698 //eof