Map API

Libmodule offers an easy to use hashmap implementation, provided by <module/map.h> header.
It is used internally to store context’s modules and modules’ subscriptions/topics.

Structures

typedef enum {
    MAP_WRONG_PARAM = -5,
    MAP_ERR,
    MAP_MISSING,
    MAP_FULL,
    MAP_OMEM,
    MAP_OK
} map_ret_code;

/* Callback for map_iterate */
typedef map_ret_code (*map_cb)(void *, void *);

/* Incomplete struct declaration for hashmap */
typedef struct _map map_t;

API

Where not specified, these functions return a map_ret_code.

map_new()

Create a new map_t object.

Returns:pointer to newly allocated map_t.
map_iterate(m, fn, userptr)

Iterate an hashmap calling cb on each element until MAP_OK is returned (or end of hashmap is reached). Returns MAP_MISSING if map is NULL.

Parameters:
  • m (map_t *) – pointer to map_t
  • fn (const map_cb) – callback to be called
  • userptr (void *) – userdata to be passed to callback as first parameter
map_put(m, key, val, dupkey, autofree)

Put a value inside hashmap. Note that if dupkey is true, key will be strdupped; it will also be automatically freed upon deletion.

Parameters:
  • m (map_t *) – pointer to map_t
  • key (const char *) – key for this value
  • val (void *) – value to be put inside map
  • dupkey (const bool) – whether to strdup key
  • autofree (const bool) – whether to automatically free val upon map_remove/map_clear/map_free
map_get(m, key)

Get an hashmap value.

Parameters:
  • m (map_t *) – pointer to map_t
  • key (const char *) – key for this value
Returns:

void pointer to value, on NULL on error.

map_has_key(m, key)

Check if key exists in map.

Parameters:
  • m (map_t *) – pointer to map_t
  • key (const char *) – desired key
Returns:

true if key exists, false otherwise.

map_remove(m, key)

Remove a key from hashmap.

Parameters:
  • m (map_t *) – pointer to map_t
  • key (const char *) – key to be removed
map_clear(m)

Clears a map object by deleting any object inside map, and eventually freeing it too if marked with autofree.

Parameters:
  • s (map_t *) – pointer to map_t
map_free(m)

Free a map object (it internally calls map_clear too).

Parameters:
  • m (map_t *) – pointer to map_t
map_length(m)

Get map length.

Parameters:
  • m (map_t *) – pointer to map_t
Returns:

map length or a map_ret_code if any error happens (map_t is null).