ICU 74.2: i18n/unicode/fmtable.h Source File

ICU 74.2 74.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
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
35U_NAMESPACE_BEGIN
36
37class CharString;
38namespace number {
39namespace impl {
40class DecimalQuantity;
41}
42}
43
64 class U_I18N_API Formattable : public UObject {
65public:
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
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 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 {
227 kDate,
228
234 kDouble,
235
241 kLong,
242
248 kString,
249
255 kArray,
256
262 kInt64,
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
385 UnicodeString& getString(UnicodeString& result) const
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 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
678private:
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
752U_NAMESPACE_END
753
754#endif /* #if !UCONFIG_NO_FORMATTING */
755
756#endif /* U_SHOW_CPLUSPLUS_API */
757
758#endif //_FMTABLE
759//eof
icu::Formattable
Formattable objects can be passed to the Format class or its subclasses for formatting.
Definition fmtable.h:64
icu::Formattable::setDecimalNumber
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...
icu::Formattable::Formattable
Formattable(StringPiece number, UErrorCode &status)
Creates a Formattable object of an appropriate numeric type from a a decimal number in string form.
icu::Formattable::Formattable
Formattable(UnicodeString *strToAdopt)
Creates a Formattable object with a UnicodeString object to adopt from.
icu::Formattable::getStaticClassID
static UClassID getStaticClassID()
ICU "poor man's RTTI", returns a UClassID for this class.
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::setString
void setString(const UnicodeString &stringToCopy)
Sets the string value of this object and changes the type to kString.
icu::Formattable::isNumeric
UBool isNumeric() const
Returns true if the data type of this Formattable object is kDouble, kLong, or kInt64.
icu::Formattable::getInt64
int64_t getInt64(void) const
Gets the int64 value of this object.
Definition fmtable.h:341
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::Formattable
Formattable(UObject *objectToAdopt)
Creates a Formattable object that adopts the given UObject.
icu::Formattable::setInt64
void setInt64(int64_t ll)
Sets the int64 value of this object and changes the type to kInt64.
icu::Formattable::Formattable
Formattable(double d)
Creates a Formattable object with a double number.
icu::Formattable::Formattable
Formattable()
Default constructor.
icu::Formattable::setDouble
void setDouble(double d)
Sets the double value of this object and changes the type to kDouble.
icu::Formattable::Formattable
Formattable(const UnicodeString &strToCopy)
Creates a Formattable object with a UnicodeString object to copy from.
icu::Formattable::adoptDecimalQuantity
void adoptDecimalQuantity(number::impl::DecimalQuantity *dq)
Adopt, and set value from, a DecimalQuantity Internal Function, do not use.
icu::Formattable::setLong
void setLong(int32_t l)
Sets the long value of this object and changes the type to kLong.
icu::Formattable::operator=
Formattable & operator=(const Formattable &rhs)
Assignment operator.
icu::Formattable::getObject
const UObject * getObject() const
Returns a pointer to the UObject contained within this formattable, or nullptr if this object does no...
icu::Formattable::Type
Type
Selector for flavor of data type contained within a Formattable object.
Definition fmtable.h:221
icu::Formattable::kDouble
@ kDouble
Selector indicating a double value.
Definition fmtable.h:234
icu::Formattable::kArray
@ kArray
Selector indicating an array of Formattables.
Definition fmtable.h:255
icu::Formattable::kLong
@ kLong
Selector indicating a 32-bit integer value.
Definition fmtable.h:241
icu::Formattable::kInt64
@ kInt64
Selector indicating a 64-bit integer value.
Definition fmtable.h:262
icu::Formattable::kString
@ kString
Selector indicating a UnicodeString value.
Definition fmtable.h:248
icu::Formattable::kDate
@ kDate
Selector indicating a UDate value.
Definition fmtable.h:227
icu::Formattable::setDate
void setDate(UDate d)
Sets the Date value of this object and changes the type to kDate.
icu::Formattable::getDouble
double getDouble(UErrorCode &status) const
Gets the double value of this object.
icu::Formattable::Formattable
Formattable(const char *strToCopy)
Creates a Formattable object with a char string pointer.
icu::Formattable::getDouble
double getDouble(void) const
Gets the double value of this object.
Definition fmtable.h:293
icu::Formattable::Formattable
Formattable(UDate d, ISDATE flag)
Creates a Formattable object with a UDate instance.
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::adoptObject
void adoptObject(UObject *objectToAdopt)
Sets and adopts the UObject value of this object and changes the type to kObject.
icu::Formattable::getString
UnicodeString & getString(UnicodeString &result) const
Gets the string value of this object.
Definition fmtable.h:385
icu::Formattable::getType
Type getType(void) const
Gets the data type of this Formattable object.
icu::Formattable::operator==
bool operator==(const Formattable &other) const
Equality comparison.
icu::Formattable::operator[]
Formattable & operator[](int32_t index)
Accesses the specified element in the array value of this Formattable object.
Definition fmtable.h:465
icu::Formattable::clone
Formattable * clone() const
Clone this object.
icu::Formattable::getDecimalQuantity
number::impl::DecimalQuantity * getDecimalQuantity() const
Internal function, do not use.
Definition fmtable.h:652
icu::Formattable::getString
UnicodeString & getString(UErrorCode &status)
Gets a reference to the string value of this object.
icu::Formattable::getLong
int32_t getLong(void) const
Gets the long value of this object.
Definition fmtable.h:315
icu::Formattable::Formattable
Formattable(const Formattable *arrayToCopy, int32_t count)
Creates a Formattable object with an array of Formattable objects.
icu::Formattable::Formattable
Formattable(const Formattable &)
Copy constructor.
icu::Formattable::Formattable
Formattable(int32_t l)
Creates a Formattable object with a long number.
icu::Formattable::getDecimalNumber
StringPiece getDecimalNumber(UErrorCode &status)
Returns a numeric string representation of the number contained within this formattable,...
icu::Formattable::getArray
const Formattable * getArray(int32_t &count, UErrorCode &status) const
Gets the array value and count of this object.
icu::Formattable::~Formattable
virtual ~Formattable()
Destructor.
icu::Formattable::ISDATE
ISDATE
This enum is only used to let callers distinguish between the Formattable(UDate) constructor and the ...
Definition fmtable.h:75
icu::Formattable::operator!=
bool operator!=(const Formattable &other) const
Equality operator.
Definition fmtable.h:193
icu::Formattable::internalGetCharString
CharString * internalGetCharString(UErrorCode &status)
Internal function to return the CharString pointer.
icu::Formattable::getArray
const Formattable * getArray(int32_t &count) const
Gets the array value and count of this object.
Definition fmtable.h:443
icu::Formattable::populateDecimalQuantity
void populateDecimalQuantity(number::impl::DecimalQuantity &output, UErrorCode &status) const
Export the value of this Formattable to a DecimalQuantity.
icu::Formattable::getDate
UDate getDate() const
Gets the Date value of this object.
Definition fmtable.h:366
icu::Formattable::getLong
int32_t getLong(UErrorCode &status) const
Gets the long value of this object.
icu::Formattable::Formattable
Formattable(int64_t ll)
Creates a Formattable object with an int64_t number.
icu::Formattable::getString
const UnicodeString & getString(UErrorCode &status) const
Gets a const reference to the string value of this object.
icu::Formattable::getInt64
int64_t getInt64(UErrorCode &status) const
Gets the int64 value of this object.
icu::Formattable::getString
UnicodeString & getString(UnicodeString &result, UErrorCode &status) const
Gets the string value of this object.
icu::Formattable::getDynamicClassID
virtual UClassID getDynamicClassID() const override
ICU "poor man's RTTI", returns a UClassID for the actual class.
icu::StringPiece
A string-like object that points to a sized piece of memory.
Definition stringpiece.h:60
icu::UObject
UObject is the common ICU "boilerplate" class.
Definition uobject.h:223
icu::UnicodeString
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition unistr.h:296
icu::operator==
U_EXPORT UBool operator==(const StringPiece &x, const StringPiece &y)
Global operator == for StringPiece.
stringpiece.h
C++ API: StringPiece: Read-only byte string wrapper class.
uformattable.h
C API: UFormattable is a thin wrapper for primitive types used for formatting and parsing.
UFormattable
void * UFormattable
Opaque type representing various types of data which may be used for formatting and parsing operation...
Definition uformattable.h:72
UBool
int8_t UBool
The ICU boolean type, a signed-byte integer.
Definition umachine.h:247
unistr.h
C++ API: Unicode String.
UClassID
void * UClassID
UClassID is used to identify classes without using the compiler's RTTI.
Definition uobject.h:96
utypes.h
Basic definitions for ICU, for both C and C++ APIs.
UErrorCode
UErrorCode
Standard ICU4C error code type, a substitute for exceptions.
Definition utypes.h:415
U_INVALID_FORMAT_ERROR
@ U_INVALID_FORMAT_ERROR
Data format is not what is expected.
Definition utypes.h:453
U_SUCCESS
#define U_SUCCESS(x)
Does the error code indicate success?
Definition utypes.h:712
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:301
UDate
double UDate
Date and Time data type.
Definition utypes.h:203