@c essay @sp 10
@c essay @comment The title is printed in a large font.
@c essay @title Data Representation in Guile
-@c essay @subtitle $Id: data-rep.texi,v 1.22 2001-04-20 13:26:55 ossau Exp $
+@c essay @subtitle $Id: data-rep.texi,v 1.23 2001-04-26 18:26:28 ossau Exp $
@c essay @subtitle For use with Guile @value{VERSION}
@c essay @author Jim Blandy
@c essay @author Free Software Foundation
@example
#include <libguile.h>
-long image_tag;
+static long image_tag;
void
init_image_type ()
@{
- image_tag = scm_make_smob_type_mfpe ("image",sizeof(struct image),
- mark_image, free_image, print_image, NULL);
+ 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);
@}
@end example
Continuing the above example, if the global variable @code{image_tag}
-contains a tag returned by @code{scm_newsmob}, here is how we could
-construct a smob whose @sc{cdr} contains a pointer to a freshly
+contains a tag returned by @code{scm_make_smob_type}, here is how we
+could construct a smob whose @sc{cdr} contains a pointer to a freshly
allocated @code{struct image}:
@example
struct image *image;
int width, height;
- SCM_ASSERT (SCM_NIMP (name) && SCM_STRINGP (name), name,
- SCM_ARG1, "make-image");
+ SCM_ASSERT (SCM_STRINGP (name), name, SCM_ARG1, "make-image");
SCM_ASSERT (SCM_INUMP (s_width), s_width, SCM_ARG2, "make-image");
SCM_ASSERT (SCM_INUMP (s_height), s_height, SCM_ARG3, "make-image");
and perhaps causing a segmentation fault. Fortunately, this is pretty
simple to do. The function need only verify that its argument is a
non-immediate, whose @sc{car} is the type tag returned by
-@code{scm_newsmob}.
+@code{scm_make_smob_type}.
For example, here is a simple function that operates on an image smob,
and checks the type of its argument. We also present an expanded
void
init_image_type ()
@{
- image_tag = scm_newsmob (&image_funs);
+ 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);
- scm_make_gsubr ("make-image", 3, 0, 0, make_image);
scm_make_gsubr ("clear-image", 1, 0, 0, clear_image);
+ scm_make_gsubr ("make-image", 3, 0, 0, make_image);
@}
@end example
SCM image_smob;
int width, height;
- SCM_ASSERT (SCM_NIMP (name) && SCM_STRINGP (name), name,
- SCM_ARG1, "make-image");
+ SCM_ASSERT (SCM_STRINGP (name), name, SCM_ARG1, "make-image");
SCM_ASSERT (SCM_INUMP (s_width), s_width, SCM_ARG2, "make-image");
SCM_ASSERT (SCM_INUMP (s_height), s_height, SCM_ARG3, "make-image");
make_image (SCM name, SCM s_width, SCM s_height)
@{
struct image *image;
- SCM image_smob;
int width, height;
- SCM_ASSERT (SCM_NIMP (name) && SCM_STRINGP (name), name,
- SCM_ARG1, "make-image");
+ SCM_ASSERT (SCM_STRINGP (name), name, SCM_ARG1, "make-image");
SCM_ASSERT (SCM_INUMP (s_width), s_width, SCM_ARG2, "make-image");
SCM_ASSERT (SCM_INUMP (s_height), s_height, SCM_ARG3, "make-image");
image->name = name;
image->update_func = SCM_BOOL_F;
- SCM_NEWSMOB (image_smob, image_tag, image);
-
- return image_smob;
+ SCM_RETURN_NEWSMOB (image_tag, image);
@}
static SCM
static SCM
mark_image (SCM image_smob)
@{
+ /* Mark the image's name and update function. */
struct image *image = (struct image *) SCM_SMOB_DATA (image_smob);
scm_gc_mark (image->name);
return 1;
@}
-static scm_smobfuns image_funs = @{
- mark_image, free_image, print_image, 0
-@};
-
void
init_image_type ()
@{
- image_tag = scm_newsmob (&image_funs);
+ 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);
scm_make_gsubr ("clear-image", 1, 0, 0, clear_image);
scm_make_gsubr ("make-image", 3, 0, 0, make_image);
struct image *image;
int width, height;
- SCM_ASSERT (SCM_NIMP (name) && SCM_STRINGP (name), name,
- SCM_ARG1, "make-image");
+ SCM_ASSERT (SCM_STRINGP (name), name, SCM_ARG1, "make-image");
SCM_ASSERT (SCM_INUMP (s_width), s_width, SCM_ARG2, "make-image");
SCM_ASSERT (SCM_INUMP (s_height), s_height, SCM_ARG3, "make-image");
int area;
struct image *image;
- SCM_ASSERT ((SCM_NIMP (image_smob)
- && SCM_CAR (image_smob) == image_tag),
- image_smob, SCM_ARG1, "clear-image");
+ SCM_ASSERT (SCM_SMOB_PREDICATE (image_tag, image_smob),
+ image_smob, SCM_ARG1, "clear-image");
- image = (struct image *) SCM_CDR (image_smob);
+ image = (struct image *) SCM_SMOB_DATA (image_smob);
area = image->width * image->height;
memset (image->pixels, 0, area);
static SCM
mark_image (SCM image_smob)
{
- struct image *image = (struct image *) SCM_CDR (image_smob);
+ /* Mark the image's name and update function. */
+ struct image *image = (struct image *) SCM_SMOB_DATA (image_smob);
scm_gc_mark (image->name);
return image->update_func;
static scm_sizet
free_image (SCM image_smob)
{
- struct image *image = (struct image *) SCM_CDR (image_smob);
+ struct image *image = (struct image *) SCM_SMOB_DATA (image_smob);
scm_sizet size = image->width * image->height + sizeof (struct image);
free (image->pixels);
static int
print_image (SCM image_smob, SCM port, scm_print_state *pstate)
{
- struct image *image = (struct image *) SCM_CDR (image_smob);
+ struct image *image = (struct image *) SCM_SMOB_DATA (image_smob);
scm_puts ("#<image ", port);
scm_display (image->name, port);