Added scm_assert_smob_type docs.
[bpt/guile.git] / doc / ref / api-smobs.texi
1 @c -*-texinfo-*-
2 @c This is part of the GNU Guile Reference Manual.
3 @c Copyright (C) 1996, 1997, 2000, 2001, 2002, 2003, 2004
4 @c Free Software Foundation, Inc.
5 @c See the file guile.texi for copying conditions.
6
7 @page
8 @node Smobs
9 @section Smobs
10
11 This chapter contains reference information related to defining and
12 working with smobs. See @ref{Defining New Types (Smobs)} for a
13 tutorial-like introduction to smobs.
14
15 @deftypefun scm_t_bits scm_make_smob_type (const char *name, size_t size)
16 This function adds a new smob type, named @var{name}, with instance size
17 @var{size}, to the system. The return value is a tag that is used in
18 creating instances of the type.
19
20 If @var{size} is 0, the default @emph{free} function will do nothing.
21
22 If @var{size} is not 0, the default @emph{free} function will
23 deallocate the memory block pointed to by @code{SCM_SMOB_DATA} with
24 @code{scm_gc_free}. The @var{WHAT} parameter in the call to
25 @code{scm_gc_free} will be @var{NAME}.
26
27 Default values are provided for the @emph{mark}, @emph{free},
28 @emph{print}, and @emph{equalp} functions, as described in
29 @ref{Defining New Types (Smobs)}. If you want to customize any of
30 these functions, the call to @code{scm_make_smob_type} should be
31 immediately followed by calls to one or several of
32 @code{scm_set_smob_mark}, @code{scm_set_smob_free},
33 @code{scm_set_smob_print}, and/or @code{scm_set_smob_equalp}.
34 @end deftypefun
35
36 @deftypefn {C Function} void scm_set_smob_mark (scm_t_bits tc, SCM (*mark) (SCM obj))
37 This function sets the smob marking procedure for the smob type specified by
38 the tag @var{tc}. @var{tc} is the tag returned by @code{scm_make_smob_type}.
39
40 The @var{mark} procedure must cause @code{scm_gc_mark} to be called
41 for every @code{SCM} value that is directly referenced by the smob
42 instance @var{obj}. One of these @code{SCM} values can be returned
43 from the procedure and Guile will call @code{scm_gc_mark} for it.
44 This can be used to avoid deep recursions for smob instances that form
45 a list.
46 @end deftypefn
47
48 @deftypefn {C Function} void scm_set_smob_free (scm_t_bits tc, size_t (*free) (SCM obj))
49 This function sets the smob freeing procedure for the smob type
50 specified by the tag @var{tc}. @var{tc} is the tag returned by
51 @code{scm_make_smob_type}.
52
53 The @var{free} procedure must deallocate all resources that are
54 directly associated with the smob instance @var{OBJ}. It must assume
55 that all @code{SCM} values that it references have already been freed
56 and are thus invalid.
57
58 The @var{free} procedure must return 0.
59 @end deftypefn
60
61 @deftypefn {C Function} void scm_set_smob_print (scm_t_bits tc, int (*print) (SCM obj, SCM port, scm_print_state* pstate))
62 This function sets the smob printing procedure for the smob type
63 specified by the tag @var{tc}. @var{tc} is the tag returned by
64 @code{scm_make_smob_type}.
65
66 The @var{print} procedure should output a textual representation of
67 the smob instance @var{obj} to @var{port}, using information in
68 @var{pstate}.
69
70 The textual representation should be of the form @code{#<name ...>}.
71 This ensures that @code{read} will not interpret it as some other
72 Scheme value.
73
74 It is often best to ignore @var{pstate} and just print to @var{port}
75 with @code{scm_display}, @code{scm_write}, @code{scm_simple_format},
76 and @code{scm_puts}.
77 @end deftypefn
78
79 @deftypefn {C Function} void scm_set_smob_equalp (scm_t_bits tc, SCM (*equalp) (SCM obj1, SCM obj1))
80 This function sets the smob equality-testing predicate for the smob
81 type specified by the tag @var{tc}. @var{tc} is the tag returned by
82 @code{scm_make_smob_type}.
83
84 The @var{equalp} procedure should return @code{SCM_BOOL_T} when
85 @var{obj1} is @code{equal?} to @var{obj2}. Else it should return
86 @var{SCM_BOOL_F}. Both @var{obj1} and @var{obj2} are instances of the
87 smob type @var{tc}.
88 @end deftypefn
89
90 @deftypefn {C Function} void scm_assert_smob_type (scm_t_bits tag, SCM val)
91 When @var{val} is a smob of the type indicated by @var{tag}, do nothing.
92 Else, signal an error.
93 @end deftypefn
94
95 @deftypefn {C Macro} int SCM_SMOB_PREDICATE (scm_t_bits tag, SCM exp)
96 Return true iff @var{exp} is a smob instance of the type indicated by
97 @var{tag}. The expression @var{exp} can be evaluated more than once,
98 so it shouldn't contain any side effects.
99 @end deftypefn
100
101 @deftypefn {C Macro} void SCM_NEWSMOB (SCM value, scm_t_bits tag, void *data)
102 @deftypefnx {C Macro} void SCM_NEWSMOB2 (SCM value, scm_t_bits tag, void *data, void *data2)
103 @deftypefnx {C Macro} void SCM_NEWSMOB3 (SCM value, scm_t_bits tag, void *data, void *data2, void *data3)
104 Make @var{value} contain a smob instance of the type with tag
105 @var{tag} and smob data @var{data}, @var{data2}, and @var{data3}, as
106 appropriate.
107
108 The @var{tag} is what has been returned by @code{scm_make_smob_type}.
109 The initial values @var{data}, @var{data2}, and @var{data3} are of
110 type @code{scm_t_bits}; when you want to use them for @code{SCM}
111 values, these values need to be converted to a @code{scm_t_bits} first
112 by using @code{SCM_UNPACK}.
113
114 The flags of the smob instance start out as zero.
115 @end deftypefn
116
117 Since it is often the case (e.g., in smob constructors) that you will
118 create a smob instance and return it, there is also a slightly specialized
119 macro for this situation:
120
121 @deftypefn {C Macro} {} SCM_RETURN_NEWSMOB (scm_t_bits tag, void *data)
122 @deftypefnx {C Macro} {} SCM_RETURN_NEWSMOB2 (scm_t_bits tag, void *data1, void *data2)
123 @deftypefnx {C Macro} {} SCM_RETURN_NEWSMOB3 (scm_t_bits tag, void *data1, void *data2, void *data3)
124 This macro expands to a block of code that creates a smob instance of
125 the type with tag @var{tag} and smob data @var{data}, @var{data2}, and
126 @var{data3}, as with @code{SCM_NEWSMOB}, etc., and causes the
127 surrounding function to return that @code{SCM} value. It should be
128 the last piece of code in a block.
129 @end deftypefn
130
131 @deftypefn {C Macro} scm_t_bits SCM_SMOB_FLAGS (SCM obj)
132 Return the 16 extra bits of the smob @var{obj}. No meaning is
133 predefined for these bits, you can use them freely.
134 @end deftypefn
135
136 @deftypefn {C Macro} scm_t_bits SCM_SET_SMOB_FLAGS (SCM obj, scm_t_bits flags)
137 Set the 16 extra bits of the smob @var{obj} to @var{flags}. No
138 meaning is predefined for these bits, you can use them freely.
139 @end deftypefn
140
141 @deftypefn {C Macro} scm_t_bits SCM_SMOB_DATA (SCM obj)
142 @deftypefnx {C Macro} scm_t_bits SCM_SMOB_DATA_2 (SCM obj)
143 @deftypefnx {C Macro} scm_t_bits SCM_SMOB_DATA_3 (SCM obj)
144 Return the first (second, third) immediate word of the smob @var{obj}
145 as a @code{scm_t_bits} value. When the word contains a @code{SCM}
146 value, use @code{SCM_SMOB_OBJECT} (etc.) instead.
147 @end deftypefn
148
149 @deftypefn {C Macro} void SCM_SET_SMOB_DATA (SCM obj, scm_t_bits val)
150 @deftypefnx {C Macro} void SCM_SET_SMOB_DATA_2 (SCM obj, scm_t_bits val)
151 @deftypefnx {C Macro} void SCM_SET_SMOB_DATA_3 (SCM obj, scm_t_bits val)
152 Set the first (second, third) immediate word of the smob @var{obj} to
153 @var{val}. When the word should be set to a @code{SCM} value, use
154 @code{SCM_SMOB_SET_OBJECT} (etc.) instead.
155 @end deftypefn
156
157 @deftypefn {C Macro} SCM SCM_SMOB_OBJECT (SCM obj)
158 @deftypefnx {C Macro} SCM SCM_SMOB_OBJECT_2 (SCM obj)
159 @deftypefnx {C Macro} SCM SCM_SMOB_OBJECT_3 (SCM obj)
160 Return the first (second, third) immediate word of the smob @var{obj}
161 as a @code{SCM} value. When the word contains a @code{scm_t_bits}
162 value, use @code{SCM_SMOB_DATA} (etc.) instead.
163 @end deftypefn
164
165 @deftypefn {C Macro} void SCM_SET_SMOB_OBJECT (SCM obj, SCM val)
166 @deftypefnx {C Macro} void SCM_SET_SMOB_OBJECT_2 (SCM obj, SCM val)
167 @deftypefnx {C Macro} void SCM_SET_SMOB_OBJECT_3 (SCM obj, SCM val)
168 Set the first (second, third) immediate word of the smob @var{obj} to
169 @var{val}. When the word should be set to a @code{scm_t_bits} value, use
170 @code{SCM_SMOB_SET_DATA} (etc.) instead.
171 @end deftypefn
172
173 @deftypefn {C Macro} {SCM *} SCM_SMOB_OBJECT_LOC (SCM obj)
174 @deftypefnx {C Macro} {SCM *} SCM_SMOB_OBJECT_2_LOC (SCM obj)
175 @deftypefnx {C Macro} {SCM *} SCM_SMOB_OBJECT_3_LOC (SCM obj)
176 Return a pointer to the first (second, third) immediate word of the
177 smob @var{obj}. Note that this is a pointer to @code{SCM}. If you
178 need to work with @code{scm_t_bits} values, use @code{SCM_PACK} and
179 @code{SCM_UNPACK}, as appropriate.
180 @end deftypefn
181
182 @deftypefun SCM scm_markcdr (SCM @var{x})
183 Mark the references in the smob @var{x}, assuming that @var{x}'s first
184 data word contains an ordinary Scheme object, and @var{x} refers to no
185 other objects. This function simply returns @var{x}'s first data word.
186 @end deftypefun
187
188 @c Local Variables:
189 @c TeX-master: "guile.texi"
190 @c End: