ICU 70.1  70.1
fmtable.h
Go to the documentation of this file.
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /*
4 ********************************************************************************
5 * Copyright (C) 1997-2014, International Business Machines
6 * Corporation and others. All Rights Reserved.
7 ********************************************************************************
8 *
9 * File FMTABLE.H
10 *
11 * Modification History:
12 *
13 * Date Name Description
14 * 02/29/97 aliu Creation.
15 ********************************************************************************
16 */
17 #ifndef FMTABLE_H
18 #define FMTABLE_H
19 
20 #include "unicode/utypes.h"
21 
22 #if U_SHOW_CPLUSPLUS_API
23 
29 #if !UCONFIG_NO_FORMATTING
30 
31 #include "unicode/unistr.h"
32 #include "unicode/stringpiece.h"
33 #include "unicode/uformattable.h"
34 
35 U_NAMESPACE_BEGIN
36 
37 class CharString;
38 namespace number {
39 namespace impl {
40 class DecimalQuantity;
41 }
42 }
43 
64 class U_I18N_API Formattable : public UObject {
65 public:
75  enum ISDATE { kIsDate };
76 
81  Formattable(); // Type kLong, value 0
82 
90 
96  Formattable(double d);
97 
103  Formattable(int32_t l);
104 
110  Formattable(int64_t ll);
111 
112 #if !UCONFIG_NO_CONVERSION
119  Formattable(const char* strToCopy);
120 #endif
121 
136 
142  Formattable(const UnicodeString& strToCopy);
143 
150 
157  Formattable(const Formattable* arrayToCopy, int32_t count);
158 
164  Formattable(UObject* objectToAdopt);
165 
171 
178 
185  bool operator==(const Formattable &other) const;
186 
193  bool 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 
386  { result=*fValue.fString; return result; }
387 
398 
406  inline const UnicodeString& getString(void) const;
407 
416  const UnicodeString& getString(UErrorCode& status) const;
417 
424  inline UnicodeString& getString(void);
425 
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 
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(StringPiece numberString,
583  UErrorCode &status);
584 
590  virtual UClassID getDynamicClassID() const override;
591 
597  static UClassID U_EXPORT2 getStaticClassID();
598 
606  static inline Formattable *fromUFormattable(UFormattable *fmt);
607 
615  static inline const Formattable *fromUFormattable(const UFormattable *fmt);
616 
623  inline UFormattable *toUFormattable();
624 
631  inline const UFormattable *toUFormattable() const;
632 
633 #ifndef U_HIDE_DEPRECATED_API
640  inline int32_t getLong(UErrorCode* status) const;
641 #endif /* U_HIDE_DEPRECATED_API */
642 
643 #ifndef U_HIDE_INTERNAL_API
652  number::impl::DecimalQuantity *getDecimalQuantity() const { return fDecimalQuantity;}
653 
658  void populateDecimalQuantity(number::impl::DecimalQuantity& output, UErrorCode& status) const;
659 
666  void adoptDecimalQuantity(number::impl::DecimalQuantity *dq);
667 
674  CharString *internalGetCharString(UErrorCode &status);
675 
676 #endif /* U_HIDE_INTERNAL_API */
677 
678 private:
683  void dispose(void);
684 
688  void init();
689 
690  UnicodeString* getBogus() const;
691 
692  union {
693  UObject* fObject;
694  UnicodeString* fString;
695  double fDouble;
696  int64_t fInt64;
697  UDate fDate;
698  struct {
699  Formattable* fArray;
700  int32_t fCount;
701  } fArrayAndCount;
702  } fValue;
703 
704  CharString *fDecimalStr;
705 
706  number::impl::DecimalQuantity *fDecimalQuantity;
707 
708  Type fType;
709  UnicodeString fBogus; // Bogus string when it's needed.
710 };
711 
712 inline UDate Formattable::getDate(UErrorCode& status) const {
713  if (fType != kDate) {
714  if (U_SUCCESS(status)) {
715  status = U_INVALID_FORMAT_ERROR;
716  }
717  return 0;
718  }
719  return fValue.fDate;
720 }
721 
722 inline const UnicodeString& Formattable::getString(void) const {
723  return *fValue.fString;
724 }
725 
726 inline UnicodeString& Formattable::getString(void) {
727  return *fValue.fString;
728 }
729 
730 #ifndef U_HIDE_DEPRECATED_API
731 inline int32_t Formattable::getLong(UErrorCode* status) const {
732  return getLong(*status);
733 }
734 #endif /* U_HIDE_DEPRECATED_API */
735 
736 inline UFormattable* Formattable::toUFormattable() {
737  return reinterpret_cast<UFormattable*>(this);
738 }
739 
740 inline const UFormattable* Formattable::toUFormattable() const {
741  return reinterpret_cast<const UFormattable*>(this);
742 }
743 
744 inline Formattable* Formattable::fromUFormattable(UFormattable *fmt) {
745  return reinterpret_cast<Formattable *>(fmt);
746 }
747 
748 inline const Formattable* Formattable::fromUFormattable(const UFormattable *fmt) {
749  return reinterpret_cast<const Formattable *>(fmt);
750 }
751 
752 U_NAMESPACE_END
753 
754 #endif /* #if !UCONFIG_NO_FORMATTING */
755 
756 #endif /* U_SHOW_CPLUSPLUS_API */
757 
758 #endif //_FMTABLE
759 //eof
Formattable objects can be passed to the Format class or its subclasses for formatting.
Definition: fmtable.h:64
void setDecimalNumber(StringPiece numberString, UErrorCode &status)
Sets the the numeric value from a decimal number string, and changes the type to to a numeric type ap...
Formattable(StringPiece number, UErrorCode &status)
Creates a Formattable object of an appropriate numeric type from a a decimal number in string form.
Formattable & operator=(const Formattable &rhs)
Assignment operator.
Formattable(UnicodeString *strToAdopt)
Creates a Formattable object with a UnicodeString object to adopt from.
Formattable * clone() const
Clone this object.
Formattable & operator[](int32_t index)
Accesses the specified element in the array value of this Formattable object.
Definition: fmtable.h:465
static UClassID getStaticClassID()
ICU "poor man's RTTI", returns a UClassID for this class.
void setArray(const Formattable *array, int32_t count)
Sets the array value and count of this object and changes the type to kArray.
void setString(const UnicodeString &stringToCopy)
Sets the string value of this object and changes the type to kString.
UBool isNumeric() const
Returns true if the data type of this Formattable object is kDouble, kLong, or kInt64.
int64_t getInt64(void) const
Gets the int64 value of this object.
Definition: fmtable.h:341
UnicodeString & getString(UErrorCode &status)
Gets a reference to the string value of this object.
void adoptString(UnicodeString *stringToAdopt)
Sets and adopts the string value and count of this object and changes the type to kArray.
Formattable(UObject *objectToAdopt)
Creates a Formattable object that adopts the given UObject.
const UObject * getObject() const
Returns a pointer to the UObject contained within this formattable, or NULL if this object does not c...
void setInt64(int64_t ll)
Sets the int64 value of this object and changes the type to kInt64.
Formattable(double d)
Creates a Formattable object with a double number.
number::impl::DecimalQuantity * getDecimalQuantity() const
Internal function, do not use.
Definition: fmtable.h:652
Formattable()
Default constructor.
void setDouble(double d)
Sets the double value of this object and changes the type to kDouble.
Formattable(const UnicodeString &strToCopy)
Creates a Formattable object with a UnicodeString object to copy from.
void adoptDecimalQuantity(number::impl::DecimalQuantity *dq)
Adopt, and set value from, a DecimalQuantity Internal Function, do not use.
void setLong(int32_t l)
Sets the long value of this object and changes the type to kLong.
Type
Selector for flavor of data type contained within a Formattable object.
Definition: fmtable.h:221
@ kDouble
Selector indicating a double value.
Definition: fmtable.h:234
@ kArray
Selector indicating an array of Formattables.
Definition: fmtable.h:255
@ kLong
Selector indicating a 32-bit integer value.
Definition: fmtable.h:241
@ kInt64
Selector indicating a 64-bit integer value.
Definition: fmtable.h:262
@ kString
Selector indicating a UnicodeString value.
Definition: fmtable.h:248
@ kDate
Selector indicating a UDate value.
Definition: fmtable.h:227
void setDate(UDate d)
Sets the Date value of this object and changes the type to kDate.
const Formattable * getArray(int32_t &count) const
Gets the array value and count of this object.
Definition: fmtable.h:443
double getDouble(UErrorCode &status) const
Gets the double value of this object.
CharString * internalGetCharString(UErrorCode &status)
Internal function to return the CharString pointer.
Formattable(const char *strToCopy)
Creates a Formattable object with a char string pointer.
double getDouble(void) const
Gets the double value of this object.
Definition: fmtable.h:293
Formattable(UDate d, ISDATE flag)
Creates a Formattable object with a UDate instance.
void adoptArray(Formattable *array, int32_t count)
Sets and adopts the array value and count of this object and changes the type to kArray.
void adoptObject(UObject *objectToAdopt)
Sets and adopts the UObject value of this object and changes the type to kObject.
Type getType(void) const
Gets the data type of this Formattable object.
bool operator==(const Formattable &other) const
Equality comparison.
const UnicodeString & getString(UErrorCode &status) const
Gets a const reference to the string value of this object.
UnicodeString & getString(UnicodeString &result, UErrorCode &status) const
Gets the string value of this object.
int32_t getLong(void) const
Gets the long value of this object.
Definition: fmtable.h:315
Formattable(const Formattable *arrayToCopy, int32_t count)
Creates a Formattable object with an array of Formattable objects.
Formattable(const Formattable &)
Copy constructor.
Formattable(int32_t l)
Creates a Formattable object with a long number.
StringPiece getDecimalNumber(UErrorCode &status)
Returns a numeric string representation of the number contained within this formattable,...
UnicodeString & getString(UnicodeString &result) const
Gets the string value of this object.
Definition: fmtable.h:385
virtual ~Formattable()
Destructor.
ISDATE
This enum is only used to let callers distinguish between the Formattable(UDate) constructor and the ...
Definition: fmtable.h:75
bool operator!=(const Formattable &other) const
Equality operator.
Definition: fmtable.h:193
void populateDecimalQuantity(number::impl::DecimalQuantity &output, UErrorCode &status) const
Export the value of this Formattable to a DecimalQuantity.
UDate getDate() const
Gets the Date value of this object.
Definition: fmtable.h:366
int32_t getLong(UErrorCode &status) const
Gets the long value of this object.
const Formattable * getArray(int32_t &count, UErrorCode &status) const
Gets the array value and count of this object.
Formattable(int64_t ll)
Creates a Formattable object with an int64_t number.
int64_t getInt64(UErrorCode &status) const
Gets the int64 value of this object.
virtual UClassID getDynamicClassID() const override
ICU "poor man's RTTI", returns a UClassID for the actual class.
A string-like object that points to a sized piece of memory.
Definition: stringpiece.h:60
UObject is the common ICU "boilerplate" class.
Definition: uobject.h:223
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition: unistr.h:296
U_EXPORT UBool operator==(const StringPiece &x, const StringPiece &y)
Global operator == for StringPiece.
C++ API: StringPiece: Read-only byte string wrapper class.
C API: UFormattable is a thin wrapper for primitive types used for formatting and parsing.
void * UFormattable
Opaque type representing various types of data which may be used for formatting and parsing operation...
Definition: uformattable.h:72
int8_t UBool
The ICU boolean type, a signed-byte integer.
Definition: umachine.h:269
C++ API: Unicode String.
void * UClassID
UClassID is used to identify classes without using the compiler's RTTI.
Definition: uobject.h:96
Basic definitions for ICU, for both C and C++ APIs.
UErrorCode
Standard ICU4C error code type, a substitute for exceptions.
Definition: utypes.h:415
@ U_INVALID_FORMAT_ERROR
Data format is not what is expected.
Definition: utypes.h:453
#define U_SUCCESS(x)
Does the error code indicate success?
Definition: utypes.h:712
#define U_I18N_API
Set to export library symbols from inside the i18n library, and to import them from outside.
Definition: utypes.h:301
double UDate
Date and Time data type.
Definition: utypes.h:203