writeHostApi method

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

Writes a single Host Api to indent.

Implementation

@override
void writeHostApi(
  CppOptions generatorOptions,
  Root root,
  Indent indent,
  AstHostApi api, {
  required String dartPackageName,
}) {
  if (getCodecClasses(api, root).isNotEmpty) {
    _writeCodec(generatorOptions, root, indent, api);
  }
  const List<String> generatedMessages = <String>[
    ' Generated interface from Pigeon that represents a handler of messages from Flutter.'
  ];
  addDocumentationComments(indent, api.documentationComments, _docCommentSpec,
      generatorComments: generatedMessages);
  indent.write('class ${api.name} ');
  indent.addScoped('{', '};', () {
    _writeAccessBlock(indent, _ClassAccess.public, () {
      // Prevent copying/assigning.
      _writeFunctionDeclaration(indent, api.name,
          parameters: <String>['const ${api.name}&'], deleted: true);
      _writeFunctionDeclaration(indent, 'operator=',
          returnType: '${api.name}&',
          parameters: <String>['const ${api.name}&'],
          deleted: true);
      // No-op virtual destructor.
      _writeFunctionDeclaration(indent, '~${api.name}',
          isVirtual: true, inlineNoop: true);
      for (final Method method in api.methods) {
        final HostDatatype returnType = getHostDatatype(
            method.returnType, _baseCppTypeForBuiltinDartType);
        final String returnTypeName = _hostApiReturnType(returnType);

        final List<String> parameters = <String>[];
        if (method.parameters.isNotEmpty) {
          final Iterable<String> argTypes =
              method.parameters.map((NamedType arg) {
            final HostDatatype hostType =
                getFieldHostDatatype(arg, _baseCppTypeForBuiltinDartType);
            return _hostApiArgumentType(hostType);
          });
          final Iterable<String> argNames =
              method.parameters.map((NamedType e) => _makeVariableName(e));
          parameters.addAll(
              map2(argTypes, argNames, (String argType, String argName) {
            return '$argType $argName';
          }));
        }

        addDocumentationComments(
            indent, method.documentationComments, _docCommentSpec);
        final String methodReturn;
        if (method.isAsynchronous) {
          methodReturn = _voidType;
          parameters.add('std::function<void($returnTypeName reply)> result');
        } else {
          methodReturn = returnTypeName;
        }
        _writeFunctionDeclaration(indent, _makeMethodName(method),
            returnType: methodReturn,
            parameters: parameters,
            isVirtual: true,
            isPureVirtual: true);
      }
      indent.newln();
      indent.writeln('$_commentPrefix The codec used by ${api.name}.');
      _writeFunctionDeclaration(indent, 'GetCodec',
          returnType: 'const flutter::StandardMessageCodec&', isStatic: true);
      indent.writeln(
          '$_commentPrefix Sets up an instance of `${api.name}` to handle messages through the `binary_messenger`.');
      _writeFunctionDeclaration(indent, 'SetUp',
          returnType: _voidType,
          isStatic: true,
          parameters: <String>[
            'flutter::BinaryMessenger* binary_messenger',
            '${api.name}* api',
          ]);
      _writeFunctionDeclaration(indent, 'SetUp',
          returnType: _voidType,
          isStatic: true,
          parameters: <String>[
            'flutter::BinaryMessenger* binary_messenger',
            '${api.name}* api',
            'const std::string& message_channel_suffix',
          ]);
      _writeFunctionDeclaration(indent, 'WrapError',
          returnType: 'flutter::EncodableValue',
          isStatic: true,
          parameters: <String>['std::string_view error_message']);
      _writeFunctionDeclaration(indent, 'WrapError',
          returnType: 'flutter::EncodableValue',
          isStatic: true,
          parameters: <String>['const FlutterError& error']);
    });
    _writeAccessBlock(indent, _ClassAccess.protected, () {
      indent.writeln('${api.name}() = default;');
    });
  }, nestCount: 0);
}