Checking for valid t_linklist bound to symbol s_thing

johnpitcairn's icon

Paranoia dept: what would be the most reliable way to check that a non-NIL pointer bound to a symbol's s_thing field is a t_linklist?

Mattijs's icon

A common way to do this in C would be to have a 'magic number' in the linklist type, right? I don't see anything like that in the API..

Mattijs

Jeremy's icon

You can do this like so:

t_object *o = the_symbol->s_thing;

if (!NOGOOD(o) && ob_sym(o) == gensym("linklist")) {
// it's a linklist
}

This will test for the magic number/object validity, and then verify that it's the class you want.

johnpitcairn's icon

Great, thanks. I was thinking I'd need to make a custom struct containing the t_linklist and my own magic number field. So this technique is applicable to any t_object?

Jeremy's icon

Yeah, as long as you know the object's class name.