writeGeneralCodec method

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

Writes the custom codec to indent.

Implementation

@override
void writeGeneralCodec(
  CppOptions generatorOptions,
  Root root,
  Indent indent, {
  required String dartPackageName,
}) {
  final List<EnumeratedType> enumeratedTypes =
      getEnumeratedTypes(root).toList();
  indent.newln();
  if (root.requiresOverflowClass) {
    _writeCodecOverflowUtilities(
        generatorOptions, root, indent, enumeratedTypes,
        dartPackageName: dartPackageName);
  }
  _writeFunctionDefinition(indent, _codecSerializerName,
      scope: _codecSerializerName);
  _writeFunctionDefinition(indent, 'ReadValueOfType',
      scope: _codecSerializerName,
      returnType: 'EncodableValue',
      parameters: <String>[
        'uint8_t type',
        'flutter::ByteStreamReader* stream',
      ],
      isConst: true, body: () {
    if (enumeratedTypes.isNotEmpty) {
      indent.writeln('switch (type) {');
      indent.inc();
      for (final EnumeratedType customType in enumeratedTypes) {
        if (customType.enumeration < maximumCodecFieldKey) {
          indent.write('case ${customType.enumeration}: ');
          indent.nest(1, () {
            _writeCodecDecode(indent, customType, 'ReadValue(stream)');
          });
        }
      }
      if (root.requiresOverflowClass) {
        indent.write('case $maximumCodecFieldKey:');
        _writeCodecDecode(indent, _enumeratedOverflow, 'ReadValue(stream)');
      }
      indent.writeln('default:');
      indent.inc();
    }
    indent.writeln(
        'return $_standardCodecSerializer::ReadValueOfType(type, stream);');
    if (enumeratedTypes.isNotEmpty) {
      indent.dec();
      indent.writeln('}');
      indent.dec();
    }
  });
  _writeFunctionDefinition(indent, 'WriteValue',
      scope: _codecSerializerName,
      returnType: _voidType,
      parameters: <String>[
        'const EncodableValue& value',
        'flutter::ByteStreamWriter* stream',
      ],
      isConst: true, body: () {
    if (enumeratedTypes.isNotEmpty) {
      indent.write(
          'if (const CustomEncodableValue* custom_value = std::get_if<CustomEncodableValue>(&value)) ');
      indent.addScoped('{', '}', () {
        for (final EnumeratedType customType in enumeratedTypes) {
          final String encodeString = customType.type ==
                  CustomTypes.customClass
              ? 'std::any_cast<${customType.name}>(*custom_value).ToEncodableList()'
              : 'static_cast<int>(std::any_cast<${customType.name}>(*custom_value))';
          final String valueString =
              customType.enumeration < maximumCodecFieldKey
                  ? encodeString
                  : 'wrap.ToEncodableList()';
          final int enumeration =
              customType.enumeration < maximumCodecFieldKey
                  ? customType.enumeration
                  : maximumCodecFieldKey;
          indent.write(
              'if (custom_value->type() == typeid(${customType.name})) ');
          indent.addScoped('{', '}', () {
            indent.writeln('stream->WriteByte($enumeration);');
            if (enumeration == maximumCodecFieldKey) {
              indent.writeln(
                  'const auto wrap = $_overflowClassName(${customType.enumeration - maximumCodecFieldKey}, $encodeString);');
            }
            indent
                .writeln('WriteValue(EncodableValue($valueString), stream);');
            indent.writeln('return;');
          });
        }
      });
    }
    indent.writeln('$_standardCodecSerializer::WriteValue(value, stream);');
  });
}