ICU 64.2  64.2
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 
27 #if !UCONFIG_NO_FORMATTING
28 
29 #include "unicode/unistr.h"
30 #include "unicode/stringpiece.h"
31 #include "unicode/uformattable.h"
32 
34 
35 class CharString;
36 namespace number {
37 namespace impl {
38 class DecimalQuantity;
39 }
40 }
41 
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(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(StringPiece numberString,
581  UErrorCode &status);
582 
588  virtual UClassID getDynamicClassID() const;
589 
595  static UClassID U_EXPORT2 getStaticClassID();
596 
604  static inline Formattable *fromUFormattable(UFormattable *fmt);
605 
613  static inline const Formattable *fromUFormattable(const UFormattable *fmt);
614 
621  inline UFormattable *toUFormattable();
622 
629  inline const UFormattable *toUFormattable() const;
630 
631 #ifndef U_HIDE_DEPRECATED_API
632 
638  inline int32_t getLong(UErrorCode* status) const;
639 #endif /* U_HIDE_DEPRECATED_API */
640 
641 #ifndef U_HIDE_INTERNAL_API
642 
650  number::impl::DecimalQuantity *getDecimalQuantity() const { return fDecimalQuantity;}
651 
656  void populateDecimalQuantity(number::impl::DecimalQuantity& output, UErrorCode& status) const;
657 
664  void adoptDecimalQuantity(number::impl::DecimalQuantity *dq);
665 
672  CharString *internalGetCharString(UErrorCode &status);
673 
674 #endif /* U_HIDE_INTERNAL_API */
675 
676 private:
681  void dispose(void);
682 
686  void init();
687 
688  UnicodeString* getBogus() const;
689 
690  union {
691  UObject* fObject;
692  UnicodeString* fString;
693  double fDouble;
694  int64_t fInt64;
695  UDate fDate;
696  struct {
697  Formattable* fArray;
698  int32_t fCount;
699  } fArrayAndCount;
700  } fValue;
701 
702  CharString *fDecimalStr;
703 
704  number::impl::DecimalQuantity *fDecimalQuantity;
705 
706  Type fType;
707  UnicodeString fBogus; // Bogus string when it's needed.
708 };
709 
710 inline UDate Formattable::getDate(UErrorCode& status) const {
711  if (fType != kDate) {
712  if (U_SUCCESS(status)) {
713  status = U_INVALID_FORMAT_ERROR;
714  }
715  return 0;
716  }
717  return fValue.fDate;
718 }
719 
720 inline const UnicodeString& Formattable::getString(void) const {
721  return *fValue.fString;
722 }
723 
724 inline UnicodeString& Formattable::getString(void) {
725  return *fValue.fString;
726 }
727 
728 #ifndef U_HIDE_DEPRECATED_API
729 inline int32_t Formattable::getLong(UErrorCode* status) const {
730  return getLong(*status);
731 }
732 #endif /* U_HIDE_DEPRECATED_API */
733 
734 inline UFormattable* Formattable::toUFormattable() {
735  return reinterpret_cast<UFormattable*>(this);
736 }
737 
738 inline const UFormattable* Formattable::toUFormattable() const {
739  return reinterpret_cast<const UFormattable*>(this);
740 }
741 
742 inline Formattable* Formattable::fromUFormattable(UFormattable *fmt) {
743  return reinterpret_cast<Formattable *>(fmt);
744 }
745 
746 inline const Formattable* Formattable::fromUFormattable(const UFormattable *fmt) {
747  return reinterpret_cast<const Formattable *>(fmt);
748 }
749 
751 
752 #endif /* #if !UCONFIG_NO_FORMATTING */
753 
754 #endif //_FMTABLE
755 //eof
C API: UFormattable is a thin wrapper for primitive types used for formatting and parsing...
UBool operator!=(const Formattable &other) const
Equality operator.
Definition: fmtable.h:191
#define U_SUCCESS(x)
Does the error code indicate success?
Definition: utypes.h:690
double UDate
Date and Time data type.
Definition: utypes.h:203
C++ API: Unicode String.
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&#39;s RTTI.
Definition: uobject.h:93
UDate getDate() const
Gets the Date value of this object.
Definition: fmtable.h:364
UnicodeString & getString(UnicodeString &result) const
Gets the string value of this object.
Definition: fmtable.h:383
Selector indicating an array of Formattables.
Definition: fmtable.h:253
int64_t getInt64(void) const
Gets the int64 value of this object.
Definition: fmtable.h:339
#define U_I18N_API
Set to export library symbols from inside the i18n library, and to import them from outside...
Definition: utypes.h:301
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:137
Selector indicating a double value.
Definition: fmtable.h:232
Formattable & operator[](int32_t index)
Accesses the specified element in the array value of this Formattable object.
Definition: fmtable.h:463
Data format is not what is expected.
Definition: utypes.h:439
ISDATE
This enum is only used to let callers distinguish between the Formattable(UDate) constructor and the ...
Definition: fmtable.h:73
int32_t getLong(void) const
Gets the long value of this object.
Definition: fmtable.h:313
number::impl::DecimalQuantity * getDecimalQuantity() const
Internal function, do not use.
Definition: fmtable.h:650
#define U_NAMESPACE_END
This is used to end a declaration of a public ICU C++ API.
Definition: uversion.h:138
double getDouble(void) const
Gets the double value of this object.
Definition: fmtable.h:291
UErrorCode
Error code to replace exception handling, so that the code is compatible with all C++ compilers...
Definition: utypes.h:401
Selector indicating a UnicodeString value.
Definition: fmtable.h:246
const Formattable * getArray(int32_t &count) const
Gets the array value and count of this object.
Definition: fmtable.h:441
Selector indicating a UDate value.
Definition: fmtable.h:225
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:289
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
void * UFormattable
Opaque type representing various types of data which may be used for formatting and parsing operation...
Definition: uformattable.h:70
A string-like object that points to a sized piece of memory.
Definition: stringpiece.h:54
UObject is the common ICU "boilerplate" class.
Definition: uobject.h:223
int8_t UBool
The ICU boolean type.
Definition: umachine.h:225
Selector indicating a 64-bit integer value.
Definition: fmtable.h:260