ICU 62.1 62.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
27#if !UCONFIG_NO_FORMATTING
28
29#include "unicode/unistr.h"
30#include "unicode/stringpiece.h"
32
34
35class CharString;
36namespace number {
37namespace impl {
38class DecimalQuantity;
39}
40}
41
63public:
73 enum ISDATE { kIsDate };
74
79 Formattable(); // Type kLong, value 0
80
88
94 Formattable(double d);
95
101 Formattable(int32_t l);
102
108 Formattable(int64_t ll);
109
110#if !UCONFIG_NO_CONVERSION
117 Formattable(const char* strToCopy);
118#endif
119
134
141
148
155 Formattable(const Formattable* arrayToCopy, int32_t count);
156
163
169
176
184
192 { return !operator==(other); }
193
198 virtual ~Formattable();
199
212
219 enum Type {
226
233
240
247
254
261
267 kObject
268 };
269
275 Type getType(void) const;
276
284
291 double getDouble(void) const { return fValue.fDouble; }
292
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
396
404 inline const UnicodeString& getString(void) const;
405
415
422 inline UnicodeString& getString(void);
423
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
492
499 void setDouble(double d);
500
507 void setLong(int32_t l);
508
515 void setInt64(int64_t ll);
516
524
532
540 void setArray(const Formattable* array, int32_t count);
541
549
555 void adoptArray(Formattable* array, int32_t count);
556
565
582
589
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
638 inline int32_t getLong(UErrorCode* status) const;
639#endif /* U_HIDE_DEPRECATED_API */
640
641#ifndef U_HIDE_INTERNAL_API
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
673
674#endif /* U_HIDE_INTERNAL_API */
675
676private:
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
710inline UDate Formattable::getDate(UErrorCode& status) const {
711 if (fType != kDate) {
712 if (U_SUCCESS(status)) {
714 }
715 return 0;
716 }
717 return fValue.fDate;
718}
719
720inline const UnicodeString& Formattable::getString(void) const {
721 return *fValue.fString;
722}
723
724inline UnicodeString& Formattable::getString(void) {
725 return *fValue.fString;
726}
727
728#ifndef U_HIDE_DEPRECATED_API
729inline int32_t Formattable::getLong(UErrorCode* status) const {
730 return getLong(*status);
731}
732#endif /* U_HIDE_DEPRECATED_API */
733
734inline UFormattable* Formattable::toUFormattable() {
735 return reinterpret_cast<UFormattable*>(this);
736}
737
738inline const UFormattable* Formattable::toUFormattable() const {
739 return reinterpret_cast<const UFormattable*>(this);
740}
741
742inline Formattable* Formattable::fromUFormattable(UFormattable *fmt) {
743 return reinterpret_cast<Formattable *>(fmt);
744}
745
746inline 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
Formattable objects can be passed to the Format class or its subclasses for formatting.
Definition fmtable.h:62
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(UnicodeString *strToAdopt)
Creates a Formattable object with a UnicodeString object to adopt from.
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:339
UBool operator!=(const Formattable &other) const
Equality operator.
Definition fmtable.h:191
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.
void setInt64(int64_t ll)
Sets the int64 value of this object and changes the type to kInt64.
UBool operator==(const Formattable &other) const
Equality comparison.
Formattable(double d)
Creates a Formattable object with a double number.
virtual UClassID getDynamicClassID() const
ICU "poor man's RTTI", returns a UClassID for the actual class.
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.
Formattable & operator=(const Formattable &rhs)
Assignment operator.
const UObject * getObject() const
Returns a pointer to the UObject contained within this formattable, or NULL if this object does not c...
Type
Selector for flavor of data type contained within a Formattable object.
Definition fmtable.h:219
@ kDouble
Selector indicating a double value.
Definition fmtable.h:232
@ kArray
Selector indicating an array of Formattables.
Definition fmtable.h:253
@ kLong
Selector indicating a 32-bit integer value.
Definition fmtable.h:239
@ kInt64
Selector indicating a 64-bit integer value.
Definition fmtable.h:260
@ kString
Selector indicating a UnicodeString value.
Definition fmtable.h:246
@ kDate
Selector indicating a UDate value.
Definition fmtable.h:225
void setDate(UDate d)
Sets the Date value of this object and changes the type to kDate.
double getDouble(UErrorCode &status) const
Gets the double value of this object.
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:291
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.
UnicodeString & getString(UnicodeString &result) const
Gets the string value of this object.
Definition fmtable.h:383
Type getType(void) const
Gets the data type of this Formattable object.
Formattable & operator[](int32_t index)
Accesses the specified element in the array value of this Formattable object.
Definition fmtable.h:463
Formattable * clone() const
Clone this object.
number::impl::DecimalQuantity * getDecimalQuantity() const
Internal function, do not use.
Definition fmtable.h:650
UnicodeString & getString(UErrorCode &status)
Gets a reference to the string value of this object.
int32_t getLong(void) const
Gets the long value of this object.
Definition fmtable.h:313
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,...
const Formattable * getArray(int32_t &count, UErrorCode &status) const
Gets the array value and count of this object.
virtual ~Formattable()
Destructor.
ISDATE
This enum is only used to let callers distinguish between the Formattable(UDate) constructor and the ...
Definition fmtable.h:73
CharString * internalGetCharString(UErrorCode &status)
Internal function to return the CharString pointer.
const Formattable * getArray(int32_t &count) const
Gets the array value and count of this object.
Definition fmtable.h:441
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:364
int32_t getLong(UErrorCode &status) const
Gets the long value of this object.
Formattable(int64_t ll)
Creates a Formattable object with an int64_t number.
const UnicodeString & getString(UErrorCode &status) const
Gets a const reference to the string value of this object.
int64_t getInt64(UErrorCode &status) const
Gets the int64 value of this object.
UnicodeString & getString(UnicodeString &result, UErrorCode &status) const
Gets the string value of this object.
"Smart pointer" base class; do not use directly: use LocalPointer etc.
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
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition unistr.h:287
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...
int8_t UBool
The ICU boolean type.
Definition umachine.h:236
C++ API: Unicode String.
void * UClassID
UClassID is used to identify classes without using the compiler's RTTI.
Definition uobject.h:93
Basic definitions for ICU, for both C and C++ APIs.
UErrorCode
Error code to replace exception handling, so that the code is compatible with all C++ compilers,...
Definition utypes.h:396
@ U_INVALID_FORMAT_ERROR
Data format is not what is expected.
Definition utypes.h:434
#define U_SUCCESS(x)
Does the error code indicate success?
Definition utypes.h:689
#define U_I18N_API
Set to export library symbols from inside the i18n library, and to import them from outside.
Definition utypes.h:360
double UDate
Date and Time data type.
Definition utypes.h:203
#define U_NAMESPACE_END
This is used to end a declaration of a public ICU C++ API.
Definition uversion.h:138
#define U_NAMESPACE_BEGIN
This is used to begin a declaration of a public ICU C++ API.
Definition uversion.h:137