ICU 66.0.1  66.0.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 
89  Formattable(UDate d, ISDATE flag);
90 
96  Formattable(double d);
97 
103  Formattable(int32_t l);
104 
110  Formattable(int64_t ll);
111 
112 #if !UCONFIG_NO_CONVERSION
113 
119  Formattable(const char* strToCopy);
120 #endif
121 
135  Formattable(StringPiece number, UErrorCode &status);
136 
142  Formattable(const UnicodeString& strToCopy);
143 
149  Formattable(UnicodeString* strToAdopt);
150 
157  Formattable(const Formattable* arrayToCopy, int32_t count);
158 
164  Formattable(UObject* objectToAdopt);
165 
170  Formattable(const Formattable&);
171 
177  Formattable& operator=(const Formattable &rhs);
178 
185  UBool operator==(const Formattable &other) const;
186 
193  UBool 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 
397  UnicodeString& getString(UnicodeString& result, UErrorCode& status) const;
398 
406  inline const UnicodeString& getString(void) const;
407 
416  const UnicodeString& getString(UErrorCode& status) const;
417 
424  inline UnicodeString& getString(void);
425 
434  UnicodeString& getString(UErrorCode& status);
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 
493  StringPiece getDecimalNumber(UErrorCode &status);
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;
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
634 
640  inline int32_t getLong(UErrorCode* status) const;
641 #endif /* U_HIDE_DEPRECATED_API */
642 
643 #ifndef U_HIDE_INTERNAL_API
644 
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
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:193
#define U_SUCCESS(x)
Does the error code indicate success?
Definition: utypes.h:704
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:221
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:366
UnicodeString & getString(UnicodeString &result) const
Gets the string value of this object.
Definition: fmtable.h:385
Selector indicating an array of Formattables.
Definition: fmtable.h:255
int64_t getInt64(void) const
Gets the int64 value of this object.
Definition: fmtable.h:341
#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.
Selector indicating a double value.
Definition: fmtable.h:234
Formattable & operator[](int32_t index)
Accesses the specified element in the array value of this Formattable object.
Definition: fmtable.h:465
Data format is not what is expected.
Definition: utypes.h:453
ISDATE
This enum is only used to let callers distinguish between the Formattable(UDate) constructor and the ...
Definition: fmtable.h:75
int32_t getLong(void) const
Gets the long value of this object.
Definition: fmtable.h:315
number::impl::DecimalQuantity * getDecimalQuantity() const
Internal function, do not use.
Definition: fmtable.h:652
double getDouble(void) const
Gets the double value of this object.
Definition: fmtable.h:293
UErrorCode
Standard ICU4C error code type, a substitute for exceptions.
Definition: utypes.h:415
Selector indicating a UnicodeString value.
Definition: fmtable.h:248
const Formattable * getArray(int32_t &count) const
Gets the array value and count of this object.
Definition: fmtable.h:443
Selector indicating a UDate value.
Definition: fmtable.h:227
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:294
Formattable objects can be passed to the Format class or its subclasses for formatting.
Definition: fmtable.h:64
Selector indicating a 32-bit integer value.
Definition: fmtable.h:241
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:60
UObject is the common ICU "boilerplate" class.
Definition: uobject.h:223
int8_t UBool
The ICU boolean type.
Definition: umachine.h:261
Selector indicating a 64-bit integer value.
Definition: fmtable.h:262