Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions packages/pigeon/lib/cpp_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -819,7 +819,11 @@ class CppSourceGenerator extends StructuredGenerator<CppOptions> {
} else {
final HostDatatype hostDatatype =
getFieldHostDatatype(field, _shortBaseCppTypeForBuiltinDartType);
return 'std::get<${hostDatatype.datatype}>($encodable)';
if (field.type.isClass) {
return _classReferenceFromEncodableValue(hostDatatype, encodable);
} else {
return 'std::get<${hostDatatype.datatype}>($encodable)';
}
}
}

Expand Down Expand Up @@ -1531,7 +1535,7 @@ ${prefix}reply(EncodableValue(std::move(wrapped)));''';
}
} else {
indent.writeln(
'const auto* $argName = &(std::any_cast<const ${hostType.datatype}&>(std::get<CustomEncodableValue>($encodableArgName)));');
'const auto* $argName = &(${_classReferenceFromEncodableValue(hostType, encodableArgName)});');
}
} else {
// Non-nullable arguments are either passed by value or reference, but the
Expand All @@ -1556,7 +1560,7 @@ ${prefix}reply(EncodableValue(std::move(wrapped)));''';
'const ${hostType.datatype}& $argName = (${hostType.datatype})$encodableArgName.LongValue();');
} else {
indent.writeln(
'const auto& $argName = std::any_cast<const ${hostType.datatype}&>(std::get<CustomEncodableValue>($encodableArgName));');
'const auto& $argName = ${_classReferenceFromEncodableValue(hostType, encodableArgName)};');
}
}
}
Expand All @@ -1567,6 +1571,13 @@ ${prefix}reply(EncodableValue(std::move(wrapped)));''';
String? _shortBaseCppTypeForBuiltinDartType(TypeDeclaration type) {
return _baseCppTypeForBuiltinDartType(type, includeFlutterNamespace: false);
}

/// Returns the code to extract a `const {type.datatype}&` from an EncodableValue
/// variable [variableName] that contains an instance of [type].
String _classReferenceFromEncodableValue(
HostDatatype type, String variableName) {
return 'std::any_cast<const ${type.datatype}&>(std::get<CustomEncodableValue>($variableName))';
}
}

/// Contains information about a host function argument.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -689,8 +689,8 @@ AllNullableTypes AllNullableTypes::FromEncodableList(
}
auto& encodable_all_nullable_types = list[16];
if (!encodable_all_nullable_types.IsNull()) {
decoded.set_all_nullable_types(
std::get<AllNullableTypes>(encodable_all_nullable_types));
decoded.set_all_nullable_types(std::any_cast<const AllNullableTypes&>(
std::get<CustomEncodableValue>(encodable_all_nullable_types)));
}
return decoded;
}
Expand Down Expand Up @@ -1236,16 +1236,19 @@ EncodableList AllClassesWrapper::ToEncodableList() const {

AllClassesWrapper AllClassesWrapper::FromEncodableList(
const EncodableList& list) {
AllClassesWrapper decoded(std::get<AllNullableTypes>(list[0]));
AllClassesWrapper decoded(std::any_cast<const AllNullableTypes&>(
std::get<CustomEncodableValue>(list[0])));
auto& encodable_all_nullable_types_without_recursion = list[1];
if (!encodable_all_nullable_types_without_recursion.IsNull()) {
decoded.set_all_nullable_types_without_recursion(
std::get<AllNullableTypesWithoutRecursion>(
encodable_all_nullable_types_without_recursion));
std::any_cast<const AllNullableTypesWithoutRecursion&>(
std::get<CustomEncodableValue>(
encodable_all_nullable_types_without_recursion)));
}
auto& encodable_all_types = list[2];
if (!encodable_all_types.IsNull()) {
decoded.set_all_types(std::get<AllTypes>(encodable_all_types));
decoded.set_all_types(std::any_cast<const AllTypes&>(
std::get<CustomEncodableValue>(encodable_all_types)));
}
return decoded;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace null_fields_pigeontest {

namespace {

using flutter::CustomEncodableValue;
using flutter::EncodableList;
using flutter::EncodableMap;
using flutter::EncodableValue;
Expand Down Expand Up @@ -113,6 +114,8 @@ TEST_F(NullFieldsTest, RequestFromListWithNulls) {
}

TEST_F(NullFieldsTest, ReplyFromListWithValues) {
NullFieldsSearchRequest request(1);
request.set_query("hello");
EncodableList list{
EncodableValue("result"),
EncodableValue("error"),
Expand All @@ -121,10 +124,7 @@ TEST_F(NullFieldsTest, ReplyFromListWithValues) {
EncodableValue(2),
EncodableValue(3),
}),
EncodableValue(EncodableList{
EncodableValue("hello"),
EncodableValue(1),
}),
CustomEncodableValue(request),
EncodableValue(0),
};
NullFieldsSearchReply reply = ReplyFromList(list);
Expand Down Expand Up @@ -194,10 +194,11 @@ TEST_F(NullFieldsTest, ReplyToMapWithValues) {
EXPECT_EQ(indices[0].LongValue(), 1L);
EXPECT_EQ(indices[1].LongValue(), 2L);
EXPECT_EQ(indices[2].LongValue(), 3L);
const EncodableList& request_list =
*ExpectAndGetIndex<EncodableList>(list, 3);
EXPECT_EQ(*ExpectAndGetIndex<std::string>(request_list, 0), "hello");
EXPECT_EQ(*ExpectAndGetIndex<int64_t>(request_list, 1), 1);
const NullFieldsSearchRequest& request_from_list =
std::any_cast<const NullFieldsSearchRequest&>(
*ExpectAndGetIndex<CustomEncodableValue>(list, 3));
EXPECT_EQ(*request_from_list.query(), "hello");
EXPECT_EQ(request_from_list.identifier(), 1);
}

TEST_F(NullFieldsTest, ReplyToListWithNulls) {
Expand Down
6 changes: 3 additions & 3 deletions packages/pigeon/test/cpp_generator_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1638,14 +1638,14 @@ void main() {
dartPackageName: DEFAULT_PACKAGE_NAME,
);
final String code = sink.toString();
// Standard types are wrapped an EncodableValues.
// Standard types are wrapped in EncodableValues.
expect(code, contains('EncodableValue(a_bool_arg)'));
expect(code, contains('EncodableValue(an_int_arg)'));
expect(code, contains('EncodableValue(a_string_arg)'));
expect(code, contains('EncodableValue(a_list_arg)'));
expect(code, contains('EncodableValue(a_map_arg)'));
// Class types use ToEncodableList.
expect(code, contains('CustomEncodableValue(*an_object_arg)'));
// Class types are wrapped in CustomEncodableValues.
expect(code, contains('CustomEncodableValue(an_object_arg)'));
}
});

Expand Down