1999-12-28 23:57:50 +00:00
|
|
|
/*
|
|
|
|
********************************************************************************
|
2003-06-03 20:58:22 +00:00
|
|
|
* Copyright (C) 1997-2003, International Business Machines
|
1999-12-28 23:57:50 +00:00
|
|
|
* 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"
|
2002-09-20 01:54:48 +00:00
|
|
|
|
|
|
|
#if !UCONFIG_NO_FORMATTING
|
|
|
|
|
2002-06-27 01:19:20 +00:00
|
|
|
#include "unicode/uobject.h"
|
1999-12-28 23:57:50 +00:00
|
|
|
#include "unicode/unistr.h"
|
|
|
|
|
2001-10-08 23:26:58 +00:00
|
|
|
U_NAMESPACE_BEGIN
|
|
|
|
|
1999-12-28 23:57:50 +00:00
|
|
|
/**
|
|
|
|
* Formattable objects can be passed to the Format class or
|
|
|
|
* its subclasses for formatting. Formattable is a thin wrapper
|
|
|
|
* class which interconverts between the primitive numeric types
|
|
|
|
* (double, long, etc.) as well as UDate and UnicodeString.
|
|
|
|
* <P>
|
|
|
|
* Note that this is fundamentally different from the Java behavior, since
|
|
|
|
* in this case the various formattable objects do not occupy a hierarchy,
|
|
|
|
* but are all wrapped within this one class. Formattable encapsulates all
|
|
|
|
* the polymorphism in itself.
|
|
|
|
* <P>
|
|
|
|
* It would be easy to change this so that Formattable was an abstract base
|
|
|
|
* class of a genuine hierarchy, and that would clean up the code that
|
|
|
|
* currently must explicitly check for type, but that seems like overkill at
|
|
|
|
* this point.
|
|
|
|
*/
|
2002-06-27 01:19:20 +00:00
|
|
|
class U_I18N_API Formattable : public UObject {
|
1999-12-28 23:57:50 +00:00
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* This enum is only used to let callers distinguish between
|
|
|
|
* the Formattable(UDate) constructor and the Formattable(double)
|
|
|
|
* constructor; the compiler cannot distinguish the signatures,
|
|
|
|
* since UDate is currently typedefed to be either double or long.
|
|
|
|
* If UDate is changed later to be a bonafide class
|
|
|
|
* or struct, then we no longer need this enum.
|
2002-12-19 00:11:32 +00:00
|
|
|
* @draft ICU 2.4
|
1999-12-28 23:57:50 +00:00
|
|
|
*/
|
2003-05-10 00:27:54 +00:00
|
|
|
enum ISDATE { kIsDate };
|
1999-12-28 23:57:50 +00:00
|
|
|
|
2002-12-19 00:11:32 +00:00
|
|
|
/**
|
|
|
|
* Default constructor
|
|
|
|
* @draft ICU 2.4
|
|
|
|
*/
|
2003-05-10 00:27:54 +00:00
|
|
|
Formattable(); // Type kLong, value 0
|
1999-12-28 23:57:50 +00:00
|
|
|
/**
|
|
|
|
* Creates a Formattable object with a UDate instance.
|
|
|
|
* @param d the UDate instance.
|
2003-06-09 06:13:14 +00:00
|
|
|
* @param flag the flag to indicate this is a date. Always set it to kIsDate
|
2002-12-05 00:30:16 +00:00
|
|
|
* @stable ICU 2.0
|
1999-12-28 23:57:50 +00:00
|
|
|
*/
|
2003-06-09 06:13:14 +00:00
|
|
|
Formattable(UDate d, ISDATE flag);
|
1999-12-28 23:57:50 +00:00
|
|
|
/**
|
|
|
|
* Creates a Formattable object with a double number.
|
|
|
|
* @param d the double number.
|
2002-12-04 23:46:16 +00:00
|
|
|
* @stable ICU 2.0
|
1999-12-28 23:57:50 +00:00
|
|
|
*/
|
2003-05-10 00:27:54 +00:00
|
|
|
Formattable(double d);
|
1999-12-28 23:57:50 +00:00
|
|
|
/**
|
|
|
|
* Creates a Formattable object with a long number.
|
2003-05-10 00:27:54 +00:00
|
|
|
* @param l the long number.
|
2002-12-04 23:46:16 +00:00
|
|
|
* @stable ICU 2.0
|
1999-12-28 23:57:50 +00:00
|
|
|
*/
|
2003-05-10 00:27:54 +00:00
|
|
|
Formattable(int32_t l);
|
1999-12-28 23:57:50 +00:00
|
|
|
/**
|
|
|
|
* Creates a Formattable object with a char string pointer.
|
|
|
|
* Assumes that the char string is null terminated.
|
|
|
|
* @param strToCopy the char string.
|
2002-12-04 23:46:16 +00:00
|
|
|
* @stable ICU 2.0
|
1999-12-28 23:57:50 +00:00
|
|
|
*/
|
2003-05-10 00:27:54 +00:00
|
|
|
Formattable(const char* strToCopy);
|
1999-12-28 23:57:50 +00:00
|
|
|
/**
|
|
|
|
* Creates a Formattable object with a UnicodeString object to copy from.
|
|
|
|
* @param strToCopy the UnicodeString string.
|
2002-12-04 23:46:16 +00:00
|
|
|
* @stable ICU 2.0
|
1999-12-28 23:57:50 +00:00
|
|
|
*/
|
2003-05-10 00:27:54 +00:00
|
|
|
Formattable(const UnicodeString& strToCopy);
|
1999-12-28 23:57:50 +00:00
|
|
|
/**
|
|
|
|
* Creates a Formattable object with a UnicodeString object to adopt from.
|
|
|
|
* @param strToAdopt the UnicodeString string.
|
2002-12-04 23:46:16 +00:00
|
|
|
* @stable ICU 2.0
|
1999-12-28 23:57:50 +00:00
|
|
|
*/
|
2003-05-10 00:27:54 +00:00
|
|
|
Formattable(UnicodeString* strToAdopt);
|
1999-12-28 23:57:50 +00:00
|
|
|
/**
|
|
|
|
* Creates a Formattable object with an array of Formattable objects.
|
|
|
|
* @param arrayToCopy the Formattable object array.
|
|
|
|
* @param count the array count.
|
2002-12-04 23:46:16 +00:00
|
|
|
* @stable ICU 2.0
|
1999-12-28 23:57:50 +00:00
|
|
|
*/
|
2003-05-10 00:27:54 +00:00
|
|
|
Formattable(const Formattable* arrayToCopy, int32_t count);
|
1999-12-28 23:57:50 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Copy constructor.
|
2002-12-04 23:46:16 +00:00
|
|
|
* @stable ICU 2.0
|
1999-12-28 23:57:50 +00:00
|
|
|
*/
|
2003-05-10 00:27:54 +00:00
|
|
|
Formattable(const Formattable&);
|
1999-12-28 23:57:50 +00:00
|
|
|
/**
|
|
|
|
* Assignment operator.
|
2003-05-10 00:27:54 +00:00
|
|
|
* @param rhs The Formattable object to copy into this object.
|
2002-12-04 23:46:16 +00:00
|
|
|
* @stable ICU 2.0
|
1999-12-28 23:57:50 +00:00
|
|
|
*/
|
2003-05-10 00:27:54 +00:00
|
|
|
Formattable& operator=(const Formattable &rhs);
|
1999-12-28 23:57:50 +00:00
|
|
|
/**
|
|
|
|
* Equality comparison.
|
2002-07-01 11:04:45 +00:00
|
|
|
* @param other the object to be compared with.
|
|
|
|
* @return TRUE if other are equal to this, FALSE otherwise.
|
2002-12-04 23:46:16 +00:00
|
|
|
* @stable ICU 2.0
|
1999-12-28 23:57:50 +00:00
|
|
|
*/
|
2003-05-10 00:27:54 +00:00
|
|
|
UBool operator==(const Formattable &other) const;
|
2002-07-01 11:04:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Equality operator.
|
|
|
|
* @param other the object to be compared with.
|
|
|
|
* @return TRUE if other are unequal to this, FALSE otherwise.
|
2002-12-04 23:46:16 +00:00
|
|
|
* @stable ICU 2.0
|
2002-07-01 11:04:45 +00:00
|
|
|
*/
|
2000-05-18 22:08:39 +00:00
|
|
|
UBool operator!=(const Formattable& other) const
|
1999-12-28 23:57:50 +00:00
|
|
|
{ return !operator==(other); }
|
|
|
|
|
2000-03-22 19:19:33 +00:00
|
|
|
/**
|
|
|
|
* Destructor.
|
2002-12-04 23:46:16 +00:00
|
|
|
* @stable ICU 2.0
|
2000-03-22 19:19:33 +00:00
|
|
|
*/
|
1999-12-28 23:57:50 +00:00
|
|
|
virtual ~Formattable();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The list of possible data types of this Formattable object.
|
2002-12-19 00:11:32 +00:00
|
|
|
* @draft ICU 2.4
|
1999-12-28 23:57:50 +00:00
|
|
|
*/
|
|
|
|
enum Type {
|
|
|
|
kDate, // Date
|
|
|
|
kDouble, // double
|
|
|
|
kLong, // long
|
|
|
|
kString, // UnicodeString
|
|
|
|
kArray // Formattable[]
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the data type of this Formattable object.
|
2002-07-01 11:04:45 +00:00
|
|
|
* @return the data type of this Formattable object.
|
2002-12-04 23:46:16 +00:00
|
|
|
* @stable ICU 2.0
|
1999-12-28 23:57:50 +00:00
|
|
|
*/
|
|
|
|
Type getType(void) const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the double value of this object.
|
2002-07-01 11:04:45 +00:00
|
|
|
* @return the double value of this object.
|
2002-12-04 23:46:16 +00:00
|
|
|
* @stable ICU 2.0
|
1999-12-28 23:57:50 +00:00
|
|
|
*/
|
|
|
|
double getDouble(void) const { return fValue.fDouble; }
|
|
|
|
/**
|
|
|
|
* Gets the long value of this object.
|
2002-07-01 11:04:45 +00:00
|
|
|
* @return the long value of this object.
|
2002-12-04 23:46:16 +00:00
|
|
|
* @stable ICU 2.0
|
1999-12-28 23:57:50 +00:00
|
|
|
*/
|
|
|
|
int32_t getLong(void) const { return fValue.fLong; }
|
|
|
|
/**
|
|
|
|
* Gets the Date value of this object.
|
2002-07-01 11:04:45 +00:00
|
|
|
* @return the Date value of this object.
|
2002-12-04 23:46:16 +00:00
|
|
|
* @stable ICU 2.0
|
1999-12-28 23:57:50 +00:00
|
|
|
*/
|
|
|
|
UDate getDate(void) const { return fValue.fDate; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the string value of this object.
|
2002-07-01 11:04:45 +00:00
|
|
|
* @param result Output param to receive the Date value of this object.
|
|
|
|
* @return A reference to 'result'.
|
2002-12-04 23:46:16 +00:00
|
|
|
* @stable ICU 2.0
|
1999-12-28 23:57:50 +00:00
|
|
|
*/
|
|
|
|
UnicodeString& getString(UnicodeString& result) const
|
|
|
|
{ result=*fValue.fString; return result; }
|
|
|
|
|
2000-06-14 20:52:50 +00:00
|
|
|
/**
|
|
|
|
* Gets a const reference to the string value of this object.
|
2002-07-01 11:04:45 +00:00
|
|
|
* @return a const reference to the string value of this object.
|
2002-12-04 23:46:16 +00:00
|
|
|
* @stable ICU 2.0
|
2000-06-14 20:52:50 +00:00
|
|
|
*/
|
|
|
|
inline const UnicodeString& getString(void) const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a reference to the string value of this object.
|
2002-07-01 11:04:45 +00:00
|
|
|
* @return a reference to the string value of this object.
|
2002-12-04 23:46:16 +00:00
|
|
|
* @stable ICU 2.0
|
2000-06-14 20:52:50 +00:00
|
|
|
*/
|
|
|
|
inline UnicodeString& getString(void);
|
|
|
|
|
1999-12-28 23:57:50 +00:00
|
|
|
/**
|
|
|
|
* Gets the array value and count of this object.
|
2002-07-01 11:04:45 +00:00
|
|
|
* @param count fill-in with the count of this object.
|
|
|
|
* @return the array value of this object.
|
2002-12-04 23:46:16 +00:00
|
|
|
* @stable ICU 2.0
|
1999-12-28 23:57:50 +00:00
|
|
|
*/
|
|
|
|
const Formattable* getArray(int32_t& count) const
|
|
|
|
{ count=fValue.fArrayAndCount.fCount; return fValue.fArrayAndCount.fArray; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Accesses the specified element in the array value of this Formattable object.
|
|
|
|
* @param index the specified index.
|
|
|
|
* @return the accessed element in the array.
|
2002-12-04 23:46:16 +00:00
|
|
|
* @stable ICU 2.0
|
1999-12-28 23:57:50 +00:00
|
|
|
*/
|
|
|
|
Formattable& operator[](int32_t index) { return fValue.fArrayAndCount.fArray[index]; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the double value of this object.
|
2002-07-01 11:04:45 +00:00
|
|
|
* @param d the new double value to be set.
|
2002-12-04 23:46:16 +00:00
|
|
|
* @stable ICU 2.0
|
1999-12-28 23:57:50 +00:00
|
|
|
*/
|
|
|
|
void setDouble(double d);
|
|
|
|
/**
|
|
|
|
* Sets the long value of this object.
|
2002-07-01 11:04:45 +00:00
|
|
|
* @param l the new long value to be set.
|
2002-12-04 23:46:16 +00:00
|
|
|
* @stable ICU 2.0
|
1999-12-28 23:57:50 +00:00
|
|
|
*/
|
|
|
|
void setLong(int32_t l);
|
|
|
|
/**
|
|
|
|
* Sets the Date value of this object.
|
2002-07-01 11:04:45 +00:00
|
|
|
* @param d the new Date value to be set.
|
2002-12-04 23:46:16 +00:00
|
|
|
* @stable ICU 2.0
|
1999-12-28 23:57:50 +00:00
|
|
|
*/
|
|
|
|
void setDate(UDate d);
|
|
|
|
/**
|
|
|
|
* Sets the string value of this object.
|
2002-07-01 11:04:45 +00:00
|
|
|
* @param stringToCopy the new string value to be set.
|
2002-12-04 23:46:16 +00:00
|
|
|
* @stable ICU 2.0
|
1999-12-28 23:57:50 +00:00
|
|
|
*/
|
|
|
|
void setString(const UnicodeString& stringToCopy);
|
|
|
|
/**
|
|
|
|
* Sets the array value and count of this object.
|
2002-07-01 11:04:45 +00:00
|
|
|
* @param array the array value.
|
|
|
|
* @param count the number of array elements to be copied.
|
2002-12-04 23:46:16 +00:00
|
|
|
* @stable ICU 2.0
|
1999-12-28 23:57:50 +00:00
|
|
|
*/
|
|
|
|
void setArray(const Formattable* array, int32_t count);
|
|
|
|
/**
|
|
|
|
* Sets and adopts the string value and count of this object.
|
2002-07-01 11:04:45 +00:00
|
|
|
* @param stringToAdopt the new string value to be adopted.
|
2002-12-04 23:46:16 +00:00
|
|
|
* @stable ICU 2.0
|
1999-12-28 23:57:50 +00:00
|
|
|
*/
|
|
|
|
void adoptString(UnicodeString* stringToAdopt);
|
|
|
|
/**
|
|
|
|
* Sets and adopts the array value and count of this object.
|
2002-12-04 23:46:16 +00:00
|
|
|
* @stable ICU 2.0
|
1999-12-28 23:57:50 +00:00
|
|
|
*/
|
|
|
|
void adoptArray(Formattable* array, int32_t count);
|
|
|
|
|
2002-06-29 00:04:16 +00:00
|
|
|
/**
|
|
|
|
* ICU "poor man's RTTI", returns a UClassID for the actual class.
|
|
|
|
*
|
|
|
|
* @draft ICU 2.2
|
|
|
|
*/
|
2003-08-31 20:53:46 +00:00
|
|
|
virtual UClassID getDynamicClassID() const;
|
2002-06-29 00:04:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* ICU "poor man's RTTI", returns a UClassID for this class.
|
|
|
|
*
|
|
|
|
* @draft ICU 2.2
|
|
|
|
*/
|
2003-08-31 20:53:46 +00:00
|
|
|
static UClassID getStaticClassID();
|
2002-06-29 00:04:16 +00:00
|
|
|
|
1999-12-28 23:57:50 +00:00
|
|
|
private:
|
|
|
|
/**
|
|
|
|
* Cleans up the memory for unwanted values. For example, the adopted
|
|
|
|
* string or array objects.
|
|
|
|
*/
|
|
|
|
void dispose(void);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new Formattable array and copies the values from the specified
|
|
|
|
* original.
|
|
|
|
* @param array the original array
|
|
|
|
* @param count the original array count
|
|
|
|
* @return the new Formattable array.
|
|
|
|
*/
|
|
|
|
static Formattable* createArrayCopy(const Formattable* array, int32_t count);
|
|
|
|
|
|
|
|
// Note: For now, we do not handle unsigned long and unsigned
|
|
|
|
// double types. Smaller unsigned types, such as unsigned
|
|
|
|
// short, can fit within a long.
|
|
|
|
union {
|
|
|
|
UnicodeString* fString;
|
|
|
|
double fDouble;
|
|
|
|
int32_t fLong;
|
|
|
|
UDate fDate;
|
|
|
|
struct
|
|
|
|
{
|
|
|
|
Formattable* fArray;
|
|
|
|
int32_t fCount;
|
|
|
|
} fArrayAndCount;
|
|
|
|
} fValue;
|
|
|
|
|
|
|
|
Type fType;
|
|
|
|
};
|
|
|
|
|
|
|
|
inline Formattable*
|
|
|
|
Formattable::createArrayCopy(const Formattable* array, int32_t count)
|
|
|
|
{
|
|
|
|
Formattable *result = new Formattable[count];
|
|
|
|
for (int32_t i=0; i<count; ++i) result[i] = array[i]; // Don't memcpy!
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2000-06-14 20:52:50 +00:00
|
|
|
inline const UnicodeString& Formattable::getString(void) const {
|
|
|
|
return *fValue.fString;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline UnicodeString& Formattable::getString(void) {
|
|
|
|
return *fValue.fString;
|
|
|
|
}
|
|
|
|
|
2001-10-08 23:26:58 +00:00
|
|
|
U_NAMESPACE_END
|
|
|
|
|
2002-09-20 01:54:48 +00:00
|
|
|
#endif /* #if !UCONFIG_NO_FORMATTING */
|
|
|
|
|
1999-12-28 23:57:50 +00:00
|
|
|
#endif //_FMTABLE
|
|
|
|
//eof
|
|
|
|
|