Refactor: Redefine Map(T) as a more generic Map(T, U) macro

To replace `Map(T)`, a new macro `PMap(T)` was defined as `Map(T, ptr_t)` for
writing maps that store pointers with less boilerplate
This commit is contained in:
Thiago de Arruda
2014-05-30 18:41:57 -03:00
parent a581173e71
commit 82e3e7047f
7 changed files with 129 additions and 118 deletions

View File

@@ -4,26 +4,26 @@
#include "nvim/map.h"
#include "nvim/api/private/handle.h"
#define HANDLE_INIT(name) name##_handles = map_new(uint64_t)()
#define HANDLE_INIT(name) name##_handles = pmap_new(uint64_t)()
#define HANDLE_IMPL(type, name) \
static Map(uint64_t) *name##_handles = NULL; \
static PMap(uint64_t) *name##_handles = NULL; \
\
type *handle_get_##name(uint64_t handle) \
{ \
return map_get(uint64_t)(name##_handles, handle); \
return pmap_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); \
pmap_put(uint64_t)(name##_handles, name->handle, name); \
} \
\
void handle_unregister_##name(type *name) \
{ \
map_del(uint64_t)(name##_handles, name->handle); \
pmap_del(uint64_t)(name##_handles, name->handle); \
}
static uint64_t next_handle = 1;

View File

@@ -21,7 +21,7 @@
/// @param obj The source object
/// @param lookup Lookup table containing pointers to all processed objects
/// @return The converted value
static Object vim_to_object_rec(typval_T *obj, Map(ptr_t) *lookup);
static Object vim_to_object_rec(typval_T *obj, PMap(ptr_t) *lookup);
static bool object_to_vim(Object obj, typval_T *tv, Error *err);
@@ -278,10 +278,10 @@ Object vim_to_object(typval_T *obj)
{
Object rv;
// We use a lookup table to break out of cyclic references
Map(ptr_t) *lookup = map_new(ptr_t)();
PMap(ptr_t) *lookup = pmap_new(ptr_t)();
rv = vim_to_object_rec(obj, lookup);
// Free the table
map_free(ptr_t)(lookup);
pmap_free(ptr_t)(lookup);
return rv;
}
@@ -422,18 +422,18 @@ static bool object_to_vim(Object obj, typval_T *tv, Error *err)
return true;
}
static Object vim_to_object_rec(typval_T *obj, Map(ptr_t) *lookup)
static Object vim_to_object_rec(typval_T *obj, PMap(ptr_t) *lookup)
{
Object rv = {.type = kObjectTypeNil};
if (obj->v_type == VAR_LIST || obj->v_type == VAR_DICT) {
// Container object, add it to the lookup table
if (map_has(ptr_t)(lookup, obj)) {
if (pmap_has(ptr_t)(lookup, obj)) {
// It's already present, meaning we alredy processed it so just return
// nil instead.
return rv;
}
map_put(ptr_t)(lookup, obj, NULL);
pmap_put(ptr_t)(lookup, obj, NULL);
}
switch (obj->v_type) {