C Collections

cjson · Section 3

cjson_declare_scoped

cjson_declare_scoped - declare a cjson variable with automatic destruction

C Collections (libccollections, -lccollections)

#include <ccollections/cjson.h>
 
cjson_declare_scoped(var_name);

Declares a cjson handle named var_name, sets it to NULL and attaches
a cleanup attribute to it.  When var_name goes out of scope, the
cleanup frees the tree that the variable holds, exactly as
cjson_destroy() does.  It runs at the end of the enclosing block on
every way out of it: A normal exit, break, continue, return and
goto.
 
Only store a tree that the caller owns in the variable, such as the
result of cjson_parse() or of a cjson_create_*() function.  A
borrowed reference from cjson_get() must not go there, because the
cleanup would free it while its parent holds it.
 
The macro relies on a GCC and Clang extension.

var_name  Name of the variable to declare.

{
    cjson_declare_scoped(root);
    root = cjson_parse("{\"k\":1}", NULL);
    /* root is freed automatically at the closing brace */
}

cjson_declare(3), cjson_destroy(3)