Skip to content

Commit 07b2cde

Browse files
committed
Make Function::call() return a LocalPointer
1 parent 6b611d6 commit 07b2cde

File tree

5 files changed

+105
-73
lines changed

5 files changed

+105
-73
lines changed

icu4c/source/i18n/messageformat2_function_registry.cpp

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -282,17 +282,19 @@ StandardFunctions::Number::create(bool isInteger, UErrorCode& success) {
282282
return result.orphan();
283283
}
284284

285-
FunctionValue* StandardFunctions::Number::call(const FunctionContext& context,
285+
LocalPointer<FunctionValue> StandardFunctions::Number::call(const FunctionContext& context,
286286
FunctionValue& operand,
287287
FunctionOptions&& options,
288288
UErrorCode& errorCode) {
289-
LocalPointer<NumberValue>
289+
if (U_FAILURE(errorCode)) {
290+
return LocalPointer<FunctionValue>();
291+
}
292+
LocalPointer<FunctionValue>
290293
val(new NumberValue(*this, context, operand, std::move(options), errorCode));
291-
if (val.isValid()) {
292-
return val.orphan();
294+
if (!val.isValid()) {
295+
errorCode = U_MEMORY_ALLOCATION_ERROR;
293296
}
294-
errorCode = U_MEMORY_ALLOCATION_ERROR;
295-
return nullptr;
297+
return val;
296298
}
297299

298300
/* static */ number::LocalizedNumberFormatter StandardFunctions::formatterForOptions(const Number& number,
@@ -783,15 +785,17 @@ StandardFunctions::DateTime::create(DateTime::DateTimeType type,
783785
return result.orphan();
784786
}
785787

786-
FunctionValue*
788+
LocalPointer<FunctionValue>
787789
StandardFunctions::DateTime::call(const FunctionContext& context,
788790
FunctionValue& val,
789791
FunctionOptions&& opts,
790792
UErrorCode& errorCode) {
791-
NULL_ON_ERROR(errorCode);
792-
793-
auto result = new DateTimeValue(type, context, val, std::move(opts), errorCode);
794-
if (result == nullptr) {
793+
if (U_FAILURE(errorCode)) {
794+
return LocalPointer<FunctionValue>();
795+
}
796+
LocalPointer<FunctionValue>
797+
result(new DateTimeValue(type, context, val, std::move(opts), errorCode));
798+
if (!result.isValid()) {
795799
errorCode = U_MEMORY_ALLOCATION_ERROR;
796800
}
797801
return result;
@@ -1103,12 +1107,20 @@ extern UnicodeString formattableToString(const Locale&,
11031107
const Formattable&,
11041108
UErrorCode&);
11051109

1106-
FunctionValue*
1110+
LocalPointer<FunctionValue>
11071111
StandardFunctions::String::call(const FunctionContext& context,
11081112
FunctionValue& val,
11091113
FunctionOptions&& opts,
11101114
UErrorCode& errorCode) {
1111-
return new StringValue(context, val, std::move(opts), errorCode);
1115+
if (U_FAILURE(errorCode)) {
1116+
return LocalPointer<FunctionValue>();
1117+
}
1118+
LocalPointer<FunctionValue>
1119+
result(new StringValue(context, val, std::move(opts), errorCode));
1120+
if (!result.isValid()) {
1121+
errorCode = U_MEMORY_ALLOCATION_ERROR;
1122+
}
1123+
return result;
11121124
}
11131125

11141126
UnicodeString StandardFunctions::StringValue::formatToString(UErrorCode& errorCode) const {

icu4c/source/i18n/messageformat2_function_registry_internal.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ namespace message2 {
5151
static DateTime* time(UErrorCode&);
5252
static DateTime* dateTime(UErrorCode&);
5353

54-
FunctionValue* call(const FunctionContext& context,
54+
LocalPointer<FunctionValue> call(const FunctionContext& context,
5555
FunctionValue& operand,
5656
FunctionOptions&& options,
5757
UErrorCode& errorCode) override;
@@ -80,7 +80,7 @@ namespace message2 {
8080
static Number* integer(UErrorCode& success);
8181
static Number* number( UErrorCode& success);
8282

83-
FunctionValue* call(const FunctionContext& context,
83+
LocalPointer<FunctionValue> call(const FunctionContext& context,
8484
FunctionValue& operand,
8585
FunctionOptions&& options,
8686
UErrorCode& errorCode) override;
@@ -158,7 +158,7 @@ namespace message2 {
158158

159159
class String : public Function {
160160
public:
161-
FunctionValue* call(const FunctionContext& context,
161+
LocalPointer<FunctionValue> call(const FunctionContext& context,
162162
FunctionValue& val,
163163
FunctionOptions&& opts,
164164
UErrorCode& errorCode) override;

icu4c/source/i18n/unicode/messageformat2_function_registry.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -291,20 +291,22 @@ namespace message2 {
291291
public:
292292
/**
293293
* Calls this Function on a FunctionValue operand and its FunctionOptions options,
294-
* returning a new pointer to a FunctionValue (which is adopted by the caller).
294+
* returning a LocalPointer to a FunctionValue.
295295
*
296296
* @param context The context of this function, based on its contextual options
297297
* @param operand The unnamed argument to the function.
298298
* @param options Resolved options for this function.
299299
* @param status Input/output error code
300+
* @return The function value that is the result of calling this function on
301+
* the arguments.
300302
*
301303
* @internal ICU 77 technology preview
302304
* @deprecated This API is for technology preview only.
303305
*/
304-
virtual FunctionValue* call(const FunctionContext& context,
305-
FunctionValue& operand,
306-
FunctionOptions&& options,
307-
UErrorCode& status) = 0;
306+
virtual LocalPointer<FunctionValue> call(const FunctionContext& context,
307+
FunctionValue& operand,
308+
FunctionOptions&& options,
309+
UErrorCode& status) = 0;
308310
/**
309311
* Destructor.
310312
*

icu4c/source/test/intltest/messageformat2test.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ class Person : public FormattableObject {
117117

118118
class PersonNameFunction : public Function {
119119
public:
120-
FunctionValue* call(const FunctionContext&, FunctionValue&, FunctionOptions&&, UErrorCode&) override;
120+
LocalPointer<FunctionValue> call(const FunctionContext&, FunctionValue&, FunctionOptions&&, UErrorCode&) override;
121121
virtual ~PersonNameFunction();
122122
PersonNameFunction() {}
123123
};
@@ -148,7 +148,7 @@ class FormattableProperties : public FormattableObject {
148148

149149
class GrammarCasesFunction : public Function {
150150
public:
151-
FunctionValue* call(const FunctionContext&, FunctionValue&, FunctionOptions&&, UErrorCode&) override;
151+
LocalPointer<FunctionValue> call(const FunctionContext&, FunctionValue&, FunctionOptions&&, UErrorCode&) override;
152152
static MFFunctionRegistry customRegistry(UErrorCode&);
153153
};
154154

@@ -167,7 +167,7 @@ class GrammarCasesValue : public FunctionValue {
167167

168168
class ListFunction : public Function {
169169
public:
170-
FunctionValue* call(const FunctionContext&, FunctionValue&, FunctionOptions&&, UErrorCode&) override;
170+
LocalPointer<FunctionValue> call(const FunctionContext&, FunctionValue&, FunctionOptions&&, UErrorCode&) override;
171171
static MFFunctionRegistry customRegistry(UErrorCode&);
172172
ListFunction() {}
173173
virtual ~ListFunction();
@@ -218,7 +218,7 @@ class AdjectiveValue : public FunctionValue {
218218

219219
class ResourceManager : public Function {
220220
public:
221-
FunctionValue* call(const FunctionContext&, FunctionValue&, FunctionOptions&&, UErrorCode&) override;
221+
LocalPointer<FunctionValue> call(const FunctionContext&, FunctionValue&, FunctionOptions&&, UErrorCode&) override;
222222
static MFFunctionRegistry customRegistry(UErrorCode&);
223223
static Hashtable* properties(UErrorCode&);
224224
static UnicodeString propertiesAsString(const Hashtable&);
@@ -243,14 +243,14 @@ class ResourceManagerValue : public FunctionValue {
243243

244244
class NounFunction : public Function {
245245
public:
246-
FunctionValue* call(const FunctionContext&, FunctionValue&, FunctionOptions&&, UErrorCode&) override;
246+
LocalPointer<FunctionValue> call(const FunctionContext&, FunctionValue&, FunctionOptions&&, UErrorCode&) override;
247247
NounFunction() { }
248248
virtual ~NounFunction();
249249
};
250250

251251
class AdjectiveFunction : public Function {
252252
public:
253-
FunctionValue* call(const FunctionContext&, FunctionValue&, FunctionOptions&&, UErrorCode&) override;
253+
LocalPointer<FunctionValue> call(const FunctionContext&, FunctionValue&, FunctionOptions&&, UErrorCode&) override;
254254
AdjectiveFunction() { }
255255
virtual ~AdjectiveFunction();
256256
};

icu4c/source/test/intltest/messageformat2test_custom.cpp

Lines changed: 64 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -276,16 +276,17 @@ static bool hasStringOption(const FunctionOptionsMap& opt,
276276
return getStringOption(opt, k) == v;
277277
}
278278

279-
FunctionValue* PersonNameFunction::call(const FunctionContext& context,
280-
FunctionValue& arg,
281-
FunctionOptions&& opts,
282-
UErrorCode& errorCode) {
283-
NULL_ON_ERROR(errorCode);
284-
279+
LocalPointer<FunctionValue> PersonNameFunction::call(const FunctionContext& context,
280+
FunctionValue& arg,
281+
FunctionOptions&& opts,
282+
UErrorCode& errorCode) {
285283
(void) context;
286284

287-
PersonNameValue* v = new PersonNameValue(arg, std::move(opts), errorCode);
288-
if (U_SUCCESS(errorCode) && v == nullptr) {
285+
if (U_FAILURE(errorCode)) {
286+
return LocalPointer<FunctionValue>();
287+
}
288+
LocalPointer<FunctionValue> v(new PersonNameValue(arg, std::move(opts), errorCode));
289+
if (!v.isValid()) {
289290
errorCode = U_MEMORY_ALLOCATION_ERROR;
290291
}
291292
return v;
@@ -393,16 +394,19 @@ PersonNameValue::~PersonNameValue() {}
393394
result += postfix;
394395
}
395396

396-
FunctionValue* GrammarCasesFunction::call(const FunctionContext& context,
397-
FunctionValue& arg,
398-
FunctionOptions&& opts,
399-
UErrorCode& errorCode) {
400-
NULL_ON_ERROR(errorCode);
401-
397+
LocalPointer<FunctionValue>
398+
GrammarCasesFunction::call(const FunctionContext& context,
399+
FunctionValue& arg,
400+
FunctionOptions&& opts,
401+
UErrorCode& errorCode) {
402402
(void) context;
403403

404-
GrammarCasesValue* v = new GrammarCasesValue(arg, std::move(opts), errorCode);
405-
if (U_SUCCESS(errorCode) && v == nullptr) {
404+
if (U_FAILURE(errorCode)) {
405+
return LocalPointer<FunctionValue>();
406+
}
407+
408+
LocalPointer<FunctionValue> v(new GrammarCasesValue(arg, std::move(opts), errorCode));
409+
if (!v.isValid()) {
406410
errorCode = U_MEMORY_ALLOCATION_ERROR;
407411
}
408412
return v;
@@ -519,14 +523,18 @@ GrammarCasesValue::~GrammarCasesValue() {}
519523
See ICU4J: CustomFormatterListTest.java
520524
*/
521525

522-
FunctionValue* ListFunction::call(const FunctionContext& context,
523-
FunctionValue& arg,
524-
FunctionOptions&& opts,
525-
UErrorCode& errorCode) {
526-
NULL_ON_ERROR(errorCode);
526+
LocalPointer<FunctionValue>
527+
ListFunction::call(const FunctionContext& context,
528+
FunctionValue& arg,
529+
FunctionOptions&& opts,
530+
UErrorCode& errorCode) {
531+
if (U_FAILURE(errorCode)) {
532+
return LocalPointer<FunctionValue>();
533+
}
527534

528-
ListValue* v = new ListValue(context.getLocale(), arg, std::move(opts), errorCode);
529-
if (U_SUCCESS(errorCode) && v == nullptr) {
535+
LocalPointer<FunctionValue>
536+
v(new ListValue(context.getLocale(), arg, std::move(opts), errorCode));
537+
if (!v.isValid()) {
530538
errorCode = U_MEMORY_ALLOCATION_ERROR;
531539
}
532540
return v;
@@ -690,20 +698,22 @@ static Arguments localToGlobal(const FunctionOptionsMap& opts, UErrorCode& statu
690698
return MessageArguments(result, status);
691699
}
692700

693-
FunctionValue* ResourceManager::call(const FunctionContext&,
694-
FunctionValue& arg,
695-
FunctionOptions&& options,
696-
UErrorCode& errorCode) {
697-
NULL_ON_ERROR(errorCode);
701+
LocalPointer<FunctionValue>
702+
ResourceManager::call(const FunctionContext&,
703+
FunctionValue& arg,
704+
FunctionOptions&& options,
705+
UErrorCode& errorCode) {
706+
if (U_FAILURE(errorCode)) {
707+
return LocalPointer<FunctionValue>();
708+
}
698709

699-
LocalPointer<ResourceManagerValue>
710+
LocalPointer<FunctionValue>
700711
result(new ResourceManagerValue(arg, std::move(options), errorCode));
701712

702-
if (U_SUCCESS(errorCode) && !result.isValid()) {
713+
if (!result.isValid()) {
703714
errorCode = U_MEMORY_ALLOCATION_ERROR;
704-
return nullptr;
705715
}
706-
return result.orphan();
716+
return result;
707717
}
708718

709719
UnicodeString message2::ResourceManagerValue::formatToString(UErrorCode&) const {
@@ -854,14 +864,18 @@ void TestMessageFormat2::testMessageRefFormatter(IcuTestErrorCode& errorCode) {
854864
TestUtils::runTestCase(*this, test, errorCode);
855865
}
856866

857-
FunctionValue* NounFunction::call(const FunctionContext&,
858-
FunctionValue& arg,
859-
FunctionOptions&& opts,
860-
UErrorCode& errorCode) {
861-
NULL_ON_ERROR(errorCode);
867+
LocalPointer<FunctionValue>
868+
NounFunction::call(const FunctionContext&,
869+
FunctionValue& arg,
870+
FunctionOptions&& opts,
871+
UErrorCode& errorCode) {
872+
if (U_FAILURE(errorCode)) {
873+
return LocalPointer<FunctionValue>();
874+
}
862875

863-
NounValue* v = new NounValue(arg, std::move(opts), errorCode);
864-
if (U_SUCCESS(errorCode) && v == nullptr) {
876+
LocalPointer<FunctionValue>
877+
v(new NounValue(arg, std::move(opts), errorCode));
878+
if (!v.isValid()) {
865879
errorCode = U_MEMORY_ALLOCATION_ERROR;
866880
}
867881
return v;
@@ -910,14 +924,18 @@ NounValue::NounValue(FunctionValue& arg,
910924
}
911925
}
912926

913-
FunctionValue* AdjectiveFunction::call(const FunctionContext&,
914-
FunctionValue& arg,
915-
FunctionOptions&& opts,
916-
UErrorCode& errorCode) {
917-
NULL_ON_ERROR(errorCode);
927+
LocalPointer<FunctionValue>
928+
AdjectiveFunction::call(const FunctionContext&,
929+
FunctionValue& arg,
930+
FunctionOptions&& opts,
931+
UErrorCode& errorCode) {
932+
if (U_FAILURE(errorCode)) {
933+
return LocalPointer<FunctionValue>();
934+
}
918935

919-
AdjectiveValue* v = new AdjectiveValue(arg, std::move(opts), errorCode);
920-
if (U_SUCCESS(errorCode) && v == nullptr) {
936+
LocalPointer<FunctionValue>
937+
v(new AdjectiveValue(arg, std::move(opts), errorCode));
938+
if (!v.isValid()) {
921939
errorCode = U_MEMORY_ALLOCATION_ERROR;
922940
}
923941
return v;

0 commit comments

Comments
 (0)