Skip to content

Commit ce31bdd

Browse files
committed
ICU-23001 Add interface to denote the class which implement getLocale() method
ICU-23001 Fix hide overloaded getLocale
1 parent 9ad9c03 commit ce31bdd

File tree

14 files changed

+130
-183
lines changed

14 files changed

+130
-183
lines changed

icu4c/source/common/brkiter.cpp

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,8 @@ BreakIterator::buildInstance(const Locale& loc, const char *type, UErrorCode &st
121121

122122
// If there is a result, set the valid locale and actual locale, and the kind
123123
if (U_SUCCESS(status) && result != nullptr) {
124-
U_LOCALE_BASED(locBased, *(BreakIterator*)result);
125-
126-
locBased.setLocaleIDs(ures_getLocaleByType(b, ULOC_VALID_LOCALE, &status),
127-
actual.data(), status);
124+
result->setLocaleIDs(ures_getLocaleByType(b, ULOC_VALID_LOCALE, &status),
125+
actual.data());
128126
LocaleBased::setLocaleID(loc.getName(), result->requestLocale, status);
129127
}
130128

@@ -207,19 +205,17 @@ BreakIterator::BreakIterator()
207205
{
208206
}
209207

210-
BreakIterator::BreakIterator(const BreakIterator &other) : UObject(other) {
208+
BreakIterator::BreakIterator(const BreakIterator &other) : UObject(other), DataLocaleInformation(other) {
211209
UErrorCode status = U_ZERO_ERROR;
212-
U_LOCALE_BASED(locBased, *this);
213-
locBased.setLocaleIDs(other.validLocale, other.actualLocale, status);
214210
LocaleBased::setLocaleID(other.requestLocale, requestLocale, status);
215211
U_ASSERT(U_SUCCESS(status));
216212
}
217213

214+
218215
BreakIterator &BreakIterator::operator =(const BreakIterator &other) {
219216
if (this != &other) {
217+
DataLocaleInformation::operator=(other);
220218
UErrorCode status = U_ZERO_ERROR;
221-
U_LOCALE_BASED(locBased, *this);
222-
locBased.setLocaleIDs(other.validLocale, other.actualLocale, status);
223219
LocaleBased::setLocaleID(other.requestLocale, requestLocale, status);
224220
U_ASSERT(U_SUCCESS(status));
225221
}
@@ -228,8 +224,6 @@ BreakIterator &BreakIterator::operator =(const BreakIterator &other) {
228224

229225
BreakIterator::~BreakIterator()
230226
{
231-
delete validLocale;
232-
delete actualLocale;
233227
delete requestLocale;
234228
}
235229

@@ -398,8 +392,7 @@ BreakIterator::createInstance(const Locale& loc, int32_t kind, UErrorCode& statu
398392
// THIS LONG is a sign of bad code -- so the action item is to
399393
// revisit this in ICU 3.0 and clean it up/fix it/remove it.
400394
if (U_SUCCESS(status) && (result != nullptr) && *actualLoc.getName() != 0) {
401-
U_LOCALE_BASED(locBased, *result);
402-
locBased.setLocaleIDs(actualLoc.getName(), actualLoc.getName(), status);
395+
result->setLocaleIDs(actualLoc.getName(), actualLoc.getName());
403396
}
404397
return result;
405398
}
@@ -509,7 +502,7 @@ BreakIterator::getLocale(ULocDataLocaleType type, UErrorCode& status) const {
509502
return requestLocale == nullptr ?
510503
Locale::getRoot() : Locale(requestLocale->data());
511504
}
512-
return LocaleBased::getLocale(validLocale, actualLocale, type, status);
505+
return DataLocaleInformation::getLocale(type, status);
513506
}
514507

515508
const char *
@@ -520,10 +513,9 @@ BreakIterator::getLocaleID(ULocDataLocaleType type, UErrorCode& status) const {
520513
if (type == ULOC_REQUESTED_LOCALE) {
521514
return requestLocale == nullptr ? "" : requestLocale->data();
522515
}
523-
return LocaleBased::getLocaleID(validLocale, actualLocale, type, status);
516+
return DataLocaleInformation::getLocaleID(type, status);
524517
}
525518

526-
527519
// This implementation of getRuleStatus is a do-nothing stub, here to
528520
// provide a default implementation for any derived BreakIterator classes that
529521
// do not implement it themselves.
@@ -547,10 +539,7 @@ int32_t BreakIterator::getRuleStatusVec(int32_t *fillInVec, int32_t capacity, UE
547539
}
548540

549541
BreakIterator::BreakIterator (const Locale& valid, const Locale& actual) {
550-
UErrorCode status = U_ZERO_ERROR;
551-
U_LOCALE_BASED(locBased, (*this));
552-
locBased.setLocaleIDs(valid.getName(), actual.getName(), status);
553-
U_ASSERT(U_SUCCESS(status));
542+
setLocaleIDs(valid.getName(), actual.getName());
554543
}
555544

556545
U_NAMESPACE_END

icu4c/source/common/locid.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
#include "uniquecharstr.h"
5959
#include "ustr_imp.h"
6060
#include "uvector.h"
61+
#include "locbased.h"
6162

6263
U_NAMESPACE_BEGIN
6364

@@ -2729,5 +2730,41 @@ Locale::getBaseName() const {
27292730

27302731
Locale::Iterator::~Iterator() = default;
27312732

2733+
DataLocaleInformation::DataLocaleInformation(const DataLocaleInformation& other) {
2734+
UErrorCode status = U_ZERO_ERROR;
2735+
actualLocale = other.actualLocale == nullptr ? nullptr : new CharString(*other.actualLocale, status);
2736+
validLocale = other.validLocale == nullptr ? nullptr : new CharString(*other.validLocale, status);
2737+
U_ASSERT(U_SUCCESS(status));
2738+
}
2739+
DataLocaleInformation::~DataLocaleInformation() {
2740+
delete actualLocale;
2741+
delete validLocale;
2742+
}
2743+
2744+
Locale DataLocaleInformation::getLocale(ULocDataLocaleType type, UErrorCode& status) const {
2745+
return LocaleBased::getLocale(validLocale, actualLocale, type, status);
2746+
}
2747+
2748+
const char* DataLocaleInformation::getLocaleID(ULocDataLocaleType type, UErrorCode& status) const {
2749+
return LocaleBased::getLocaleID(validLocale, actualLocale, type, status);
2750+
}
2751+
2752+
void DataLocaleInformation::setLocaleIDs(const char* valid, const char* actual) {
2753+
UErrorCode status = U_ZERO_ERROR;
2754+
U_LOCALE_BASED(locBased, *this);
2755+
locBased.setLocaleIDs(valid, actual, status);
2756+
U_ASSERT(U_SUCCESS(status));
2757+
}
2758+
2759+
DataLocaleInformation& DataLocaleInformation::operator=(const DataLocaleInformation& other) {
2760+
delete actualLocale;
2761+
delete validLocale;
2762+
UErrorCode status = U_ZERO_ERROR;
2763+
actualLocale = other.actualLocale == nullptr ? nullptr : new CharString(*other.actualLocale, status);
2764+
validLocale = other.validLocale == nullptr ? nullptr : new CharString(*other.validLocale, status);
2765+
U_ASSERT(U_SUCCESS(status));
2766+
return *this;
2767+
}
2768+
27322769
//eof
27332770
U_NAMESPACE_END

icu4c/source/common/unicode/brkiter.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ class CharString;
105105
* and in the sample program icu/source/samples/break/break.cpp
106106
*
107107
*/
108-
class U_COMMON_API BreakIterator : public UObject {
108+
class U_COMMON_API BreakIterator : public UObject , public DataLocaleInformation {
109109
public:
110110
/**
111111
* destructor
@@ -584,7 +584,7 @@ class U_COMMON_API BreakIterator : public UObject {
584584
* actual locale.
585585
* @stable ICU 2.8
586586
*/
587-
Locale getLocale(ULocDataLocaleType type, UErrorCode& status) const;
587+
Locale getLocale(ULocDataLocaleType type, UErrorCode& status) const override;
588588

589589
#ifndef U_HIDE_INTERNAL_API
590590
/** Get the locale for this break iterator object. You can choose between valid and actual locale.
@@ -648,8 +648,6 @@ class U_COMMON_API BreakIterator : public UObject {
648648
private:
649649

650650
/** @internal (private) */
651-
CharString* actualLocale = nullptr;
652-
CharString* validLocale = nullptr;
653651
CharString* requestLocale = nullptr;
654652
};
655653

icu4c/source/common/unicode/locid.h

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ void U_CALLCONV locale_available_init(); /**< @internal */
5555

5656
class StringEnumeration;
5757
class UnicodeString;
58+
class CharString;
5859

5960
/**
6061
* A <code>Locale</code> object represents a specific geographical, political,
@@ -1283,6 +1284,63 @@ Locale::isBogus() const {
12831284
return fIsBogus;
12841285
}
12851286

1287+
/**
1288+
* <code>DataLocaleInformation</code> is an abstract base class for objects
1289+
* that perform getLocale operations to query locale data resolution.
1290+
*/
1291+
class U_COMMON_API DataLocaleInformation : public UMemory {
1292+
public:
1293+
DataLocaleInformation() = default;
1294+
1295+
/**
1296+
* Initializes a DataLocaleInformation object from another
1297+
* DataLocaleInformation object.
1298+
*
1299+
* @param other The DataLocaleInformation object being copied in.
1300+
*/
1301+
DataLocaleInformation(const DataLocaleInformation& other);
1302+
1303+
virtual ~DataLocaleInformation();
1304+
1305+
/**
1306+
* Set the locale meta-data for the service object wrapped by this
1307+
* object. If either parameter is zero, it is ignored.
1308+
* @param valid the ID of the valid locale
1309+
* @param actual the ID of the actual locale
1310+
*/
1311+
virtual void setLocaleIDs(const char* valid, const char* actual);
1312+
1313+
/** Get the locale for this object. You can choose between valid and actual locale.
1314+
* @param type type of the locale we're looking for (valid or actual)
1315+
* @param status error code for the operation
1316+
* @return the locale
1317+
*/
1318+
virtual Locale getLocale(ULocDataLocaleType type, UErrorCode& status) const;
1319+
1320+
#ifndef U_HIDE_INTERNAL_API
1321+
/** Get the locale for this object. You can choose between valid and actual locale.
1322+
* @param type type of the locale we're looking for (valid or actual)
1323+
* @param status error code for the operation
1324+
* @return the locale
1325+
* @internal
1326+
*/
1327+
const char* getLocaleID(ULocDataLocaleType type, UErrorCode &status) const;
1328+
#endif /* U_HIDE_INTERNAL_API */
1329+
1330+
/**
1331+
* Replaces the entire contents of *this with the specified value.
1332+
*
1333+
* @param other The DataLocaleInformation object being copied in.
1334+
* @return *this
1335+
*/
1336+
DataLocaleInformation& operator=(const DataLocaleInformation& other);
1337+
1338+
1339+
private:
1340+
CharString* validLocale = nullptr;
1341+
CharString* actualLocale = nullptr;
1342+
};
1343+
12861344
U_NAMESPACE_END
12871345

12881346
#endif /* U_SHOW_CPLUSPLUS_API */

icu4c/source/i18n/calendar.cpp

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -774,14 +774,12 @@ fSkippedWallTime(UCAL_WALLTIME_LAST)
774774
Calendar::~Calendar()
775775
{
776776
delete fZone;
777-
delete actualLocale;
778-
delete validLocale;
779777
}
780778

781779
// -------------------------------------
782780

783781
Calendar::Calendar(const Calendar &source)
784-
: UObject(source)
782+
: UObject(source), DataLocaleInformation(source)
785783
{
786784
*this = source;
787785
}
@@ -792,6 +790,7 @@ Calendar &
792790
Calendar::operator=(const Calendar &right)
793791
{
794792
if (this != &right) {
793+
DataLocaleInformation::operator=(right);
795794
uprv_arrayCopy(right.fFields, fFields, UCAL_FIELD_COUNT);
796795
uprv_arrayCopy(right.fStamp, fStamp, UCAL_FIELD_COUNT);
797796
fTime = right.fTime;
@@ -814,10 +813,6 @@ Calendar::operator=(const Calendar &right)
814813
fWeekendCease = right.fWeekendCease;
815814
fWeekendCeaseMillis = right.fWeekendCeaseMillis;
816815
fNextStamp = right.fNextStamp;
817-
UErrorCode status = U_ZERO_ERROR;
818-
U_LOCALE_BASED(locBased, *this);
819-
locBased.setLocaleIDs(right.validLocale, right.actualLocale, status);
820-
U_ASSERT(U_SUCCESS(status));
821816
}
822817

823818
return *this;
@@ -4109,9 +4104,8 @@ Calendar::setWeekData(const Locale& desiredLocale, const char *type, UErrorCode&
41094104
}
41104105

41114106
if (U_SUCCESS(status)) {
4112-
U_LOCALE_BASED(locBased,*this);
4113-
locBased.setLocaleIDs(ures_getLocaleByType(monthNames.getAlias(), ULOC_VALID_LOCALE, &status),
4114-
ures_getLocaleByType(monthNames.getAlias(), ULOC_ACTUAL_LOCALE, &status), status);
4107+
setLocaleIDs(ures_getLocaleByType(monthNames.getAlias(), ULOC_VALID_LOCALE, &status),
4108+
ures_getLocaleByType(monthNames.getAlias(), ULOC_ACTUAL_LOCALE, &status));
41154109
} else {
41164110
status = U_USING_FALLBACK_WARNING;
41174111
return;
@@ -4197,16 +4191,6 @@ Calendar::updateTime(UErrorCode& status)
41974191
fAreFieldsVirtuallySet = false;
41984192
}
41994193

4200-
Locale
4201-
Calendar::getLocale(ULocDataLocaleType type, UErrorCode& status) const {
4202-
return LocaleBased::getLocale(validLocale, actualLocale, type, status);
4203-
}
4204-
4205-
const char *
4206-
Calendar::getLocaleID(ULocDataLocaleType type, UErrorCode& status) const {
4207-
return LocaleBased::getLocaleID(validLocale, actualLocale, type, status);
4208-
}
4209-
42104194
void
42114195
Calendar::recalculateStamp() {
42124196
int32_t index;

icu4c/source/i18n/dtfmtsym.cpp

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ DateFormatSymbols::DateFormatSymbols(const char *type, UErrorCode& status)
318318
}
319319

320320
DateFormatSymbols::DateFormatSymbols(const DateFormatSymbols& other)
321-
: UObject(other)
321+
: UObject(other), DataLocaleInformation(other)
322322
{
323323
copyData(other);
324324
}
@@ -400,10 +400,6 @@ DateFormatSymbols::createZoneStrings(const UnicodeString *const * otherStrings)
400400
*/
401401
void
402402
DateFormatSymbols::copyData(const DateFormatSymbols& other) {
403-
UErrorCode status = U_ZERO_ERROR;
404-
U_LOCALE_BASED(locBased, *this);
405-
locBased.setLocaleIDs(other.validLocale, other.actualLocale, status);
406-
U_ASSERT(U_SUCCESS(status));
407403
assignArray(fEras, fErasCount, other.fEras, other.fErasCount);
408404
assignArray(fEraNames, fEraNamesCount, other.fEraNames, other.fEraNamesCount);
409405
assignArray(fNarrowEras, fNarrowErasCount, other.fNarrowEras, other.fNarrowErasCount);
@@ -487,6 +483,7 @@ DateFormatSymbols::copyData(const DateFormatSymbols& other) {
487483
DateFormatSymbols& DateFormatSymbols::operator=(const DateFormatSymbols& other)
488484
{
489485
if (this == &other) { return *this; } // self-assignment: no-op
486+
DataLocaleInformation::operator=(other);
490487
dispose();
491488
copyData(other);
492489

@@ -496,8 +493,6 @@ DateFormatSymbols& DateFormatSymbols::operator=(const DateFormatSymbols& other)
496493
DateFormatSymbols::~DateFormatSymbols()
497494
{
498495
dispose();
499-
delete actualLocale;
500-
delete validLocale;
501496
}
502497

503498
void DateFormatSymbols::dispose()
@@ -537,10 +532,6 @@ void DateFormatSymbols::dispose()
537532
delete[] fStandaloneWideDayPeriods;
538533
delete[] fStandaloneNarrowDayPeriods;
539534

540-
delete actualLocale;
541-
actualLocale = nullptr;
542-
delete validLocale;
543-
validLocale = nullptr;
544535
disposeZoneStrings();
545536
}
546537

@@ -2302,12 +2293,11 @@ DateFormatSymbols::initializeData(const Locale& locale, const char *type, UError
23022293
}
23032294
}
23042295

2305-
U_LOCALE_BASED(locBased, *this);
23062296
// if we make it to here, the resource data is cool, and we can get everything out
23072297
// of it that we need except for the time-zone and localized-pattern data, which
23082298
// are stored in a separate file
2309-
locBased.setLocaleIDs(ures_getLocaleByType(cb.getAlias(), ULOC_VALID_LOCALE, &status),
2310-
ures_getLocaleByType(cb.getAlias(), ULOC_ACTUAL_LOCALE, &status), status);
2299+
setLocaleIDs(ures_getLocaleByType(cb.getAlias(), ULOC_VALID_LOCALE, &status),
2300+
ures_getLocaleByType(cb.getAlias(), ULOC_ACTUAL_LOCALE, &status));
23112301

23122302
// Load eras
23132303
initField(&fEras, fErasCount, calendarSink, buildResourcePath(path, gErasTag, gNamesAbbrTag, status), status);
@@ -2531,11 +2521,6 @@ DateFormatSymbols::initializeData(const Locale& locale, const char *type, UError
25312521
}
25322522
}
25332523

2534-
Locale
2535-
DateFormatSymbols::getLocale(ULocDataLocaleType type, UErrorCode& status) const {
2536-
return LocaleBased::getLocale(validLocale, actualLocale, type, status);
2537-
}
2538-
25392524
U_NAMESPACE_END
25402525

25412526
#endif /* #if !UCONFIG_NO_FORMATTING */

0 commit comments

Comments
 (0)