Skip to content

Commit

Permalink
Make handle objects for async responses, and result objects for sync …
Browse files Browse the repository at this point in the history
…responses
  • Loading branch information
robert-ancell committed Dec 8, 2023
1 parent 399cb75 commit 5235913
Show file tree
Hide file tree
Showing 3 changed files with 255 additions and 105 deletions.
250 changes: 200 additions & 50 deletions packages/pigeon/example/app/linux/messages.g.cc
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,171 @@ FlValue* my_message_data_get_data(MyMessageData* object) {
return object->data;
}

struct _MyExampleHostApiGetHostLanguageResponse {
GObject parent_instance;

FlValue* value;
};

G_DEFINE_TYPE(MyExampleHostApiGetHostLanguageResponse,
my_example_host_api_get_host_language_response, G_TYPE_OBJECT)

static void my_example_host_api_get_host_language_dispose(GObject* object) {
MyExampleHostApiGetHostLanguageResponse* self =
MY_EXAMPLE_HOST_API_GET_HOST_LANGUAGE_RESPONSE(object);
g_clear_object(&self->value);
G_OBJECT_CLASS(my_example_host_api_get_host_language_response_parent_class)
->dispose(object);
}

static void my_example_host_api_get_host_language_response_init(
MyExampleHostApiGetHostLanguageResponse* self) {}

static void my_example_host_api_get_host_language_response_class_init(
MyExampleHostApiGetHostLanguageResponseClass* klass) {
GObjectClass* object_class = G_OBJECT_CLASS(klass);
object_class->dispose = my_example_host_api_get_host_language_dispose;
}

MyExampleHostApiGetHostLanguageResponse*
my_example_host_api_get_host_language_response_new(const gchar* value) {
MyExampleHostApiGetHostLanguageResponse* self =
MY_EXAMPLE_HOST_API_GET_HOST_LANGUAGE_RESPONSE(g_object_new(
my_example_host_api_get_host_language_response_get_type(), nullptr));
self->value = fl_value_new_list();
fl_value_append_take(self->value, fl_value_new_string(value));
return self;
}

MyExampleHostApiGetHostLanguageResponse*
my_example_host_api_get_host_language_response_new_error(const gchar* code,
const gchar* message,
FlValue* details) {
MyExampleHostApiGetHostLanguageResponse* self =
MY_EXAMPLE_HOST_API_GET_HOST_LANGUAGE_RESPONSE(g_object_new(
my_example_host_api_get_host_language_response_get_type(), nullptr));
self->value = fl_value_new_list();
fl_value_append_take(self->value, fl_value_new_string(code));
fl_value_append_take(self->value, fl_value_new_string(message));
fl_value_append(self->value, details);
return self;
}

struct _MyExampleHostApiAddResponse {
GObject parent_instance;

FlValue* value;
};

G_DEFINE_TYPE(MyExampleHostApiAddResponse, my_example_host_api_add_response,
G_TYPE_OBJECT)

static void my_example_host_api_add_response_dispose(GObject* object) {
MyExampleHostApiAddResponse* self = MY_EXAMPLE_HOST_API_ADD_RESPONSE(object);
g_clear_object(&self->value);
G_OBJECT_CLASS(my_example_host_api_add_response_parent_class)
->dispose(object);
}

static void my_example_host_api_add_response_init(
MyExampleHostApiAddResponse* self) {}

static void my_example_host_api_add_response_class_init(
MyExampleHostApiAddResponseClass* klass) {
GObjectClass* object_class = G_OBJECT_CLASS(klass);
object_class->dispose = my_example_host_api_add_response_dispose;
}

MyExampleHostApiAddResponse* my_example_host_api_add_response_new(
int64_t value) {
MyExampleHostApiAddResponse* self = MY_EXAMPLE_HOST_API_ADD_RESPONSE(
g_object_new(my_example_host_api_add_response_get_type(), nullptr));
self->value = fl_value_new_list();
fl_value_append_take(self->value, fl_value_new_int(value));
return self;
}

MyExampleHostApiAddResponse* my_example_host_api_add_response_new_error(
const gchar* code, const gchar* message, FlValue* details) {
MyExampleHostApiAddResponse* self = MY_EXAMPLE_HOST_API_ADD_RESPONSE(
g_object_new(my_example_host_api_add_response_get_type(), nullptr));
self->value = fl_value_new_list();
fl_value_append_take(self->value, fl_value_new_string(code));
fl_value_append_take(self->value, fl_value_new_string(message));
fl_value_append(self->value, details);
return self;
}

struct _MyExampleHostApiSendMessageResponseHandle {
GObject parent_instance;

FlBasicMessageChannel* channel;
FlBasicMessageChannelResponseHandle* response_handle;
};

G_DEFINE_TYPE(MyExampleHostApiSendMessageResponseHandle,
my_example_host_api_send_message_response_handle, G_TYPE_OBJECT)

static void my_example_host_api_send_message_response_handle_dispose(
GObject* object) {
MyExampleHostApiSendMessageResponseHandle* self =
MY_EXAMPLE_HOST_API_SEND_MESSAGE_RESPONSE_HANDLE(object);
g_clear_object(&self->channel);
g_clear_object(&self->response_handle);
G_OBJECT_CLASS(my_example_host_api_send_message_response_handle_parent_class)
->dispose(object);
}

static void my_example_host_api_send_message_response_handle_init(
MyExampleHostApiSendMessageResponseHandle* self) {}

static void my_example_host_api_send_message_response_handle_class_init(
MyExampleHostApiSendMessageResponseHandleClass* klass) {
GObjectClass* object_class = G_OBJECT_CLASS(klass);
object_class->dispose =
my_example_host_api_send_message_response_handle_dispose;
}

MyExampleHostApiSendMessageResponseHandle*
my_example_host_api_send_message_response_handle_new(
FlBasicMessageChannel* channel,
FlBasicMessageChannelResponseHandle* response_handle) {
MyExampleHostApiSendMessageResponseHandle* self =
MY_EXAMPLE_HOST_API_SEND_MESSAGE_RESPONSE_HANDLE(g_object_new(
my_example_host_api_send_message_response_handle_get_type(),
nullptr));
self->channel = g_object_ref(channel);
self->response_handle = g_object_ref(response_handle);
return self;
}

void my_example_host_api_send_message_response_handle_respond(
MyExampleHostApiSendMessageResponseHandle* self, gboolean result) {
g_autoptr(FlValue) value = fl_value_new_list();
fl_value_append_take(value, fl_value_new_bool(result));

g_autoptr(GError) error = NULL;
if (!fl_basic_message_channel_respond(self->channel, self->response_handle,
value, &error)) {
g_warning("Failed to send response to SendMessage: %s\n", error->message);
}
}

void my_example_host_api_send_message_response_handle_respond_error(
MyExampleHostApiSendMessageResponseHandle* self, const gchar* code,
const gchar* message, FlValue* details) {
g_autoptr(FlValue) value = fl_value_new_list();
fl_value_append_take(value, fl_value_new_string(code));
fl_value_append_take(value, fl_value_new_string(message));
fl_value_append(value, details);

g_autoptr(GError) error = NULL;
if (!fl_basic_message_channel_respond(self->channel, self->response_handle,
value, &error)) {
g_warning("Failed to send response to SendMessage: %s\n", error->message);
}
}

struct _MyExampleHostApi {
GObject parent_instance;

Expand All @@ -89,8 +254,22 @@ static void get_host_language_cb(
FlBasicMessageChannel* channel, FlValue* message,
FlBasicMessageChannelResponseHandle* response_handle, gpointer user_data) {
MyExampleHostApi* self = MY_EXAMPLE_HOST_API(user_data);
if (self->vtable->get_host_language != nullptr) {
self->vtable->get_host_language(self, response_handle, self->user_data);
if (self->vtable->get_host_language == nullptr) {
return;
}

g_autoptr(MyExampleHostApiGetHostLanguageResponse) response =
self->vtable->get_host_language(self, self->user_data);
if (response == nullptr) {
g_warning("No response returned to GetHostLanguage");
return;
}

g_autoptr(GError) error = NULL;
if (!fl_basic_message_channel_respond(channel, response_handle,
response->value, &error)) {
g_warning("Failed to send response to GetHostLanguage: %s\n",
error->message);
}
}

Expand All @@ -99,8 +278,21 @@ static void add_cb(FlBasicMessageChannel* channel, FlValue* message,
gpointer user_data) {
MyExampleHostApi* self = MY_EXAMPLE_HOST_API(user_data);
int64_t a = 0, b = 0;
if (self->vtable->add != nullptr) {
self->vtable->add(self, response_handle, a, b, self->user_data);
if (self->vtable->add == nullptr) {
return;
}

g_autoptr(MyExampleHostApiAddResponse) response =
self->vtable->add(self, a, b, self->user_data);
if (response == nullptr) {
g_warning("No response returned to Add");
return;
}

g_autoptr(GError) error = NULL;
if (!fl_basic_message_channel_respond(channel, response_handle,
response->value, &error)) {
g_warning("Failed to send response to Add: %s\n", error->message);
}
}

Expand All @@ -112,8 +304,10 @@ static void send_message_cb(
g_autoptr(MyMessageData) message_data =
my_message_data_new(MY_CODE_ONE, data);
if (self->vtable->send_message != nullptr) {
self->vtable->send_message(self, response_handle, message_data,
self->user_data);
g_autoptr(MyExampleHostApiSendMessageResponseHandle) handle =
my_example_host_api_send_message_response_handle_new(channel,
response_handle);
self->vtable->send_message(self, handle, message_data, self->user_data);
}
}

Expand Down Expand Up @@ -170,50 +364,6 @@ MyExampleHostApi* my_example_host_api_new(FlBinaryMessenger* messenger,
return self;
}

gboolean my_example_host_api_respond_get_host_language(
MyExampleHostApi* self,
FlBasicMessageChannelResponseHandle* response_handle, const gchar* result,
GError** error) {
g_autoptr(FlValue) message = fl_value_new_list();
fl_value_append_take(message, fl_value_new_string(result));
return fl_basic_message_channel_respond(self->get_host_language_channel,
response_handle, message, error);
}

gboolean my_example_host_api_respond_error_get_host_language(
MyExampleHostApi* self,
FlBasicMessageChannelResponseHandle* response_handle, const gchar* code,
const gchar* message_, FlValue* details, GError** error) {
g_autoptr(FlValue) message = fl_value_new_list();
fl_value_append_take(message, fl_value_new_string(code));
fl_value_append_take(message, fl_value_new_string(message_));
fl_value_append(message, details);
return fl_basic_message_channel_respond(self->get_host_language_channel,
response_handle, message, error);
}

gboolean my_example_host_api_respond_add(
MyExampleHostApi* self,
FlBasicMessageChannelResponseHandle* response_handle, int64_t result,
GError** error) {
g_autoptr(FlValue) message = fl_value_new_list();
fl_value_append_take(message, fl_value_new_int(result));
return fl_basic_message_channel_respond(self->add_channel, response_handle,
message, error);
}

gboolean my_example_host_api_respond_error_add(
MyExampleHostApi* self,
FlBasicMessageChannelResponseHandle* response_handle, const gchar* code,
const gchar* message_, FlValue* details, GError** error) {
g_autoptr(FlValue) message = fl_value_new_list();
fl_value_append_take(message, fl_value_new_string(code));
fl_value_append_take(message, fl_value_new_string(message_));
fl_value_append(message, details);
return fl_basic_message_channel_respond(self->add_channel, response_handle,
message, error);
}

gboolean my_example_host_api_respond_send_message(
MyExampleHostApi* self,
FlBasicMessageChannelResponseHandle* response_handle, gboolean result,
Expand Down
79 changes: 41 additions & 38 deletions packages/pigeon/example/app/linux/messages.g.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,56 +29,59 @@ 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 {
void (*get_host_language)(
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,
FlBasicMessageChannelResponseHandle* response_handle, gpointer user_data);
void (*add)(MyExampleHostApi* object,
FlBasicMessageChannelResponseHandle* response_handle, int64_t a,
int64_t b, gpointer user_data);
void (*send_message)(MyExampleHostApi* object,
FlBasicMessageChannelResponseHandle* response_handle,
MyMessageData* message, gpointer user_data);
MyExampleHostApiSendMessageResponseHandle* response_handle,
MyMessageData* message, gpointer user_data);
} MyExampleHostApiVTable;

MyExampleHostApi* my_example_host_api_new(FlBinaryMessenger* messenger,
const MyExampleHostApiVTable* vtable,
gpointer user_data,
GDestroyNotify user_data_free_func);

gboolean my_example_host_api_respond_get_host_language(
MyExampleHostApi* object,
FlBasicMessageChannelResponseHandle* response_handle, const gchar* result,
GError** error);

gboolean my_example_host_api_respond_error_get_host_language(
MyExampleHostApi* object,
FlBasicMessageChannelResponseHandle* response_handle, const gchar* code,
const gchar* message, FlValue* details, GError** error);

gboolean my_example_host_api_respond_add(
MyExampleHostApi* object,
FlBasicMessageChannelResponseHandle* response_handle, int64_t result,
GError** error);

gboolean my_example_host_api_respond_error_add(
MyExampleHostApi* object,
FlBasicMessageChannelResponseHandle* response_handle, const gchar* code,
const gchar* message, FlValue* details, GError** error);

gboolean my_example_host_api_respond_send_message(
MyExampleHostApi* object,
FlBasicMessageChannelResponseHandle* response_handle, gboolean result,
GError** error);

gboolean my_example_host_api_respond_error_send_message(
MyExampleHostApi* object,
FlBasicMessageChannelResponseHandle* response_handle, const gchar* code,
const gchar* message, FlValue* details, GError** error);

G_DECLARE_FINAL_TYPE(MyMessageFlutterApi, my_message_flutter_api, MY,
MESSAGE_FLUTTER_API, GObject)

Expand Down
Loading

0 comments on commit 5235913

Please sign in to comment.