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