Replaced SCM_NEWCELL and SCM_NEWCELL2 with scm_alloc_cell and
[bpt/guile.git] / libguile / smob.h
1 /* classes: h_files */
2
3 #ifndef SCM_SMOB_H
4 #define SCM_SMOB_H
5
6 /* Copyright (C) 1995,1996,1998,1999,2000,2001 Free Software Foundation, Inc.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2, or (at your option)
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this software; see the file COPYING. If not, write to
20 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
21 * Boston, MA 02111-1307 USA
22 *
23 * As a special exception, the Free Software Foundation gives permission
24 * for additional uses of the text contained in its release of GUILE.
25 *
26 * The exception is that, if you link the GUILE library with other files
27 * to produce an executable, this does not by itself cause the
28 * resulting executable to be covered by the GNU General Public License.
29 * Your use of that executable is in no way restricted on account of
30 * linking the GUILE library code into it.
31 *
32 * This exception does not however invalidate any other reasons why
33 * the executable file might be covered by the GNU General Public License.
34 *
35 * This exception applies only to the code released by the
36 * Free Software Foundation under the name GUILE. If you copy
37 * code from other Free Software Foundation releases into a copy of
38 * GUILE, as the General Public License permits, the exception does
39 * not apply to the code that you add in this way. To avoid misleading
40 * anyone as to the status of such modified files, you must delete
41 * this exception notice from them.
42 *
43 * If you write modifications of your own for GUILE, it is your choice
44 * whether to permit this exception to apply to your modifications.
45 * If you do not wish that, delete this exception notice. */
46
47 \f
48
49 #include "libguile/__scm.h"
50 #include "libguile/print.h"
51
52 \f
53 /* This is the internal representation of a smob type */
54
55 typedef struct scm_smob_descriptor
56 {
57 char *name;
58 size_t size;
59 SCM (*mark) (SCM);
60 size_t (*free) (SCM);
61 int (*print) (SCM exp, SCM port, scm_print_state *pstate);
62 SCM (*equalp) (SCM, SCM);
63 SCM (*apply) ();
64 SCM (*apply_0) (SCM);
65 SCM (*apply_1) (SCM, SCM);
66 SCM (*apply_2) (SCM, SCM, SCM);
67 SCM (*apply_3) (SCM, SCM, SCM, SCM);
68 int gsubr_type; /* Used in procprop.c */
69 } scm_smob_descriptor;
70
71 \f
72
73 #define SCM_NEWSMOB(z, tc, data) \
74 do { \
75 z = scm_alloc_cell ((tc), (scm_t_bits) (data)); \
76 } while (0)
77
78 #define SCM_RETURN_NEWSMOB(tc, data) \
79 do { SCM __SCM_smob_answer; \
80 SCM_NEWSMOB (__SCM_smob_answer, (tc), (data)); \
81 return __SCM_smob_answer; \
82 } while (0)
83
84 #define SCM_NEWSMOB2(z, tc, data1, data2) \
85 do { \
86 z = scm_alloc_double_cell ((tc), (scm_t_bits)(data1), \
87 (scm_t_bits)(data2), 0); \
88 } while (0)
89
90 #define SCM_RETURN_NEWSMOB2(tc, data1, data2) \
91 do { SCM __SCM_smob_answer; \
92 SCM_NEWSMOB2 (__SCM_smob_answer, (tc), (data1), (data2)); \
93 return __SCM_smob_answer; \
94 } while (0)
95
96 #define SCM_NEWSMOB3(z, tc, data1, data2, data3) \
97 do { \
98 z = scm_alloc_double_cell ((tc), (scm_t_bits)(data1), \
99 (scm_t_bits)(data2), (scm_t_bits)(data3)); \
100 } while (0)
101
102 #define SCM_RETURN_NEWSMOB3(tc, data1, data2, data3) \
103 do { SCM __SCM_smob_answer; \
104 SCM_NEWSMOB3 (__SCM_smob_answer, (tc), (data1), (data2), (data3)); \
105 return __SCM_smob_answer; \
106 } while (0)
107
108
109 #define SCM_SMOB_DATA(x) (SCM_CELL_WORD_1 (x))
110 #define SCM_SET_SMOB_DATA(x, data) (SCM_SET_CELL_WORD_1 ((x), (data)))
111 #define SCM_TC2SMOBNUM(x) (0x0ff & ((x) >> 8))
112 #define SCM_SMOBNUM(x) (SCM_TC2SMOBNUM (SCM_CELL_TYPE (x)))
113 /* SCM_SMOBNAME can be 0 if name is missing */
114 #define SCM_SMOBNAME(smobnum) (scm_smobs[smobnum].name)
115 #define SCM_SMOB_PREDICATE(tag, obj) SCM_TYP16_PREDICATE (tag, obj)
116 #define SCM_SMOB_DESCRIPTOR(x) (scm_smobs[SCM_SMOBNUM (x)])
117 #define SCM_SMOB_APPLICABLE_P(x) (SCM_SMOB_DESCRIPTOR (x).apply)
118 #define SCM_SMOB_APPLY_0(x) (SCM_SMOB_DESCRIPTOR (x).apply_0 (x))
119 #define SCM_SMOB_APPLY_1(x,a1) (SCM_SMOB_DESCRIPTOR (x).apply_1 (x, (a1)))
120 #define SCM_SMOB_APPLY_2(x,a1,a2) (SCM_SMOB_DESCRIPTOR (x).apply_2 (x, (a1), (a2)))
121 #define SCM_SMOB_APPLY_3(x,a1,a2,rst) (SCM_SMOB_DESCRIPTOR (x).apply_3 (x, (a1), (a2), (rst)))
122
123 SCM_API long scm_numsmob;
124 SCM_API scm_smob_descriptor scm_smobs[];
125
126 \f
127
128 SCM_API SCM scm_mark0 (SCM ptr);
129 SCM_API SCM scm_markcdr (SCM ptr);
130 SCM_API size_t scm_free0 (SCM ptr);
131 SCM_API size_t scm_smob_free (SCM obj);
132 SCM_API int scm_smob_print (SCM exp, SCM port, scm_print_state *pstate);
133
134 /* The following set of functions is the standard way to create new
135 * SMOB types.
136 *
137 * Create a type tag using `scm_make_smob_type', accept default values
138 * for mark, free, print and/or equalp functions, or set your own
139 * values using `scm_set_smob_xxx'.
140 */
141
142 SCM_API scm_t_bits scm_make_smob_type (char *name, size_t size);
143
144 SCM_API void scm_set_smob_mark (scm_t_bits tc, SCM (*mark) (SCM));
145 SCM_API void scm_set_smob_free (scm_t_bits tc, size_t (*free) (SCM));
146 SCM_API void scm_set_smob_print (scm_t_bits tc,
147 int (*print) (SCM, SCM, scm_print_state*));
148 SCM_API void scm_set_smob_equalp (scm_t_bits tc, SCM (*equalp) (SCM, SCM));
149 SCM_API void scm_set_smob_apply (scm_t_bits tc,
150 SCM (*apply) (),
151 unsigned int req,
152 unsigned int opt,
153 unsigned int rst);
154
155 /* Function for creating smobs */
156
157 SCM_API SCM scm_make_smob (scm_t_bits tc);
158 SCM_API void scm_smob_prehistory (void);
159
160 #endif /* SCM_SMOB_H */
161
162 /*
163 Local Variables:
164 c-file-style: "gnu"
165 End:
166 */