diff options
author | Thiago de Arruda <tpadilha84@gmail.com> | 2014-05-23 15:49:40 -0300 |
---|---|---|
committer | Thiago de Arruda <tpadilha84@gmail.com> | 2014-05-23 16:06:58 -0300 |
commit | 1e67b13fdcb6ed7f2a951b9053f0a829b3a48906 (patch) | |
tree | 205f0d0438c7adaf76c32a46f828c91c48b58395 | |
parent | 92307201b51c399e8109c91cc131ff3602389db1 (diff) | |
download | rneovim-1e67b13fdcb6ed7f2a951b9053f0a829b3a48906.tar.gz rneovim-1e67b13fdcb6ed7f2a951b9053f0a829b3a48906.tar.bz2 rneovim-1e67b13fdcb6ed7f2a951b9053f0a829b3a48906.zip |
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
-rw-r--r-- | src/nvim/api/private/defs.h | 5 | ||||
-rw-r--r-- | src/nvim/os/msgpack_rpc.c | 74 | ||||
-rw-r--r-- | src/nvim/os/msgpack_rpc.h | 9 |
3 files changed, 45 insertions, 43 deletions
diff --git a/src/nvim/api/private/defs.h b/src/nvim/api/private/defs.h index 9b0639067d..a6c2eea8e5 100644 --- a/src/nvim/api/private/defs.h +++ b/src/nvim/api/private/defs.h @@ -34,10 +34,7 @@ REMOTE_TYPE(Tabpage); typedef struct object Object; -typedef struct { - String *items; - size_t size; -} StringArray; +TYPED_ARRAY_OF(String); typedef struct { Integer row, col; 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) + diff --git a/src/nvim/os/msgpack_rpc.h b/src/nvim/os/msgpack_rpc.h index 0f644273cd..45632c7389 100644 --- a/src/nvim/os/msgpack_rpc.h +++ b/src/nvim/os/msgpack_rpc.h @@ -8,6 +8,8 @@ #include "nvim/api/private/defs.h" +#define ARRAY_DICT_INIT {.size = 0, .items = NULL} + /// Validates the basic structure of the msgpack-rpc call and fills `res` /// with the basic response structure. /// @@ -80,9 +82,9 @@ void msgpack_rpc_from_dictionary(Dictionary result, msgpack_packer *res); #define msgpack_rpc_init_window #define msgpack_rpc_init_tabpage #define msgpack_rpc_init_object = {.type = kObjectTypeNil} -#define msgpack_rpc_init_stringarray = {.items = NULL, .size = 0} -#define msgpack_rpc_init_array = {.items = NULL, .size = 0} -#define msgpack_rpc_init_dictionary = {.items = NULL, .size = 0} +#define msgpack_rpc_init_stringarray = ARRAY_DICT_INIT +#define msgpack_rpc_init_array = ARRAY_DICT_INIT +#define msgpack_rpc_init_dictionary = ARRAY_DICT_INIT /// Helpers for freeing arguments/return value /// @@ -102,6 +104,5 @@ void msgpack_rpc_free_stringarray(StringArray value); void msgpack_rpc_free_array(Array value); void msgpack_rpc_free_dictionary(Dictionary value); - #endif // NVIM_OS_MSGPACK_RPC_H |