C Collections

cyaml · Section 3

cyaml_declare_scoped

cyaml_declare_scoped - declare a cyaml variable with automatic RAII destruction

C Collections (libccollections, -lccollections)

#include <ccollections/cyaml.h>
 
cyaml_declare_scoped(var_name);

Declares a cyaml 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 whole tree that the variable holds, exactly as
cyaml_destroy() does, and leaves the variable NULL.
 
Cleanups run at the end of the enclosing block in reverse declaration
order, whether the block exits normally or through break, continue,
return or goto.
 
The macro relies on a GCC and Clang extension that MSVC doesn't have.

var_name  Name of the variable to declare.

{
    cyaml_declare_scoped(doc);
    doc = cyaml_parse("x: 1\n", NULL);
    if (!doc) return;
    /* ... use doc ... */
}  /* doc is freed automatically here */

cyaml_declare(3), cyaml_destroy(3)