From 92307201b51c399e8109c91cc131ff3602389db1 Mon Sep 17 00:00:00 2001 From: Thiago de Arruda Date: Fri, 23 May 2014 15:49:38 -0300 Subject: API: Refactor: Generalize buffer, window and tabpage types/functions - Extract remote types definitions into a macro - Extract msgpack_rpc helper functions for remote types into a macro --- src/nvim/os/msgpack_rpc.c | 44 ++++++++++++++------------------------------ 1 file changed, 14 insertions(+), 30 deletions(-) (limited to 'src/nvim/os/msgpack_rpc.c') diff --git a/src/nvim/os/msgpack_rpc.c b/src/nvim/os/msgpack_rpc.c index 45832070b1..c1e749a000 100644 --- a/src/nvim/os/msgpack_rpc.c +++ b/src/nvim/os/msgpack_rpc.c @@ -4,6 +4,16 @@ #include "nvim/vim.h" #include "nvim/memory.h" +#define REMOTE_FUNCS_IMPL(t, lt) \ + bool msgpack_rpc_to_##lt(msgpack_object *obj, t *arg) \ + { \ + return msgpack_rpc_to_integer(obj, arg); \ + } \ + \ + void msgpack_rpc_from_##lt(t result, msgpack_packer *res) \ + { \ + msgpack_rpc_from_integer(result, res); \ + } void msgpack_rpc_call(msgpack_object *req, msgpack_packer *res) { // The initial response structure is the same no matter what happens, @@ -104,21 +114,6 @@ bool msgpack_rpc_to_string(msgpack_object *obj, String *arg) return obj->type == MSGPACK_OBJECT_RAW; } -bool msgpack_rpc_to_buffer(msgpack_object *obj, Buffer *arg) -{ - return msgpack_rpc_to_integer(obj, arg); -} - -bool msgpack_rpc_to_window(msgpack_object *obj, Window *arg) -{ - return msgpack_rpc_to_integer(obj, arg); -} - -bool msgpack_rpc_to_tabpage(msgpack_object *obj, Tabpage *arg) -{ - return msgpack_rpc_to_integer(obj, arg); -} - bool msgpack_rpc_to_object(msgpack_object *obj, Object *arg) { switch (obj->type) { @@ -251,21 +246,6 @@ void msgpack_rpc_from_string(String result, msgpack_packer *res) msgpack_pack_raw_body(res, result.data, result.size); } -void msgpack_rpc_from_buffer(Buffer result, msgpack_packer *res) -{ - msgpack_rpc_from_integer(result, res); -} - -void msgpack_rpc_from_window(Window result, msgpack_packer *res) -{ - msgpack_rpc_from_integer(result, res); -} - -void msgpack_rpc_from_tabpage(Tabpage result, msgpack_packer *res) -{ - msgpack_rpc_from_integer(result, res); -} - void msgpack_rpc_from_object(Object result, msgpack_packer *res) { switch (result.type) { @@ -390,3 +370,7 @@ void msgpack_rpc_free_dictionary(Dictionary value) free(value.items); } +REMOTE_FUNCS_IMPL(Buffer, buffer) +REMOTE_FUNCS_IMPL(Window, window) +REMOTE_FUNCS_IMPL(Tabpage, tabpage) + -- cgit From 1e67b13fdcb6ed7f2a951b9053f0a829b3a48906 Mon Sep 17 00:00:00 2001 From: Thiago de Arruda Date: Fri, 23 May 2014 15:49:40 -0300 Subject: API: Refactor: Add macro infrastructure for typed arrays - Add macros supporting typed arrays in the remote API - Refactor StringArray-related functions on top of the new macros --- src/nvim/os/msgpack_rpc.c | 74 +++++++++++++++++++++++++---------------------- 1 file changed, 39 insertions(+), 35 deletions(-) (limited to 'src/nvim/os/msgpack_rpc.c') diff --git a/src/nvim/os/msgpack_rpc.c b/src/nvim/os/msgpack_rpc.c index c1e749a000..6c18c7310d 100644 --- a/src/nvim/os/msgpack_rpc.c +++ b/src/nvim/os/msgpack_rpc.c @@ -14,6 +14,43 @@ { \ msgpack_rpc_from_integer(result, res); \ } + +#define TYPED_ARRAY_IMPL(t, lt) \ + bool msgpack_rpc_to_##lt##array(msgpack_object *obj, t##Array *arg) \ + { \ + if (obj->type != MSGPACK_OBJECT_ARRAY) { \ + return false; \ + } \ + \ + arg->size = obj->via.array.size; \ + arg->items = xcalloc(obj->via.array.size, sizeof(t)); \ + \ + for (size_t i = 0; i < obj->via.array.size; i++) { \ + if (!msgpack_rpc_to_##lt(obj->via.array.ptr + i, &arg->items[i])) { \ + return false; \ + } \ + } \ + \ + return true; \ + } \ + \ + void msgpack_rpc_from_##lt##array(t##Array result, msgpack_packer *res) \ + { \ + msgpack_pack_array(res, result.size); \ + \ + for (size_t i = 0; i < result.size; i++) { \ + msgpack_rpc_from_##lt(result.items[i], res); \ + } \ + } \ + \ + void msgpack_rpc_free_##lt##array(t##Array value) { \ + for (size_t i = 0; i < value.size; i++) { \ + msgpack_rpc_free_##lt(value.items[i]); \ + } \ + \ + free(value.items); \ + } + void msgpack_rpc_call(msgpack_object *req, msgpack_packer *res) { // The initial response structure is the same no matter what happens, @@ -151,24 +188,6 @@ bool msgpack_rpc_to_object(msgpack_object *obj, Object *arg) } } -bool msgpack_rpc_to_stringarray(msgpack_object *obj, StringArray *arg) -{ - if (obj->type != MSGPACK_OBJECT_ARRAY) { - return false; - } - - arg->size = obj->via.array.size; - arg->items = xcalloc(obj->via.array.size, sizeof(String)); - - for (uint32_t i = 0; i < obj->via.array.size; i++) { - if (!msgpack_rpc_to_string(obj->via.array.ptr + i, &arg->items[i])) { - return false; - } - } - - return true; -} - bool msgpack_rpc_to_position(msgpack_object *obj, Position *arg) { return obj->type == MSGPACK_OBJECT_ARRAY @@ -282,15 +301,6 @@ void msgpack_rpc_from_object(Object result, msgpack_packer *res) } } -void msgpack_rpc_from_stringarray(StringArray result, msgpack_packer *res) -{ - msgpack_pack_array(res, result.size); - - for (size_t i = 0; i < result.size; i++) { - msgpack_rpc_from_string(result.items[i], res); - } -} - void msgpack_rpc_from_position(Position result, msgpack_packer *res) { msgpack_pack_array(res, 2);; @@ -343,14 +353,6 @@ void msgpack_rpc_free_object(Object value) } } -void msgpack_rpc_free_stringarray(StringArray value) { - for (uint32_t i = 0; i < value.size; i++) { - msgpack_rpc_free_string(value.items[i]); - } - - free(value.items); -} - void msgpack_rpc_free_array(Array value) { for (uint32_t i = 0; i < value.size; i++) { @@ -374,3 +376,5 @@ REMOTE_FUNCS_IMPL(Buffer, buffer) REMOTE_FUNCS_IMPL(Window, window) REMOTE_FUNCS_IMPL(Tabpage, tabpage) +TYPED_ARRAY_IMPL(String, string) + -- cgit From f70f9bfac1ae62828d222bc6ccb528e6e93be523 Mon Sep 17 00:00:00 2001 From: Thiago de Arruda Date: Fri, 23 May 2014 15:49:42 -0300 Subject: API: Refactor: Change the integer type of remote objects to uint64_t --- src/nvim/os/msgpack_rpc.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'src/nvim/os/msgpack_rpc.c') diff --git a/src/nvim/os/msgpack_rpc.c b/src/nvim/os/msgpack_rpc.c index 6c18c7310d..5840d25375 100644 --- a/src/nvim/os/msgpack_rpc.c +++ b/src/nvim/os/msgpack_rpc.c @@ -7,12 +7,13 @@ #define REMOTE_FUNCS_IMPL(t, lt) \ bool msgpack_rpc_to_##lt(msgpack_object *obj, t *arg) \ { \ - return msgpack_rpc_to_integer(obj, arg); \ + *arg = obj->via.u64; \ + return obj->type == MSGPACK_OBJECT_POSITIVE_INTEGER; \ } \ \ void msgpack_rpc_from_##lt(t result, msgpack_packer *res) \ { \ - msgpack_rpc_from_integer(result, res); \ + msgpack_pack_uint64(res, result); \ } #define TYPED_ARRAY_IMPL(t, lt) \ -- cgit From a842fe4dc1e0624a6332ee0fef40517e7925dfb4 Mon Sep 17 00:00:00 2001 From: Thiago de Arruda Date: Fri, 23 May 2014 15:49:44 -0300 Subject: API: Refactor: Return handles instead of indexes - Define specialized arrays for each remote object type - Implement msgpack_rpc functions for dealing with the new types - Refactor all functions dealing with buffers, windows and tabpages to return/accept handles instead of list indexes. --- src/nvim/os/msgpack_rpc.c | 3 +++ 1 file changed, 3 insertions(+) (limited to 'src/nvim/os/msgpack_rpc.c') diff --git a/src/nvim/os/msgpack_rpc.c b/src/nvim/os/msgpack_rpc.c index 5840d25375..d7ffa6f559 100644 --- a/src/nvim/os/msgpack_rpc.c +++ b/src/nvim/os/msgpack_rpc.c @@ -377,5 +377,8 @@ REMOTE_FUNCS_IMPL(Buffer, buffer) REMOTE_FUNCS_IMPL(Window, window) REMOTE_FUNCS_IMPL(Tabpage, tabpage) +TYPED_ARRAY_IMPL(Buffer, buffer) +TYPED_ARRAY_IMPL(Window, window) +TYPED_ARRAY_IMPL(Tabpage, tabpage) TYPED_ARRAY_IMPL(String, string) -- cgit