Remove unused prototype in score.hh.
[lilypond/mpolesky.git] / lily / scheme-engraver.cc
blob476f6d4dea610e2efb532373884047d77917bd8e
1 /*
2 scheme-engraver.cc -- implement Scheme_engraver
4 source file of the GNU LilyPond music typesetter
6 Copyright (c) 2009--2010 Han-Wen Nienhuys <hanwen@lilypond.org>
8 LilyPond 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 3 of the License, or
11 (at your option) any later version.
13 LilyPond 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.
18 You should have received a copy of the GNU General Public License
19 along with LilyPond. If not, see <http://www.gnu.org/licenses/>.
22 #include "scheme-engraver.hh"
24 #include "grob.hh"
26 #include "translator.icc"
28 Scheme_engraver::Scheme_engraver ()
30 stop_translation_timestep_function_ = SCM_EOL;
31 start_translation_timestep_function_ = SCM_EOL;
32 process_music_function_ = SCM_EOL;
33 process_acknowledged_function_ = SCM_EOL;
34 initialize_function_ = SCM_EOL;
35 finalize_function_ = SCM_EOL;
36 listeners_alist_ = SCM_EOL;
38 interface_acknowledger_hash_ = SCM_EOL;
39 interface_end_acknowledger_hash_ = SCM_EOL;
41 must_be_last_ = false;
42 per_instance_listeners_ = 0;
45 Scheme_engraver::~Scheme_engraver ()
47 translator_listener_record *next = 0;
48 for (translator_listener_record *r = per_instance_listeners_;
49 r; r = next)
51 next = r->next_;
52 delete r;
56 // Extracts the value if callable, if not return #f.
57 static SCM
58 callable (SCM symbol, SCM defn)
60 SCM val = ly_assoc_get (symbol, defn, SCM_BOOL_F);
61 return ly_is_procedure (val) ? val : SCM_BOOL_F;
64 bool
65 Scheme_engraver::must_be_last () const
67 return must_be_last_;
70 void
71 Scheme_engraver::init_from_scheme (SCM definition)
73 start_translation_timestep_function_ = callable (ly_symbol2scm ("start-translation-timestep"),
74 definition);
75 stop_translation_timestep_function_ = callable (ly_symbol2scm ("stop-translation-timestep"),
76 definition);
77 process_music_function_ = callable (ly_symbol2scm ("process-music"), definition);
78 process_acknowledged_function_ = callable (ly_symbol2scm ("process-acknowledged"),
79 definition);
80 initialize_function_ = callable (ly_symbol2scm ("initialize"), definition);
81 finalize_function_ = callable (ly_symbol2scm ("finalize"), definition);
83 SCM listeners = ly_assoc_get (ly_symbol2scm ("listeners"), definition, SCM_EOL);
85 listeners_alist_ = SCM_EOL;
87 must_be_last_ = to_boolean (ly_assoc_get (ly_symbol2scm ("must-be-last"),
88 definition,
89 SCM_BOOL_F));
91 translator_listener_record **tail = &per_instance_listeners_;
92 for (SCM p = listeners; scm_is_pair (p); p = scm_cdr (p))
94 SCM event_class = scm_caar (p);
95 SCM proc = scm_cdar (p);
97 if (!(ly_is_procedure (proc) && ly_is_symbol (event_class)))
98 continue;
100 // We should check the arity of the function?
102 // Record for later lookup.
103 listeners_alist_ = scm_acons (event_class, proc, listeners_alist_);
105 translator_listener_record *rec = new translator_listener_record;
106 *tail = rec;
107 rec->event_class_ = event_class;
108 rec->get_listener_ = &Scheme_engraver::get_listener;
109 tail = &rec->next_;
112 init_acknowledgers (ly_assoc_get(ly_symbol2scm ("acknowledgers"),
113 definition, SCM_EOL),
114 &interface_acknowledger_hash_);
116 init_acknowledgers (ly_assoc_get(ly_symbol2scm ("end-acknowledgers"),
117 definition, SCM_EOL),
118 &interface_end_acknowledger_hash_);
120 // TODO: hook up description, props read/written, grobs created
121 // etc. to provide automatic documentation.
124 void
125 Scheme_engraver::init_acknowledgers (SCM alist,
126 SCM *hash)
128 *hash = scm_c_make_hash_table(7);
129 for (SCM p = alist; scm_is_pair (p); p = scm_cdr (p))
131 SCM iface = scm_caar (p);
132 SCM proc = scm_cdar (p);
134 if (!(ly_is_procedure (proc) && ly_is_symbol (iface)))
135 continue;
137 scm_hashq_set_x (*hash, iface, proc);
141 // This is the easy way to do it, at the cost of too many invocations
142 // of Scheme_engraver::acknowledge_grob. The clever dispatching of
143 // acknowledgers is hardwired to have 1 method per engraver per
144 // grob-type, which doesn't work for this case.
145 void
146 Scheme_engraver::acknowledge_grob (Grob_info info)
148 acknowledge_grob_by_hash (info, interface_acknowledger_hash_);
151 void
152 Scheme_engraver::acknowledge_end_grob (Grob_info info)
154 acknowledge_grob_by_hash (info, interface_end_acknowledger_hash_);
157 void
158 Scheme_engraver::acknowledge_grob_by_hash (Grob_info info,
159 SCM iface_function_hash)
161 SCM meta = info.grob ()->internal_get_property (ly_symbol2scm ("meta"));
162 SCM ifaces = scm_cdr (scm_assoc (ly_symbol2scm ("interfaces"), meta));
163 for (SCM s = ifaces; scm_is_pair (s); s = scm_cdr (s))
165 SCM func = scm_hashq_ref (iface_function_hash,
166 scm_car (s), SCM_BOOL_F);
168 if (ly_is_procedure (func))
169 scm_call_3 (func, self_scm (), info.grob ()->self_scm (),
170 info.origin_translator ()->self_scm ());
174 static
175 void call_listen_closure (void *target, SCM ev)
177 SCM cl = (SCM) target;
178 SCM func = scm_car (cl);
179 SCM engraver = scm_cdr (cl);
180 scm_call_2 (func, engraver, ev);
183 static
184 void mark_listen_closure (void *target)
186 scm_gc_mark ((SCM)target);
189 Listener_function_table listen_closure = {
190 call_listen_closure, mark_listen_closure
193 /* static */
194 Listener
195 Scheme_engraver::get_listener (void *arg, SCM name)
197 Scheme_engraver *me = (Scheme_engraver*) arg;
198 SCM func = ly_assoc_get (name, me->listeners_alist_, SCM_BOOL_F);
199 assert (ly_is_procedure (func));
201 SCM closure = scm_cons (func, me->self_scm());
202 return Listener((void*)closure, &listen_closure);
205 translator_listener_record *
206 Scheme_engraver::get_listener_list () const
208 return per_instance_listeners_;
211 #define DISPATCH(what) \
212 void \
213 Scheme_engraver::what () \
215 if (what ## _function_ != SCM_BOOL_F) \
216 scm_call_1 (what ## _function_, self_scm ()); \
219 DISPATCH(start_translation_timestep);
220 DISPATCH(stop_translation_timestep);
221 DISPATCH(initialize);
222 DISPATCH(finalize);
223 DISPATCH(process_music);
224 DISPATCH(process_acknowledged);
226 void
227 Scheme_engraver::derived_mark () const
229 scm_gc_mark (start_translation_timestep_function_);
230 scm_gc_mark (stop_translation_timestep_function_);
231 scm_gc_mark (initialize_function_);
232 scm_gc_mark (finalize_function_);
233 scm_gc_mark (process_music_function_);
234 scm_gc_mark (process_acknowledged_function_);
235 scm_gc_mark (listeners_alist_);
236 scm_gc_mark (interface_acknowledger_hash_);
237 scm_gc_mark (interface_end_acknowledger_hash_);
240 ADD_ACKNOWLEDGER (Scheme_engraver, grob);
241 ADD_END_ACKNOWLEDGER (Scheme_engraver, grob);
243 ADD_TRANSLATOR (Scheme_engraver,
244 /* doc */
245 "Implement engravers in Scheme. Interprets arguments to @code{\\consists} "
246 "as callbacks. ",
248 /* create */
251 /* read */
254 /* write */