2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1997--2015 Han-Wen Nienhuys <hanwen@xs4all.nl>,
5 Erik Sandberg <mandolaerik@gmail.com>
7 LilyPond is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 LilyPond is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with LilyPond. If not, see <http://www.gnu.org/licenses/>.
21 #include "translator-group.hh"
23 #include "context-def.hh"
25 #include "dispatcher.hh"
26 #include "engraver.hh"
27 #include "engraver-group.hh"
28 #include "international.hh"
31 #include "output-def.hh"
32 #include "performer.hh"
33 #include "performer-group.hh"
34 #include "scheme-engraver.hh"
35 #include "scm-hash.hh"
41 translator_each (SCM list, Translator_method method)
43 for (SCM p = list; scm_is_pair (p); p = scm_cdr (p))
44 (unsmob<Translator> (scm_car (p))->*method) ();
48 Translator_group::initialize ()
50 precompute_method_bindings ();
54 Translator_group::connect_to_context (Context *c)
58 programming_error ("translator group is already connected to context "
59 + context_->context_name ());
63 c->event_source ()->add_listener (GET_LISTENER (Translator_group, create_child_translator),
64 ly_symbol2scm ("AnnounceNewContext"));
65 for (SCM tr_list = simple_trans_list_; scm_is_pair (tr_list); tr_list = scm_cdr (tr_list))
67 Translator *tr = unsmob<Translator> (scm_car (tr_list));
68 tr->connect_to_context (c);
73 Translator_group::disconnect_from_context ()
75 for (SCM tr_list = simple_trans_list_; scm_is_pair (tr_list); tr_list = scm_cdr (tr_list))
77 Translator *tr = unsmob<Translator> (scm_car (tr_list));
78 tr->disconnect_from_context (context_);
80 context_->event_source ()->remove_listener (GET_LISTENER (Translator_group, create_child_translator),
81 ly_symbol2scm ("AnnounceNewContext"));
83 protected_events_ = SCM_EOL;
87 Translator_group::finalize ()
92 Both filter_performers and filter_engravers used to use a direct dynamic_cast
93 on the unsmobbed translator to be filtered, i.e.,
95 if (unsmob<Performer> (scm_car (*tail)))
97 but this caused mysterious optimisation issues in several GUB builds. See
98 issue #818 for the background to this change.
101 filter_performers (SCM ell)
104 for (SCM p = ell; scm_is_pair (p); p = scm_cdr (p))
106 if (unsmob<Performer> (scm_car (*tail)))
107 *tail = scm_cdr (*tail);
109 tail = SCM_CDRLOC (*tail);
115 filter_engravers (SCM ell)
118 for (SCM p = ell; scm_is_pair (p); p = scm_cdr (p))
120 if (unsmob<Engraver> (scm_car (*tail)))
121 *tail = scm_cdr (*tail);
123 tail = SCM_CDRLOC (*tail);
129 Protects the parameter from being garbage collected. The object is
130 protected until the next disconnect_from_context call.
132 Whenever a child translator hears an event, the event is added to
133 this list. This eliminates the need for derived_mark methods in most
134 translators; all incoming events are instead protected by the
137 TODO: Should the list also be flushed at the beginning of each new
141 Translator_group::protect_event (SCM ev)
143 protected_events_ = scm_cons (ev, protected_events_);
147 Create a new translator for a newly created child context. Triggered
148 by AnnounceNewContext events.
151 Translator_group::create_child_translator (SCM sev)
153 Stream_event *ev = unsmob<Stream_event> (sev);
154 // get from AnnounceNewContext
155 SCM cs = ev->get_property ("context");
156 Context *new_context = unsmob<Context> (cs);
157 Context_def *def = unsmob<Context_def> (new_context->get_definition ());
158 SCM ops = new_context->get_definition_mods ();
160 SCM trans_names = def->get_translator_names (ops);
162 Translator_group *g = get_translator_group (def->get_translator_group_type ());
163 SCM trans_list = SCM_EOL;
165 for (SCM s = trans_names; scm_is_pair (s); s = scm_cdr (s))
167 SCM definition = scm_car (s);
168 bool is_scheme = false;
170 Translator *type = 0;
171 if (ly_is_symbol (definition))
172 type = get_translator (definition);
173 else if (ly_is_pair (definition))
175 type = get_translator (ly_symbol2scm ("Scheme_engraver"));
178 else if (ly_is_procedure (definition))
180 // `definition' is a procedure, which takes the context as
181 // an argument and evaluates to an a-list scheme engraver
183 definition = scm_call_1 (definition, cs);
184 type = get_translator (ly_symbol2scm ("Scheme_engraver"));
189 warning (_f ("cannot find: `%s'", ly_symbol2string (scm_car (s)).c_str ()));
192 Translator *instance = type->clone ();
194 dynamic_cast<Scheme_engraver *> (instance)->init_from_scheme (definition);
196 SCM str = instance->self_scm ();
198 if (instance->must_be_last ())
200 SCM cons = scm_cons (str, SCM_EOL);
201 if (scm_is_pair (trans_list))
202 scm_set_cdr_x (scm_last_pair (trans_list), cons);
207 trans_list = scm_cons (str, trans_list);
209 instance->daddy_context_ = new_context;
210 instance->unprotect ();
214 /* Filter unwanted translator types. Required to make
215 \with { \consists "..." } work. */
216 if (dynamic_cast<Engraver_group *> (g))
217 g->simple_trans_list_ = filter_performers (trans_list);
218 else if (dynamic_cast<Performer_group *> (g))
219 g->simple_trans_list_ = filter_engravers (trans_list);
221 // TODO: scrap Context::implementation
222 new_context->implementation_ = g;
224 g->connect_to_context (new_context);
227 recurse_over_translators (new_context,
228 &Translator::initialize,
229 &Translator_group::initialize,
234 Translator_group::get_simple_trans_list ()
236 return simple_trans_list_;
240 precomputed_recurse_over_translators (Context *c, Translator_precompute_index idx, Direction dir)
243 = dynamic_cast<Translator_group *> (c->implementation ());
245 if (tg && dir == DOWN)
247 tg->precomputed_translator_foreach (idx);
248 tg->call_precomputed_self_method (idx);
251 for (SCM s = c->children_contexts (); scm_is_pair (s);
253 precomputed_recurse_over_translators (unsmob<Context> (scm_car (s)), idx, dir);
257 tg->precomputed_translator_foreach (idx);
258 tg->call_precomputed_self_method (idx);
263 recurse_over_translators (Context *c, Translator_method ptr,
264 Translator_group_method tg_ptr, Direction dir)
267 = dynamic_cast<Translator_group *> (c->implementation ());
269 if (tg && dir == DOWN)
272 translator_each (tg->get_simple_trans_list (), ptr);
275 for (SCM s = c->children_contexts (); scm_is_pair (s);
277 recurse_over_translators (unsmob<Context> (scm_car (s)), ptr, tg_ptr, dir);
281 translator_each (tg->get_simple_trans_list (),
288 Translator_group::Translator_group ()
290 simple_trans_list_ = SCM_EOL;
291 protected_events_ = SCM_EOL;
297 Translator_group::derived_mark () const
302 Translator_group::precompute_method_bindings ()
304 for (SCM s = simple_trans_list_; scm_is_pair (s); s = scm_cdr (s))
306 Translator *tr = unsmob<Translator> (scm_car (s));
307 Translator::Callback ptrs[TRANSLATOR_METHOD_PRECOMPUTE_COUNT];
308 tr->fetch_precomputable_methods (ptrs);
311 for (int i = 0; i < TRANSLATOR_METHOD_PRECOMPUTE_COUNT; i++)
314 precomputed_method_bindings_[i].push_back (Translator_method_binding (tr, ptrs[i]));
318 fetch_precomputable_methods (precomputed_self_method_bindings_);
322 Translator_group::precomputed_translator_foreach (Translator_precompute_index idx)
324 vector<Translator_method_binding> &bindings (precomputed_method_bindings_[idx]);
325 for (vsize i = 0; i < bindings.size (); i++)
326 bindings[i].invoke ();
330 Translator_group::fetch_precomputable_methods (Translator_group_void_method ptrs[])
332 for (int i = 0; i < TRANSLATOR_METHOD_PRECOMPUTE_COUNT; i++)
337 Translator_group::call_precomputed_self_method (Translator_precompute_index idx)
339 if (precomputed_self_method_bindings_[idx])
340 (*precomputed_self_method_bindings_[idx]) (this);
343 Translator_group::~Translator_group ()
348 const char Translator_group::type_p_name_[] = "ly:translator-group?";
351 Translator_group::print_smob (SCM port, scm_print_state *) const
353 scm_puts ("#<Translator_group ", port);
354 scm_puts (class_name (), port);
355 scm_display (simple_trans_list_, port);
356 scm_puts (" >", port);
361 Translator_group::mark_smob () const
364 scm_gc_mark (protected_events_);
365 return simple_trans_list_;