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
This commit is contained in:
Thiago de Arruda 2014-05-23 15:49:38 -03:00
parent 5fdf854f78
commit 92307201b5
2 changed files with 25 additions and 33 deletions

View File

@ -5,6 +5,14 @@
#include <stdbool.h>
#include <string.h>
#define REMOTE_TYPE(type) typedef Integer type
#define TYPED_ARRAY_OF(type) \
typedef struct { \
type *items; \
size_t size; \
} type##Array
// Basic types
typedef struct {
char msg[256];
@ -20,9 +28,9 @@ typedef struct {
size_t size;
} String;
typedef Integer Buffer;
typedef Integer Window;
typedef Integer Tabpage;
REMOTE_TYPE(Buffer);
REMOTE_TYPE(Window);
REMOTE_TYPE(Tabpage);
typedef struct object Object;

View File

@ -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)