ICU-3667 prefer UErrorCode& to UErrorCode* in C++ API

X-SVN-Rev: 14785
This commit is contained in:
Alan Liu 2004-03-27 04:49:02 +00:00
parent c6be3074bb
commit 06c4bd41f3
6 changed files with 164 additions and 111 deletions

View file

@ -638,7 +638,7 @@ ChoiceFormat::format(const Formattable* objs,
UnicodeString buffer;
for (int32_t i = 0; i < cnt; i++) {
double objDouble = objs[i].getDouble(&status);
double objDouble = objs[i].getDouble(status);
if (U_SUCCESS(status)) {
buffer.remove();
appendTo += format(objDouble, buffer, pos);

View file

@ -1,6 +1,6 @@
/*
*******************************************************************************
* Copyright (C) 1997-2003, International Business Machines Corporation and *
* Copyright (C) 1997-2004, International Business Machines Corporation and *
* others. All Rights Reserved. *
*******************************************************************************
*
@ -28,6 +28,15 @@ U_NAMESPACE_BEGIN
UOBJECT_DEFINE_RTTI_IMPLEMENTATION(Formattable)
/**
* Set 'ec' to 'err' only if 'ec' is not already set to a failing UErrorCode.
*/
inline void setError(UErrorCode& ec, UErrorCode err) {
if (U_SUCCESS(ec)) {
ec = err;
}
}
// -------------------------------------
// default constructor.
// Creates a formattable object with a long value 0.
@ -225,10 +234,7 @@ void Formattable::dispose()
case kArray:
delete[] fValue.fArrayAndCount.fArray;
break;
case kDate:
case kDouble:
case kLong:
case kInt64:
default:
break;
}
}
@ -248,46 +254,49 @@ Formattable::getType() const
// -------------------------------------
int32_t
Formattable::getLong(UErrorCode* status) const
//Formattable::getLong(UErrorCode* status) const
Formattable::getLong(UErrorCode& status) const
{
if(U_FAILURE(*status))
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;
status = U_INVALID_FORMAT_ERROR;
return INT32_MAX;
} else if (fValue.fInt64 < INT32_MIN) {
*status = U_INVALID_FORMAT_ERROR;
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;
status = U_INVALID_FORMAT_ERROR;
return INT32_MAX;
} else if (fValue.fDouble < INT32_MIN) {
*status = U_INVALID_FORMAT_ERROR;
status = U_INVALID_FORMAT_ERROR;
return INT32_MIN;
} else {
return (int32_t)fValue.fDouble;
return (int32_t)fValue.fDouble; // loses fraction
}
default:
*status = U_INVALID_FORMAT_ERROR;
status = U_INVALID_FORMAT_ERROR;
return 0;
}
}
// -------------------------------------
int64_t
Formattable::getInt64(UErrorCode* status) const
Formattable::getInt64(UErrorCode& status) const
{
if(U_FAILURE(*status))
if (U_FAILURE(status)) {
return 0;
}
switch (fType) {
case Formattable::kLong:
@ -295,26 +304,27 @@ Formattable::getInt64(UErrorCode* status) const
return fValue.fInt64;
case Formattable::kDouble:
if (fValue.fDouble > U_INT64_MAX) {
*status = U_INVALID_FORMAT_ERROR;
status = U_INVALID_FORMAT_ERROR;
return U_INT64_MAX;
} else if (fValue.fDouble < U_INT64_MIN) {
*status = U_INVALID_FORMAT_ERROR;
status = U_INVALID_FORMAT_ERROR;
return U_INT64_MIN;
} else {
return (int64_t)fValue.fDouble;
}
default:
*status = U_INVALID_FORMAT_ERROR;
status = U_INVALID_FORMAT_ERROR;
return 0;
}
}
// -------------------------------------
double
Formattable::getDouble(UErrorCode* status) const
Formattable::getDouble(UErrorCode& status) const
{
if(U_FAILURE(*status))
if (U_FAILURE(status)) {
return 0;
}
switch (fType) {
case Formattable::kLong:
@ -323,7 +333,7 @@ Formattable::getDouble(UErrorCode* status) const
case Formattable::kDouble:
return fValue.fDouble;
default:
*status = U_INVALID_FORMAT_ERROR;
status = U_INVALID_FORMAT_ERROR;
return 0;
}
}
@ -420,10 +430,10 @@ Formattable::adoptArray(Formattable* array, int32_t count)
// -------------------------------------
UnicodeString&
Formattable::getString(UnicodeString& result, UErrorCode* status) const
Formattable::getString(UnicodeString& result, UErrorCode& status) const
{
if (status && U_SUCCESS(*status) && fType != kString) {
*status = U_INVALID_FORMAT_ERROR;
if (fType != kString) {
setError(status, U_INVALID_FORMAT_ERROR);
result.setToBogus();
} else {
result = *fValue.fString;
@ -433,10 +443,10 @@ Formattable::getString(UnicodeString& result, UErrorCode* status) const
// -------------------------------------
const UnicodeString&
Formattable::getString(UErrorCode* status) const
Formattable::getString(UErrorCode& status) const
{
if (status && U_SUCCESS(*status) && fType != kString) {
*status = U_INVALID_FORMAT_ERROR;
if (fType != kString) {
setError(status, U_INVALID_FORMAT_ERROR);
return *getBogus();
}
return *fValue.fString;
@ -444,10 +454,10 @@ Formattable::getString(UErrorCode* status) const
// -------------------------------------
UnicodeString&
Formattable::getString(UErrorCode* status)
Formattable::getString(UErrorCode& status)
{
if (status && U_SUCCESS(*status) && fType != kString) {
*status = U_INVALID_FORMAT_ERROR;
if (fType != kString) {
setError(status, U_INVALID_FORMAT_ERROR);
return *getBogus();
}
return *fValue.fString;
@ -455,11 +465,11 @@ Formattable::getString(UErrorCode* status)
// -------------------------------------
const Formattable*
Formattable::getArray(int32_t& count, UErrorCode* status) const
Formattable::getArray(int32_t& count, UErrorCode& status) const
{
if (status && U_SUCCESS(*status) && fType != kArray) {
if (fType != kArray) {
setError(status, U_INVALID_FORMAT_ERROR);
count = 0;
*status = U_INVALID_FORMAT_ERROR;
return NULL;
}
count = fValue.fArrayAndCount.fCount;

View file

@ -441,7 +441,7 @@ NFSubstitution::doParse(const UnicodeString& text,
// if possible, or a Double
if (parsePosition.getIndex() != 0) {
UErrorCode status = U_ZERO_ERROR;
double tempResult = result.getDouble(&status);
double tempResult = result.getDouble(status);
// composeRuleValue() produces a full parse result from
// the partial parse result passed to this function from
@ -861,7 +861,7 @@ FractionalPartSubstitution::doParse(const UnicodeString& text,
Formattable temp;
getRuleSet()->parse(workText, workPos, 10, temp);
UErrorCode status = U_ZERO_ERROR;
digit = temp.getLong(&status);
digit = temp.getLong(status);
// digit = temp.getType() == Formattable::kLong ?
// temp.getLong() :
// (int32_t)temp.getDouble();

View file

@ -1,6 +1,6 @@
/*
********************************************************************************
* Copyright (C) 1997-2003, International Business Machines
* Copyright (C) 1997-2004, International Business Machines
* Corporation and others. All Rights Reserved.
********************************************************************************
*
@ -183,7 +183,7 @@ public:
/** @stable ICU 2.4 */
kArray, // Formattable[]
/** @draft ICU 2.8 */
kInt64 // int64
kInt64 // int64
};
/**
@ -194,81 +194,92 @@ public:
Type getType(void) const;
/**
* Gets the double value of this object.
* Gets the double value of this object. If this object is not of type
* kDouble then the result is undefined.
* @return the double value of this object.
* @stable ICU 2.0
*/
double getDouble(void) const { return fValue.fDouble; }
/**
* Gets the double value of this object. This converts from long or
* int64 values as required (conversion from int64 can lose precision).
* If the type is not a numeric type, 0 is returned and the status
* is set to U_INVALID_FORMAT_ERROR.
* Gets the double value of this object. If this object is of type
* long or int64 then a casting conversion is peformed, with
* possible loss of precision. If the type is not a numeric type,
* 0 is returned and the status is set to U_INVALID_FORMAT_ERROR.
* @param status the error code
* @return the double value of this object.
* @draft ICU 2.8
* @return the double value of this object.
* @draft ICU 3.0
*/
double getDouble(UErrorCode* status) const;
double getDouble(UErrorCode& status) const;
/**
* Gets the long value of this object.
* Gets the long value of this object. If this object is not of type
* kLong then the result is undefined.
* @return the long value of this object.
* @stable ICU 2.0
*/
int32_t getLong(void) const { return (int32_t)fValue.fInt64; }
/**
* Gets the long value of this object. This converts from double or
* int64 values as required. If the magnitude is too large to fit in a long,
* the maximum or minimum long value, as appropriate, is returned and
* the status is set to U_INVALID_FORMAT_ERROR.
* If the type is not a numeric type, 0 is returned and the status
* is set to U_INVALID_FORMAT_ERROR.
* Gets the long value of this object. If the magnitude is too
* large to fit in a long, then the maximum or minimum long value,
* as appropriate, is returned and the status is set to
* U_INVALID_FORMAT_ERROR. If this object is of type kInt64 and
* it fits within a long, then no precision is lost. If it is of
* type kDouble, then a casting conversion is peformed, with
* truncation of any fractional part. If the type is not a
* numeric type, 0 is returned and the status is set to
* U_INVALID_FORMAT_ERROR.
* @param status the error code
* @return the long value of this object.
* @stable ICU 2.0
* @draft ICU 3.0
*/
int32_t getLong(UErrorCode* status) const;
int32_t getLong(UErrorCode& status) const;
/**
* Gets the int64 value of this object.
* Gets the int64 value of this object. If this object is not of type
* kInt64 then the result is undefined.
* @return the int64 value of this object.
* @draft ICU 2.8
*/
int64_t getInt64(void) const { return fValue.fInt64; }
/**
* Gets the int64 value of this object. This converts from double or
* int64 values as required. If the value value won't fit in an int64,
* the maximum or minimum in64 value, as appropriate, is returned and
* the status is set to U_INVALID_FORMAT_ERROR.
* If the type is not a numeric type, 0 is returned and the status
* is set to U_INVALID_FORMAT_ERROR.
* Gets the int64 value of this object. If this object is of type
* kDouble and the magnitude is too large to fit in an int64, then
* the maximum or minimum int64 value, as appropriate, is returned
* and the status is set to U_INVALID_FORMAT_ERROR. If the
* magnitude fits in an int64, then a casting conversion is
* peformed, with truncation of any fractional part. If the type
* is not a numeric type, 0 is returned and the status is set to
* U_INVALID_FORMAT_ERROR.
* @param status the error code
* @return the int64 value of this object.
* @draft ICU 2.8
* @draft ICU 3.0
*/
int64_t getInt64(UErrorCode* status) const;
int64_t getInt64(UErrorCode& status) const;
/**
* Gets the Date value of this object.
* Gets the Date value of this object. If this object is not of type
* kDate then the result is undefined.
* @return the Date value of this object.
* @stable ICU 2.0
*/
UDate getDate() const { return fValue.fDate; }
/**
* Gets the Date value of this object.
* @param status the error code. If the type is not a date, status
* is set to U_INVALID_FORMAT_ERROR and the return value is undefined.
* Gets the Date value of this object. If the type is not a date,
* status is set to U_INVALID_FORMAT_ERROR and the return value is
* undefined.
* @param status the error code.
* @return the Date value of this object.
* @draft ICU 2.8
* @draft ICU 3.0
*/
UDate getDate(UErrorCode* status) const;
UDate getDate(UErrorCode& status) const;
/**
* Gets the string value of this object.
* Gets the string value of this object. If this object is not of type
* kString then the result is undefined.
* @param result Output param to receive the Date value of this object.
* @return A reference to 'result'.
* @stable ICU 2.0
@ -277,49 +288,56 @@ public:
{ result=*fValue.fString; return result; }
/**
* Gets the string value of this object.
* Gets the string value of this object. If the type is not a
* string, status is set to U_INVALID_FORMAT_ERROR and a bogus
* string is returned.
* @param result Output param to receive the Date value of this object.
* @param status the error code. If the type is not a string, status
* is set to U_INVALID_FORMAT_ERROR and the result is set to bogus.
* @param status the error code.
* @return A reference to 'result'.
* @draft ICU 2.8
* @draft ICU 3.0
*/
UnicodeString& getString(UnicodeString& result, UErrorCode* status) const;
UnicodeString& getString(UnicodeString& result, UErrorCode& status) const;
/**
* Gets a const reference to the string value of this object.
* Gets a const reference to the string value of this object. If
* this object is not of type kString then the result is
* undefined.
* @return a const reference to the string value of this object.
* @stable ICU 2.0
*/
inline const UnicodeString& getString(void) const;
/**
* Gets a const reference to the string value of this object.
* @param status the error code. If the type is not a string, status
* is set to U_INVALID_FORMAT_ERROR and the result is a bogus string.
* Gets a const reference to the string value of this object. If
* the type is not a string, status is set to
* U_INVALID_FORMAT_ERROR and the result is a bogus string.
* @param status the error code.
* @return a const reference to the string value of this object.
* @draft ICU 2.8
* @draft ICU 3.0
*/
const UnicodeString& getString(UErrorCode* status) const;
const UnicodeString& getString(UErrorCode& status) const;
/**
* Gets a reference to the string value of this object.
* Gets a reference to the string value of this object. If this
* object is not of type kString then the result is undefined.
* @return a reference to the string value of this object.
* @stable ICU 2.0
*/
inline UnicodeString& getString(void);
/**
* Gets a reference to the string value of this object.
* @param status the error code. If the type is not a string, status
* is set to U_INVALID_FORMAT_ERROR and the result is a bogus string.
* Gets a reference to the string value of this object. If the
* type is not a string, status is set to U_INVALID_FORMAT_ERROR
* and the result is a bogus string.
* @param status the error code.
* @return a reference to the string value of this object.
* @draft ICU 2.8
* @draft ICU 3.0
*/
UnicodeString& getString(UErrorCode* status);
UnicodeString& getString(UErrorCode& status);
/**
* Gets the array value and count of this object.
* Gets the array value and count of this object. If this object
* is not of type kArray then the result is undefined.
* @param count fill-in with the count of this object.
* @return the array value of this object.
* @stable ICU 2.0
@ -328,17 +346,20 @@ public:
{ count=fValue.fArrayAndCount.fCount; return fValue.fArrayAndCount.fArray; }
/**
* Gets the array value and count of this object.
* Gets the array value and count of this object. If the type is
* not an array, status is set to U_INVALID_FORMAT_ERROR, count is
* set to 0, and the result is NULL.
* @param count fill-in with the count of this object.
* @param status the error code. If the type is not an array, status
* is set to U_INVALID_FORMAT_ERROR, count is set to 0, and the result is NULL.
* @param status the error code.
* @return the array value of this object.
* @draft ICU 2.8
* @draft ICU 3.0
*/
const Formattable* getArray(int32_t& count, UErrorCode* status) const;
const Formattable* getArray(int32_t& count, UErrorCode& status) const;
/**
* Accesses the specified element in the array value of this Formattable object.
* Accesses the specified element in the array value of this
* Formattable object. If this object is not of type kArray then
* the result is undefined.
* @param index the specified index.
* @return the accessed element in the array.
* @stable ICU 2.0
@ -346,42 +367,48 @@ public:
Formattable& operator[](int32_t index) { return fValue.fArrayAndCount.fArray[index]; }
/**
* Sets the double value of this object.
* Sets the double value of this object and changes the type to
* kDouble.
* @param d the new double value to be set.
* @stable ICU 2.0
*/
void setDouble(double d);
/**
* Sets the long value of this object.
* Sets the long value of this object and changes the type to
* kLong.
* @param l the new long value to be set.
* @stable ICU 2.0
*/
void setLong(int32_t l);
/**
* Sets the int64 value of this object.
* Sets the int64 value of this object and changes the type to
* kInt64.
* @param ll the new int64 value to be set.
* @draft ICU 2.8
*/
void setInt64(int64_t ll);
/**
* Sets the Date value of this object.
* Sets the Date value of this object and changes the type to
* kDate.
* @param d the new Date value to be set.
* @stable ICU 2.0
*/
void setDate(UDate d);
/**
* Sets the string value of this object.
* Sets the string value of this object and changes the type to
* kString.
* @param stringToCopy the new string value to be set.
* @stable ICU 2.0
*/
void setString(const UnicodeString& stringToCopy);
/**
* Sets the array value and count of this object.
* Sets the array value and count of this object and changes the
* type to kArray.
* @param array the array value.
* @param count the number of array elements to be copied.
* @stable ICU 2.0
@ -389,14 +416,16 @@ public:
void setArray(const Formattable* array, int32_t count);
/**
* Sets and adopts the string value and count of this object.
* Sets and adopts the string value and count of this object and
* changes the type to kArray.
* @param stringToAdopt the new string value to be adopted.
* @stable ICU 2.0
*/
void adoptString(UnicodeString* stringToAdopt);
/**
* Sets and adopts the array value and count of this object.
* Sets and adopts the array value and count of this object and
* changes the type to kArray.
* @stable ICU 2.0
*/
void adoptArray(Formattable* array, int32_t count);
@ -415,6 +444,14 @@ public:
*/
static UClassID getStaticClassID();
/**
* Deprecated variant of getLong(UErrorCode&).
* @param status the error code
* @return the long value of this object.
* @deprecated ICU 3.0 use getLong(UErrorCode&) instead
*/
inline int32_t getLong(UErrorCode* status) const;
private:
/**
* Cleans up the memory for unwanted values. For example, the adopted
@ -460,10 +497,12 @@ Formattable::createArrayCopy(const Formattable* array, int32_t count)
return result;
}
inline UDate Formattable::getDate(UErrorCode* status) const {
if (status && U_SUCCESS(*status) && fType != kDate) {
*status = U_INVALID_FORMAT_ERROR;
return 0;
inline UDate Formattable::getDate(UErrorCode& status) const {
if (fType != kDate) {
if (U_SUCCESS(status)) {
status = U_INVALID_FORMAT_ERROR;
}
return 0;
}
return fValue.fDate;
}
@ -476,6 +515,10 @@ inline UnicodeString& Formattable::getString(void) {
return *fValue.fString;
}
inline int32_t Formattable::getLong(UErrorCode* status) const {
return getLong(*status);
}
U_NAMESPACE_END
#endif /* #if !UCONFIG_NO_FORMATTING */

View file

@ -288,7 +288,7 @@ unum_parse( const UNumberFormat* fmt,
{
Formattable res;
parseRes(res, fmt, text, textLength, parsePos, status);
return res.getLong(status);
return res.getLong(*status);
}
U_CAPI int64_t U_EXPORT2
@ -300,7 +300,7 @@ unum_parseInt64( const UNumberFormat* fmt,
{
Formattable res;
parseRes(res, fmt, text, textLength, parsePos, status);
return res.getInt64(status);
return res.getInt64(*status);
}
U_CAPI double U_EXPORT2
@ -312,7 +312,7 @@ unum_parseDouble( const UNumberFormat* fmt,
{
Formattable res;
parseRes(res, fmt, text, textLength, parsePos, status);
return res.getDouble(status);
return res.getDouble(*status);
}
U_CAPI const char* U_EXPORT2

View file

@ -324,7 +324,7 @@ NumberFormatTest::TestExponential(void)
if (af.getType() == Formattable::kLong ||
af.getType() == Formattable::kInt64) {
UErrorCode status = U_ZERO_ERROR;
int32_t a = af.getLong(&status);
int32_t a = af.getLong(status);
if (pos.getIndex() == s.length())
{
logln((UnicodeString)" -parse-> " + a);