ICU 76.1  76.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 
39 namespace number::impl {
40 class DecimalQuantity;
41 }
42 
63 class U_I18N_API Formattable : public UObject {
64 public:
74  enum ISDATE { kIsDate };
75 
80  Formattable(); // Type kLong, value 0
81 
88  Formattable(UDate d, ISDATE flag);
89 
95  Formattable(double d);
96 
102  Formattable(int32_t l);
103 
109  Formattable(int64_t ll);
110 
111 #if !UCONFIG_NO_CONVERSION
112 
118  Formattable(const char* strToCopy);
119 #endif
120 
134  Formattable(StringPiece number, UErrorCode &status);
135 
141  Formattable(const UnicodeString& strToCopy);
142 
148  Formattable(UnicodeString* strToAdopt);
149 
156  Formattable(const Formattable* arrayToCopy, int32_t count);
157 
163  Formattable(UObject* objectToAdopt);
164 
169  Formattable(const Formattable&);
170 
176  Formattable& operator=(const Formattable &rhs);
177 
184  bool operator==(const Formattable &other) const;
185 
192  bool operator!=(const Formattable& other) const
193  { return !operator==(other); }
194 
199  virtual ~Formattable();
200 
212  Formattable *clone() const;
213 
220  enum Type {
227 
234 
241 
248 
255 
262 
268  kObject
269  };
270 
276  Type getType() const;
277 
284  UBool isNumeric() const;
285 
292  double getDouble() const { return fValue.fDouble; }
293 
306  double getDouble(UErrorCode& status) const;
307 
314  int32_t getLong() const { return static_cast<int32_t>(fValue.fInt64); }
315 
332  int32_t getLong(UErrorCode& status) const;
333 
340  int64_t getInt64() const { return fValue.fInt64; }
341 
357  int64_t getInt64(UErrorCode& status) const;
358 
365  UDate getDate() const { return fValue.fDate; }
366 
375  UDate getDate(UErrorCode& status) const;
376 
385  { result=*fValue.fString; return result; }
386 
396  UnicodeString& getString(UnicodeString& result, UErrorCode& status) const;
397 
405  inline const UnicodeString& getString() const;
406 
415  const UnicodeString& getString(UErrorCode& status) const;
416 
423  inline UnicodeString& getString();
424 
433  UnicodeString& getString(UErrorCode& status);
434 
442  const Formattable* getArray(int32_t& count) const
443  { count=fValue.fArrayAndCount.fCount; return fValue.fArrayAndCount.fArray; }
444 
454  const Formattable* getArray(int32_t& count, UErrorCode& status) const;
455 
464  Formattable& operator[](int32_t index) { return fValue.fArrayAndCount.fArray[index]; }
465 
472  const UObject* getObject() const;
473 
492  StringPiece getDecimalNumber(UErrorCode &status);
493 
500  void setDouble(double d);
501 
508  void setLong(int32_t l);
509 
516  void setInt64(int64_t ll);
517 
524  void setDate(UDate d);
525 
532  void setString(const UnicodeString& stringToCopy);
533 
541  void setArray(const Formattable* array, int32_t count);
542 
549  void adoptString(UnicodeString* stringToAdopt);
550 
556  void adoptArray(Formattable* array, int32_t count);
557 
565  void adoptObject(UObject* objectToAdopt);
566 
581  void setDecimalNumber(StringPiece numberString,
582  UErrorCode &status);
583 
589  virtual UClassID getDynamicClassID() const override;
590 
596  static UClassID U_EXPORT2 getStaticClassID();
597 
605  static inline Formattable *fromUFormattable(UFormattable *fmt);
606 
614  static inline const Formattable *fromUFormattable(const UFormattable *fmt);
615 
622  inline UFormattable *toUFormattable();
623 
630  inline const UFormattable *toUFormattable() const;
631 
632 #ifndef U_HIDE_DEPRECATED_API
633 
639  inline int32_t getLong(UErrorCode* status) const;
640 #endif /* U_HIDE_DEPRECATED_API */
641 
642 #ifndef U_HIDE_INTERNAL_API
643 
651  number::impl::DecimalQuantity *getDecimalQuantity() const { return fDecimalQuantity;}
652 
657  void populateDecimalQuantity(number::impl::DecimalQuantity& output, UErrorCode& status) const;
658 
665  void adoptDecimalQuantity(number::impl::DecimalQuantity *dq);
666 
673  CharString *internalGetCharString(UErrorCode &status);
674 
675 #endif /* U_HIDE_INTERNAL_API */
676 
677 private:
682  void dispose();
683 
687  void init();
688 
689  UnicodeString* getBogus() const;
690 
691  union {
692  UObject* fObject;
693  UnicodeString* fString;
694  double fDouble;
695  int64_t fInt64;
696  UDate fDate;
697  struct {
698  Formattable* fArray;
699  int32_t fCount;
700  } fArrayAndCount;
701  } fValue;
702 
703  CharString *fDecimalStr;
704 
705  number::impl::DecimalQuantity *fDecimalQuantity;
706 
707  Type fType;
708  UnicodeString fBogus; // Bogus string when it's needed.
709 };
710 
711 inline UDate Formattable::getDate(UErrorCode& status) const {
712  if (fType != kDate) {
713  if (U_SUCCESS(status)) {
714  status = U_INVALID_FORMAT_ERROR;
715  }
716  return 0;
717  }
718  return fValue.fDate;
719 }
720 
721 inline const UnicodeString& Formattable::getString() const {
722  return *fValue.fString;
723 }
724 
725 inline UnicodeString& Formattable::getString() {
726  return *fValue.fString;
727 }
728 
729 #ifndef U_HIDE_DEPRECATED_API
730 inline int32_t Formattable::getLong(UErrorCode* status) const {
731  return getLong(*status);
732 }
733 #endif /* U_HIDE_DEPRECATED_API */
734 
735 inline UFormattable* Formattable::toUFormattable() {
736  return reinterpret_cast<UFormattable*>(this);
737 }
738 
739 inline const UFormattable* Formattable::toUFormattable() const {
740  return reinterpret_cast<const UFormattable*>(this);
741 }
742 
743 inline Formattable* Formattable::fromUFormattable(UFormattable *fmt) {
744  return reinterpret_cast<Formattable *>(fmt);
745 }
746 
747 inline const Formattable* Formattable::fromUFormattable(const UFormattable *fmt) {
748  return reinterpret_cast<const Formattable *>(fmt);
749 }
750 
751 U_NAMESPACE_END
752 
753 #endif /* #if !UCONFIG_NO_FORMATTING */
754 
755 #endif /* U_SHOW_CPLUSPLUS_API */
756 
757 #endif //_FMTABLE
758 //eof
C API: UFormattable is a thin wrapper for primitive types used for formatting and parsing...
#define U_SUCCESS(x)
Does the error code indicate success?
Definition: utypes.h:742
int64_t getInt64() const
Gets the int64 value of this object.
Definition: fmtable.h:340
double getDouble() const
Gets the double value of this object.
Definition: fmtable.h:292
double UDate
Date and Time data type.
Definition: utypes.h:218
C++ API: Unicode String.
Type
Selector for flavor of data type contained within a Formattable object.
Definition: fmtable.h:220
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:96
UDate getDate() const
Gets the Date value of this object.
Definition: fmtable.h:365
UnicodeString & getString(UnicodeString &result) const
Gets the string value of this object.
Definition: fmtable.h:384
Selector indicating an array of Formattables.
Definition: fmtable.h:254
#define U_I18N_API
Set to export library symbols from inside the i18n library, and to import them from outside...
Definition: utypes.h:316
C++ API: StringPiece: Read-only byte string wrapper class.
Selector indicating a double value.
Definition: fmtable.h:233
Formattable & operator[](int32_t index)
Accesses the specified element in the array value of this Formattable object.
Definition: fmtable.h:464
Data format is not what is expected.
Definition: utypes.h:469
int32_t getLong() const
Gets the long value of this object.
Definition: fmtable.h:314
ISDATE
This enum is only used to let callers distinguish between the Formattable(UDate) constructor and the ...
Definition: fmtable.h:74
virtual UClassID getDynamicClassID() const
ICU4C "poor man&#39;s RTTI", returns a UClassID for the actual ICU class.
number::impl::DecimalQuantity * getDecimalQuantity() const
Internal function, do not use.
Definition: fmtable.h:651
bool operator!=(const Formattable &other) const
Equality operator.
Definition: fmtable.h:192
UErrorCode
Standard ICU4C error code type, a substitute for exceptions.
Definition: utypes.h:430
Selector indicating a UnicodeString value.
Definition: fmtable.h:247
const Formattable * getArray(int32_t &count) const
Gets the array value and count of this object.
Definition: fmtable.h:442
Selector indicating a UDate value.
Definition: fmtable.h:226
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:295
Formattable objects can be passed to the Format class or its subclasses for formatting.
Definition: fmtable.h:63
Selector indicating a 32-bit integer value.
Definition: fmtable.h:240
void * UFormattable
Opaque type representing various types of data which may be used for formatting and parsing operation...
Definition: uformattable.h:72
A string-like object that points to a sized piece of memory.
Definition: stringpiece.h:61
UObject is the common ICU "boilerplate" class.
Definition: uobject.h:223
int8_t UBool
The ICU boolean type, a signed-byte integer.
Definition: umachine.h:247
Selector indicating a 64-bit integer value.
Definition: fmtable.h:261