Skip to content

Commit

Permalink
Start writing C code
Browse files Browse the repository at this point in the history
  • Loading branch information
robert-ancell committed Dec 11, 2023
1 parent 9668eb1 commit 8378c7d
Showing 1 changed file with 61 additions and 7 deletions.
68 changes: 61 additions & 7 deletions packages/pigeon/lib/linux_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ class LinuxHeaderGenerator extends StructuredGenerator<LinuxOptions> {
indent.newln();
indent.writeln(
'G_DECLARE_FINAL_TYPE(${className}, ${methodPrefix}, ${upperNamespace}, ${upperSnakeClassName}, GObject)');

indent.newln();
var constructorArgs = <String>[];
for (var field in classDefinition.fields) {
Expand Down Expand Up @@ -363,7 +364,6 @@ class LinuxSourceGenerator extends StructuredGenerator<LinuxOptions> {
}
indent.writeln('$_commentPrefix ${getGeneratedCodeWarning()}');
indent.writeln('$_commentPrefix $seeAlsoWarning');
indent.newln();
}

@override
Expand All @@ -373,12 +373,8 @@ class LinuxSourceGenerator extends StructuredGenerator<LinuxOptions> {
Indent indent, {
required String dartPackageName,
}) {
indent.writeln('#include "${generatorOptions.headerIncludePath}"');
indent.newln();
_writeSystemHeaderIncludeBlock(indent, <String>[
'flutter_linux/flutter_linux.h',
]);
indent.newln();
indent.writeln('#include "${generatorOptions.headerIncludePath}"');
}

@override
Expand All @@ -404,7 +400,65 @@ class LinuxSourceGenerator extends StructuredGenerator<LinuxOptions> {
Indent indent,
Class classDefinition, {
required String dartPackageName,
}) {}
}) {
var namespace = 'My';
var snakeNamespace = _snakeCaseFromCamelCase(namespace);
var upperNamespace = namespace.toUpperCase();
var className = '${namespace}${classDefinition.name}';
var snakeClassName = _snakeCaseFromCamelCase(classDefinition.name);

var methodPrefix = '${snakeNamespace}_${snakeClassName}';
var castMacro = methodPrefix.toUpperCase();

indent.newln();
indent.addScoped('struct _${className} {', '};', () {
indent.writeln('GObject parent_instance;');
indent.newln();

for (var field in classDefinition.fields) {
var fieldName = _snakeCaseFromCamelCase(field.name);
var fieldType = _getType(field.type, isOutput: true);
indent.writeln('${fieldType} ${fieldName};');
}
});

indent.newln();
indent
.writeln('G_DEFINE_TYPE(${className}, ${methodPrefix}, G_TYPE_OBJECT)');

indent.newln();
indent.addScoped(
'static void ${methodPrefix}_dispose(GObject* object) {', '}', () {
indent.writeln('${className}* self = ${castMacro}(object);');
indent.writeln(
'G_OBJECT_CLASS(${methodPrefix}_parent_class)->dispose(object);');
});

indent.newln();
indent.writeln("static void ${methodPrefix}_init(${className}* self) {}");

indent.newln();
indent.addScoped(
'static void ${methodPrefix}_class_init(${className}Class* klass) {',
'}', () {
indent
.writeln('G_OBJECT_CLASS(klass)->dispose = ${methodPrefix}_dispose;');
});

var constructorArgs = <String>[];
for (var field in classDefinition.fields) {
var fieldName = _snakeCaseFromCamelCase(field.name);
var type = _getType(field.type);
constructorArgs.add('$type $fieldName');
}
indent.addScoped(
"${className}* ${methodPrefix}_new(${constructorArgs.join(', ')}) {",
'}', () {
indent.writeln(
'${className}* self = ${castMacro}(g_object_new(${methodPrefix}_get_type(), nullptr);');
indent.writeln('return self;');
});
}

@override
void writeClassEncode(
Expand Down

0 comments on commit 8378c7d

Please sign in to comment.