#include "unicode/utypes.h"
#if !UCONFIG_NO_FORMATTING
#include "unicode/fmtable.h"
#include "unicode/ustring.h"
#include "unicode/measure.h"
#include "unicode/curramt.h"
#include "cmemory.h"
U_NAMESPACE_BEGIN
UOBJECT_DEFINE_RTTI_IMPLEMENTATION(Formattable)
inline UBool objectEquals(const UObject* a, const UObject* b) {
return *((const Measure*) a) == *((const Measure*) b);
}
inline UObject* objectClone(const UObject* a) {
return ((const Measure*) a)->clone();
}
inline UBool instanceOfMeasure(const UObject* a) {
return a->getDynamicClassID() ==
CurrencyAmount::getStaticClassID();
}
inline void setError(UErrorCode& ec, UErrorCode err) {
if (U_SUCCESS(ec)) {
ec = err;
}
}
Formattable::Formattable()
: UObject(), fType(kLong)
{
fBogus.setToBogus();
fValue.fInt64 = 0;
}
Formattable::Formattable(UDate date, ISDATE )
: UObject(), fType(kDate)
{
fBogus.setToBogus();
fValue.fDate = date;
}
Formattable::Formattable(double value)
: UObject(), fType(kDouble)
{
fBogus.setToBogus();
fValue.fDouble = value;
}
Formattable::Formattable(int32_t value)
: UObject(), fType(kLong)
{
fBogus.setToBogus();
fValue.fInt64 = value;
}
Formattable::Formattable(int64_t value)
: UObject(), fType(kInt64)
{
fBogus.setToBogus();
fValue.fInt64 = value;
}
Formattable::Formattable(const char* stringToCopy)
: UObject(), fType(kString)
{
fBogus.setToBogus();
fValue.fString = new UnicodeString(stringToCopy);
}
Formattable::Formattable(const UnicodeString& stringToCopy)
: UObject(), fType(kString)
{
fBogus.setToBogus();
fValue.fString = new UnicodeString(stringToCopy);
}
Formattable::Formattable(UnicodeString* stringToAdopt)
: UObject(), fType(kString)
{
fBogus.setToBogus();
fValue.fString = stringToAdopt;
}
Formattable::Formattable(UObject* objectToAdopt)
: UObject(), fType(kObject)
{
fBogus.setToBogus();
fValue.fObject = objectToAdopt;
}
Formattable::Formattable(const Formattable* arrayToCopy, int32_t count)
: UObject(), fType(kArray)
{
fBogus.setToBogus();
fValue.fArrayAndCount.fArray = createArrayCopy(arrayToCopy, count);
fValue.fArrayAndCount.fCount = count;
}
Formattable::Formattable(const Formattable &source)
: UObject(source), fType(kLong)
{
fBogus.setToBogus();
*this = source;
}
Formattable&
Formattable::operator=(const Formattable& source)
{
if (this != &source)
{
dispose();
fType = source.fType;
switch (fType)
{
case kArray:
fValue.fArrayAndCount.fCount = source.fValue.fArrayAndCount.fCount;
fValue.fArrayAndCount.fArray = createArrayCopy(source.fValue.fArrayAndCount.fArray,
source.fValue.fArrayAndCount.fCount);
break;
case kString:
fValue.fString = new UnicodeString(*source.fValue.fString);
break;
case kDouble:
fValue.fDouble = source.fValue.fDouble;
break;
case kLong:
case kInt64:
fValue.fInt64 = source.fValue.fInt64;
break;
case kDate:
fValue.fDate = source.fValue.fDate;
break;
case kObject:
fValue.fObject = objectClone(source.fValue.fObject);
break;
}
}
return *this;
}
UBool
Formattable::operator==(const Formattable& that) const
{
int32_t i;
if (this == &that) return TRUE;
if (fType != that.fType) return FALSE;
UBool equal = TRUE;
switch (fType) {
case kDate:
equal = (fValue.fDate == that.fValue.fDate);
break;
case kDouble:
equal = (fValue.fDouble == that.fValue.fDouble);
break;
case kLong:
case kInt64:
equal = (fValue.fInt64 == that.fValue.fInt64);
break;
case kString:
equal = (*(fValue.fString) == *(that.fValue.fString));
break;
case kArray:
if (fValue.fArrayAndCount.fCount != that.fValue.fArrayAndCount.fCount) {
equal = FALSE;
break;
}
for (i=0; i<fValue.fArrayAndCount.fCount; ++i) {
if (fValue.fArrayAndCount.fArray[i] != that.fValue.fArrayAndCount.fArray[i]) {
equal = FALSE;
break;
}
}
break;
case kObject:
equal = objectEquals(fValue.fObject, that.fValue.fObject);
break;
}
return equal;
}
Formattable::~Formattable()
{
dispose();
}
void Formattable::dispose()
{
switch (fType) {
case kString:
delete fValue.fString;
break;
case kArray:
delete[] fValue.fArrayAndCount.fArray;
break;
case kObject:
delete fValue.fObject;
break;
default:
break;
}
}
Formattable *
Formattable::clone() const {
return new Formattable(*this);
}
Formattable::Type
Formattable::getType() const
{
return fType;
}
UBool
Formattable::isNumeric() const {
switch (fType) {
case kDouble:
case kLong:
case kInt64:
return TRUE;
default:
return FALSE;
}
}
int32_t
Formattable::getLong(UErrorCode& status) const
{
if (U_FAILURE(status)) {
return 0;
}
switch (fType) {
case Formattable::kLong:
return (int32_t)fValue.fInt64;
case Formattable::kInt64:
if (fValue.fInt64 > INT32_MAX) {
status = U_INVALID_FORMAT_ERROR;
return INT32_MAX;
} else if (fValue.fInt64 < INT32_MIN) {
status = U_INVALID_FORMAT_ERROR;
return INT32_MIN;
} else {
return (int32_t)fValue.fInt64;
}
case Formattable::kDouble:
if (fValue.fDouble > INT32_MAX) {
status = U_INVALID_FORMAT_ERROR;
return INT32_MAX;
} else if (fValue.fDouble < INT32_MIN) {
status = U_INVALID_FORMAT_ERROR;
return INT32_MIN;
} else {
return (int32_t)fValue.fDouble; }
case Formattable::kObject:
if (instanceOfMeasure(fValue.fObject)) {
return ((const Measure*) fValue.fObject)->
getNumber().getLong(status);
}
default:
status = U_INVALID_FORMAT_ERROR;
return 0;
}
}
int64_t
Formattable::getInt64(UErrorCode& status) const
{
if (U_FAILURE(status)) {
return 0;
}
switch (fType) {
case Formattable::kLong:
case Formattable::kInt64:
return fValue.fInt64;
case Formattable::kDouble:
if (fValue.fDouble > U_INT64_MAX) {
status = U_INVALID_FORMAT_ERROR;
return U_INT64_MAX;
} else if (fValue.fDouble < U_INT64_MIN) {
status = U_INVALID_FORMAT_ERROR;
return U_INT64_MIN;
} else {
return (int64_t)fValue.fDouble;
}
case Formattable::kObject:
if (instanceOfMeasure(fValue.fObject)) {
return ((const Measure*) fValue.fObject)->
getNumber().getInt64(status);
}
default:
status = U_INVALID_FORMAT_ERROR;
return 0;
}
}
double
Formattable::getDouble(UErrorCode& status) const
{
if (U_FAILURE(status)) {
return 0;
}
switch (fType) {
case Formattable::kLong:
case Formattable::kInt64: return (double)fValue.fInt64;
case Formattable::kDouble:
return fValue.fDouble;
case Formattable::kObject:
if (instanceOfMeasure(fValue.fObject)) {
return ((const Measure*) fValue.fObject)->
getNumber().getDouble(status);
}
default:
status = U_INVALID_FORMAT_ERROR;
return 0;
}
}
const UObject*
Formattable::getObject() const {
return (fType == kObject) ? fValue.fObject : NULL;
}
void
Formattable::setDouble(double d)
{
dispose();
fType = kDouble;
fValue.fDouble = d;
}
void
Formattable::setLong(int32_t l)
{
dispose();
fType = kLong;
fValue.fInt64 = l;
}
void
Formattable::setInt64(int64_t ll)
{
dispose();
fType = kInt64;
fValue.fInt64 = ll;
}
void
Formattable::setDate(UDate d)
{
dispose();
fType = kDate;
fValue.fDate = d;
}
void
Formattable::setString(const UnicodeString& stringToCopy)
{
dispose();
fType = kString;
fValue.fString = new UnicodeString(stringToCopy);
}
void
Formattable::setArray(const Formattable* array, int32_t count)
{
dispose();
fType = kArray;
fValue.fArrayAndCount.fArray = createArrayCopy(array, count);
fValue.fArrayAndCount.fCount = count;
}
void
Formattable::adoptString(UnicodeString* stringToAdopt)
{
dispose();
fType = kString;
fValue.fString = stringToAdopt;
}
void
Formattable::adoptArray(Formattable* array, int32_t count)
{
dispose();
fType = kArray;
fValue.fArrayAndCount.fArray = array;
fValue.fArrayAndCount.fCount = count;
}
void
Formattable::adoptObject(UObject* objectToAdopt) {
dispose();
fType = kObject;
fValue.fObject = objectToAdopt;
}
UnicodeString&
Formattable::getString(UnicodeString& result, UErrorCode& status) const
{
if (fType != kString) {
setError(status, U_INVALID_FORMAT_ERROR);
result.setToBogus();
} else {
result = *fValue.fString;
}
return result;
}
const UnicodeString&
Formattable::getString(UErrorCode& status) const
{
if (fType != kString) {
setError(status, U_INVALID_FORMAT_ERROR);
return *getBogus();
}
return *fValue.fString;
}
UnicodeString&
Formattable::getString(UErrorCode& status)
{
if (fType != kString) {
setError(status, U_INVALID_FORMAT_ERROR);
return *getBogus();
}
return *fValue.fString;
}
const Formattable*
Formattable::getArray(int32_t& count, UErrorCode& status) const
{
if (fType != kArray) {
setError(status, U_INVALID_FORMAT_ERROR);
count = 0;
return NULL;
}
count = fValue.fArrayAndCount.fCount;
return fValue.fArrayAndCount.fArray;
}
UnicodeString*
Formattable::getBogus() const
{
return (UnicodeString*)&fBogus;
}
#if 0
#ifdef _DEBUG
#if U_IOSTREAM_SOURCE >= 199711
#include <iostream>
using namespace std;
#elif U_IOSTREAM_SOURCE >= 198506
#include <iostream.h>
#endif
#include "unicode/datefmt.h"
#include "unistrm.h"
class FormattableStreamer {
public:
static void streamOut(ostream& stream, const Formattable& obj);
private:
FormattableStreamer() {} };
void
FormattableStreamer::streamOut(ostream& stream, const Formattable& obj)
{
static DateFormat *defDateFormat = 0;
UnicodeString buffer;
switch(obj.getType()) {
case Formattable::kDate :
if (defDateFormat == 0) {
defDateFormat = DateFormat::createInstance();
}
defDateFormat->format(obj.getDate(), buffer);
stream << buffer;
break;
case Formattable::kDouble :
stream << obj.getDouble() << 'D';
break;
case Formattable::kLong :
stream << obj.getLong() << 'L';
break;
case Formattable::kString:
stream << '"' << obj.getString(buffer) << '"';
break;
case Formattable::kArray:
int32_t i, count;
const Formattable* array;
array = obj.getArray(count);
stream << '[';
for (i=0; i<count; ++i) {
FormattableStreamer::streamOut(stream, array[i]);
stream << ( (i==(count-1)) ? "" : ", " );
}
stream << ']';
break;
default:
stream << "INVALID_Formattable";
}
stream.flush();
}
#endif
#endif
U_NAMESPACE_END
#endif