C Collections

clrucache · Section 3

ccache_redeclare

ccache_redeclare - restore the type-tracking variables for a cache that crosses scopes

C Collections (libccollections, -lccollections)

#include <ccollections/clrucache.h>
 
ccache_redeclare(name, KeyT, ValT);

The type-dispatching macros ccache_get() and ccache_set() need the hidden
companion variables that ccache_declare() and ccache_construct() create.
Once you pass a ccache handle to another function those variables
don't exist there, and ccache_redeclare() creates them again so that
every type-inferred macro works in the new scope.

name  The existing ccache variable that gets the type information
KeyT  Key type. It must match the type of the construction
ValT  Value type. It must match the type of the construction

Always call ccache_redeclare() before ccache_get() or ccache_set() in a new
scope; a missing call is the most common mistake when a cache crosses
a function boundary.
The name argument must be a plain identifier, because the macro pastes
it into the names of the companion variables. Any other expression,
such as the struct member ga->cache, cannot be used, so copy such a
handle into a local variable and redeclare that variable:
 
    ccache c = ga->cache;
    ccache_redeclare(c, int, double);
 
An array key type or value type, such as int[2] or char[16], is a
compile error whose message tells you to wrap the array in a struct.
The macros copy a key or a value by assignment and C cannot assign an
array, so without that error the cache would store the address of the
caller's array instead of its elements. A struct that holds the array
works.

void use_cache(ccache c) {
    ccache_redeclare(c, int, double);
    double out = 0.0;
    if (ccache_get(c, 42, &out) == ccol_success) {
        printf("%.2f\n", out);
    }
}

ccache_declare(3), ccache_construct(3), ccache_get(3), ccache_set(3)