Next: Creating Instances, Up: Defining New Types (Smobs)
To define a new type, the programmer must write four functions to manage instances of the type:
mark (obj)free (obj)scm_make_smob_type is non-zero) using
scm_must_free and returns the size of that struct.
See Garbage Collecting Smobs, for more details.
print (exp, port, pstate)display or write. The function should
write a printed representation of exp on port, in accordance
with the parameters in pstate. (For more information on print
states, see Port Data.) The default print function prints
#<NAME ADDRESS> where NAME is the first argument passed to
scm_make_smob_type.
equalp (a, b)equal? function to compare two instances
of the same smob type, Guile calls this function. It should return
SCM_BOOL_T if a and b should be considered
equal?, or SCM_BOOL_F otherwise. If equalp is
NULL, equal? will assume that two instances of this type are
never equal? unless they are eq?.
To actually register the new smob type, call scm_make_smob_type:
This function implements the standard way of adding a new smob type, named name, with instance size size, to the system. The return value is a tag that is used in creating instances of the type. If size is 0, then no memory will be allocated when instances of the smob are created, and nothing will be freed by the default free function. Default values are provided for mark, free, print, and, equalp, as described above. If you want to customize any of these functions, the call to
scm_make_smob_typeshould be immediately followed by calls to one or several ofscm_set_smob_mark,scm_set_smob_free,scm_set_smob_print, and/orscm_set_smob_equalp.
Each of the below scm_set_smob_XXX functions registers a smob
special function for a given type. Each function is intended to be used
only zero or one time per type, and the call should be placed
immediately following the call to scm_make_smob_type.
This function sets the smob marking procedure for the smob type specified by the tag tc (returned by
scm_make_smob_type).
This function sets the smob freeing procedure for the smob type specified by the tag tc (returned by
scm_make_smob_type).
This function sets the smob printing procedure for the smob type specified by the tag tc (returned by
scm_make_smob_type).
This function sets the smob equality-testing predicate for the smob type specified by the tag tc (returned by
scm_make_smob_type).
Instead of using scm_make_smob_type and calling each of the
individual scm_set_smob_XXX functions to register each special
function independently, you could use scm_make_smob_type_mfpe to
register all of the special functions at once as you create the smob
type.
This function invokes
scm_make_smob_typeon its first two arguments to add a new smob type named name, with instance size size to the system. It also registers the mark, free, print, equalp smob special functions for that new type. Any of these parameters can beNULLto have that special function use the default behavior for guile. The return value is a tag that is used in creating instances of the type. If size is 0, then no memory will be allocated when instances of the smob are created, and nothing will be freed by the default free function.
For example, here is how one might declare and register a new type representing eight-bit gray-scale images:
#include <libguile.h>
static scm_bits_t image_tag;
void
init_image_type (void)
{
image_tag = scm_make_smob_type ("image", sizeof (struct image));
scm_set_smob_mark (image_tag, mark_image);
scm_set_smob_free (image_tag, free_image);
scm_set_smob_print (image_tag, print_image);
}