]> git.donarmstrong.com Git - lilypond.git/blob - lily/include/lily-guile-macros.hh
* scm/music-functions.scm (has-request-chord): don't use
[lilypond.git] / lily / include / lily-guile-macros.hh
1 /*
2   lily-guile-macros.hh -- declare
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 2005 Han-Wen Nienhuys <hanwen@xs4all.nl>
7
8 */
9
10 #ifndef LILY_GUILE_MACROS_HH
11 #define LILY_GUILE_MACROS_HH
12
13 #ifndef SMOB_FREE_RETURN_VAL
14 #define SMOB_FREE_RETURN_VAL(CL) 0
15 #endif
16
17 #ifndef SCM_PACK
18 #define SCM_PACK(x) ((SCM) x)
19 #endif
20
21 #ifndef SCM_UNPACK
22 #define SCM_UNPACK(x) (x)
23 #endif
24
25
26 #if (__GNUC__ > 2)
27 /* Unreliable with gcc-2.x
28    FIXME: should add check for x86 as well?  */
29 #define CACHE_SYMBOLS
30 #endif
31
32 #ifdef CACHE_SYMBOLS
33
34 /* Using this trick we cache the value of scm_str2symbol ("fooo") where
35   "fooo" is a constant string. This is done at the cost of one static
36   variable per ly_symbol2scm() use, and one boolean evaluation for
37   every call.
38
39   The overall speedup of lily is about 5% on a run of wtk1-fugue2.  */
40 #define ly_symbol2scm(x) \
41 ({ \
42   static SCM cached; \
43   /* We store this one locally, since G++ -O2 fucks up else */ \
44   SCM value = cached; \
45   if ( __builtin_constant_p ((x))) \
46     { \
47       if (!cached) \
48         value = cached =  scm_gc_protect_object (scm_str2symbol ((x))); \
49     } \
50   else \
51     value = scm_str2symbol ((char*) (x)); \
52   value; \
53 })
54 #else
55 inline SCM ly_symbol2scm(char const* x) { return scm_str2symbol ((x)); }
56 #endif
57
58
59 /*
60   TODO: rename me to ly_c_lily_module_eval
61  */
62 #define ly_lily_module_constant(x) \
63 ({ \
64   static SCM cached; \
65   /* We store this one locally, since G++ -O2 fucks up else */ \
66   SCM value = cached; \
67   if ( __builtin_constant_p ((x))) \
68     { \
69       if (!cached) \
70         value = cached = scm_gc_protect_object (scm_eval (scm_str2symbol (x), \
71                                                 global_lily_module)); \
72     } \
73   else \
74     value = scm_eval (scm_str2symbol (x), global_lily_module); \
75   value; \
76 })
77
78
79
80 /*
81   Adds the NAME as a Scheme function, and a variable to store the SCM
82   version of the function in the static variable NAME_proc
83  */
84 #define DECLARE_SCHEME_CALLBACK(NAME, ARGS) \
85         static SCM NAME ARGS; \
86         static SCM NAME ## _proc
87
88 /*
89   Make TYPE::FUNC available as a Scheme function.
90  */
91 #define MAKE_SCHEME_CALLBACK(TYPE, FUNC, ARGCOUNT) \
92 SCM TYPE :: FUNC ## _proc; \
93 void \
94 TYPE ## _ ## FUNC ## _init_functions () \
95 { \
96   TYPE :: FUNC ## _proc = scm_c_define_gsubr (#TYPE "::" #FUNC, \
97                                               (ARGCOUNT), 0, 0, \
98                           (Scheme_function_unknown)TYPE :: FUNC); \
99   scm_c_export (#TYPE "::" #FUNC, NULL); \
100 } \
101  \
102 ADD_SCM_INIT_FUNC (TYPE ## _ ## FUNC ## _callback, \
103                    TYPE ## _ ## FUNC ## _init_functions);
104
105
106 void
107 ly_add_function_documentation (SCM proc, char const *fname,
108                                char const *varlist,
109                                char const *doc);
110
111 #define ADD_SCM_INIT_FUNC(name, func) \
112 class name ## _scm_initter \
113 { \
114 public: \
115   name ## _scm_initter () \
116   { \
117     add_scm_init_func (func); \
118   } \
119 } _ ## name ## _scm_initter; \
120 /* end define */
121
122 #define LY_DEFINE_WITHOUT_DECL(INITPREFIX, FNAME, PRIMNAME, REQ, OPT, VAR, \
123                                ARGLIST, DOCSTRING) \
124 SCM FNAME ## _proc; \
125 void \
126 INITPREFIX ## init () \
127 { \
128   FNAME ## _proc = scm_c_define_gsubr (PRIMNAME, REQ, OPT, VAR, \
129                                        (Scheme_function_unknown) FNAME); \
130   ly_add_function_documentation (FNAME ## _proc, PRIMNAME, #ARGLIST, \
131                                  DOCSTRING); \
132   scm_c_export (PRIMNAME, NULL); \
133 } \
134 ADD_SCM_INIT_FUNC (INITPREFIX ## init_unique_prefix, INITPREFIX ## init); \
135 SCM \
136 FNAME ARGLIST
137
138
139 #define LY_DEFINE(FNAME, PRIMNAME, REQ, OPT, VAR, ARGLIST, DOCSTRING) \
140 SCM FNAME ARGLIST; \
141 LY_DEFINE_WITHOUT_DECL (FNAME, FNAME, PRIMNAME, REQ, OPT, VAR, ARGLIST, \
142                         DOCSTRING)
143
144 #define LY_DEFINE_MEMBER_FUNCTION(CLASS, FNAME, PRIMNAME, REQ, OPT, VAR, \
145                                   ARGLIST, DOCSTRING) \
146 SCM FNAME ARGLIST; \
147 LY_DEFINE_WITHOUT_DECL (CLASS ## FNAME, CLASS::FNAME, PRIMNAME, REQ, OPT, \
148                         VAR, ARGLIST, DOCSTRING)
149
150 #define get_property(x) internal_get_property (ly_symbol2scm (x))
151 #define set_property(x, y) internal_set_property (ly_symbol2scm (x), y)
152
153 #endif /* LILY_GUILE_MACROS_HH */