Skip to content

Commit f304db2

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

File tree

12 files changed

+123
-153
lines changed

12 files changed

+123
-153
lines changed

Diff for: icu4c/source/common/brkiter.cpp

+9-20
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

Diff for: icu4c/source/common/locid.cpp

+37
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

@@ -2748,5 +2749,41 @@ Locale::getBaseName() const {
27482749

27492750
Locale::Iterator::~Iterator() = default;
27502751

2752+
DataLocaleInformation::DataLocaleInformation(const DataLocaleInformation& other) {
2753+
UErrorCode status = U_ZERO_ERROR;
2754+
actualLocale = other.actualLocale == nullptr ? nullptr : new CharString(*other.actualLocale, status);
2755+
validLocale = other.validLocale == nullptr ? nullptr : new CharString(*other.validLocale, status);
2756+
U_ASSERT(U_SUCCESS(status));
2757+
}
2758+
DataLocaleInformation::~DataLocaleInformation() {
2759+
delete actualLocale;
2760+
delete validLocale;
2761+
}
2762+
2763+
Locale DataLocaleInformation::getLocale(ULocDataLocaleType type, UErrorCode& status) const {
2764+
return LocaleBased::getLocale(validLocale, actualLocale, type, status);
2765+
}
2766+
2767+
const char* DataLocaleInformation::getLocaleID(ULocDataLocaleType type, UErrorCode& status) const {
2768+
return LocaleBased::getLocaleID(validLocale, actualLocale, type, status);
2769+
}
2770+
2771+
void DataLocaleInformation::setLocaleIDs(const char* valid, const char* actual) {
2772+
UErrorCode status = U_ZERO_ERROR;
2773+
U_LOCALE_BASED(locBased, *this);
2774+
locBased.setLocaleIDs(valid, actual, status);
2775+
U_ASSERT(U_SUCCESS(status));
2776+
}
2777+
2778+
DataLocaleInformation& DataLocaleInformation::operator=(const DataLocaleInformation& other) {
2779+
delete actualLocale;
2780+
delete validLocale;
2781+
UErrorCode status = U_ZERO_ERROR;
2782+
actualLocale = other.actualLocale == nullptr ? nullptr : new CharString(*other.actualLocale, status);
2783+
validLocale = other.validLocale == nullptr ? nullptr : new CharString(*other.validLocale, status);
2784+
U_ASSERT(U_SUCCESS(status));
2785+
return *this;
2786+
}
2787+
27512788
//eof
27522789
U_NAMESPACE_END

Diff for: icu4c/source/common/unicode/brkiter.h

+2-4
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

Diff for: icu4c/source/common/unicode/locid.h

+58
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,
@@ -1290,6 +1291,63 @@ Locale::isBogus() const {
12901291
return fIsBogus;
12911292
}
12921293

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

12951353
#endif /* U_SHOW_CPLUSPLUS_API */

Diff for: icu4c/source/i18n/calendar.cpp

+4-20
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;
@@ -4115,9 +4110,8 @@ Calendar::setWeekData(const Locale& desiredLocale, const char *type, UErrorCode&
41154110
}
41164111

41174112
if (U_SUCCESS(status)) {
4118-
U_LOCALE_BASED(locBased,*this);
4119-
locBased.setLocaleIDs(ures_getLocaleByType(monthNames.getAlias(), ULOC_VALID_LOCALE, &status),
4120-
ures_getLocaleByType(monthNames.getAlias(), ULOC_ACTUAL_LOCALE, &status), status);
4113+
setLocaleIDs(ures_getLocaleByType(monthNames.getAlias(), ULOC_VALID_LOCALE, &status),
4114+
ures_getLocaleByType(monthNames.getAlias(), ULOC_ACTUAL_LOCALE, &status));
41214115
} else {
41224116
status = U_USING_FALLBACK_WARNING;
41234117
return;
@@ -4203,16 +4197,6 @@ Calendar::updateTime(UErrorCode& status)
42034197
fAreFieldsVirtuallySet = false;
42044198
}
42054199

4206-
Locale
4207-
Calendar::getLocale(ULocDataLocaleType type, UErrorCode& status) const {
4208-
return LocaleBased::getLocale(validLocale, actualLocale, type, status);
4209-
}
4210-
4211-
const char *
4212-
Calendar::getLocaleID(ULocDataLocaleType type, UErrorCode& status) const {
4213-
return LocaleBased::getLocaleID(validLocale, actualLocale, type, status);
4214-
}
4215-
42164200
void
42174201
Calendar::recalculateStamp() {
42184202
int32_t index;

Diff for: icu4c/source/i18n/dtfmtsym.cpp

+4-19
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)