Skip to content

Commit c199c1c

Browse files
committed
Make Function::call() return a LocalPointer
1 parent c100e35 commit c199c1c

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
@@ -248,17 +248,19 @@ StandardFunctions::Number::create(bool isInteger, UErrorCode& success) {
248248
return result.orphan();
249249
}
250250

251-
FunctionValue* StandardFunctions::Number::call(const FunctionContext& context,
251+
LocalPointer<FunctionValue> StandardFunctions::Number::call(const FunctionContext& context,
252252
FunctionValue& operand,
253253
FunctionOptions&& options,
254254
UErrorCode& errorCode) {
255-
LocalPointer<NumberValue>
255+
if (U_FAILURE(errorCode)) {
256+
return LocalPointer<FunctionValue>();
257+
}
258+
LocalPointer<FunctionValue>
256259
val(new NumberValue(*this, context, operand, std::move(options), errorCode));
257-
if (val.isValid()) {
258-
return val.orphan();
260+
if (!val.isValid()) {
261+
errorCode = U_MEMORY_ALLOCATION_ERROR;
259262
}
260-
errorCode = U_MEMORY_ALLOCATION_ERROR;
261-
return nullptr;
263+
return val;
262264
}
263265

264266
/* static */ number::LocalizedNumberFormatter StandardFunctions::formatterForOptions(const Number& number,
@@ -719,15 +721,17 @@ StandardFunctions::DateTime::create(DateTime::DateTimeType type,
719721
return result.orphan();
720722
}
721723

722-
FunctionValue*
724+
LocalPointer<FunctionValue>
723725
StandardFunctions::DateTime::call(const FunctionContext& context,
724726
FunctionValue& val,
725727
FunctionOptions&& opts,
726728
UErrorCode& errorCode) {
727-
NULL_ON_ERROR(errorCode);
728-
729-
auto result = new DateTimeValue(type, context, val, std::move(opts), errorCode);
730-
if (result == nullptr) {
729+
if (U_FAILURE(errorCode)) {
730+
return LocalPointer<FunctionValue>();
731+
}
732+
LocalPointer<FunctionValue>
733+
result(new DateTimeValue(type, context, val, std::move(opts), errorCode));
734+
if (!result.isValid()) {
731735
errorCode = U_MEMORY_ALLOCATION_ERROR;
732736
}
733737
return result;
@@ -1039,12 +1043,20 @@ extern UnicodeString formattableToString(const Locale&,
10391043
const Formattable&,
10401044
UErrorCode&);
10411045

1042-
FunctionValue*
1046+
LocalPointer<FunctionValue>
10431047
StandardFunctions::String::call(const FunctionContext& context,
10441048
FunctionValue& val,
10451049
FunctionOptions&& opts,
10461050
UErrorCode& errorCode) {
1047-
return new StringValue(context, val, std::move(opts), errorCode);
1051+
if (U_FAILURE(errorCode)) {
1052+
return LocalPointer<FunctionValue>();
1053+
}
1054+
LocalPointer<FunctionValue>
1055+
result(new StringValue(context, val, std::move(opts), errorCode));
1056+
if (!result.isValid()) {
1057+
errorCode = U_MEMORY_ALLOCATION_ERROR;
1058+
}
1059+
return result;
10481060
}
10491061

10501062
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
@@ -40,7 +40,7 @@ namespace message2 {
4040
static DateTime* time(UErrorCode&);
4141
static DateTime* dateTime(UErrorCode&);
4242

43-
FunctionValue* call(const FunctionContext& context,
43+
LocalPointer<FunctionValue> call(const FunctionContext& context,
4444
FunctionValue& operand,
4545
FunctionOptions&& options,
4646
UErrorCode& errorCode) override;
@@ -69,7 +69,7 @@ namespace message2 {
6969
static Number* integer(UErrorCode& success);
7070
static Number* number( UErrorCode& success);
7171

72-
FunctionValue* call(const FunctionContext& context,
72+
LocalPointer<FunctionValue> call(const FunctionContext& context,
7373
FunctionValue& operand,
7474
FunctionOptions&& options,
7575
UErrorCode& errorCode) override;
@@ -147,7 +147,7 @@ namespace message2 {
147147

148148
class String : public Function {
149149
public:
150-
FunctionValue* call(const FunctionContext& context,
150+
LocalPointer<FunctionValue> call(const FunctionContext& context,
151151
FunctionValue& val,
152152
FunctionOptions&& opts,
153153
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
@@ -289,20 +289,22 @@ namespace message2 {
289289
public:
290290
/**
291291
* Calls this Function on a FunctionValue operand and its FunctionOptions options,
292-
* returning a new pointer to a FunctionValue (which is adopted by the caller).
292+
* returning a LocalPointer to a FunctionValue.
293293
*
294294
* @param context The context of this function, based on its contextual options
295295
* @param operand The unnamed argument to the function.
296296
* @param options Resolved options for this function.
297297
* @param status Input/output error code
298+
* @return The function value that is the result of calling this function on
299+
* the arguments.
298300
*
299301
* @internal ICU 77 technology preview
300302
* @deprecated This API is for technology preview only.
301303
*/
302-
virtual FunctionValue* call(const FunctionContext& context,
303-
FunctionValue& operand,
304-
FunctionOptions&& options,
305-
UErrorCode& status) = 0;
304+
virtual LocalPointer<FunctionValue> call(const FunctionContext& context,
305+
FunctionValue& operand,
306+
FunctionOptions&& options,
307+
UErrorCode& status) = 0;
306308
/**
307309
* Destructor.
308310
*

icu4c/source/test/intltest/messageformat2test.h

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

115115
class PersonNameFunction : public Function {
116116
public:
117-
FunctionValue* call(const FunctionContext&, FunctionValue&, FunctionOptions&&, UErrorCode&) override;
117+
LocalPointer<FunctionValue> call(const FunctionContext&, FunctionValue&, FunctionOptions&&, UErrorCode&) override;
118118
virtual ~PersonNameFunction();
119119
PersonNameFunction() {}
120120
};
@@ -145,7 +145,7 @@ class FormattableProperties : public FormattableObject {
145145

146146
class GrammarCasesFunction : public Function {
147147
public:
148-
FunctionValue* call(const FunctionContext&, FunctionValue&, FunctionOptions&&, UErrorCode&) override;
148+
LocalPointer<FunctionValue> call(const FunctionContext&, FunctionValue&, FunctionOptions&&, UErrorCode&) override;
149149
static MFFunctionRegistry customRegistry(UErrorCode&);
150150
};
151151

@@ -164,7 +164,7 @@ class GrammarCasesValue : public FunctionValue {
164164

165165
class ListFunction : public Function {
166166
public:
167-
FunctionValue* call(const FunctionContext&, FunctionValue&, FunctionOptions&&, UErrorCode&) override;
167+
LocalPointer<FunctionValue> call(const FunctionContext&, FunctionValue&, FunctionOptions&&, UErrorCode&) override;
168168
static MFFunctionRegistry customRegistry(UErrorCode&);
169169
ListFunction() {}
170170
virtual ~ListFunction();
@@ -215,7 +215,7 @@ class AdjectiveValue : public FunctionValue {
215215

216216
class ResourceManager : public Function {
217217
public:
218-
FunctionValue* call(const FunctionContext&, FunctionValue&, FunctionOptions&&, UErrorCode&) override;
218+
LocalPointer<FunctionValue> call(const FunctionContext&, FunctionValue&, FunctionOptions&&, UErrorCode&) override;
219219
static MFFunctionRegistry customRegistry(UErrorCode&);
220220
static Hashtable* properties(UErrorCode&);
221221
static UnicodeString propertiesAsString(const Hashtable&);
@@ -240,14 +240,14 @@ class ResourceManagerValue : public FunctionValue {
240240

241241
class NounFunction : public Function {
242242
public:
243-
FunctionValue* call(const FunctionContext&, FunctionValue&, FunctionOptions&&, UErrorCode&) override;
243+
LocalPointer<FunctionValue> call(const FunctionContext&, FunctionValue&, FunctionOptions&&, UErrorCode&) override;
244244
NounFunction() { }
245245
virtual ~NounFunction();
246246
};
247247

248248
class AdjectiveFunction : public Function {
249249
public:
250-
FunctionValue* call(const FunctionContext&, FunctionValue&, FunctionOptions&&, UErrorCode&) override;
250+
LocalPointer<FunctionValue> call(const FunctionContext&, FunctionValue&, FunctionOptions&&, UErrorCode&) override;
251251
AdjectiveFunction() { }
252252
virtual ~AdjectiveFunction();
253253
};

icu4c/source/test/intltest/messageformat2test_custom.cpp

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

276-
FunctionValue* PersonNameFunction::call(const FunctionContext& context,
277-
FunctionValue& arg,
278-
FunctionOptions&& opts,
279-
UErrorCode& errorCode) {
280-
NULL_ON_ERROR(errorCode);
281-
276+
LocalPointer<FunctionValue> PersonNameFunction::call(const FunctionContext& context,
277+
FunctionValue& arg,
278+
FunctionOptions&& opts,
279+
UErrorCode& errorCode) {
282280
(void) context;
283281

284-
PersonNameValue* v = new PersonNameValue(arg, std::move(opts), errorCode);
285-
if (U_SUCCESS(errorCode) && v == nullptr) {
282+
if (U_FAILURE(errorCode)) {
283+
return LocalPointer<FunctionValue>();
284+
}
285+
LocalPointer<FunctionValue> v(new PersonNameValue(arg, std::move(opts), errorCode));
286+
if (!v.isValid()) {
286287
errorCode = U_MEMORY_ALLOCATION_ERROR;
287288
}
288289
return v;
@@ -390,16 +391,19 @@ PersonNameValue::~PersonNameValue() {}
390391
result += postfix;
391392
}
392393

393-
FunctionValue* GrammarCasesFunction::call(const FunctionContext& context,
394-
FunctionValue& arg,
395-
FunctionOptions&& opts,
396-
UErrorCode& errorCode) {
397-
NULL_ON_ERROR(errorCode);
398-
394+
LocalPointer<FunctionValue>
395+
GrammarCasesFunction::call(const FunctionContext& context,
396+
FunctionValue& arg,
397+
FunctionOptions&& opts,
398+
UErrorCode& errorCode) {
399399
(void) context;
400400

401-
GrammarCasesValue* v = new GrammarCasesValue(arg, std::move(opts), errorCode);
402-
if (U_SUCCESS(errorCode) && v == nullptr) {
401+
if (U_FAILURE(errorCode)) {
402+
return LocalPointer<FunctionValue>();
403+
}
404+
405+
LocalPointer<FunctionValue> v(new GrammarCasesValue(arg, std::move(opts), errorCode));
406+
if (!v.isValid()) {
403407
errorCode = U_MEMORY_ALLOCATION_ERROR;
404408
}
405409
return v;
@@ -516,14 +520,18 @@ GrammarCasesValue::~GrammarCasesValue() {}
516520
See ICU4J: CustomFormatterListTest.java
517521
*/
518522

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

525-
ListValue* v = new ListValue(context.getLocale(), arg, std::move(opts), errorCode);
526-
if (U_SUCCESS(errorCode) && v == nullptr) {
532+
LocalPointer<FunctionValue>
533+
v(new ListValue(context.getLocale(), arg, std::move(opts), errorCode));
534+
if (!v.isValid()) {
527535
errorCode = U_MEMORY_ALLOCATION_ERROR;
528536
}
529537
return v;
@@ -687,20 +695,22 @@ static Arguments localToGlobal(const FunctionOptionsMap& opts, UErrorCode& statu
687695
return MessageArguments(result, status);
688696
}
689697

690-
FunctionValue* ResourceManager::call(const FunctionContext&,
691-
FunctionValue& arg,
692-
FunctionOptions&& options,
693-
UErrorCode& errorCode) {
694-
NULL_ON_ERROR(errorCode);
698+
LocalPointer<FunctionValue>
699+
ResourceManager::call(const FunctionContext&,
700+
FunctionValue& arg,
701+
FunctionOptions&& options,
702+
UErrorCode& errorCode) {
703+
if (U_FAILURE(errorCode)) {
704+
return LocalPointer<FunctionValue>();
705+
}
695706

696-
LocalPointer<ResourceManagerValue>
707+
LocalPointer<FunctionValue>
697708
result(new ResourceManagerValue(arg, std::move(options), errorCode));
698709

699-
if (U_SUCCESS(errorCode) && !result.isValid()) {
710+
if (!result.isValid()) {
700711
errorCode = U_MEMORY_ALLOCATION_ERROR;
701-
return nullptr;
702712
}
703-
return result.orphan();
713+
return result;
704714
}
705715

706716
UnicodeString message2::ResourceManagerValue::formatToString(UErrorCode&) const {
@@ -851,14 +861,18 @@ void TestMessageFormat2::testMessageRefFormatter(IcuTestErrorCode& errorCode) {
851861
TestUtils::runTestCase(*this, test, errorCode);
852862
}
853863

854-
FunctionValue* NounFunction::call(const FunctionContext&,
855-
FunctionValue& arg,
856-
FunctionOptions&& opts,
857-
UErrorCode& errorCode) {
858-
NULL_ON_ERROR(errorCode);
864+
LocalPointer<FunctionValue>
865+
NounFunction::call(const FunctionContext&,
866+
FunctionValue& arg,
867+
FunctionOptions&& opts,
868+
UErrorCode& errorCode) {
869+
if (U_FAILURE(errorCode)) {
870+
return LocalPointer<FunctionValue>();
871+
}
859872

860-
NounValue* v = new NounValue(arg, std::move(opts), errorCode);
861-
if (U_SUCCESS(errorCode) && v == nullptr) {
873+
LocalPointer<FunctionValue>
874+
v(new NounValue(arg, std::move(opts), errorCode));
875+
if (!v.isValid()) {
862876
errorCode = U_MEMORY_ALLOCATION_ERROR;
863877
}
864878
return v;
@@ -907,14 +921,18 @@ NounValue::NounValue(FunctionValue& arg,
907921
}
908922
}
909923

910-
FunctionValue* AdjectiveFunction::call(const FunctionContext&,
911-
FunctionValue& arg,
912-
FunctionOptions&& opts,
913-
UErrorCode& errorCode) {
914-
NULL_ON_ERROR(errorCode);
924+
LocalPointer<FunctionValue>
925+
AdjectiveFunction::call(const FunctionContext&,
926+
FunctionValue& arg,
927+
FunctionOptions&& opts,
928+
UErrorCode& errorCode) {
929+
if (U_FAILURE(errorCode)) {
930+
return LocalPointer<FunctionValue>();
931+
}
915932

916-
AdjectiveValue* v = new AdjectiveValue(arg, std::move(opts), errorCode);
917-
if (U_SUCCESS(errorCode) && v == nullptr) {
933+
LocalPointer<FunctionValue>
934+
v(new AdjectiveValue(arg, std::move(opts), errorCode));
935+
if (!v.isValid()) {
918936
errorCode = U_MEMORY_ALLOCATION_ERROR;
919937
}
920938
return v;

0 commit comments

Comments
 (0)