writeDataClass method

  1. @override
void writeDataClass(
  1. CppOptions generatorOptions,
  2. Root root,
  3. Indent indent,
  4. Class classDefinition, {
  5. required String dartPackageName,
})
override

Writes a single data class to indent.

Implementation

@override
void writeDataClass(
  CppOptions generatorOptions,
  Root root,
  Indent indent,
  Class classDefinition, {
  required String dartPackageName,
}) {
  indent.writeln('$_commentPrefix ${classDefinition.name}');
  indent.newln();

  final Iterable<NamedType> orderedFields =
      getFieldsInSerializationOrder(classDefinition);
  final Iterable<NamedType> requiredFields =
      orderedFields.where((NamedType type) => !type.type.isNullable);
  // Minimal constructor, if needed.
  if (requiredFields.length != orderedFields.length) {
    _writeClassConstructor(root, indent, classDefinition, requiredFields);
  }
  // All-field constructor.
  _writeClassConstructor(root, indent, classDefinition, orderedFields);

  // Custom copy/assign to handle pointer fields, if necessary.
  if (orderedFields.any((NamedType field) => _isPointerField(
      getFieldHostDatatype(field, _baseCppTypeForBuiltinDartType)))) {
    _writeCopyConstructor(root, indent, classDefinition, orderedFields);
    _writeAssignmentOperator(root, indent, classDefinition, orderedFields);
  }

  // Getters and setters.
  for (final NamedType field in orderedFields) {
    _writeCppSourceClassField(
        generatorOptions, root, indent, classDefinition, field);
  }

  // Serialization.
  writeClassEncode(
    generatorOptions,
    root,
    indent,
    classDefinition,
    dartPackageName: dartPackageName,
  );

  // Deserialization.
  writeClassDecode(
    generatorOptions,
    root,
    indent,
    classDefinition,
    dartPackageName: dartPackageName,
  );
}