fmtable.h
Go to the documentation of this file.
// © 2016 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html
/*
********************************************************************************
* Copyright (C) 1997-2014, International Business Machines
* Corporation and others. All Rights Reserved.
********************************************************************************
*
* File FMTABLE.H
*
* Modification History:
*
* Date Name Description
* 02/29/97 aliu Creation.
********************************************************************************
*/
#ifndef FMTABLE_H
#define FMTABLE_H
#include "unicode/utypes.h"
#if U_SHOW_CPLUSPLUS_API
#if !UCONFIG_NO_FORMATTING
#include "unicode/unistr.h"
#include "unicode/stringpiece.h"
#include "unicode/uformattable.h"
U_NAMESPACE_BEGIN
class CharString;
namespace number::impl {
class DecimalQuantity;
}
class U_I18N_API Formattable : public UObject {
public:
enum ISDATE { kIsDate };
Formattable(); // Type kLong, value 0
Formattable(UDate d, ISDATE flag);
Formattable(double d);
Formattable(int32_t l);
Formattable(int64_t ll);
#if !UCONFIG_NO_CONVERSION
Formattable(const char* strToCopy);
#endif
Formattable(StringPiece number, UErrorCode &status);
Formattable(const UnicodeString& strToCopy);
Formattable(UnicodeString* strToAdopt);
Formattable(const Formattable* arrayToCopy, int32_t count);
Formattable(UObject* objectToAdopt);
Formattable(const Formattable&);
Formattable& operator=(const Formattable &rhs);
bool operator==(const Formattable &other) const;
bool operator!=(const Formattable& other) const
{ return !operator==(other); }
virtual ~Formattable();
Formattable *clone() const;
enum Type {
kDate,
kDouble,
kLong,
kString,
kArray,
kInt64,
kObject
};
Type getType() const;
UBool isNumeric() const;
double getDouble() const { return fValue.fDouble; }
double getDouble(UErrorCode& status) const;
int32_t getLong() const { return static_cast<int32_t>(fValue.fInt64); }
int32_t getLong(UErrorCode& status) const;
int64_t getInt64() const { return fValue.fInt64; }
int64_t getInt64(UErrorCode& status) const;
UDate getDate() const { return fValue.fDate; }
UDate getDate(UErrorCode& status) const;
UnicodeString& getString(UnicodeString& result) const
{ result=*fValue.fString; return result; }
UnicodeString& getString(UnicodeString& result, UErrorCode& status) const;
inline const UnicodeString& getString() const;
const UnicodeString& getString(UErrorCode& status) const;
inline UnicodeString& getString();
UnicodeString& getString(UErrorCode& status);
const Formattable* getArray(int32_t& count) const
{ count=fValue.fArrayAndCount.fCount; return fValue.fArrayAndCount.fArray; }
const Formattable* getArray(int32_t& count, UErrorCode& status) const;
Formattable& operator[](int32_t index) { return fValue.fArrayAndCount.fArray[index]; }
const UObject* getObject() const;
StringPiece getDecimalNumber(UErrorCode &status);
void setDouble(double d);
void setLong(int32_t l);
void setInt64(int64_t ll);
void setDate(UDate d);
void setString(const UnicodeString& stringToCopy);
void setArray(const Formattable* array, int32_t count);
void adoptString(UnicodeString* stringToAdopt);
void adoptArray(Formattable* array, int32_t count);
void adoptObject(UObject* objectToAdopt);
void setDecimalNumber(StringPiece numberString,
UErrorCode &status);
virtual UClassID getDynamicClassID() const override;
static UClassID U_EXPORT2 getStaticClassID();
static inline Formattable *fromUFormattable(UFormattable *fmt);
static inline const Formattable *fromUFormattable(const UFormattable *fmt);
inline UFormattable *toUFormattable();
inline const UFormattable *toUFormattable() const;
#ifndef U_HIDE_DEPRECATED_API
inline int32_t getLong(UErrorCode* status) const;
#endif /* U_HIDE_DEPRECATED_API */
#ifndef U_HIDE_INTERNAL_API
number::impl::DecimalQuantity *getDecimalQuantity() const { return fDecimalQuantity;}
void populateDecimalQuantity(number::impl::DecimalQuantity& output, UErrorCode& status) const;
void adoptDecimalQuantity(number::impl::DecimalQuantity *dq);
CharString *internalGetCharString(UErrorCode &status);
#endif /* U_HIDE_INTERNAL_API */
private:
void dispose();
void init();
UnicodeString* getBogus() const;
union {
UObject* fObject;
UnicodeString* fString;
double fDouble;
int64_t fInt64;
UDate fDate;
struct {
Formattable* fArray;
int32_t fCount;
} fArrayAndCount;
} fValue;
CharString *fDecimalStr;
number::impl::DecimalQuantity *fDecimalQuantity;
Type fType;
UnicodeString fBogus; // Bogus string when it's needed.
};
inline UDate Formattable::getDate(UErrorCode& status) const {
if (fType != kDate) {
if (U_SUCCESS(status)) {
status = U_INVALID_FORMAT_ERROR;
}
return 0;
}
return fValue.fDate;
}
inline const UnicodeString& Formattable::getString() const {
return *fValue.fString;
}
inline UnicodeString& Formattable::getString() {
return *fValue.fString;
}
#ifndef U_HIDE_DEPRECATED_API
inline int32_t Formattable::getLong(UErrorCode* status) const {
return getLong(*status);
}
#endif /* U_HIDE_DEPRECATED_API */
inline UFormattable* Formattable::toUFormattable() {
return reinterpret_cast< UFormattable*>(this);
}
inline const UFormattable* Formattable::toUFormattable() const {
return reinterpret_cast<const UFormattable*>(this);
}
inline Formattable* Formattable::fromUFormattable(UFormattable *fmt) {
return reinterpret_cast< Formattable *>(fmt);
}
inline const Formattable* Formattable::fromUFormattable(const UFormattable *fmt) {
return reinterpret_cast<const Formattable *>(fmt);
}
U_NAMESPACE_END
#endif /* #if !UCONFIG_NO_FORMATTING */
#endif /* U_SHOW_CPLUSPLUS_API */
#endif //_FMTABLE
//eof
icu::Formattable
Formattable objects can be passed to the Format class or its subclasses for formatting.
Definition: fmtable.h:63
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::operator=
Formattable & operator=(const Formattable &rhs)
Assignment operator.
icu::Formattable::Formattable
Formattable(UnicodeString *strToAdopt)
Creates a Formattable object with a UnicodeString object to adopt from.
icu::Formattable::clone
Formattable * clone() const
Clone this object.
icu::Formattable::getType
Type getType() const
Gets the data type of this Formattable object.
icu::Formattable::operator[]
Formattable & operator[](int32_t index)
Accesses the specified element in the array value of this Formattable object.
Definition: fmtable.h:464
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::getString
UnicodeString & getString(UErrorCode &status)
Gets a reference to the string value of this object.
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::getObject
const UObject * getObject() const
Returns a pointer to the UObject contained within this formattable, or nullptr if this object does no...
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::getDecimalQuantity
number::impl::DecimalQuantity * getDecimalQuantity() const
Internal function, do not use.
Definition: fmtable.h:651
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::getLong
int32_t getLong() const
Gets the long value of this object.
Definition: fmtable.h:314
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::Type
Type
Selector for flavor of data type contained within a Formattable object.
Definition: fmtable.h:220
icu::Formattable::kDouble
@ kDouble
Selector indicating a double value.
Definition: fmtable.h:233
icu::Formattable::kArray
@ kArray
Selector indicating an array of Formattables.
Definition: fmtable.h:254
icu::Formattable::kLong
@ kLong
Selector indicating a 32-bit integer value.
Definition: fmtable.h:240
icu::Formattable::kInt64
@ kInt64
Selector indicating a 64-bit integer value.
Definition: fmtable.h:261
icu::Formattable::kString
@ kString
Selector indicating a UnicodeString value.
Definition: fmtable.h:247
icu::Formattable::kDate
@ kDate
Selector indicating a UDate value.
Definition: fmtable.h:226
icu::Formattable::setDate
void setDate(UDate d)
Sets the Date value of this object and changes the type to kDate.
icu::Formattable::getArray
const Formattable * getArray(int32_t &count) const
Gets the array value and count of this object.
Definition: fmtable.h:442
icu::Formattable::getDouble
double getDouble(UErrorCode &status) const
Gets the double value of this object.
icu::Formattable::internalGetCharString
CharString * internalGetCharString(UErrorCode &status)
Internal function to return the CharString pointer.
icu::Formattable::Formattable
Formattable(const char *strToCopy)
Creates a Formattable object with a char string pointer.
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::operator==
bool operator==(const Formattable &other) const
Equality comparison.
icu::Formattable::getString
const UnicodeString & getString(UErrorCode &status) const
Gets a const reference to the string value of this object.
icu::Formattable::getDouble
double getDouble() const
Gets the double value of this object.
Definition: fmtable.h:292
icu::Formattable::getString
UnicodeString & getString(UnicodeString &result, UErrorCode &status) const
Gets the string value of this object.
icu::Formattable::Formattable
Formattable(const Formattable *arrayToCopy, int32_t count)
Creates a Formattable object with an array of Formattable objects.
icu::Formattable::getInt64
int64_t getInt64() const
Gets the int64 value of this object.
Definition: fmtable.h:340
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::getString
UnicodeString & getString(UnicodeString &result) const
Gets the string value of this object.
Definition: fmtable.h:384
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:74
icu::Formattable::operator!=
bool operator!=(const Formattable &other) const
Equality operator.
Definition: fmtable.h:192
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:365
icu::Formattable::getLong
int32_t getLong(UErrorCode &status) const
Gets the long value of this object.
icu::Formattable::getArray
const Formattable * getArray(int32_t &count, UErrorCode &status) const
Gets the array value and count of this object.
icu::Formattable::Formattable
Formattable(int64_t ll)
Creates a Formattable object with an int64_t number.
icu::Formattable::getInt64
int64_t getInt64(UErrorCode &status) const
Gets the int64 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:61
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:430
U_INVALID_FORMAT_ERROR
@ U_INVALID_FORMAT_ERROR
Data format is not what is expected.
Definition: utypes.h:469
U_SUCCESS
#define U_SUCCESS(x)
Does the error code indicate success?
Definition: utypes.h:742
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:316
UDate
double UDate
Date and Time data type.
Definition: utypes.h:218