#include "unicode/utypes.h"
#if !UCONFIG_NO_FORMATTING
#include "unicode/fmtable.h"
#include "cmemory.h"
U_NAMESPACE_BEGIN
const char Formattable::fgClassID=0;
Formattable::Formattable()
: UObject(), fType(kLong)
{
fValue.fLong = 0;
}
Formattable::Formattable(UDate date, ISDATE )
: UObject(), fType(kDate)
{
fValue.fDate = date;
}
Formattable::Formattable(double value)
: UObject(), fType(kDouble)
{
fValue.fDouble = value;
}
Formattable::Formattable(int32_t value)
: UObject(), fType(kLong)
{
fValue.fLong = value;
}
Formattable::Formattable(const char* stringToCopy)
: UObject(), fType(kString)
{
fValue.fString = new UnicodeString(stringToCopy);
}
Formattable::Formattable(const UnicodeString& stringToCopy)
: UObject(), fType(kString)
{
fValue.fString = new UnicodeString(stringToCopy);
}
Formattable::Formattable(UnicodeString* stringToAdopt)
: UObject(), fType(kString)
{
fValue.fString = stringToAdopt;
}
Formattable::Formattable(const Formattable* arrayToCopy, int32_t count)
: UObject(), fType(kArray)
{
fValue.fArrayAndCount.fArray = createArrayCopy(arrayToCopy, count);
fValue.fArrayAndCount.fCount = count;
}
Formattable::Formattable(const Formattable &source)
: UObject(source), fType(kLong)
{
*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:
fValue.fLong = source.fValue.fLong;
break;
case kDate:
fValue.fDate = source.fValue.fDate;
break;
}
}
return *this;
}
UBool
Formattable::operator==(const Formattable& that) const
{
if (this == &that) return TRUE;
if (fType != that.fType) return FALSE;
switch (fType) {
case kDate:
return fValue.fDate == that.fValue.fDate;
case kDouble:
return fValue.fDouble == that.fValue.fDouble;
case kLong:
return fValue.fLong == that.fValue.fLong;
case kString:
return *(fValue.fString) == *(that.fValue.fString);
case kArray:
if (fValue.fArrayAndCount.fCount != that.fValue.fArrayAndCount.fCount)
return FALSE;
for (int32_t i=0; i<fValue.fArrayAndCount.fCount; ++i)
if (fValue.fArrayAndCount.fArray[i] != that.fValue.fArrayAndCount.fArray[i])
return FALSE;
break;
}
return TRUE;
}
Formattable::~Formattable()
{
dispose();
}
void Formattable::dispose()
{
switch (fType) {
case kString:
delete fValue.fString;
break;
case kArray:
delete[] fValue.fArrayAndCount.fArray;
break;
case kDate:
case kDouble:
case kLong:
break;
}
}
Formattable::Type
Formattable::getType() const
{
return fType;
}
void
Formattable::setDouble(double d)
{
dispose();
fType = kDouble;
fValue.fDouble = d;
}
void
Formattable::setLong(int32_t l)
{
dispose();
fType = kLong;
fValue.fLong = l;
}
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;
}
#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