ICU-13351 Adding additional CurrencyUnit constructors and stabilizing API constract of MeasureUnit default constructor.

X-SVN-Rev: 40379
This commit is contained in:
Shane Carr 2017-09-12 05:20:50 +00:00
parent 57913822e0
commit d0c762e71c
4 changed files with 46 additions and 6 deletions

View File

@ -16,6 +16,7 @@
#include "unicode/currunit.h"
#include "unicode/ustring.h"
#include "cstring.h"
U_NAMESPACE_BEGIN
@ -33,11 +34,30 @@ CurrencyUnit::CurrencyUnit(ConstChar16Ptr _isoCode, UErrorCode& ec) {
}
}
CurrencyUnit::CurrencyUnit(const CurrencyUnit& other) :
MeasureUnit(other) {
CurrencyUnit::CurrencyUnit(const CurrencyUnit& other) : MeasureUnit(other) {
u_strcpy(isoCode, other.isoCode);
}
CurrencyUnit::CurrencyUnit(const MeasureUnit& other, UErrorCode& ec) : MeasureUnit(other) {
// Make sure this is a currency.
// OK to hard-code the string because we are comparing against another hard-coded string.
if (uprv_strcmp("currency", getType()) != 0) {
ec = U_ILLEGAL_ARGUMENT_ERROR;
isoCode[0] = 0;
} else {
// Get the ISO Code from the subtype field.
u_charsToUChars(getSubtype(), isoCode, 4);
isoCode[3] = 0; // make 100% sure it is NUL-terminated
}
}
CurrencyUnit::CurrencyUnit() : MeasureUnit() {
u_strcpy(isoCode, u"XXX");
char simpleIsoCode[4];
u_UCharsToChars(isoCode, simpleIsoCode, 4);
initCurrency(simpleIsoCode);
}
CurrencyUnit& CurrencyUnit::operator=(const CurrencyUnit& other) {
if (this == &other) {
return *this;

View File

@ -1079,7 +1079,12 @@ static int32_t binarySearch(
}
return -1;
}
MeasureUnit::MeasureUnit() {
fCurrency[0] = 0;
initNoUnit("base");
}
MeasureUnit::MeasureUnit(const MeasureUnit &other)
: fTypeId(other.fTypeId), fSubTypeId(other.fSubTypeId) {
uprv_strcpy(fCurrency, other.fCurrency);

View File

@ -36,6 +36,12 @@ U_NAMESPACE_BEGIN
*/
class U_I18N_API CurrencyUnit: public MeasureUnit {
public:
/**
* Default constructor. Initializes currency code to "XXX" (no currency).
* @draft ICU 60
*/
CurrencyUnit();
/**
* Construct an object with the given ISO currency code.
* @param isoCode the 3-letter ISO 4217 currency code; must not be
@ -52,6 +58,16 @@ class U_I18N_API CurrencyUnit: public MeasureUnit {
*/
CurrencyUnit(const CurrencyUnit& other);
/**
* Copy constructor from MeasureUnit. This constructor allows you to
* restore a CurrencyUnit that was sliced to MeasureUnit.
*
* @param measureUnit The MeasureUnit to copy from.
* @param ec Set to a failing value if the MeasureUnit is not a currency.
* @draft ICU 60
*/
CurrencyUnit(const MeasureUnit& measureUnit, UErrorCode &ec);
/**
* Assignment operator
* @stable ICU 3.0

View File

@ -40,11 +40,10 @@ class U_I18N_API MeasureUnit: public UObject {
/**
* Default constructor.
* Populates the instance with the base dimensionless unit.
* @stable ICU 3.0
*/
MeasureUnit() : fTypeId(0), fSubTypeId(0) {
fCurrency[0] = 0;
}
MeasureUnit();
/**
* Copy constructor.