writeDataClass method
void
writeDataClass(
- CppOptions generatorOptions,
- Root root,
- Indent indent,
- Class klass, {
- required String dartPackageName,
override
Writes a single data class to indent
.
Implementation
@override
void writeDataClass(
CppOptions generatorOptions,
Root root,
Indent indent,
Class klass, {
required String dartPackageName,
}) {
// When generating for a Pigeon unit test, add a test fixture friend class to
// allow unit testing private methods, since testing serialization via public
// methods is essentially an end-to-end test.
String? testFixtureClass;
if (generatorOptions.namespace?.endsWith('_pigeontest') ?? false) {
testFixtureClass =
'${_pascalCaseFromSnakeCase(generatorOptions.namespace!.replaceAll('_pigeontest', ''))}Test';
}
indent.newln();
const List<String> generatedMessages = <String>[
' Generated class from Pigeon that represents data sent in messages.'
];
addDocumentationComments(
indent, klass.documentationComments, _docCommentSpec,
generatorComments: generatedMessages);
final Iterable<NamedType> orderedFields =
getFieldsInSerializationOrder(klass);
indent.write('class ${klass.name} ');
indent.addScoped('{', '};', () {
_writeAccessBlock(indent, _ClassAccess.public, () {
final Iterable<NamedType> requiredFields =
orderedFields.where((NamedType type) => !type.type.isNullable);
// Minimal constructor, if needed.
if (requiredFields.length != orderedFields.length) {
_writeClassConstructor(root, indent, klass, requiredFields,
'Constructs an object setting all non-nullable fields.');
}
// All-field constructor.
_writeClassConstructor(root, indent, klass, orderedFields,
'Constructs an object setting all fields.');
for (final NamedType field in orderedFields) {
addDocumentationComments(
indent, field.documentationComments, _docCommentSpec);
final HostDatatype baseDatatype = getFieldHostDatatype(
field, root.classes, root.enums, _baseCppTypeForBuiltinDartType);
// Declare a getter and setter.
_writeFunctionDeclaration(indent, _makeGetterName(field),
returnType: _getterReturnType(baseDatatype), isConst: true);
final String setterName = _makeSetterName(field);
_writeFunctionDeclaration(indent, setterName,
returnType: _voidType,
parameters: <String>[
'${_unownedArgumentType(baseDatatype)} value_arg'
]);
if (field.type.isNullable) {
// Add a second setter that takes the non-nullable version of the
// argument for convenience, since setting literal values with the
// pointer version is non-trivial.
final HostDatatype nonNullType = _nonNullableType(baseDatatype);
_writeFunctionDeclaration(indent, setterName,
returnType: _voidType,
parameters: <String>[
'${_unownedArgumentType(nonNullType)} value_arg'
]);
}
indent.newln();
}
});
_writeAccessBlock(indent, _ClassAccess.private, () {
_writeFunctionDeclaration(indent, 'FromEncodableList',
returnType: klass.name,
parameters: <String>['const flutter::EncodableList& list'],
isStatic: true);
_writeFunctionDeclaration(indent, 'ToEncodableList',
returnType: 'flutter::EncodableList', isConst: true);
for (final Class friend in root.classes) {
if (friend != klass &&
friend.fields.any(
(NamedType element) => element.type.baseName == klass.name)) {
indent.writeln('friend class ${friend.name};');
}
}
for (final Api api in root.apis) {
// TODO(gaaclarke): Find a way to be more precise with our
// friendships.
indent.writeln('friend class ${api.name};');
indent.writeln('friend class ${_getCodecSerializerName(api)};');
}
if (testFixtureClass != null) {
indent.writeln('friend class $testFixtureClass;');
}
for (final NamedType field in orderedFields) {
final HostDatatype hostDatatype = getFieldHostDatatype(
field, root.classes, root.enums, _baseCppTypeForBuiltinDartType);
indent.writeln(
'${_valueType(hostDatatype)} ${_makeInstanceVariableName(field)};');
}
});
}, nestCount: 0);
indent.newln();
}