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 
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
const Formattable * getArray(int32_t &count) const
Gets the array value and count of this object.
Definition: fmtable.h:441
#define U_SUCCESS(x)
Does the error code indicate success?
Definition: utypes.h:705
double UDate
Date and Time data type.
Definition: utypes.h:201
virtual UClassID getDynamicClassID() const =0
ICU4C "poor man's RTTI", returns a UClassID for the actual ICU class.
C++ API: Unicode String.
double getDouble(void) const
Gets the double value of this object.
Definition: fmtable.h:291
Type
Selector for flavor of data type contained within a Formattable object.
Definition: fmtable.h:219
U_EXPORT UBool operator==(const StringPiece &x, const StringPiece &y)
Global operator == for StringPiece.
void * UClassID
UClassID is used to identify classes without using the compiler's RTTI.
Definition: uobject.h:96
UDate getDate() const
Gets the Date value of this object.
Definition: fmtable.h:364
#define UNUM_INTERNAL_STACKARRAY_SIZE
Definition: fmtable.h:39
Selector indicating an array of Formattables.
Definition: fmtable.h:253
#define U_I18N_API
Set to export library symbols from inside the i18n library, and to import them from outside...
Definition: utypes.h:358
C++ API: StringPiece: Read-only byte string wrapper class.
#define U_NAMESPACE_BEGIN
This is used to begin a declaration of a public ICU C++ API.
Definition: uversion.h:129
Selector indicating a double value.
Definition: fmtable.h:232
int32_t getLong(void) const
Gets the long value of this object.
Definition: fmtable.h:313
Formattable & operator[](int32_t index)
Accesses the specified element in the array value of this Formattable object.
Definition: fmtable.h:463
int64_t getInt64(void) const
Gets the int64 value of this object.
Definition: fmtable.h:339
Data format is not what is expected.
Definition: utypes.h:509
DigitList * getDigitList() const
Internal function, do not use.
Definition: fmtable.h:616
ISDATE
This enum is only used to let callers distinguish between the Formattable(UDate) constructor and the ...
Definition: fmtable.h:73
UnicodeString & getString(UnicodeString &result) const
Gets the string value of this object.
Definition: fmtable.h:383
#define U_NAMESPACE_END
This is used to end a declaration of a public ICU C++ API.
Definition: uversion.h:130
UErrorCode
Error code to replace exception handling, so that the code is compatible with all C++ compilers...
Definition: utypes.h:476
Selector indicating a UnicodeString value.
Definition: fmtable.h:246
Selector indicating a UDate value.
Definition: fmtable.h:225
UBool operator!=(const Formattable &other) const
Equality operator.
Definition: fmtable.h:191
Basic definitions for ICU, for both C and C++ APIs.
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition: unistr.h:246
Formattable objects can be passed to the Format class or its subclasses for formatting.
Definition: fmtable.h:62
Selector indicating a 32-bit integer value.
Definition: fmtable.h:239
A string-like object that points to a sized piece of memory.
Definition: stringpiece.h:52
UObject is the common ICU "boilerplate" class.
Definition: uobject.h:229
int8_t UBool
The ICU boolean type.
Definition: umachine.h:200
Selector indicating a 64-bit integer value.
Definition: fmtable.h:260