ICU 55.1  55.1
fmtable.h
Go to the documentation of this file.
1 /*
2 ********************************************************************************
3 * Copyright (C) 1997-2014, 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 
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 
152 
159  Formattable(const Formattable* arrayToCopy, int32_t count);
160 
166  Formattable(UObject* objectToAdopt);
167 
173 
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 
400 
408  inline const UnicodeString& getString(void) const;
409 
418  const UnicodeString& getString(UErrorCode& status) const;
419 
426  inline UnicodeString& getString(void);
427 
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 
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 
608  static inline Formattable *fromUFormattable(UFormattable *fmt);
609 
617  static inline const Formattable *fromUFormattable(const UFormattable *fmt);
618 
625  inline UFormattable *toUFormattable();
626 
633  inline const UFormattable *toUFormattable() const;
634 
635 #ifndef U_HIDE_DEPRECATED_API
636 
642  inline int32_t getLong(UErrorCode* status) const;
643 #endif /* U_HIDE_DEPRECATED_API */
644 
645 #ifndef U_HIDE_INTERNAL_API
646 
654  DigitList *getDigitList() const { return fDecimalNum;}
655 
659  DigitList *getInternalDigitList();
660 
667  void adoptDigitList(DigitList *dl);
668 
675  CharString *internalGetCharString(UErrorCode &status);
676 
677 #endif /* U_HIDE_INTERNAL_API */
678 
679 private:
684  void dispose(void);
685 
689  void init();
690 
691  UnicodeString* getBogus() const;
692 
693  union {
694  UObject* fObject;
695  UnicodeString* fString;
696  double fDouble;
697  int64_t fInt64;
698  UDate fDate;
699  struct {
700  Formattable* fArray;
701  int32_t fCount;
702  } fArrayAndCount;
703  } fValue;
704 
705  CharString *fDecimalStr;
706 
707  DigitList *fDecimalNum;
708 
709  char fStackData[UNUM_INTERNAL_STACKARRAY_SIZE]; // must be big enough for DigitList
710 
711  Type fType;
712  UnicodeString fBogus; // Bogus string when it's needed.
713 };
714 
715 inline UDate Formattable::getDate(UErrorCode& status) const {
716  if (fType != kDate) {
717  if (U_SUCCESS(status)) {
718  status = U_INVALID_FORMAT_ERROR;
719  }
720  return 0;
721  }
722  return fValue.fDate;
723 }
724 
725 inline const UnicodeString& Formattable::getString(void) const {
726  return *fValue.fString;
727 }
728 
729 inline UnicodeString& Formattable::getString(void) {
730  return *fValue.fString;
731 }
732 
733 #ifndef U_HIDE_DEPRECATED_API
734 inline int32_t Formattable::getLong(UErrorCode* status) const {
735  return getLong(*status);
736 }
737 #endif /* U_HIDE_DEPRECATED_API */
738 
739 inline UFormattable* Formattable::toUFormattable() {
740  return reinterpret_cast<UFormattable*>(this);
741 }
742 
743 inline const UFormattable* Formattable::toUFormattable() const {
744  return reinterpret_cast<const UFormattable*>(this);
745 }
746 
747 inline Formattable* Formattable::fromUFormattable(UFormattable *fmt) {
748  return reinterpret_cast<Formattable *>(fmt);
749 }
750 
751 inline const Formattable* Formattable::fromUFormattable(const UFormattable *fmt) {
752  return reinterpret_cast<const Formattable *>(fmt);
753 }
754 
756 
757 #endif /* #if !UCONFIG_NO_FORMATTING */
758 
759 #endif //_FMTABLE
760 //eof
icu::Formattable::adoptArray
void adoptArray(Formattable *array, int32_t count)
Sets and adopts the array value and count of this object and changes the type to kArray.
icu::Formattable::setDouble
void setDouble(double d)
Sets the double value of this object and changes the type to kDouble.
UFormattable
void * UFormattable
Opaque type representing various types of data which may be used for formatting and parsing operation...
Definition: uformattable.h:62
utypes.h
Basic definitions for ICU, for both C and C++ APIs.
U_INVALID_FORMAT_ERROR
@ U_INVALID_FORMAT_ERROR
Data format is not what is expected.
Definition: utypes.h:509
icu::Formattable::kDate
@ kDate
Selector indicating a UDate value.
Definition: fmtable.h:229
U_I18N_API
#define U_I18N_API
Set to export library symbols from inside the i18n library, and to import them from outside.
Definition: utypes.h:358
icu::Formattable::kDouble
@ kDouble
Selector indicating a double value.
Definition: fmtable.h:236
UBool
int8_t UBool
The ICU boolean type.
Definition: umachine.h:234
icu::Formattable::Formattable
Formattable(double d)
Creates a Formattable object with a double number.
icu::Formattable
Formattable objects can be passed to the Format class or its subclasses for formatting.
Definition: fmtable.h:66
icu::Formattable::getStaticClassID
static UClassID getStaticClassID()
ICU "poor man's RTTI", returns a UClassID for this class.
icu::Formattable::operator!=
UBool operator!=(const Formattable &other) const
Equality operator.
Definition: fmtable.h:195
icu::operator==
U_EXPORT UBool operator==(const StringPiece &x, const StringPiece &y)
Global operator == for StringPiece.
icu::Formattable::adoptObject
void adoptObject(UObject *objectToAdopt)
Sets and adopts the UObject value of this object and changes the type to kObject.
icu::Formattable::clone
Formattable * clone() const
Clone this object.
icu::Formattable::Formattable
Formattable(int64_t ll)
Creates a Formattable object with an int64_t number.
icu::Formattable::setLong
void setLong(int32_t l)
Sets the long value of this object and changes the type to kLong.
icu::Formattable::getLong
int32_t getLong(UErrorCode &status) const
Gets the long value of this object.
UNUM_INTERNAL_STACKARRAY_SIZE
#define UNUM_INTERNAL_STACKARRAY_SIZE
Definition: fmtable.h:41
icu::Formattable::Formattable
Formattable(UnicodeString *strToAdopt)
Creates a Formattable object with a UnicodeString object to adopt from.
icu::Formattable::ISDATE
ISDATE
This enum is only used to let callers distinguish between the Formattable(UDate) constructor and the ...
Definition: fmtable.h:77
icu::Formattable::kInt64
@ kInt64
Selector indicating a 64-bit integer value.
Definition: fmtable.h:264
uformattable.h
C API: UFormattable is a thin wrapper for primitive types used for formatting and parsing.
icu::Formattable::getInt64
int64_t getInt64(void) const
Gets the int64 value of this object.
Definition: fmtable.h:343
icu::Formattable::operator==
UBool operator==(const Formattable &other) const
Equality comparison.
stringpiece.h
C++ API: StringPiece: Read-only byte string wrapper class.
icu::UnicodeString
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition: unistr.h:246
icu::Formattable::getString
UnicodeString & getString(UnicodeString &result, UErrorCode &status) const
Gets the string value of this object.
icu::Formattable::Formattable
Formattable()
Default constructor.
icu::Formattable::getObject
const UObject * getObject() const
Returns a pointer to the UObject contained within this formattable, or NULL if this object does not c...
icu::Formattable::getString
UnicodeString & getString(UErrorCode &status)
Gets a reference to the string value of this object.
icu::Formattable::getDigitList
DigitList * getDigitList() const
Internal function, do not use.
Definition: fmtable.h:654
icu::Formattable::setDecimalNumber
void setDecimalNumber(const StringPiece &numberString, UErrorCode &status)
Sets the the numeric value from a decimal number string, and changes the type to to a numeric type ap...
icu::UObject
UObject is the common ICU "boilerplate" class.
Definition: uobject.h:221
UClassID
void * UClassID
UClassID is used to identify classes without using the compiler's RTTI.
Definition: uobject.h:91
UErrorCode
UErrorCode
Error code to replace exception handling, so that the code is compatible with all C++ compilers,...
Definition: utypes.h:476
icu::Formattable::adoptString
void adoptString(UnicodeString *stringToAdopt)
Sets and adopts the string value and count of this object and changes the type to kArray.
icu::Formattable::getLong
int32_t getLong(void) const
Gets the long value of this object.
Definition: fmtable.h:317
icu::Formattable::getDouble
double getDouble(UErrorCode &status) const
Gets the double value of this object.
icu::Formattable::~Formattable
virtual ~Formattable()
Destructor.
icu::Formattable::setString
void setString(const UnicodeString &stringToCopy)
Sets the string value of this object and changes the type to kString.
icu::Formattable::getDecimalNumber
StringPiece getDecimalNumber(UErrorCode &status)
Returns a numeric string representation of the number contained within this formattable,...
icu::Formattable::internalGetCharString
CharString * internalGetCharString(UErrorCode &status)
Internal function to return the CharString pointer.
icu::Formattable::setDate
void setDate(UDate d)
Sets the Date value of this object and changes the type to kDate.
icu::Formattable::Formattable
Formattable(UObject *objectToAdopt)
Creates a Formattable object that adopts the given UObject.
icu::Formattable::getString
UnicodeString & getString(UnicodeString &result) const
Gets the string value of this object.
Definition: fmtable.h:387
icu::Formattable::operator=
Formattable & operator=(const Formattable &rhs)
Assignment operator.
icu::Formattable::Formattable
Formattable(UDate d, ISDATE flag)
Creates a Formattable object with a UDate instance.
icu::Formattable::getType
Type getType(void) const
Gets the data type of this Formattable object.
icu::Formattable::getInt64
int64_t getInt64(UErrorCode &status) const
Gets the int64 value of this object.
icu::Formattable::operator[]
Formattable & operator[](int32_t index)
Accesses the specified element in the array value of this Formattable object.
Definition: fmtable.h:467
icu::Formattable::kLong
@ kLong
Selector indicating a 32-bit integer value.
Definition: fmtable.h:243
icu::Formattable::setInt64
void setInt64(int64_t ll)
Sets the int64 value of this object and changes the type to kInt64.
icu::Formattable::getDynamicClassID
virtual UClassID getDynamicClassID() const
ICU "poor man's RTTI", returns a UClassID for the actual class.
icu::Formattable::adoptDigitList
void adoptDigitList(DigitList *dl)
Adopt, and set value from, a DigitList Internal Function, do not use.
U_SUCCESS
#define U_SUCCESS(x)
Does the error code indicate success?
Definition: utypes.h:711
icu::Formattable::Formattable
Formattable(const UnicodeString &strToCopy)
Creates a Formattable object with a UnicodeString object to copy from.
icu::Formattable::isNumeric
UBool isNumeric() const
Returns TRUE if the data type of this Formattable object is kDouble, kLong, or kInt64.
UDate
double UDate
Date and Time data type.
Definition: utypes.h:201
icu::Formattable::Formattable
Formattable(int32_t l)
Creates a Formattable object with a long number.
icu::Formattable::Formattable
Formattable(const StringPiece &number, UErrorCode &status)
Creates a Formattable object of an appropriate numeric type from a a decimal number in string form.
icu::Formattable::getString
const UnicodeString & getString(UErrorCode &status) const
Gets a const reference to the string value of this object.
icu::Formattable::getDate
UDate getDate() const
Gets the Date value of this object.
Definition: fmtable.h:368
icu::Formattable::setArray
void setArray(const Formattable *array, int32_t count)
Sets the array value and count of this object and changes the type to kArray.
icu::Formattable::Formattable
Formattable(const char *strToCopy)
Creates a Formattable object with a char string pointer.
icu::Formattable::getArray
const Formattable * getArray(int32_t &count) const
Gets the array value and count of this object.
Definition: fmtable.h:445
icu::Formattable::kArray
@ kArray
Selector indicating an array of Formattables.
Definition: fmtable.h:257
icu::Formattable::getDouble
double getDouble(void) const
Gets the double value of this object.
Definition: fmtable.h:295
icu::Formattable::kString
@ kString
Selector indicating a UnicodeString value.
Definition: fmtable.h:250
icu::Formattable::Type
Type
Selector for flavor of data type contained within a Formattable object.
Definition: fmtable.h:223
icu::Formattable::getInternalDigitList
DigitList * getInternalDigitList()
U_NAMESPACE_END
#define U_NAMESPACE_END
This is used to end a declaration of a public ICU C++ API.
Definition: uversion.h:130
U_NAMESPACE_BEGIN
#define U_NAMESPACE_BEGIN
This is used to begin a declaration of a public ICU C++ API.
Definition: uversion.h:129
icu::StringPiece
A string-like object that points to a sized piece of memory.
Definition: stringpiece.h:52
icu::Formattable::Formattable
Formattable(const Formattable *arrayToCopy, int32_t count)
Creates a Formattable object with an array of Formattable objects.
unistr.h
C++ API: Unicode String.
icu::Formattable::Formattable
Formattable(const Formattable &)
Copy constructor.
icu::Formattable::getArray
const Formattable * getArray(int32_t &count, UErrorCode &status) const
Gets the array value and count of this object.