API: Refactor: Implement api/handle module

This module will be used to implement remote management of objects through the
API. Object types to be registered must have a `uint64_t` field named 'handle'.
This commit is contained in:
Thiago de Arruda 2014-05-23 15:49:28 -03:00
parent 72e3125f45
commit 7dfc7bc2e1
5 changed files with 49 additions and 0 deletions

View File

@ -0,0 +1,31 @@
#include <stdint.h>
#include "nvim/vim.h"
#include "nvim/map.h"
#include "nvim/api/private/handle.h"
#define HANDLE_INIT(name) name##_handles = map_new(uint64_t)()
#define HANDLE_IMPL(type, name) \
static Map(uint64_t) *name##_handles = NULL; \
\
type *handle_get_##name(uint64_t handle) \
{ \
return map_get(uint64_t)(name##_handles, handle); \
} \
\
void handle_register_##name(type *name) \
{ \
assert(!name->handle); \
name->handle = next_handle++; \
map_put(uint64_t)(name##_handles, name->handle, name); \
} \
\
void handle_unregister_##name(type *name) \
{ \
map_del(uint64_t)(name##_handles, name->handle); \
}
void handle_init()
{
}

View File

@ -0,0 +1,14 @@
#ifndef NVIM_API_HANDLE_H
#define NVIM_API_HANDLE_H
#include "nvim/vim.h"
#define HANDLE_DECLS(type, name) \
type *handle_get_##name(uint64_t handle); \
void handle_register_##name(type *name); \
void handle_unregister_##name(type *name);
void handle_init(void);
#endif // NVIM_API_HANDLE_H

View File

@ -85,3 +85,4 @@
MAP_IMPL(cstr_t)
MAP_IMPL(ptr_t)
MAP_IMPL(uint64_t)

View File

@ -21,6 +21,7 @@
MAP_DECLS(cstr_t)
MAP_DECLS(ptr_t)
MAP_DECLS(uint64_t)
#define map_new(T) map_##T##_new
#define map_free(T) map_##T##_free

View File

@ -27,6 +27,7 @@
#include <string.h>
#include "nvim/api/private/handle.h"
#include "nvim/vim.h"
#include "nvim/os_unix.h"
#include "nvim/buffer.h"
@ -542,6 +543,7 @@ int mch_nodetype(char_u *name)
void mch_early_init()
{
handle_init();
time_init();
}