C Collections

cvector · Section 3

cvec_declare

cvec_declare - declare a vector variable and its companion type variables

C Collections (libccollections, -lccollections)

#include <ccollections/cvector.h>
 
cvec_declare(v, type);

Declares a vector variable named v that holds elements of the given type,
together with the two hidden companion type variables that the
type-dispatching macros read wherever they are called. No vector is
created: v stays uninitialized, and cvec_init(3) or cvec_init_mp(3) must
give it a vector before any other macro or function touches it.
 
Use it when the declaration and the creation have to be separate, for
example when the allocator is chosen further down the function, or for a
variable at the top of a block that later paths create in different ways.
When they don't need to be separate, cvec_construct(3) and
cvec_construct_mp(3) do both at once, and cvec_construct_scoped(3) and
cvec_construct_mp_scoped(3) also destroy the vector automatically at the
end of the scope.
 
The companion variables are named after v. They carry type information
only; nothing reads them at run time and they are marked unused, so a
vector that only goes to the raw functions costs nothing extra. The value
companion carries the element type: cvec_init(3) takes the element size
from it, and cvec_at(3), cvec_push(3) and the other type-dispatching
macros type their results with it. The key companion carries size_t, the
type of the index that a vector iterator reports through
ccol_iter_key_ptr(3).

v     Name of the vector variable to declare
type  Element type, for example int, double or struct foo
      Any type name works, a function pointer type such
      as void (*)(int) and an array type such as int[4]
      included. For an array element type, give
      cvec_push(3) an array or a string literal, whose
      bytes the macro copies into the element

The macro expands to a sequence of declarations and needs a semicolon at
the end of the call, so it goes where declarations are legal and can't be
the body of an if or a loop without braces.
 
The companion variables have to be in scope wherever you use a
type-dispatching macro on v. For a handle that comes from another scope,
cvec_redeclare(3) makes them again.
 
Once it is initialized, destroy a vector declared with this macro using
cvec_destroy(3).

v holds an indeterminate value until cvec_init(3) or cvec_init_mp(3)
assigns one, and reading v before that is undefined behavior, a call to
cvec_destroy(3) on a path that skipped the initialization included.
cvec_declare_scoped(3) is the variant that starts at NULL.

cvec_declare(my_vec, int);
cvec_init(my_vec);
cvec_push(my_vec, 42);
cvec_destroy(my_vec);

cvec_declare_scoped(3), cvec_init(3), cvec_init_mp(3), cvec_construct(3), cvec_construct_mp(3), cvec_redeclare(3), cvec_destroy(3)