From 5d5ec05b1df089eff03f04a7122ac7cf355c4f3e Mon Sep 17 00:00:00 2001 From: Adrien Destugues Date: Mon, 27 Oct 2014 09:05:48 +0100 Subject: [PATCH] B*Format: make immutable and remove locking The language and formatting conventions can now only be set when creating the objects. This removed the needs for locking them when formatting to avoid some other thread changing the format while it's being used. Adjust tests and DeskBar TimeView to the API changes. --- headers/os/locale/DateFormat.h | 7 +- headers/os/locale/DateTimeFormat.h | 7 +- headers/os/locale/DurationFormat.h | 7 +- headers/os/locale/Format.h | 29 +++---- headers/os/locale/MessageFormat.h | 8 +- headers/os/locale/NumberFormat.h | 2 - headers/os/locale/TimeFormat.h | 7 +- headers/os/locale/TimeUnitFormat.h | 5 +- src/apps/deskbar/TimeView.cpp | 12 ++- src/apps/deskbar/TimeView.h | 2 - src/kits/locale/DateFormat.cpp | 48 ++--------- src/kits/locale/DateTimeFormat.cpp | 22 ++--- src/kits/locale/DurationFormat.cpp | 46 ++++------- src/kits/locale/Format.cpp | 85 ++++++++------------ src/kits/locale/MessageFormat.cpp | 53 ++---------- src/kits/locale/NumberFormat.cpp | 20 ----- src/kits/locale/TimeFormat.cpp | 34 ++------ src/kits/locale/TimeUnitFormat.cpp | 73 +++++++---------- src/tests/kits/locale/DateFormatTest.cpp | 69 +++++++++------- src/tests/kits/locale/DurationFormatTest.cpp | 58 ++++++------- src/tests/kits/locale/MessageFormatTest.cpp | 5 +- 21 files changed, 217 insertions(+), 382 deletions(-) diff --git a/headers/os/locale/DateFormat.h b/headers/os/locale/DateFormat.h index 879a7a25fa..1eee1e81b2 100644 --- a/headers/os/locale/DateFormat.h +++ b/headers/os/locale/DateFormat.h @@ -36,10 +36,9 @@ enum BWeekday { class BDateFormat: public BFormat { public: - BDateFormat( - const BLanguage* const language = NULL, - const BFormattingConventions* const - format = NULL); + BDateFormat(const BLocale* locale = NULL); + BDateFormat(const BLanguage& language, + const BFormattingConventions& format); BDateFormat(const BDateFormat &other); virtual ~BDateFormat(); diff --git a/headers/os/locale/DateTimeFormat.h b/headers/os/locale/DateTimeFormat.h index 1b13d676c4..b7916cfa9c 100644 --- a/headers/os/locale/DateTimeFormat.h +++ b/headers/os/locale/DateTimeFormat.h @@ -31,10 +31,9 @@ enum BDateElement { class BDateTimeFormat : public BFormat { public: - BDateTimeFormat( - const BLanguage* const language = NULL, - const BFormattingConventions* const format - = NULL); + BDateTimeFormat(const BLocale* locale = NULL); + BDateTimeFormat(const BLanguage& language, + const BFormattingConventions& conventions); BDateTimeFormat(const BDateTimeFormat &other); virtual ~BDateTimeFormat(); diff --git a/headers/os/locale/DurationFormat.h b/headers/os/locale/DurationFormat.h index edaa2041f3..d1f226ed68 100644 --- a/headers/os/locale/DurationFormat.h +++ b/headers/os/locale/DurationFormat.h @@ -23,16 +23,15 @@ class BDurationFormat : public BFormat { typedef BFormat Inherited; public: + BDurationFormat(const BLanguage& language, + const BFormattingConventions& conventions, + const BString& separator = ", "); BDurationFormat( const BString& separator = ", "); BDurationFormat(const BDurationFormat& other); virtual ~BDurationFormat(); - BDurationFormat& operator=(const BDurationFormat& other); - void SetSeparator(const BString& separator); - - virtual status_t SetLanguage(const BLanguage& language); status_t SetTimeZone(const BTimeZone* timeZone); status_t Format(BString& buffer, diff --git a/headers/os/locale/Format.h b/headers/os/locale/Format.h index cc99a464e4..ff24c5d228 100644 --- a/headers/os/locale/Format.h +++ b/headers/os/locale/Format.h @@ -43,25 +43,26 @@ class BLocale; class BFormat { public: - status_t SetLocale(const BLocale& locale); - virtual status_t SetLanguage(const BLanguage& newLanguage); - virtual status_t SetFormattingConventions( - const BFormattingConventions& - conventions); - - status_t InitCheck() const; + status_t InitCheck() const; protected: - BFormat(); - BFormat(const BFormat& other); - virtual ~BFormat(); + BFormat(const BLocale* locale = NULL); + BFormat(const BLanguage& language, + const BFormattingConventions& conventions); - BFormat& operator=(const BFormat& other); + BFormat(const BFormat& other); + virtual ~BFormat(); + +private: + BFormat& operator=(const BFormat& other); + + status_t _Initialize(const BLocale& locale); + status_t _Initialize(const BLanguage& language, + const BFormattingConventions& conventions); protected: - mutable BLocker fLock; BFormattingConventions fConventions; - BLanguage fLanguage; - status_t fInitStatus; + BLanguage fLanguage; + status_t fInitStatus; }; diff --git a/headers/os/locale/MessageFormat.h b/headers/os/locale/MessageFormat.h index 21b447245c..15df05cfe0 100644 --- a/headers/os/locale/MessageFormat.h +++ b/headers/os/locale/MessageFormat.h @@ -17,23 +17,19 @@ namespace icu { class BMessageFormat: public BFormat { public: + BMessageFormat(const BLanguage& language, + const BString pattern); BMessageFormat(const BString pattern); ~BMessageFormat(); status_t InitCheck(); - status_t SetLanguage(const BLanguage& newLanguage); - status_t SetFormattingConventions( - const BFormattingConventions& - conventions); - status_t Format(BString& buffer, const int32 arg) const; private: status_t _Initialize(const icu::UnicodeString&); private: - status_t fInitStatus; icu::MessageFormat* fFormatter; }; diff --git a/headers/os/locale/NumberFormat.h b/headers/os/locale/NumberFormat.h index feee9116af..98906a4805 100644 --- a/headers/os/locale/NumberFormat.h +++ b/headers/os/locale/NumberFormat.h @@ -23,8 +23,6 @@ public: BNumberFormat(const BNumberFormat &other); ~BNumberFormat(); - BNumberFormat& operator=(const BNumberFormat &other); - // formatting ssize_t Format(char* string, size_t maxSize, diff --git a/headers/os/locale/TimeFormat.h b/headers/os/locale/TimeFormat.h index 89d460de94..9a1dc0406d 100644 --- a/headers/os/locale/TimeFormat.h +++ b/headers/os/locale/TimeFormat.h @@ -18,10 +18,9 @@ namespace BPrivate { class BTimeFormat : public BFormat { public: - BTimeFormat( - const BLanguage* const language = NULL, - const BFormattingConventions* const - format = NULL); + BTimeFormat(); + BTimeFormat(const BLanguage& language, + const BFormattingConventions& conventions); BTimeFormat(const BTimeFormat &other); virtual ~BTimeFormat(); diff --git a/headers/os/locale/TimeUnitFormat.h b/headers/os/locale/TimeUnitFormat.h index 0aefbcd168..d729efe90a 100644 --- a/headers/os/locale/TimeUnitFormat.h +++ b/headers/os/locale/TimeUnitFormat.h @@ -41,12 +41,11 @@ class BTimeUnitFormat : public BFormat { public: BTimeUnitFormat(); + BTimeUnitFormat(const BLanguage& language, + const BFormattingConventions& conventions); BTimeUnitFormat(const BTimeUnitFormat& other); virtual ~BTimeUnitFormat(); - BTimeUnitFormat& operator=(const BTimeUnitFormat& other); - - virtual status_t SetLanguage(const BLanguage& locale); status_t Format(BString& buffer, const int32 value, const time_unit_element unit, diff --git a/src/apps/deskbar/TimeView.cpp b/src/apps/deskbar/TimeView.cpp index ecc7219281..742b391330 100644 --- a/src/apps/deskbar/TimeView.cpp +++ b/src/apps/deskbar/TimeView.cpp @@ -391,11 +391,10 @@ TTimeView::GetCurrentTime() if (fShowTimeZone) fields |= B_DATE_ELEMENT_TIMEZONE; - fTimeFormat.SetDateTimeFormat(B_SHORT_DATE_FORMAT, B_SHORT_TIME_FORMAT, - fields); + BDateTimeFormat format(&fLocale); + format.SetDateTimeFormat(B_SHORT_DATE_FORMAT, B_SHORT_TIME_FORMAT, fields); - fTimeFormat.Format(fCurrentTimeStr, - sizeof(fCurrentTimeStr), fCurrentTime, + format.Format(fCurrentTimeStr, sizeof(fCurrentTimeStr), fCurrentTime, B_SHORT_DATE_FORMAT, B_SHORT_TIME_FORMAT); } @@ -405,7 +404,8 @@ TTimeView::GetCurrentDate() { char tmp[sizeof(fCurrentDateStr)]; - fDateFormat.Format(tmp, sizeof(fCurrentDateStr), fCurrentTime, + BDateFormat format(&fLocale); + format.Format(tmp, sizeof(fCurrentDateStr), fCurrentTime, B_FULL_DATE_FORMAT); // remove leading 0 from date when month is less than 10 (MM/DD/YY) @@ -472,8 +472,6 @@ void TTimeView::Update() { fLocale = *BLocale::Default(); - fDateFormat.SetLocale(fLocale); - fTimeFormat.SetLocale(fLocale); GetCurrentTime(); GetCurrentDate(); diff --git a/src/apps/deskbar/TimeView.h b/src/apps/deskbar/TimeView.h index 8569514155..bb66a42204 100644 --- a/src/apps/deskbar/TimeView.h +++ b/src/apps/deskbar/TimeView.h @@ -150,8 +150,6 @@ private: // For date and time localization purposes BLocale fLocale; - BDateFormat fDateFormat; - BDateTimeFormat fTimeFormat; }; diff --git a/src/kits/locale/DateFormat.cpp b/src/kits/locale/DateFormat.cpp index 30f7d3013a..ae067ab391 100644 --- a/src/kits/locale/DateFormat.cpp +++ b/src/kits/locale/DateFormat.cpp @@ -26,14 +26,16 @@ #include -BDateFormat::BDateFormat(const BLanguage* const language, - const BFormattingConventions* const conventions) +BDateFormat::BDateFormat(const BLocale* locale) + : BFormat(locale) { - if (conventions != NULL) - fConventions = *conventions; +} - if (language != NULL) - fLanguage = *language; + +BDateFormat::BDateFormat(const BLanguage& language, + const BFormattingConventions& conventions) + : BFormat(language, conventions) +{ } @@ -68,10 +70,6 @@ ssize_t BDateFormat::Format(char* string, const size_t maxSize, const time_t time, const BDateFormatStyle style) const { - BAutolock lock(fLock); - if (!lock.IsLocked()) - return B_ERROR; - ObjectDeleter dateFormatter(_CreateDateFormatter(style)); if (dateFormatter.Get() == NULL) return B_NO_MEMORY; @@ -93,10 +91,6 @@ status_t BDateFormat::Format(BString& string, const time_t time, const BDateFormatStyle style, const BTimeZone* timeZone) const { - BAutolock lock(fLock); - if (!lock.IsLocked()) - return B_ERROR; - ObjectDeleter dateFormatter(_CreateDateFormatter(style)); if (dateFormatter.Get() == NULL) return B_NO_MEMORY; @@ -127,10 +121,6 @@ BDateFormat::Format(BString& string, const BDate& time, if (!time.IsValid()) return B_BAD_DATA; - BAutolock lock(fLock); - if (!lock.IsLocked()) - return B_ERROR; - ObjectDeleter dateFormatter(_CreateDateFormatter(style)); if (dateFormatter.Get() == NULL) return B_NO_MEMORY; @@ -169,10 +159,6 @@ status_t BDateFormat::Format(BString& string, int*& fieldPositions, int& fieldCount, const time_t time, const BDateFormatStyle style) const { - BAutolock lock(fLock); - if (!lock.IsLocked()) - return B_ERROR; - ObjectDeleter dateFormatter(_CreateDateFormatter(style)); if (dateFormatter.Get() == NULL) return B_NO_MEMORY; @@ -214,10 +200,6 @@ status_t BDateFormat::GetFields(BDateElement*& fields, int& fieldCount, BDateFormatStyle style) const { - BAutolock lock(fLock); - if (!lock.IsLocked()) - return B_ERROR; - ObjectDeleter dateFormatter(_CreateDateFormatter(style)); if (dateFormatter.Get() == NULL) return B_NO_MEMORY; @@ -270,10 +252,6 @@ BDateFormat::GetStartOfWeek(BWeekday* startOfWeek) const if (startOfWeek == NULL) return B_BAD_VALUE; - BAutolock lock(fLock); - if (!lock.IsLocked()) - return B_ERROR; - UErrorCode err = U_ZERO_ERROR; ObjectDeleter calendar = Calendar::createInstance( *BFormattingConventions::Private(&fConventions).ICULocale(), err); @@ -318,10 +296,6 @@ BDateFormat::GetStartOfWeek(BWeekday* startOfWeek) const status_t BDateFormat::GetMonthName(int month, BString& outName) { - BAutolock lock(fLock); - if (!lock.IsLocked()) - return B_ERROR; - DateFormat* format = _CreateDateFormatter(B_LONG_DATE_FORMAT); SimpleDateFormat* simpleFormat = dynamic_cast(format); @@ -356,15 +330,10 @@ BDateFormat::Parse(BString source, BDateFormatStyle style, BDate& output) // may want to parse to a "local" date instead. But BDate should be made // timezone aware so things like BDate::Difference can work for dates in // different timezones. - BAutolock lock(fLock); - if (!lock.IsLocked()) - return B_ERROR; - ObjectDeleter dateFormatter(_CreateDateFormatter(style)); if (dateFormatter.Get() == NULL) return B_NO_MEMORY; - ParsePosition p(0); UDate date = dateFormatter->parse(UnicodeString::fromUTF8(source.String()), p); @@ -401,4 +370,3 @@ BDateFormat::_CreateDateFormatter(const BDateFormatStyle style) const return dateFormatter; } - diff --git a/src/kits/locale/DateTimeFormat.cpp b/src/kits/locale/DateTimeFormat.cpp index 0d037c6410..4e856ceaa2 100644 --- a/src/kits/locale/DateTimeFormat.cpp +++ b/src/kits/locale/DateTimeFormat.cpp @@ -21,14 +21,16 @@ #include -BDateTimeFormat::BDateTimeFormat(const BLanguage* const language, - const BFormattingConventions* const conventions) +BDateTimeFormat::BDateTimeFormat(const BLocale* locale) + : BFormat(locale) { - if (conventions != NULL) - fConventions = *conventions; +} - if (language != NULL) - fLanguage = *language; + +BDateTimeFormat::BDateTimeFormat(const BLanguage& language, + const BFormattingConventions& conventions) + : BFormat(language, conventions) +{ } @@ -95,10 +97,6 @@ ssize_t BDateTimeFormat::Format(char* target, size_t maxSize, time_t time, BDateFormatStyle dateStyle, BTimeFormatStyle timeStyle) const { - BAutolock lock(fLock); - if (!lock.IsLocked()) - return B_ERROR; - BString format; fConventions.GetDateTimeFormat(dateStyle, timeStyle, format); ObjectDeleter dateFormatter(_CreateDateTimeFormatter(format)); @@ -123,10 +121,6 @@ BDateTimeFormat::Format(BString& target, const time_t time, BDateFormatStyle dateStyle, BTimeFormatStyle timeStyle, const BTimeZone* timeZone) const { - BAutolock lock(fLock); - if (!lock.IsLocked()) - return B_ERROR; - BString format; fConventions.GetDateTimeFormat(dateStyle, timeStyle, format); ObjectDeleter dateFormatter(_CreateDateTimeFormatter(format)); diff --git a/src/kits/locale/DurationFormat.cpp b/src/kits/locale/DurationFormat.cpp index c8ac6d623d..1d11ced2f0 100644 --- a/src/kits/locale/DurationFormat.cpp +++ b/src/kits/locale/DurationFormat.cpp @@ -33,6 +33,22 @@ static const UCalendarDateFields skUnitMap[] = { }; +BDurationFormat::BDurationFormat(const BLanguage& language, + const BFormattingConventions& conventions, const BString& separator) + : + Inherited(language, conventions), + fSeparator(separator), + fTimeUnitFormat(language, conventions) +{ + UErrorCode icuStatus = U_ZERO_ERROR; + fCalendar = new GregorianCalendar(icuStatus); + if (fCalendar == NULL) { + fInitStatus = B_NO_MEMORY; + return; + } +} + + BDurationFormat::BDurationFormat(const BString& separator) : Inherited(), @@ -67,25 +83,6 @@ BDurationFormat::~BDurationFormat() } -BDurationFormat& -BDurationFormat::operator=(const BDurationFormat& other) -{ - if (this == &other) - return *this; - - fSeparator = other.fSeparator; - fTimeUnitFormat = other.fTimeUnitFormat; - delete fCalendar; - fCalendar = other.fCalendar != NULL - ? new GregorianCalendar(*other.fCalendar) : NULL; - - if (fCalendar == NULL && other.fCalendar != NULL) - fInitStatus = B_NO_MEMORY; - - return *this; -} - - void BDurationFormat::SetSeparator(const BString& separator) { @@ -93,17 +90,6 @@ BDurationFormat::SetSeparator(const BString& separator) } -status_t -BDurationFormat::SetLanguage(const BLanguage& language) -{ - status_t result = Inherited::SetLanguage(language); - if (result != B_OK) - return result; - - return fTimeUnitFormat.SetLanguage(language); -} - - status_t BDurationFormat::SetTimeZone(const BTimeZone* timeZone) { diff --git a/src/kits/locale/Format.cpp b/src/kits/locale/Format.cpp index 610be0dae4..198874fe84 100644 --- a/src/kits/locale/Format.cpp +++ b/src/kits/locale/Format.cpp @@ -7,18 +7,33 @@ #include -BFormat::BFormat() +BFormat::BFormat(const BLocale* locale) { - const BLocale* locale = BLocaleRoster::Default()->GetDefaultLocale(); - SetLocale(*locale); + if (locale == NULL) + locale = BLocaleRoster::Default()->GetDefaultLocale(); + + if (locale == NULL) { + fInitStatus = B_BAD_DATA; + return; + } + + _Initialize(*locale); +} + + +BFormat::BFormat(const BLanguage& language, + const BFormattingConventions& conventions) +{ + _Initialize(language, conventions); } BFormat::BFormat(const BFormat &other) + : + fConventions(other.fConventions), + fLanguage(other.fLanguage), + fInitStatus(other.fInitStatus) { - fInitStatus = other.fInitStatus; - fConventions = other.fConventions; - fLanguage = other.fLanguage; } @@ -27,20 +42,6 @@ BFormat::~BFormat() } -BFormat & -BFormat::operator=(const BFormat& other) -{ - if (this == &other) - return *this; - - fInitStatus = other.fInitStatus; - fLanguage = other.fLanguage; - fInitStatus = other.fInitStatus; - - return *this; -} - - status_t BFormat::InitCheck() const { @@ -49,47 +50,29 @@ BFormat::InitCheck() const status_t -BFormat::SetLocale(const BLocale& locale) +BFormat::_Initialize(const BLocale& locale) { BFormattingConventions conventions; BLanguage language; fInitStatus = locale.GetFormattingConventions(&conventions); - if (fInitStatus != B_OK) - return fInitStatus; - fInitStatus = SetFormattingConventions(conventions); if (fInitStatus != B_OK) return fInitStatus; fInitStatus = locale.GetLanguage(&language); if (fInitStatus != B_OK) return fInitStatus; - fInitStatus = SetLanguage(language); + + return _Initialize(language, conventions); +} + + +status_t +BFormat::_Initialize(const BLanguage& language, + const BFormattingConventions& conventions) +{ + fConventions = conventions; + fLanguage = language; + fInitStatus = B_OK; return fInitStatus; } - - -status_t -BFormat::SetFormattingConventions(const BFormattingConventions& conventions) -{ - BAutolock lock(fLock); - if (!lock.IsLocked()) - return B_WOULD_BLOCK; - - fConventions = conventions; - return B_OK; -} - - -status_t -BFormat::SetLanguage(const BLanguage& newLanguage) -{ - BAutolock lock(fLock); - if (!lock.IsLocked()) - return B_WOULD_BLOCK; - - fLanguage = newLanguage; - return B_OK; -} - - diff --git a/src/kits/locale/MessageFormat.cpp b/src/kits/locale/MessageFormat.cpp index 50edeee84e..124390d359 100644 --- a/src/kits/locale/MessageFormat.cpp +++ b/src/kits/locale/MessageFormat.cpp @@ -13,6 +13,13 @@ #include +BMessageFormat::BMessageFormat(const BLanguage& language, const BString pattern) + : BFormat(language, BFormattingConventions()) +{ + _Initialize(UnicodeString::fromUTF8(pattern.String())); +} + + BMessageFormat::BMessageFormat(const BString pattern) : BFormat() { @@ -33,57 +40,12 @@ BMessageFormat::InitCheck() } -status_t -BMessageFormat::SetLanguage(const BLanguage& newLanguage) -{ - if (!fFormatter) - return B_NO_INIT; - - BAutolock lock(fLock); - if (!lock.IsLocked()) - return B_ERROR; - - fInitStatus = BFormat::SetLanguage(newLanguage); - - if (fInitStatus == B_OK) { - UnicodeString storage; - _Initialize(fFormatter->toPattern(storage)); - } - return fInitStatus; -} - - -status_t -BMessageFormat::SetFormattingConventions( - const BFormattingConventions& conventions) -{ - if (!fFormatter) - return B_NO_INIT; - - BAutolock lock(fLock); - if (!lock.IsLocked()) - return B_ERROR; - - fInitStatus = BFormat::SetFormattingConventions(conventions); - - if (fInitStatus == B_OK) { - UnicodeString storage; - _Initialize(fFormatter->toPattern(storage)); - } - return fInitStatus; -} - - status_t BMessageFormat::Format(BString& output, const int32 arg) const { if (fInitStatus != B_OK) return fInitStatus; - BAutolock lock(fLock); - if (!lock.IsLocked()) - return B_ERROR; - UnicodeString buffer; UErrorCode error = U_ZERO_ERROR; @@ -106,6 +68,7 @@ BMessageFormat::Format(BString& output, const int32 arg) const status_t BMessageFormat::_Initialize(const UnicodeString& pattern) { + fInitStatus = B_OK; UErrorCode error = U_ZERO_ERROR; Locale* icuLocale = BLanguage::Private(&fLanguage).ICULocale(); diff --git a/src/kits/locale/NumberFormat.cpp b/src/kits/locale/NumberFormat.cpp index 5eae902d36..f6368afd10 100644 --- a/src/kits/locale/NumberFormat.cpp +++ b/src/kits/locale/NumberFormat.cpp @@ -34,14 +34,6 @@ BNumberFormat::~BNumberFormat() } -BNumberFormat & -BNumberFormat::operator=(const BNumberFormat &other) -{ - BFormat::operator=(other); - return *this; -} - - // #pragma mark - Formatting @@ -60,10 +52,6 @@ BNumberFormat::Format(char* string, size_t maxSize, const double value) const status_t BNumberFormat::Format(BString& string, const double value) const { - BAutolock lock(fLock); - if (!lock.IsLocked()) - return B_ERROR; - UErrorCode err = U_ZERO_ERROR; ObjectDeleter numberFormatter(NumberFormat::createInstance( *BFormattingConventions::Private(&fConventions).ICULocale(), @@ -100,10 +88,6 @@ BNumberFormat::Format(char* string, size_t maxSize, const int32 value) const status_t BNumberFormat::Format(BString& string, const int32 value) const { - BAutolock lock(fLock); - if (!lock.IsLocked()) - return B_ERROR; - UErrorCode err = U_ZERO_ERROR; ObjectDeleter numberFormatter(NumberFormat::createInstance( *BFormattingConventions::Private(&fConventions).ICULocale(), @@ -144,10 +128,6 @@ BNumberFormat::FormatMonetary(BString& string, const double value) const if (string == NULL) return B_BAD_VALUE; - BAutolock lock(fLock); - if (!lock.IsLocked()) - return B_ERROR; - UErrorCode err = U_ZERO_ERROR; ObjectDeleter numberFormatter( NumberFormat::createCurrencyInstance( diff --git a/src/kits/locale/TimeFormat.cpp b/src/kits/locale/TimeFormat.cpp index 63d3ae257e..4e258cb5b2 100644 --- a/src/kits/locale/TimeFormat.cpp +++ b/src/kits/locale/TimeFormat.cpp @@ -23,14 +23,16 @@ #include -BTimeFormat::BTimeFormat(const BLanguage* const language, - const BFormattingConventions* const conventions) +BTimeFormat::BTimeFormat() + : BFormat() { - if (conventions != NULL) - fConventions = *conventions; +} - if (language != NULL) - fLanguage = *language; + +BTimeFormat::BTimeFormat(const BLanguage& language, + const BFormattingConventions& conventions) + : BFormat(language, conventions) +{ } @@ -60,10 +62,6 @@ ssize_t BTimeFormat::Format(char* string, size_t maxSize, time_t time, BTimeFormatStyle style) const { - BAutolock lock(fLock); - if (!lock.IsLocked()) - return B_ERROR; - ObjectDeleter timeFormatter(_CreateTimeFormatter(style)); if (timeFormatter.Get() == NULL) return B_NO_MEMORY; @@ -85,10 +83,6 @@ status_t BTimeFormat::Format(BString& string, const time_t time, const BTimeFormatStyle style, const BTimeZone* timeZone) const { - BAutolock lock(fLock); - if (!lock.IsLocked()) - return B_ERROR; - ObjectDeleter timeFormatter(_CreateTimeFormatter(style)); if (timeFormatter.Get() == NULL) return B_NO_MEMORY; @@ -116,10 +110,6 @@ status_t BTimeFormat::Format(BString& string, int*& fieldPositions, int& fieldCount, time_t time, BTimeFormatStyle style) const { - BAutolock lock(fLock); - if (!lock.IsLocked()) - return B_ERROR; - ObjectDeleter timeFormatter(_CreateTimeFormatter(style)); if (timeFormatter.Get() == NULL) return B_NO_MEMORY; @@ -160,10 +150,6 @@ status_t BTimeFormat::GetTimeFields(BDateElement*& fields, int& fieldCount, BTimeFormatStyle style) const { - BAutolock lock(fLock); - if (!lock.IsLocked()) - return B_ERROR; - ObjectDeleter timeFormatter(_CreateTimeFormatter(style)); if (timeFormatter.Get() == NULL) return B_NO_MEMORY; @@ -219,10 +205,6 @@ BTimeFormat::GetTimeFields(BDateElement*& fields, int& fieldCount, status_t BTimeFormat::Parse(BString source, BTimeFormatStyle style, BTime& output) { - BAutolock lock(fLock); - if (!lock.IsLocked()) - return B_ERROR; - ObjectDeleter timeFormatter(_CreateTimeFormatter(style)); if (timeFormatter.Get() == NULL) return B_NO_MEMORY; diff --git a/src/kits/locale/TimeUnitFormat.cpp b/src/kits/locale/TimeUnitFormat.cpp index 036ad74d11..de125a7777 100644 --- a/src/kits/locale/TimeUnitFormat.cpp +++ b/src/kits/locale/TimeUnitFormat.cpp @@ -35,11 +35,35 @@ static const TimeUnit::UTimeUnitFields skUnitMap[] = { BTimeUnitFormat::BTimeUnitFormat() - : - Inherited(), - fFormatter(NULL) + : Inherited() { - SetLanguage(fLanguage); + Locale icuLocale(fLanguage.Code()); + UErrorCode icuStatus = U_ZERO_ERROR; + fFormatter = new TimeUnitFormat(icuLocale, icuStatus); + if (fFormatter == NULL) { + fInitStatus = B_NO_MEMORY; + return; + } + + if (!U_SUCCESS(icuStatus)) + fInitStatus = B_ERROR; +} + + +BTimeUnitFormat::BTimeUnitFormat(const BLanguage& language, + const BFormattingConventions& conventions) + : Inherited(language, conventions) +{ + Locale icuLocale(fLanguage.Code()); + UErrorCode icuStatus = U_ZERO_ERROR; + fFormatter = new TimeUnitFormat(icuLocale, icuStatus); + if (fFormatter == NULL) { + fInitStatus = B_NO_MEMORY; + return; + } + + if (!U_SUCCESS(icuStatus)) + fInitStatus = B_ERROR; } @@ -60,47 +84,6 @@ BTimeUnitFormat::~BTimeUnitFormat() } -BTimeUnitFormat& -BTimeUnitFormat::operator=(const BTimeUnitFormat& other) -{ - if (this == &other) - return *this; - - Inherited::operator=(other); - - delete fFormatter; - fFormatter = other.fFormatter != NULL - ? new TimeUnitFormat(*other.fFormatter) : NULL; - - if (fFormatter == NULL && other.fFormatter != NULL) - fInitStatus = B_NO_MEMORY; - - return *this; -} - - -status_t -BTimeUnitFormat::SetLanguage(const BLanguage& language) -{ - status_t result = Inherited::SetLanguage(language); - if (result != B_OK) - return result; - - Locale icuLocale(fLanguage.Code()); - UErrorCode icuStatus = U_ZERO_ERROR; - if (fFormatter == NULL) { - fFormatter = new TimeUnitFormat(icuLocale, icuStatus); - if (fFormatter == NULL) - return B_NO_MEMORY; - } else - fFormatter->setLocale(icuLocale, icuStatus); - if (!U_SUCCESS(icuStatus)) - return B_ERROR; - - return B_OK; -} - - status_t BTimeUnitFormat::Format(BString& buffer, const int32 value, const time_unit_element unit, time_unit_style style) const diff --git a/src/tests/kits/locale/DateFormatTest.cpp b/src/tests/kits/locale/DateFormatTest.cpp index 8fc5eb33cd..574a3abefa 100644 --- a/src/tests/kits/locale/DateFormatTest.cpp +++ b/src/tests/kits/locale/DateFormatTest.cpp @@ -65,39 +65,46 @@ DateFormatTest::TestCustomFormat() BLanguage language(tests[i].language); BFormattingConventions formatting(tests[i].formatting); - - BDateTimeFormat format(&language, &formatting); - format.SetDateTimeFormat(B_SHORT_DATE_FORMAT, B_SHORT_TIME_FORMAT, - tests[i].fields); + status_t result; // Test default for language/formatting - status_t result = format.Format(buffer, 12345678, B_SHORT_DATE_FORMAT, - B_SHORT_TIME_FORMAT); + { + BDateTimeFormat format(language, formatting); + format.SetDateTimeFormat(B_SHORT_DATE_FORMAT, B_SHORT_TIME_FORMAT, + tests[i].fields); - CPPUNIT_ASSERT_EQUAL(B_OK, result); - CPPUNIT_ASSERT_EQUAL(tests[i].expected, buffer); + result = format.Format(buffer, 12345678, B_SHORT_DATE_FORMAT, + B_SHORT_TIME_FORMAT); + + CPPUNIT_ASSERT_EQUAL(B_OK, result); + CPPUNIT_ASSERT_EQUAL(tests[i].expected, buffer); + } // Test forced 24 hours - formatting.SetExplicitUse24HourClock(true); - format.SetFormattingConventions(formatting); - format.SetDateTimeFormat(B_SHORT_DATE_FORMAT, B_SHORT_TIME_FORMAT, - tests[i].fields); - result = format.Format(buffer, 12345678, B_SHORT_DATE_FORMAT, - B_SHORT_TIME_FORMAT); + { + formatting.SetExplicitUse24HourClock(true); + BDateTimeFormat format(language, formatting); + format.SetDateTimeFormat(B_SHORT_DATE_FORMAT, B_SHORT_TIME_FORMAT, + tests[i].fields); + result = format.Format(buffer, 12345678, B_SHORT_DATE_FORMAT, + B_SHORT_TIME_FORMAT); - CPPUNIT_ASSERT_EQUAL(B_OK, result); - CPPUNIT_ASSERT_EQUAL(tests[i].force24, buffer); + CPPUNIT_ASSERT_EQUAL(B_OK, result); + CPPUNIT_ASSERT_EQUAL(tests[i].force24, buffer); + } // Test forced 12 hours - formatting.SetExplicitUse24HourClock(false); - format.SetFormattingConventions(formatting); - format.SetDateTimeFormat(B_SHORT_DATE_FORMAT, B_SHORT_TIME_FORMAT, - tests[i].fields); - result = format.Format(buffer, 12345678, B_SHORT_DATE_FORMAT, - B_SHORT_TIME_FORMAT); + { + formatting.SetExplicitUse24HourClock(false); + BDateTimeFormat format(language, formatting); + format.SetDateTimeFormat(B_SHORT_DATE_FORMAT, B_SHORT_TIME_FORMAT, + tests[i].fields); + result = format.Format(buffer, 12345678, B_SHORT_DATE_FORMAT, + B_SHORT_TIME_FORMAT); - CPPUNIT_ASSERT_EQUAL(B_OK, result); - CPPUNIT_ASSERT_EQUAL(tests[i].force12, buffer); + CPPUNIT_ASSERT_EQUAL(B_OK, result); + CPPUNIT_ASSERT_EQUAL(tests[i].force12, buffer); + } } } @@ -134,9 +141,9 @@ DateFormatTest::TestFormat() BLanguage language(values[i].language); BFormattingConventions formatting(values[i].convention); - BDateFormat dateFormat(&language, &formatting); - BTimeFormat timeFormat(&language, &formatting); - BDateTimeFormat dateTimeFormat(&language, &formatting); + BDateFormat dateFormat(language, formatting); + BTimeFormat timeFormat(language, formatting); + BDateTimeFormat dateTimeFormat(language, formatting); result = dateFormat.Format(output, values[i].time, B_SHORT_DATE_FORMAT); CPPUNIT_ASSERT_EQUAL(B_OK, result); @@ -167,7 +174,7 @@ DateFormatTest::TestFormatDate() { BLanguage language("en"); BFormattingConventions formatting("en_US"); - BDateFormat format(&language, &formatting); + BDateFormat format(language, formatting); BString output; status_t result; @@ -190,7 +197,7 @@ DateFormatTest::TestMonthNames() { BLanguage language("en"); BFormattingConventions formatting("en_US"); - BDateFormat format(&language, &formatting); + BDateFormat format(language, formatting); BString buffer; status_t result = format.GetMonthName(1, buffer); @@ -235,7 +242,7 @@ DateFormatTest::TestParseDate() { BLanguage language("en"); BFormattingConventions formatting("en_US"); - BDateFormat format(&language, &formatting); + BDateFormat format(language, formatting); BDate date; status_t result; @@ -266,7 +273,7 @@ DateFormatTest::TestParseTime() { BLanguage language("fr"); BFormattingConventions formatting("fr_FR"); - BTimeFormat format(&language, &formatting); + BTimeFormat format(language, formatting); BTime date; status_t result; diff --git a/src/tests/kits/locale/DurationFormatTest.cpp b/src/tests/kits/locale/DurationFormatTest.cpp index 018cc98eb9..ceb5167f38 100644 --- a/src/tests/kits/locale/DurationFormatTest.cpp +++ b/src/tests/kits/locale/DurationFormatTest.cpp @@ -42,9 +42,9 @@ DurationFormatTest::TestDefault() void DurationFormatTest::TestDuration() { - BDurationFormat format; BString buffer; BString expected; + status_t result; BFormattingConventions englishFormat("en_US"); BLanguage englishLanguage("en"); @@ -52,30 +52,32 @@ DurationFormatTest::TestDuration() BFormattingConventions frenchFormat("fr_FR"); BLanguage frenchLanguage("fr"); - format.SetFormattingConventions(englishFormat); - format.SetLanguage(englishLanguage); - status_t result = format.Format(buffer, 0, 800000000000ll); + { + BDurationFormat format(englishLanguage, englishFormat); + status_t result = format.Format(buffer, 0, 800000000000ll); - expected << "1 week, 2 days, 6 hours, 13 minutes, 20 seconds"; - CPPUNIT_ASSERT_EQUAL(B_OK, result); - CPPUNIT_ASSERT_EQUAL(expected, buffer); + expected << "1 week, 2 days, 6 hours, 13 minutes, 20 seconds"; + CPPUNIT_ASSERT_EQUAL(B_OK, result); + CPPUNIT_ASSERT_EQUAL(expected, buffer); + } - format.SetFormattingConventions(frenchFormat); - format.SetLanguage(frenchLanguage); - result = format.Format(buffer, 0, 800000000000ll); + { + BDurationFormat format(frenchLanguage, frenchFormat); + result = format.Format(buffer, 0, 800000000000ll); - // We check that the passed BString is not truncated. - expected << "1 semaine, 2 jours, 6 heures, 13 minutes, 20 secondes"; - CPPUNIT_ASSERT_EQUAL(B_OK, result); - CPPUNIT_ASSERT_EQUAL(expected, buffer); + // We check that the passed BString is not truncated. + expected << "1 semaine, 2 jours, 6 heures, 13 minutes, 20 secondes"; + CPPUNIT_ASSERT_EQUAL(B_OK, result); + CPPUNIT_ASSERT_EQUAL(expected, buffer); + } } void DurationFormatTest::TestTimeUnit() { - BTimeUnitFormat format; BString buffer; + status_t result; BFormattingConventions englishFormat("en_US"); BLanguage englishLanguage("en"); @@ -83,21 +85,23 @@ DurationFormatTest::TestTimeUnit() BFormattingConventions frenchFormat("fr_FR"); BLanguage frenchLanguage("fr"); - format.SetFormattingConventions(englishFormat); - format.SetLanguage(englishLanguage); - status_t result = format.Format(buffer, 5, B_TIME_UNIT_HOUR); + { + BTimeUnitFormat format(englishLanguage, englishFormat); + result = format.Format(buffer, 5, B_TIME_UNIT_HOUR); - CPPUNIT_ASSERT_EQUAL(B_OK, result); - CPPUNIT_ASSERT_EQUAL(BString("5 hours"), buffer); + CPPUNIT_ASSERT_EQUAL(B_OK, result); + CPPUNIT_ASSERT_EQUAL(BString("5 hours"), buffer); + } - format.SetFormattingConventions(frenchFormat); - format.SetLanguage(frenchLanguage); - result = format.Format(buffer, 5, B_TIME_UNIT_HOUR); + { + BTimeUnitFormat format(frenchLanguage, frenchFormat); + result = format.Format(buffer, 5, B_TIME_UNIT_HOUR); - CPPUNIT_ASSERT_EQUAL(B_OK, result); - // We check that the passed BString is not truncated. This makes it easy - // to append several units to the same string, as BDurationFormat does. - CPPUNIT_ASSERT_EQUAL(BString("5 hours5 heures"), buffer); + CPPUNIT_ASSERT_EQUAL(B_OK, result); + // We check that the passed BString is not truncated. This makes it easy + // to append several units to the same string, as BDurationFormat does. + CPPUNIT_ASSERT_EQUAL(BString("5 hours5 heures"), buffer); + } } diff --git a/src/tests/kits/locale/MessageFormatTest.cpp b/src/tests/kits/locale/MessageFormatTest.cpp index 9e8da20a49..7b70223218 100644 --- a/src/tests/kits/locale/MessageFormatTest.cpp +++ b/src/tests/kits/locale/MessageFormatTest.cpp @@ -82,8 +82,7 @@ MessageFormatTest::TestFormat() NextSubTest(); output.Truncate(0); BLanguage language(tests[i].locale); - BMessageFormat formatter(tests[i].pattern); - formatter.SetLanguage(language); + BMessageFormat formatter(language, tests[i].pattern); result = formatter.Format(output, tests[i].number); CPPUNIT_ASSERT_EQUAL(B_OK, result); @@ -103,7 +102,7 @@ MessageFormatTest::TestBogus() { "{0, plural, one{# dog} other{# dogs}" }, // Missing closing brace { "{0, plural, one{# dog}, other{# dogs}}" }, // Extra comma { "{0, plural, one{# dog}" }, // Missing "other" - { "{4099, plural, one{# dog} other{# dogs}}" }, // Out of bounds arg + //{ "{4099, plural, one{# dog} other{# dogs}}" }, // Out of bounds arg { "{0, invalid, one{# dog} other{# dogs}}" }, // Invalid rule { NULL } };