]> git.donarmstrong.com Git - lilypond.git/blob - lily/include/lily-guile.hh
(scm_from_double): more compat glue.
[lilypond.git] / lily / include / lily-guile.hh
1 /*
2   lily-guile.hh encapsulate guile
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1998--2004 Jan Nieuwenhuizen <janneke@gnu.org>
7 */
8
9 #ifndef LILY_GUILE_HH
10 #define LILY_GUILE_HH
11
12 #include <libguile.h>
13 #include "flower-proto.hh"
14
15 #include "drul-array.hh"
16 #include "direction.hh"
17
18 #if SCM_MINOR_VERSION < 7
19 /* guile-1.6.x compatibility */
20 #define scm_gc_unregister_collectable_memory(a, b, c) scm_done_free (b)
21 #define scm_gc_register_collectable_memory(a, b, c) scm_done_malloc (b)
22 #define SCM_VECTOR_REF(v,i) (SCM_VELTS ((v))[(i)])
23 #define scm_from_int(x) SCM_MAKINUM (x)
24 #define scm_is_integer(x) SCM_INUMP (x)
25 #define scm_is_string(x) SCM_STRINGP(x)
26 #define scm_hash_table_p scm_vector_p
27 #define scm_i_string_chars(x) SCM_STRING_CHARS(x)
28 #define scm_i_string_length(x) SCM_STRING_LENGTH(x)
29 #define scm_is_number(x) (scm_number_p(x)==SCM_BOOL_T)
30 #define scm_to_int(x) (ly_scm2int(x))
31 #define scm_to_double(x) (ly_scm2double(x))
32 #define scm_from_double(x) (scm_make_real(x))
33 #endif /* SCM_MINOR_VERSION < 7 */
34
35 #ifndef SMOB_FREE_RETURN_VAL
36 #define SMOB_FREE_RETURN_VAL(CL) 0
37 #endif
38
39 #ifndef SCM_PACK
40 #define SCM_PACK(x) ((SCM) x)
41 #endif
42
43 #ifndef SCM_UNPACK
44 #define SCM_UNPACK(x) (x)
45 #endif
46
47 /** Conversion functions follow the GUILE naming convention, i.e.
48     A ly_B2A (B b);  */
49
50 SCM ly_last (SCM list);
51 SCM ly_write2scm (SCM s);
52 SCM ly_deep_copy (SCM);
53 SCM ly_truncate_list (int k, SCM lst);
54
55 #if (__GNUC__ > 2)
56 /* Unreliable with gcc-2.x
57    FIXME: should add check for x86 as well?  */
58 #define CACHE_SYMBOLS
59 #endif
60
61 #ifdef CACHE_SYMBOLS
62
63 /* Using this trick we cache the value of scm_str2symbol ("fooo") where
64   "fooo" is a constant string. This is done at the cost of one static
65   variable per ly_symbol2scm() use, and one boolean evaluation for
66   every call.
67
68   The overall speedup of lily is about 5% on a run of wtk1-fugue2.  */
69 #define ly_symbol2scm(x) \
70 ({ \
71   static SCM cached; \
72   /* We store this one locally, since G++ -O2 fucks up else */ \
73   SCM value = cached; \
74   if ( __builtin_constant_p ((x))) \
75     { \
76       if (!cached) \
77         value = cached =  scm_gc_protect_object (scm_str2symbol ((x))); \
78     } \
79   else \
80     value = scm_str2symbol ((char*) (x)); \
81   value; \
82 })
83 #else
84 inline SCM ly_symbol2scm(char const* x) { return scm_str2symbol ((x)); }
85 #endif
86
87 extern SCM global_lily_module;
88
89 /*
90   TODO: rename me to ly_c_lily_module_eval
91  */
92 #define ly_scheme_function(x) \
93 ({ \
94   static SCM cached; \
95   /* We store this one locally, since G++ -O2 fucks up else */ \
96   SCM value = cached; \
97   if ( __builtin_constant_p ((x))) \
98     { \
99       if (!cached) \
100         value = cached = scm_gc_protect_object (scm_eval (scm_str2symbol (x), \
101                                                 global_lily_module)); \
102     } \
103   else \
104     value = scm_eval (scm_str2symbol (x), global_lily_module); \
105   value; \
106 })
107
108
109 String ly_scm2string (SCM s);
110 String ly_symbol2string (SCM);
111 SCM ly_offset2scm (Offset);
112 Offset ly_scm2offset (SCM);
113 SCM ly_assoc_chain (SCM key, SCM achain);
114 SCM ly_assoc_cdr (SCM key, SCM alist);
115 Interval ly_scm2interval (SCM);
116 Drul_array<Real> ly_scm2realdrul (SCM);
117 Slice int_list_to_slice (SCM l);
118 SCM ly_interval2scm (Drul_array<Real>);
119 char *ly_scm2newstr (SCM str, size_t *lenp);
120
121 Real robust_scm2double (SCM, double);
122 int robust_scm2int (SCM, int);
123 Drul_array<Real> robust_scm2drul (SCM, Drul_array<Real>);
124 Interval robust_scm2interval (SCM, Drul_array<Real>);
125 Offset robust_scm2offset (SCM, Offset);
126
127 SCM ly_quote_scm (SCM s);
128 bool type_check_assignment (SCM val, SCM sym,  SCM type_symbol) ;
129 String print_scm_val (SCM val);
130 SCM ly_number2string (SCM s);
131
132 SCM parse_symbol_list (char const *);
133 SCM robust_list_ref(int i, SCM l);
134 SCM alist_to_hashq (SCM);
135
136 inline SCM ly_cdr (SCM x) { return SCM_CDR (x); }
137 inline SCM ly_car (SCM x) { return SCM_CAR (x); }
138 inline SCM ly_caar (SCM x) { return SCM_CAAR (x); }
139 inline SCM ly_cdar (SCM x) { return SCM_CDAR (x); }
140 inline SCM ly_cadr (SCM x) { return SCM_CADR (x); }
141 inline SCM ly_cddr (SCM x) { return SCM_CDDR (x); }
142 inline SCM ly_caddr (SCM x) { return SCM_CADDR (x); }
143 inline SCM ly_cdadr (SCM x) { return SCM_CDADR (x); }
144 inline SCM ly_caadr (SCM x) { return SCM_CAADR (x); }
145 inline SCM ly_cadar (SCM x) { return SCM_CADAR (x); }
146 /* inserts at front, removing dublicates */
147 inline SCM ly_assoc_front_x(SCM alist, SCM key, SCM val)
148 {
149   return scm_acons(key, val, scm_assoc_remove_x (alist, key));
150 }
151 inline bool ly_c_pair_p (SCM x) { return SCM_NFALSEP (scm_pair_p (x)); }
152 inline bool ly_c_symbol_p (SCM x) { return SCM_SYMBOLP (x); }
153 inline bool ly_c_boolean_p (SCM x) { return SCM_BOOLP (x); }
154 inline bool ly_c_char_p (SCM x) { return SCM_CHARP (x); }
155 inline bool ly_c_number_p (SCM x) { return SCM_NUMBERP (x); }
156 inline bool ly_c_string_p (SCM x) { return scm_is_string (x); }
157 inline bool ly_c_vector_p (SCM x) { return SCM_VECTORP (x); }
158 inline bool ly_c_list_p (SCM x) { return SCM_NFALSEP (scm_list_p (x)); }
159 inline bool ly_c_procedure_p (SCM x) { return SCM_NFALSEP (scm_procedure_p (x)); }
160 inline bool ly_c_eq_p (SCM x, SCM y) { return SCM_EQ_P (x, y); }
161 inline bool ly_c_equal_p (SCM x, SCM y) {
162   return SCM_NFALSEP (scm_equal_p (x, y));
163 }
164
165 inline bool ly_scm2bool (SCM x) { return SCM_NFALSEP (x); }
166 inline char ly_scm2char (SCM x) { return SCM_CHAR(x); }
167 inline int ly_scm2int (SCM x) { return scm_num2int (x, 0, "ly_scm2int"); }
168 inline double ly_scm2double (SCM x) { return scm_num2dbl (x, "ly_scm2double"); }
169 inline unsigned long ly_length (SCM x) {
170   return scm_num2ulong (scm_length (x), 0, "ly_length");
171 }
172 inline unsigned long ly_vector_length (SCM x) { return SCM_VECTOR_LENGTH (x); }
173
174 inline SCM ly_bool2scm (bool x) { return SCM_BOOL (x); }
175
176 inline SCM ly_append2 (SCM x1, SCM x2) {
177   return scm_append (scm_listify (x1, x2, SCM_UNDEFINED));
178 }
179 inline SCM ly_append3 (SCM x1, SCM x2, SCM x3) {
180   return scm_append (scm_listify (x1, x2, x3, SCM_UNDEFINED));
181 }
182 inline SCM ly_append4 (SCM x1, SCM x2, SCM x3, SCM x4) {
183   return scm_append (scm_listify (x1, x2, x3, x4, SCM_UNDEFINED));
184 }
185
186 /*
187   display and print newline.
188  */
189 extern "C" {
190 void ly_display_scm (SCM s);
191 }
192
193 void read_lily_scm_file (String);
194 void ly_c_init_guile ();
195
196 bool is_direction (SCM s);
197 bool is_number_pair (SCM);
198 bool is_axis (SCM);
199
200 /*
201   these conversion functions also do a typecheck on the argument, and
202   return a default value if S has the wrong type.
203 */
204
205 Direction to_dir (SCM s);
206 bool to_boolean (SCM s);
207
208 void init_ly_protection ();
209 unsigned int ly_scm_hash (SCM s);
210
211 SCM index_get_cell (SCM cell, Direction d);
212 SCM index_set_cell (SCM cell, Direction d, SCM val);
213
214 SCM ly_snoc (SCM s, SCM list);
215 SCM ly_split_list (SCM s, SCM lst);
216 SCM ly_unique (SCM lst);
217 SCM ly_list_qsort_uniq_x (SCM lst);
218
219 SCM ly_output_formats();
220 SCM ly_kpathsea_expand_path (SCM);
221
222 /*
223   snarfing.
224  */
225 void add_scm_init_func (void (*) ());
226
227 extern "C" {
228 typedef SCM (*Scheme_function_unknown) ();
229 }
230
231 #if __GNUC__ > 2 || __GNUC_MINOR__ >= 96
232 typedef SCM (*Scheme_function_0) ();
233 typedef SCM (*Scheme_function_1) (SCM);
234 typedef SCM (*Scheme_function_2) (SCM,SCM);     
235 typedef SCM (*Scheme_function_3) (SCM,SCM, SCM);        
236 #else
237 typedef SCM (*Scheme_function_0) (...);
238 typedef SCM (*Scheme_function_1) (...);
239 typedef SCM (*Scheme_function_2) (...);
240 typedef SCM (*Scheme_function_3) (...);
241 #endif
242
243
244 /*
245   Adds the NAME as a Scheme function, and a variable to store the SCM
246   version of the function in the static variable NAME_proc
247  */
248 #define DECLARE_SCHEME_CALLBACK(NAME, ARGS) \
249         static SCM NAME ARGS; \
250         static SCM NAME ## _proc
251
252 /*
253   Make TYPE::FUNC available as a Scheme function.
254  */
255 #define MAKE_SCHEME_CALLBACK(TYPE, FUNC, ARGCOUNT) \
256 SCM TYPE :: FUNC ## _proc; \
257 void \
258 TYPE ## _ ## FUNC ## _init_functions () \
259 { \
260   TYPE :: FUNC ## _proc = scm_c_define_gsubr (#TYPE "::" #FUNC, \
261                                               (ARGCOUNT), 0, 0, \
262                           (Scheme_function_unknown)TYPE :: FUNC); \
263   scm_c_export (#TYPE "::" #FUNC, NULL); \
264 } \
265  \
266 ADD_SCM_INIT_FUNC (TYPE ## _ ## FUNC ## _callback, \
267                    TYPE ## _ ## FUNC ## _init_functions);
268
269
270 void
271 ly_add_function_documentation (SCM proc, char const *fname,
272                                char const *varlist,
273                                char const *doc);
274
275 #define ADD_SCM_INIT_FUNC(name, func) \
276 class name ## _scm_initter \
277 { \
278 public: \
279   name ## _scm_initter () \
280   { \
281     add_scm_init_func (func); \
282   } \
283 } _ ## name ## _scm_initter; \
284 /* end define */
285
286 #define LY_DEFINE_WITHOUT_DECL(INITPREFIX, FNAME, PRIMNAME, REQ, OPT, VAR, \
287                                ARGLIST, DOCSTRING) \
288 SCM FNAME ## _proc; \
289 void \
290 INITPREFIX ## init () \
291 { \
292   FNAME ## _proc = scm_c_define_gsubr (PRIMNAME,REQ, OPT, VAR, \
293                                        (Scheme_function_unknown) FNAME); \
294   ly_add_function_documentation (FNAME ## _proc, PRIMNAME, #ARGLIST, \
295                                  DOCSTRING); \
296   scm_c_export (PRIMNAME, NULL); \
297 } \
298 ADD_SCM_INIT_FUNC (INITPREFIX ## init_unique_prefix, INITPREFIX ## init); \
299 SCM \
300 FNAME ARGLIST
301
302
303 #define LY_DEFINE(FNAME, PRIMNAME, REQ, OPT, VAR, ARGLIST, DOCSTRING) \
304 SCM FNAME ARGLIST; \
305 LY_DEFINE_WITHOUT_DECL (FNAME, FNAME, PRIMNAME, REQ, OPT, VAR, ARGLIST, \
306                         DOCSTRING)
307
308 #define LY_DEFINE_MEMBER_FUNCTION(CLASS, FNAME, PRIMNAME, REQ, OPT, VAR, \
309                                   ARGLIST, DOCSTRING) \
310 SCM FNAME ARGLIST; \
311 LY_DEFINE_WITHOUT_DECL (CLASS ## FNAME, CLASS::FNAME, PRIMNAME, REQ, OPT, \
312                         VAR, ARGLIST, DOCSTRING)
313
314 #define get_property(x) internal_get_property (ly_symbol2scm (x))
315 #define set_property(x,y) internal_set_property (ly_symbol2scm (x), y)
316
317 #endif /* LILY_GUILE_HH */