cbstmap · Section 3
cbmap_redeclare
NAME
cbmap_redeclare - re-establish the companion type variables for an ordered map handle from another scope
LIBRARY
C Collections (libccollections, -lccollections)
SYNOPSIS
#include <ccollections/cbstmap.h> cbmap_redeclare(hm_name, KeyT, ValT);
DESCRIPTION
Declares the companion type variables that carry KeyT and ValT for an ordered map handle that already exists. It declares no handle, and it neither creates nor destroys a map; all it gives back is the type information that the type-inferred macros need. Each type-inferred macro learns the key type and the value type of a map from two ordinary local variables named after the handle, which cbmap_construct(3) and cbmap_declare(3) create next to it. Being local declarations, they don't travel with the handle into another function. Inside that function cbmap_insert(hm_name, ...) and its siblings would name variables that nothing declares there, and the translation unit would not compile. This macro declares those variables again, so every caller that passes a cbmap across a function boundary needs it. The types must be the ones the caller created the map with, because the macros use them to read a key, to size a value and to decide whether a value is a char * that the map stores by contents.
PARAMETERS
hm_name Name of the map handle that already exists KeyT Key type that the caller created the map with ValT Value type that the caller created the map with
RETURN VALUE
None. The macro expands to declarations, not to an expression.
NOTES
Because the macro is a sequence of declarations, put it where a declaration is legal and end it with a semicolon of its own. The declared variables are pointers that nothing dereferences at run time. They are there so that typeof and the type-introspection macros can read the types from them, and they cost nothing in the generated code. Since it only declares variables, this macro is also how you reach a handle held in a struct field or a global: Name it in each function that works on that handle. An array key type or value type, such as int[2] or char[16], is a compile error whose message says to wrap the array in a struct. The macros copy keys and values by assignment and C can't assign an array, so the map would otherwise store the address of the caller's array instead of its elements. A struct that holds the array works.
WARNINGS
The declared value type can have a different width from the stored one. cbmap_get(3) and cbmap_get_ptr(3) catch that at run time: They describe the size mismatch and stop the program through ccol__fatal_err(). They can't see a mismatch between two types of the same width, though, so the types given here are a promise that the caller keeps and the compiler doesn't check. A key of the wrong width is reported rather than accepted, since the raw entry points return ccol_invalid_args for it. The order of the tree only means something for keys of the width that the caller created the map for.
EXAMPLES
void bump(cbmap scores, int who) { cbmap_redeclare(scores, int, int); int *slot = cbmap_get_ptr(scores, who); if (slot) { (*slot)++; } } cbmap_construct(scores, int, int); int who = 7, score = 42; cbmap_insert(scores, who, score); bump(scores, 7); cbmap_destroy(scores);
SEE ALSO
cbmap_declare(3), cbmap_construct(3), cbmap_get(3), cbmap_get_ptr(3), cbmap_insert(3)