Name

ecl_va_arg — Accepting a variable number of arguments

Macros

typedef struct { ... } ecl_va_list[1];

ecl_va_start( ecl_va_list arglist, last_argument, narg, n_ordinary);

cl_object ecl_va_arg( ecl_va_list arglist);

cl_object ecl_va_end( ecl_va_list arglist);

Description

The macros above are used to code a function that accepts an arbitrary number of arguments. We will describe them in a practical example

cl_object my_plus(cl_narg narg, cl_object required1, ...)
{
    cl_env_ptr env = ecl_process_env();
    cl_object other_value;
    ecl_va_list varargs;
    ecl_va_start(varargs, required1, narg, 1);
    while (narg > 1) {
        cl_object other_value = ecl_va_arg(varargs);
	required1 = ecl_plus(required1, other_value);
    }
    ecl_va_end(varargs);
    ecl_return1(env, required1);
}
      

The first thing to do is to declare the variable that will hold the arguments. This is varargs in our example and it has the type ecl_va_list.

This arguments list is initialized with the ecl_va_start macro, based on the supplied number of arguments, narg, the number of required arguments which are passed as ordinary C arguments (1 in this case), the last such ordinary arguments, required, and the buffer for the argument list, varargs.

Once varargs has been initialized, we can retrieve these values one by one using ecl_va_arg. Note that the returned value always has the type cl_object, for it is always a Common Lisp object.

The last statement before returning the output of the function is ecl_va_end. This macro performs any required cleanup and should never be omitted.