Skip to content

Commit

Permalink
Finish generating header
Browse files Browse the repository at this point in the history
  • Loading branch information
robert-ancell committed Dec 11, 2023
1 parent 2b41d5b commit 9668eb1
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 65 deletions.
58 changes: 9 additions & 49 deletions packages/pigeon/example/app/linux/messages.g.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,8 @@ typedef enum { MY_CODE_ONE = 0, MY_CODE_TWO = 1 } MyCode;

G_DECLARE_FINAL_TYPE(MyMessageData, my_message_data, MY, MESSAGE_DATA, GObject)

MyMessageData* my_message_data_new(MyCode code, FlValue* data);

MyMessageData* my_message_data_new_full(const gchar* name,
const gchar* description, MyCode code,
FlValue* data);
MyMessageData* my_message_data_new(const gchar* name, const gchar* description,
MyCode code, FlValue* data);

const gchar* my_message_data_get_name(MyMessageData* object);

Expand All @@ -29,52 +26,16 @@ MyCode my_message_data_get_code(MyMessageData* object);

FlValue* my_message_data_get_data(MyMessageData* object);

G_DECLARE_FINAL_TYPE(MyExampleHostApiGetHostLanguageResponse,
my_example_host_api_get_host_language_response, MY,
EXAMPLE_HOST_API_GET_HOST_LANGUAGE_RESPONSE, GObject)

MyExampleHostApiGetHostLanguageResponse*
my_example_host_api_get_host_language_response_new(const gchar* value);

MyExampleHostApiGetHostLanguageResponse*
my_example_host_api_get_host_language_response_new_error(const gchar* code,
const gchar* message,
FlValue* details);

G_DECLARE_FINAL_TYPE(MyExampleHostApiAddResponse,
my_example_host_api_add_response, MY,
EXAMPLE_HOST_API_ADD_RESPONSE, GObject)

MyExampleHostApiAddResponse* my_example_host_api_add_response_new(
int64_t value);

MyExampleHostApiAddResponse* my_example_host_api_add_response_new_error(
const gchar* code, const gchar* message, FlValue* details);

G_DECLARE_FINAL_TYPE(MyExampleHostApiSendMessageResponseHandle,
my_example_host_api_send_message_response_handle, MY,
EXAMPLE_HOST_API_SEND_MESSAGE_RESPONSE_HANDLE, GObject)

void my_example_host_api_send_message_response_handle_respond(
MyExampleHostApiSendMessageResponseHandle* response_handle,
gboolean result);

void my_example_host_api_send_message_response_handle_respond_error(
MyExampleHostApiSendMessageResponseHandle* response_handle,
const gchar* code, const gchar* message, FlValue* details);

G_DECLARE_FINAL_TYPE(MyExampleHostApi, my_example_host_api, MY,
EXAMPLE_HOST_API, GObject)

typedef struct {
MyExampleHostApiGetHostLanguageResponse* (*get_host_language)(
MyExampleHostApi* object, gpointer user_data);
MyExampleHostApiAddResponse* (*add)(MyExampleHostApi* object, int64_t a,
int64_t b, gpointer user_data);
void (*send_message)(
MyExampleHostApi* object,
MyExampleHostApiSendMessageResponseHandle* response_handle,
MyMessageData* message, gpointer user_data);
gboolean (*get_host_language)(MyExampleHostApi* object, gchar** return_value,
GError** error, gpointer user_data);
gboolean (*add)(MyExampleHostApi* object, int64_t a, int64_t b,
int64_t* return_value, GError** error, gpointer user_data);
gboolean (*send_message)(MyExampleHostApi* object, MyMessageData* message,
gpointer user_data);
} MyExampleHostApiVTable;

MyExampleHostApi* my_example_host_api_new(FlBinaryMessenger* messenger,
Expand All @@ -94,9 +55,8 @@ void my_message_flutter_api_flutter_method_async(MyMessageFlutterApi* object,
gpointer user_data);

gboolean my_message_flutter_api_flutter_method_finish(
MyMessageFlutterApi* object, GAsyncResult* result, gchar** value,
MyMessageFlutterApi* object, GAsyncResult* result, gchar** return_value,
GError** error);

G_END_DECLS

#endif // PIGEON_MESSAGES_G_H_
70 changes: 54 additions & 16 deletions packages/pigeon/lib/linux_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -254,24 +254,30 @@ class LinuxHeaderGenerator extends StructuredGenerator<LinuxOptions> {
for (var method in api.methods) {
var methodName = _snakeCaseFromCamelCase(method.name);

var asyncArgs = [
'${className}* object',
var asyncArgs = ['${className}* object'];
for (var param in method.parameters) {
var paramName = _snakeCaseFromCamelCase(param.name);
var paramType = _getType(param.type);
asyncArgs.add('${paramType} ${paramName}');
}
asyncArgs.addAll([
'GCancellable* cancellable',
'GAsyncReadyCallback callback',
'gpointer user_data'
];
]);
indent.newln();
indent.writeln(
"void ${methodPrefix}_${methodName}_async(${asyncArgs.join(', ')}));");
"void ${methodPrefix}_${methodName}_async(${asyncArgs.join(', ')});");

var finishArgs = [
'${className}* object',
'GAsyncResult* result',
'GError** error'
];
var finishArgs = ['${className}* object', 'GAsyncResult* result'];
var returnType = _getType(method.returnType, isOutput: true);
if (returnType != 'void') {
finishArgs.add('${returnType}* return_value');
}
finishArgs.add('GError** error');
indent.newln();
indent.writeln(
"gboolean ${methodPrefix}_${methodName}_finish(${finishArgs.join(', ')}));");
"gboolean ${methodPrefix}_${methodName}_finish(${finishArgs.join(', ')});");
}
}

Expand All @@ -291,16 +297,38 @@ class LinuxHeaderGenerator extends StructuredGenerator<LinuxOptions> {
var upperSnakeClassName = snakeClassName.toUpperCase();

var methodPrefix = '${snakeNamespace}_${snakeClassName}';
var vtableName = '${className}VTable';

indent.newln();
indent.writeln(
'G_DECLARE_FINAL_TYPE(${className}, ${methodPrefix}, ${upperNamespace}, ${upperSnakeClassName}, GObject)');
indent.newln();
indent.writeln('typedef struct {');
indent.writeln('} ${className}VTable;');
indent.addScoped('typedef struct {', '} ${vtableName};', () {
for (var method in api.methods) {
var methodName = _snakeCaseFromCamelCase(method.name);

var methodArgs = ['${className}* object'];
for (var param in method.parameters) {
var paramName = _snakeCaseFromCamelCase(param.name);
var paramType = _getType(param.type);
methodArgs.add('${paramType} ${paramName}');
}
if (method.isAsynchronous) {
// FIXME: Pass handle
} else {
var returnType = _getType(method.returnType, isOutput: true);
if (returnType != 'void') {
methodArgs.add('${returnType}* return_value');
}
methodArgs.add('GError** error');
}
methodArgs.add('gpointer user_data');
indent.writeln("gboolean (*${methodName})(${methodArgs.join(', ')});");
}
});
indent.newln();
indent.writeln(
'${className}* ${methodPrefix}_new(FlBinaryMessenger* messenger, const ${className}VTable* vtable, gpointer user_data, GDestroyNotify user_data_free_func);');
'${className}* ${methodPrefix}_new(FlBinaryMessenger* messenger, const ${vtableName}* vtable, gpointer user_data, GDestroyNotify user_data_free_func);');
}

@override
Expand Down Expand Up @@ -436,13 +464,23 @@ String _snakeCaseFromCamelCase(String camelCase) {
(Match m) => '${m.start == 0 ? '' : '_'}${m[0]!.toLowerCase()}');
}

String _getType(TypeDeclaration type) {
String _getType(TypeDeclaration type, {bool isOutput = false}) {
var namespace = 'My';

if (type.isClass || type.isEnum) {
if (type.isEnum) {
return '${namespace}${type.baseName}';
} else if (type.isClass) {
return '${namespace}${type.baseName}*';
} else if (type.baseName == 'void') {
return 'void';
} else if (type.baseName == 'bool') {
return 'gboolean';
} else if (type.baseName == 'int') {
return 'int64_t';
} else if (type.baseName == 'double') {
return 'double';
} else if (type.baseName == 'String') {
return 'const gchar*';
return isOutput ? 'gchar*' : 'const gchar*';
} else {
return 'FlValue*';
}
Expand Down

0 comments on commit 9668eb1

Please sign in to comment.